我当前的输出是
4 + 6
现在,点击输入后,它将重置并显示
代替 答案:10
// Here is my source
#include "Header.h"
string answer = "0";
string print = "Answer: ";
string forinvalid = "";
int main()
{
while (true)
{
cout << print << answer << endl;
cout << forinvalid;
cout << "---------------------------\n";
// Get Equation
if (GetExpression())
{
cout << "Answer: ";
cout << CalculateExpression(ExpressionText) << endl;
cout << "---------------------------";
forinvalid = "";
answer = to_string(CalculateExpression(ExpressionText));
}
else
{
// it's a invalid equation
cout << "invalid equation\n";
forinvalid = "* invalid equation\n";
print = "";
cout << "---------------------------\n";
}
cin.ignore();
system("cls");
}
return 0;
}
//Header
#include "Functions.h"
//Global variables
string EquationText = "";
// To get the equation
bool GetEquation()
{
// Input equation
getline(cin, EquationText);
// Remove extra spaces in the equation
EquationText = SpaceRemoval(EquationText);
// Check if the equation is valid
return EquationValidation(EquationText);
}
我想提到的是,calculateexpression是double computeexpression的代码很长,因此我没有上传它,但是如果您需要查看整个代码,可以询问
答案 0 :(得分:0)
为什么收到“答案:10.00000”(消息2)而不是“答案:10”(消息1)?
消息2通过此行'cout << print << answer << endl'
打印
,其中字符串answer
是在打印消息1之后由表达式'to_string(10.)'
计算出来的。此方法在字符串表示中添加了额外的零。
您可以使用std::stod
切掉多余的零:cout << print << std::stod (answer) << endl;
此外,您可以结合使用std::setprecision
和函数NumberLength
来更精确地控制打印过程。