我的代码应该能够在一个文本文件中打印多个不同的方程;
导致方程停止并转到下一行的字符,并且^
字符取数字的平方。然而,该代码只打印一个方程然后停止。有人告诉我,我需要在已经建立的循环上再循环一次。但我不知道该怎么做。谢谢。
INPUT
5^ + 5 - 4^;
2 - 1;
1 + 5^ + 2;
我的输出
14
MY CODE
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
char op;
int result, operand;
int readnumber();
int readnumber()
{
int val;
cin >> val;
if (cin.peek() == '^')
{ // found ^
val *= val;
cin.ignore();
}
return val;
}
int main() {
result = readnumber();
while (cin >> op) {
if (op == ';') {
cout << result << endl;
cin >> result;
}
operand = readnumber();
if (op == '+') {
result += operand;
}
if (op == '-') {
result -= operand;
}
}
return 0;
}
您在打印结果后尝试读取一个数字(cin >> result;
)。通过删除该行,我可以添加更多项目(尽管结果看起来不正确)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)