还是用C++的类编写的一道题目T.T 这个工程貌似更为浩大了

•Design a class named MyInteger.The class contains the following:
•An intdata field named value that stores the intvalue represented by this object.
•A constructor that creates a MyIntegerobject for the specified intvalue.
•A constant get function that return the intvalue.
•Constant functions isEven(),isOdd(),isPrime()that return true if the value is even, odd, or prime, respectively•Static functions isEven(int),isOdd(int),isPrime(int)that return true if the specified value is even, odd, or prime, respectively.
•Static functions isEven(constMyInteger&),isOdd(constMyinteger&),isPrime(constMyInteger&)that returns true I f the specified value is even, odd, or prime, respectively.
•Constant functions equals(int)and equals(constMyInteger&)that return true if the value in the object is equal to the specified value.
•A static function parseInt(conststring&) that converts a string to an intvalue.•Implement the class and write a programto test all functions in the class.

myinteger.h

#include <string>

class MyInteger
{
public:
  MyInteger(int _value)
    : value(_value)
  {
  }

  int getValue() const
  {
    return value;
  }

  bool isEven() const
  {
    return isEven(value);
  }
  bool isOdd() const
  {
    return isOdd(value);
  }
  bool isPrime() const
  {
    return isPrime(value);
  }
  static bool isEven(const MyInteger &myInt)
  {
    return myInt.isEven();
  }
  static bool isOdd(const MyInteger &myInt)
  {
    return myInt.isOdd();
  }
  static bool isPrime(const MyInteger &myInt)
  {
    return myInt.isPrime();
  }
  static bool isEven(int value);
  static bool isOdd(int value);
  static bool isPrime(int value);

  bool equals(int other) const
  {
    return value == other;
  }
  bool equals(const MyInteger &other) const
  {
    return other.equals(value);
  }

  static int parseInt(const std::string &str);

private:
  int value;
};

 

myinteger.cpp

#include "myinteger.h"
#include <stdlib.h>

bool MyInteger::isEven(int value)
{
  return 0 == value % 2;
}
bool MyInteger::isOdd(int value)
{
  return value % 2;
}
bool MyInteger::isPrime(int value)
{
  for(int i = 2; i * i <= value; ++i) {
    if(0 == value % i) {
      return 0;
    }
  }
  return 1;
}

int MyInteger::parseInt(const string &str)
{
  return atoi(str.c_str());
}

追问

谢谢,把运行的主函数也敲给我吧,麻烦了!

追答

myinteger.cpp

#include "myinteger.h"
#include <assert.h>
#include <stdlib.h>

bool MyInteger::isEven(int value)
{
  return 0 == value % 2;
}
bool MyInteger::isOdd(int value)
{
  return value % 2;
}
bool MyInteger::isPrime(int value)
{
  for(int i = 2; i * i <= value; ++i) {
    if(0 == value % i) {
      return 0;
    }
  }
  return 1;
}
 
int MyInteger::parseInt(const std::string &str)
{
  return atoi(str.c_str());
}

int main()
{
  MyInteger v1(1);
  MyInteger v2(2);
  MyInteger v3(3);
  MyInteger v4(4);
  MyInteger _v4(4);

  assert(1 == v1.getValue());
  assert(v1.isOdd());
  assert(v2.isEven());
  assert(v3.isPrime());
  assert(MyInteger::isOdd(v1));
  assert(MyInteger::isEven(v2));
  assert(MyInteger::isPrime(v3));
  assert(MyInteger::isOdd(v1.getValue()));
  assert(MyInteger::isEven(v2.getValue()));
  assert(MyInteger::isPrime(v3.getValue()));
  assert(v4.equals(4));
  assert(v4.equals(_v4));
  assert(5 == MyInteger::parseInt("5"));
  
  return 0;
}

追问

非常感谢,不过- - CODEBLOCKS我自己都还没建过工程导致大牛的代码还没运行 不过也已经万分感谢了~

温馨提示:答案为网友推荐,仅供参考