将字符串数组位置分配给字符串变量 我有一个C++程序,我正在做一个括号检查,把表达式从中缀转换成后缀,计算一个后缀方程,同时做三个。我已经完成了括号检查。我遇到的问题在后缀计算中。首先,我将包含方程式的文本文档读入字符串行数组。例如,第[6]行包含“73+9*”。因此,我想知道是否有一种方法可以让字符串暂时等于每个后缀等式 parentheses tests x = ((1 + 2)*3 x = (1 + 2))*3 y = (z * j)/(b * 8)^2 postfix equation solving 7 3 + 9 * 5 2 + 1 9 * * 8 9 * 3 5 + - 2 + 8 4 / infix to postfix conversion (8 + 4) * 9 (6 + 3) * (2 * 8) (9 * 8) – (5 + 3) 3 + (9 / 3) Parentheses check, infix to postfix conversion, postfix evaluation (8 + 4) * 9 (6 + 3) * (2 * 8) (9 * 8) – (5 + 3) 3 + (9 / 3)
这是我正在读的文件^^将字符串数组位置分配给字符串变量 我有一个C++程序,我正在做一个括号检查,把表达式从中缀转换成后缀,计算一个后缀方程,同时做三个。我已经完成了括号检查。我遇到的问题在后缀计算中。首先,我将包含方程式的文本文档读入字符串行数组。例如,第[6]行包含“73+9*”。因此,我想知道是否有一种方法可以让字符串暂时等于每个后缀等式 parentheses tests x = ((1 + 2)*3 x = (1 + 2))*3 y = (z * j)/(b * 8)^2 postfix equation solving 7 3 + 9 * 5 2 + 1 9 * * 8 9 * 3 5 + - 2 + 8 4 / infix to postfix conversion (8 + 4) * 9 (6 + 3) * (2 * 8) (9 * 8) – (5 + 3) 3 + (9 / 3) Parentheses check, infix to postfix conversion, postfix evaluation (8 + 4) * 9 (6 + 3) * (2 * 8) (9 * 8) – (5 + 3) 3 + (9 / 3),c++,arrays,postfix-notation,C++,Arrays,Postfix Notation,这是我正在读的文件^^ ifstream dataFile; dataFile.open("buff.txt"); string line[20]; //Array to hold data //read file into array while(dataFile) { for(int i = 0; i < 20; i++) { getline(dataFile, line[i]);
ifstream dataFile;
dataFile.open("buff.txt");
string line[20]; //Array to hold data
//read file into array
while(dataFile)
{
for(int i = 0; i < 20; i++)
{
getline(dataFile, line[i]);
}
}
dataFile.close();
ifstream数据文件;
打开(“buff.txt”);
弦线[20]//用于保存数据的数组
//将文件读入数组
while(数据文件)
{
对于(int i=0;i<20;i++)
{
getline(数据文件,第[i]行);
}
}
dataFile.close();
上面是我用来将文本文件中的信息存储到数组中的方法^^
//POSTFIX EVALUATION
stack<int> operation;
int operand1, operand2, result, number;
string input;
string s;
for(int k = 5; k < 9; k++)
{
input = line[k];//Assigning the postfix Eq to string(?)
for(int i = 0; i < input.length(); i++)
{
if (isdigit(input[i]))
{
s = input[i];
istringstream iss(s);
iss >> number;
operation.push(number);
}
else
{
operand2 = operation.top();
operation.pop();
operand1 = operation.top();
operation.pop();
if(input[i] == '+')
{
result = operand2 + operand1;
}
else if(input[i] == '-')
{
result = operand2 - operand1;
}
else if(input[i] == '*')
{
result = operand2 * operand1;
}
else
{
result = operand2 / operand1;
}
operation.push(result);
}
}
cout << "The result is: "<<result<<endl;
}
//后缀求值
堆栈操作;
整数操作数1,操作数2,结果,数字;
字符串输入;
字符串s;
对于(int k=5;k<9;k++)
{
输入=行[k];//将后缀Eq分配给字符串(?)
对于(int i=0;i>数字;
操作。推送(编号);
}
其他的
{
操作数2=operation.top();
pop()操作;
操作数1=operation.top();
pop()操作;
如果(输入[i]=='+')
{
结果=操作数2+操作数1;
}
else if(输入[i]='-')
{
结果=操作数2-操作数1;
}
else if(输入[i]='*')
{
结果=操作数2*操作数1;
}
其他的
{
结果=操作数2/操作数1;
}
操作推送(结果);
}
}
如果我正确理解了你的问题(我不确定我是否理解),它应该按原样工作(我确定)。不是吗?不幸的是,它没有。