三位数运算计算器

问题描述:

问题是,如果程序计算的第一个动作(取决于操作顺序)除以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

有由部门的专项检查在输入后(并在if之前)输入零点,如果是这样,则继续循环。

因此,像

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

这是一个好主意。我想我会照你说的去做,但不要用“继续”。我将把它作为第一个“if”,然后将第二个作为“else if” – Sela12

正如你所知道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