2012-07-20 204 views
0
using namespace std; 

//a class that will handle all the calculations requested by user 

class MathOperations{ 
    public: 
    void Message(); 
    int setSum(int,int); 
    int setSuB(int,int); 
    int setMul(int,int); 
    float setDiv(float,float *); 
    int setSqrt(int); 
}; 

//Implementation: 

void MathOperations:: Message(){ 
    cout<< " Welcome. This program simulates a calculator "<<endl; 
} 
// implementing the setters methods. 
int MathOperations::setSum(int a, int b){ 

    int total; 
    total = a + b; 
    return total; 
} 
int MathOperations:: setSuB(int a, int b){ 
    int total; 
    total = a - b; 
    return total; 
} 
int MathOperations:: setMul(int a, int b){ 
    int total; 
    total = a * b; 
    return total; 
} 

float MathOperations:: setDiv(float a, float *b){ 
    float result; 
    if(b ==0){ 
     cout << "Using the Default Value of 1 because you can't devide by 0"<<endl; 
    } 
    else 
     result = (a/*b); 
    return result; 
} 

int MathOperations::setSqrt(int Square){ 
    int total; 
    total = sqrt(Square); 
    return total; 
} 

int main(int argc, const char * argv[]) 
{ 
    //creating instances of class MathOperations 
    MathOperations add, sub, mul, div, sqrt; 
    ///creating variable to hold user input 

    int fnum; 
    float snum; 
    char opt= '0'; 

    //propt user for values 
    cout << " Enter a Number"<<endl; 
    cin >> fnum; 

    cout << " Enter a second number " <<endl; 
    cin >> snum; 

    float total = div.setDiv(fnum, &snum); 

    cout << total <<endl; 


    cout << " What would you like to do '+','-','*','/' ?"<<endl; 
    cin >> opt; 

    switch (opt) { 
     case '+' : 
     { 
      int total = add.setSum(fnum, snum); 
      cout << " The Total Sum of both numbers is " << total <<endl; 
     } 
     break; 
     case '-' : 
     { 
      int total = sub.setSuB(fnum, snum); 
      cout << " The Subtraction of both Numbers is " << total << endl; 
     } 
     break; 
     case '*' : 
     { 
      int total = mul.setMul(fnum, snum); 
      cout << " The Multiplication of both Numbers is " << total << endl; 
     } 
     break; 
     case '/' : 
     { 
      int total = div.setDiv(fnum, &snum); 
      cout << " The Division of both Numbers is " << total <<endl; 
     } 
     default: 
     cout << " Not a valid Option"<<endl; 
    } 

} 

该部门工作不正常。我在这里做错了什么?我试图用数学运算创建一个类。我是一个尝试在这里做一些练习的初学者。你能特意让我知道我在做什么错吗?我该如何解决这个问题?

+0

的价值,当你调用'setDiv'(它可能是这样的4.9999999你应该四舍五入的结果,其中4是'int'),而第二个参数的指针看起来毫无意义。 – chris 2012-07-20 16:22:03

+2

你需要学习如何提出更好的问题:告诉我们什么是错的(你期望发生什么?发生了什么?),我们会告诉你你做错了什么。 – 2012-07-20 16:22:42

+0

为什么'setDiv'有一个指向float(不是float)作为参数的指针? – 2012-07-20 16:22:50

回答

0

函数div的第二个参数不应该是一个指针。至少我没有看到它是一个指针的任何理由。

所以只需从变量snum中删除*和&即可。

+0

@petermim ...如果放置如12/2,但如果12/0类型是假设返回12,因为除以0不能完成 – user1535963 2012-07-20 16:29:00

+0

但是如果它应该返回12,为什么你不回12? – 2012-07-20 16:31:33

0

你并没有在setDiv函数中保护你自己除0,因为你没有将第二个float与0进行比较。你只是将它的地址与NULL进行比较。

而错误消息没有意义:程序不以任何方式“使用1”。

如果指针确实发生NULL,那么您将返回未初始化的值。

0

当你说你正在使用1而不是0,你永远不会改变b键1