2011-04-13 102 views
0

一個簡單的C++的計算器程序來計算加法,減法,乘法和加法...一個簡單的加法,減法,乘法和除法程序

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    //input declarations as doubles for total and counter 
    double total = 0, counter =0; 
    //input declarations sign and Q as character 
    char sign, Q = 0; 
    //input declaration value as double 
    double value; 



     //A do..while will loop forever (or until we hit the break statement) 
     do 
     { 
      //The current value is 0. 
      cout << "Result :"<<" "<< total << '\n'; 

      //Please enter an operation 
      cout << "Please enter an operation and number : "; 
     cin >> sign; 

     //If the operation is Q, the program will end. 
     if (sign != 'Q') 

     cin >> value; 
      cin.ignore(); 


      // If the value input is <=0, you can't divide anything by zero. 
      if (value <= 0) 
      { 
       cout << "Unknown Operator " << sign <<'\n' ; 
      } 

      //Otherwise procede with the calulator program 
      else 
      { 
       //If the operation is equal to '+', then the total is added. 
       if (sign == '+') 
       { 
        total += value; 
       } 

       // If the operation is equal to '-', then the value is subtracted from the previous number input. 
       else 
       { 
        if (sign == '-') 
        { 
         total -= value; 
        } 

        // If the operation is equal to '*', then the value is multiplied to the previous number input. 
        else 
        { 
         if (sign == '*') 
         { 
          total *= value; 
         } 

         // If the operation is equal to '/', then the value is divided by the previous number input. 
         else 
         { 
          if ((sign == '/')&&(value != 0)) 
          { 
           total /= value; 
          } 
         } 
        } 
       } 
      } 
     } 



      //While the operation is not equal to 'Q', the program will run. 
      while (sign != 'Q'); 



     return 0; 
} 

用於上述程序的編碼沒有錯誤,但如果我按「Q」退出,它將顯示最後的結果不間斷。 。一遍又一遍。 。 。無論如何,任何人都知道如何爲程序添加平方根。 。

+3

「上述程序的編碼沒有錯誤」 – 2011-04-13 12:12:15

+0

您是否按「q」或「Q」。您的代碼只檢查「Q」 2011-04-13 12:14:07

+0

我按「Q」,程序沒有退出。 。 – Matrix 2011-04-13 13:29:36

回答

0

作爲一個簡單的練習,我重構了您的程序,使其更簡單。我不能保證它的工作原理,但它應該給你一個很好的基礎,建立在:

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    //input declarations as doubles for total and counter 
    double total = 0, counter =0; 
    //input declarations sign and Q as character 
    char sign = 0; 
    //input declaration value as double 
    double value; 

    //A do..while will loop forever (or until we hit the break statement) 
    do 
    { 
    //The current value is 0. 
    cout << "Current result: " << total << '\n'; 

    //Please enter an operation 
    cout << "Please enter an operation (or Q to quit) and number: "; 
    cin >> sign; 

    //If the operation is Q, the program will end. 
    if (sign == 'Q' || sign == 'q') { 
     cout << "See you!\n"; 
     break; 
    } 

    // If the input cannot be transformed into a double 
    // abort loop and try again 
    if (!(cin >> value)) { 
     cerr << "Invalid value entered, try again\n"; 
     continue; 
    } 

    switch(sign) 
    { 
    case '+': total += value; break; 
    case '-': total -= value; break; 
    case '*': total *= value; break; 
    case '/': 
     if (value == 0) { 
     cerr << "Divide by 0 prevented!\n"; 
     } else { 
     total /= value; break; 
     } 
    default: 
     cerr << "Unknown sign: " << sign << "\n"; 
    } 

    } while (true); 

    return 0; 
} 

要點:

  • 避免壓痕可能時,他們妨礙可讀性(更喜歡繼續/中斷)
  • 使用開關時,可能(也可能爲char和數字)
  • 檢查錯誤在必要的時候:從用戶輸入到double轉換可能會失敗,0可能是耳鼻喉科ered
+0

owh,非常感謝。 。你的程序是一個非常簡單的程序。 。我非常感謝這! – Matrix 2011-04-13 13:41:28

0
#include <math.h> 
double result = sqrt (value); 
+0

好的,謝謝。我會將這些添加到我的代碼中。 。 – Matrix 2011-04-13 13:28:43

1

通過if (sign == 'Q') break;

  • 此更換if (sign != 'Q') ...將解決衆多可能出現的錯誤。
  • 這將使您的代碼更易於使用較少的縮進和大括號。

編輯:正如有人提到,你應該也可以檢查小寫字母。 (if(sign =='Q'|| sign =='q'))。

+0

謝謝。 .i將這些添加到我的代碼和「Q」工作中!非常感謝!! – Matrix 2011-04-13 13:42:22

0

對於平方根, #include <math> double r=sqrt(e);

關於你的其他問題:請縮進代碼更好;-)

你確定你沒有忘記把塊這一行之後? if (sign != 'Q')

最簡單的方法,在步進模式下使用調試器,你就會明白!

+0

感謝您的平方根代碼。 。通過在該行之後放置一個塊,你是什麼意思?對不起,我只是一個初學者,所以我不熟悉所有的C++術語.. – Matrix 2011-04-13 13:26:47

+1

測試後,把一個明確的塊{},它有助於。因爲你的代碼奇怪地縮進了,它會提高可讀性。 – Bruce 2011-04-13 14:07:03