三数运算计算器
A three numbers operation Calculator
问题是,如果程序计算的第一个动作(取决于操作顺序)除以 0(例如 7/0+3,或 3+7/0),它会打印错误消息但执行下一个操作。
示例-
输入:7/0+2 -----> 输出:不能除以零。7/0+2=2
它应该做什么 ----> 输出:你不能除以零。
发生这种情况是因为我无法让它跳过下一个 switch 操作。如果您有想法或知道如何解决它,请提供帮助。
谢谢
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";
}
}
如您所知,break
只会让您跳出 switch
块,而不是循环。
如果你想避免 goto
(我会这样做),那么一个简单的补救方法是在单独的函数中编写 while
循环,并使用 return
如果遇到被零除的情况以提前退出该函数。无论如何,将您的代码拆分为各种功能是一件好事。
(我使用 Boost Spirit - www.boost.org - 在生产系统中进行表达式解析。学习曲线非常陡峭,但非常值得。)
在输入之后(在 if
之前)特别检查是否被零除,如果是这样就继续循环。
所以像
while (...)
{
...
std::cin >> ...
if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0)
{
std::cout << "Division by zero\n";
continue;
}
if (...)
...
}
问题是,如果程序计算的第一个动作(取决于操作顺序)除以 0(例如 7/0+3,或 3+7/0),它会打印错误消息但执行下一个操作。
示例-
输入:7/0+2 -----> 输出:不能除以零。7/0+2=2
它应该做什么 ----> 输出:你不能除以零。
发生这种情况是因为我无法让它跳过下一个 switch 操作。如果您有想法或知道如何解决它,请提供帮助。 谢谢
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";
}
}
如您所知,break
只会让您跳出 switch
块,而不是循环。
如果你想避免 goto
(我会这样做),那么一个简单的补救方法是在单独的函数中编写 while
循环,并使用 return
如果遇到被零除的情况以提前退出该函数。无论如何,将您的代码拆分为各种功能是一件好事。
(我使用 Boost Spirit - www.boost.org - 在生产系统中进行表达式解析。学习曲线非常陡峭,但非常值得。)
在输入之后(在 if
之前)特别检查是否被零除,如果是这样就继续循环。
所以像
while (...)
{
...
std::cin >> ...
if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0)
{
std::cout << "Division by zero\n";
continue;
}
if (...)
...
}