接受多个输入(如 +、- 和平方数字)的计算器.从文本文件中提取信息
Calculator that accepts multiple inputs like +, -, and a number squared. Pulling info from text file
我有这个任务:
编写一个更好的计算器程序calc3.cpp它可以理解平方数。我们将使用简化的符号 X^ 来表示 X2。例如,10^ + 7 - 51^ 应表示 10^2 + 7 − 51^2。
假设您有一个文件公式.txt其求和公式如下:
5^; = 25 1000 + 6^ - 5^ + 1; = 1012
起初我使用了开关/案例语句,这并不完全有效,而且对我来说太难理解了。我现在正在使用 if 语句。我不确定我的 if 语句有什么问题,我认为我需要在 + 和 - 语句中的每个 if 语句中添加更多内容,但我不知道是什么。
非常感谢!
cin >> sum;
sum == rightNum;
while (cin >> op) {
if(op == '^') {
sum += rightNum * rightNum;
cin >> op;
}
if(op == '+') {
cin >> rightNum;
cin >> op;
if(op == '^') {
sum += rightNum * rightNum;
}
else {
sum += rightNum;
}
}
if(op == '-') {
cin >> rightNum;
cin >> op;
if(op == '^') {
sum -= rightNum * rightNum;
}
else {
sum -= rightNum;
}
}
if(op == ';') {
cout << sum << endl;
cin >> sum;
}
}
由于您已将收集和执行下一个操作构建到处理上一个操作的代码中,因此您构建了一个无法有效扩展的程序。而是循环获取运算符并获取正确的操作数,然后,如果可以忽略运算符优先级,则对上一个结果和操作数执行操作。如果您必须考虑运算符优先级,请忽略此答案并开始查看优先级队列和树。
类似的东西
cin >> result; // get first operand. If there is no operator, this is the answer
while (true) // loop forever!
{
cin >> op;
if (op == ';')
{
print result and exit loop.
}
cin >> operand;
switch(op)
{
case '+':
result += operand;
break;
case '-':
result -= operand;
break;
other operations go here
}
}
现在我们有一个可以处理任意数量操作的基本框架,我们可以处理 ^。最好不要将其视为操作,因为它未用作操作。它更像是一个修饰符。
如果语法看起来像 10 ^ 2,你会有一个运算符,但 10^ 没有右操作数,这会搞砸其余代码的左操作数、运算符、右操作数结构。
那么我们如何做到这一点呢?每当您从用户那里读取数字时,请查看下一个字符。如果是 ^,则乘以自己读取的数字。
将T
替换为所需的任何类型。
T readnumber()
{
T val;
cin >> val;
if (cin.peek() == '^')
{ // found ^
val *= val; // square the value
cin.ignore() // remove the ^ so no one else trips over it
}
return val;
}
请注意,上述内容完全忽略了输入验证,并且会错误地处理错误的输入。用户是出了名的愚蠢和糟糕的打字员。不要相信用户会给程序良好的输入。一般来说,根本不信任用户。
用readnumber
替换cin >>
,我们会得到类似的东西
result = readnumber();
while (true)
{
cin >> op;
if (op == ';)
{
print result and exit loop.
}
operand = readnumber();
switch(op)
{
case '+':
result += operand;
break;
case '-':
result -= operand;
break;
other operations go here
}
}
旁注:请考虑将cin
替换为通用std::istream
,以便这些函数可以与任何类型的流一起使用。
基于评论的建议:
int calculate(istream & in)
{
result = readnumber(in);
while (true)
{
in >> op;
if (op == ';)
{
break;
}
operand = readnumber(in);
switch(op)
{
case '+':
result += operand;
break;
case '-':
result -= operand;
break;
other operations go here
}
}
return result;
}
然后calculate
坐在循环中并反复调用,直到输入用完为止。
奖励建议:使用std::getline
获取由 ';" 分隔的语句
string statement;
while (getline(in, statement, ';'))
{
calculate(stringstream(statement));
}
getline
删除了";",因此它不能用于退出calculate
中的循环,但这实际上使事情变得更容易一些:您可以在stringstream
为空或处于失败状态时退出,
- 正在查找文档以获得PS4平台的C++中的设备信息
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何设置一个范围来提取我想要获得的信息
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 使用 BMI 计算器对C++中的数字进行四舍五入的问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 我在c ++矩阵计算器程序中找不到错误
- C++计算器,不能单独除以 0
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 如何从WebAssembly模块检测浏览器信息?
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 接受多个输入(如 +、- 和平方数字)的计算器.从文本文件中提取信息