所以我的代码基本上应该读取后缀表达式并计算值。到目前为止,这是我的代码,并且似乎可以正常工作,只是每次在Xcode上运行它时,输出的开头都会出现“程序以退出代码结尾:0”。我的代码中是否存在导致此错误的错误?此外,我应该只让用户输入一次具有相同名称的变量的值,但始终必须输入两次。有没有解决这个问题的简单方法?
这是我的代码
#include <iostream>
#include <string>
#include <stack>
#include <sstream>
int main()
{
char input = 'y';
while (input == 'y')
{
std::string expression;
std::stack<int> myStack;
std::cout << "Enter a postfix expression with a $ at the end: ";
std::getline(std::cin,expression);
int i = 0;
std::istringstream ss(expression);
while (ss >> expression)
{
if (expression[i] >= 'a' && expression[i] <= 'z')
{
int number = 0;
std::cout << "Enter the value of " << expression << ": ";
std::cin >> number;
myStack.push(number);
}
else if (expression[i] >= '0' && expression[i] <= '9')
{
int number = std::stoi(expression);
myStack.push(number);
}
else
{
switch (expression[i]) {
case '+':
{
int number1 = myStack.top();
myStack.pop();
int number2 = myStack.top();
myStack.pop();
int total = number1 + number2;
myStack.push(total);
break;
}
case '-':
{
int number1 = myStack.top();
myStack.pop();
int number2 = myStack.top();
myStack.pop();
int total = number1 - number2;
myStack.push(total);
break;
}
case '*':
{
int number1 = myStack.top();
myStack.pop();
int number2 = myStack.top();
myStack.pop();
int total = number1 * number2;
myStack.push(total);
break;
}
case '/':
{
int number1 = myStack.top();
myStack.pop();
int number2 = myStack.top();
myStack.pop();
int total = number1 / number2;
myStack.push(total);
break;
}
}
}
}
std::cout << "\tFinal value = " << myStack.top() << std::endl;
std::cout << "Continue(y/n)? ";
std::cin >> input;
std::cin.clear();
std::cin.ignore();
}
return 0;
}
This is the output after entering 'n' to terminate the program
答案 0 :(得分:0)
在Xcode版本10.1 beta 3(10O45e)中,此问题似乎已为我解决了