2012-07-20 143 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