1809 The MyInteger class (eden)

来源:互联网 发布:数控车床车圆球g41编程 编辑:程序博客网 时间:2024/06/15 20:58

Design a class named MyInteger. The class contains:

  • An int data field named value that stores the int value represented by this object.

  • A constructor that creates a MyInteger object for the specified int value.

  • A get function that returns the int value.

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(MyInteger), isOdd(MyInteger), isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.

Functions equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.

A static function parseInt(string) that coverts a string to an int value.

From 陈雄涛(TA)

Provided Codes

main.cpp

#include "MyInteger.hpp"#include <iostream>using namespace std;int main() {    const string words[3][2] = {"not even", "even",                                "not odd", "odd",                                "not prime", "prime"};    int a, b, c, d, e;    string num;    cin >> a >> b >> c;    cin >> d >> e;    cin >> num;    MyInteger myInt(a);    cout << "Integer "  << myInt.getValue() << " is: ";    cout << words[0][myInt.isEven()] << ", ";    cout << words[1][myInt.isOdd()] << ", ";    cout << words[2][myInt.isPrime()] << ".\n";    cout << "Integer "  << b << " is: ";    cout << words[0][MyInteger::isEven(b)] << ", ";    cout << words[1][MyInteger::isOdd(b)] << ", ";    cout << words[2][MyInteger::isPrime(b)] << ".\n";    MyInteger myInt2(c);    cout << "Integer "  << myInt2.getValue() << " is: ";    cout << words[0][MyInteger::isEven(myInt2)] << ", ";    cout << words[1][MyInteger::isOdd(myInt2)] << ", ";    cout << words[2][MyInteger::isPrime(myInt2)] << ".\n";    MyInteger myInt3(d);    MyInteger myInt4(e);    if (myInt3.equals(e) && myInt3.equals(myInt4))        cout << d << " and " << e << " are equal.\n";    else        cout << d << " and " << e << " are not equal.\n";    cout << "String '" << num << "' convert to "         << MyInteger::parseInt(num) << endl;    return 0;}

MyInteger.hpp

#ifndef _MYINTEGER_H#define _MYINTEGER_H#include <string>class MyInteger{  private:    int value;  public:    MyInteger(int);    int getValue() const;    bool isEven() const;    bool isOdd() const;    bool isPrime() const;    static bool isEven(int);    static bool isOdd(int);    static bool isPrime(int);    static bool isEven(const MyInteger&);    static bool isOdd(const MyInteger&);    static bool isPrime(const MyInteger&);    bool equals(int);    bool equals(const MyInteger&);    static int parseInt(const std::string&);};#endif

Submission

MyInteger.cpp

#include "MyInteger.hpp"#include <iostream>#include <cstdlib>#include <cmath>using namespace std;    MyInteger::MyInteger(int a){         value=a;       }    int MyInteger::getValue() const{         return value;       }    bool MyInteger::isEven() const{         if(value%2)           return false;         return true;       }    bool MyInteger::isOdd() const{         if(value%2)           return true;         return false;       }    bool MyInteger::isPrime() const{         if(value==1)            return false;         int s=sqrt(value),flag=0;         for(int i=2;i<=s;i++)            if(value%i==0){                flag=1;                break;            }                    if(flag)            return false;         return true;       }    bool MyInteger::isEven(int a){        if(a%2)           return false;         return true;       }    bool MyInteger::isOdd(int a){       if(a%2)           return true;         return false;       }    bool MyInteger::isPrime(int a){         if(a==1)            return false;         int s=sqrt(a),flag=0;         for(int i=2;i<=s;i++)            if(a%i==0){                flag=1;                break;            }                    if(flag)            return false;         return true;       }    bool MyInteger::isEven(const MyInteger& a){         return a.isEven();       }    bool MyInteger::isOdd(const MyInteger& a){         return a.isOdd();       }    bool MyInteger::isPrime(const MyInteger& a){         return a.isPrime();       }    bool MyInteger::equals(int a){         if(value==a)            return true;         return false;       }    bool MyInteger::equals(const MyInteger& a){         if(value==a.value)            return true;         return false;       }    int MyInteger::parseInt(const std::string& a){         return atoi(a.c_str());       }
原创粉丝点击