2016-11-28 74 views
-2

問題是,如果程序計算的第一個動作(取決於操作順序)除以0(例如7/0 + 3或3 + 7/0),它將打印錯誤消息但執行下一個操作。三位數運算計算器

例 -

輸入:7/0 + 2 ----->輸出:您無法通過zero.7/0 + 2 = 2

它應該做的劃分 - - >輸出:你不能被零除。

它發生是因爲我無法得到它跳過下一個開關的行動。如果你有一個想法或知道如何解決它,請幫助。 謝謝

P.S 如果你知道更好的方法來解決操作順序問題,請幫助。

縮短代碼: 只需輸入類似7 + 4/0或7-4/0

#include"stdafx.h" 
using namespace System; 
#include<iostream> 
using namespace std; 
int main() 
{ 
    long double num1, num2, num3, res; 
    char action1, action2; 
    cin >> num1 >> action1 >> num2 >> action2 >> num3; 
    if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+')) //action2 will be preformed before action1 (Order of operation) 
    { 
     switch (action2) //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'. 
     { 
     case('/'): 
      if(num3==0) 
       cout<< "Error, you can't divide by zero"; 
      else 
       res = num2/num3; 
      break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
      switch (action1) 
      { 
      case('+'): 
       cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1; 
       break; 
      case('-'): 
       cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1; 
       break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
    } 
    cout << "\n\n"; 
    system("PAUSE"); 
    return 0; 
} 

全碼:

// Three numbers Calculator 
/*Known problems: when the first action is divided by zero the programs prints the error message but runs the next action. 
Other than that I think most of the stuff works but I need to check*/ 
#include "stdafx.h" 
using namespace System; 
#include<iostream> 
using namespace std; 
int main() 
{ 
    cout << "Enter action as # to exit program" << endl; 
    cout << "Possible actions:+,-,*,/\n" << endl; 
    int a = 1; //loop veriable 
    while (a == 1) //loop 
    { 
     long double num1, num2, num3, res; 
     char action1, action2; 
     cin >> num1 >> action1 >> num2 >> action2 >> num3; 
     if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+')) //action2 will be preformed before action1 (Order of operation) 
     { 
      switch (action2) //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'. 
      { 
      case('/'): 
       if (num3 == 0)  //The problem I described at the top occurs here and at another place below 
        cout << "You can't divide by zero."; 
       else 
        res = num2/num3; 
       break; 
      case('*'): 
       res = num2*num3; 
       break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
      switch (action1) //I didn't include the options for '*' or '/' because the if statement requires action1 to be '+' or '-'. 
      { 
      case('+'): 
       cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1; 
       break; 
      case('-'): 
       cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1; 
       break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
     } 
     else //action1 will be performed before action2 (Order of operation) 
     { 
      switch (action1) 
      { 
      case('+'): 
       res = num1 + num2; 
       break; 
      case('-'): 
       res = num1 - num2; 
       break; 
      case('/'): 
       if (num2 == 0)  //The problem I described at the top occurs here and at anothe place above 
        cout << "You can't divide by zero."; 
       else 
        res = num1/num2; 
       break; 
      case('*'): 
       res = num1*num2; 
       break; 
      case('#'): 
       system("PAUSE"); 
       return 0; 
       break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
      switch (action2) 
      { 
      case('+'): 
       cout << num1 << action1 << num2 << "+" << num3 << "=" << res + num3; 
       break; 
      case('-'): 
       cout << num1 << action1 << num2 << "-" << num3 << "=" << res - num3; 
       break; 
      case('/'): 
       if (num3 == 0) 
        cout << "You can't divide by zero."; 
       else 
        res = num2/num3; 
       break; 
      case('*'): 
       res = num2*num3; 
       break; 
      case('#'): 
       system("PAUSE"); 
       return 0; 
       break; 
      default: 
       cout << "Input not recognized"; 
       break; 
      } 
     } 
     cout << "\n\n"; 
    } 
} 
+0

對於操作的順序,你可能希望看到綴以postifx轉換及其評價。 – Sniper

回答

1

有由部門的專項檢查在輸入後(並在if之前)輸入零點,如果是這樣,則繼續循環。

因此,像

while (...) 
{ 
    ... 
    std::cin >> ... 
    if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0) 
    { 
     std::cout << "Division by zero\n"; 
     continue; 
    } 
    if (...) 
    ... 
} 
+0

這是一個好主意。我想我會照你說的去做,但不要用「繼續」。我將把它作爲第一個「if」,然後將第二個作爲「else if」 – Sela12

0

正如你所知道break只有打破你出去的switch塊,而不是循環。

如果你是熱衷於避免goto(我會),然後進行簡單的補救方法是編寫在一個單獨的功能while迴路,並使用return如果遇到零條件劃分退出該功能過早。無論如何,將代碼分解成各種功能是件好事。

(我使用升壓精神 - www.boost.org - 我的表達在生產系統解析的學習曲線是非常,但非常值得。)

+0

爲什麼你不推薦使用「goto」? – Sela12

+0

查看http://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto有趣的是,你的情況就是你可能會考慮使用goto的地方之一,但我會如果我是你,仍然會重構成一個獨立的函數。 – Bathsheba