我正在执行一个在两个多项式之间进行运算的项目。从一个名为“ functions.txt”的文本文件中读取多项式,格式为“(2 * x ^ 2 + 5 * x ^ 4-3 * x)+(6 * x + 2 * x ^ 3 + x ^ 5)”。这些方程式的数量未知,其运算符可以是“-”,“ +”或“''。我设法读取文件并将每个字符存储到字符数组中。在这一点上,我只是很难找到数学运算符(“,”-“或” +“)。我想出了在字符串中找到')'并在紧接其后的字符并将其存储到mathOperator中的方法;如果')'之后的字符不是'\ 0'。但是,这似乎不起作用,因为它返回“”。任何建议和帮助,我们将不胜感激。这是问题所在:
if(polyFile.good())
{
while(!polyFile.eof() && counter < MAX_SIZE)
{
polyFile.get(polyArray[counter]);
++counter;
}
polyArray[counter - 1] = '\0';
for(int i = 0; i < polyFile.eof(); ++i)
{
if(polyArray[i] = ')')
{
polyArray[i + 1] = mathOperator;
cout << mathOperator;
}
}
}
else
{
cout << "The file could not be opened." << endl;
}
最佳答案
这个区块有一些问题
for(int i = 0; i < polyFile.eof(); ++i)
{
if(polyArray[i] = ')')
{
polyArray[i + 1] = mathOperator;
cout << mathOperator;
}
}
1 /在for循环中,您要使用i
2 /在if语句中,您可能要使用if(!strcmp(polyArray [i],“)”))); “ =“是赋值运算符,不是比较运算符
3 /这行:
polyArray[i + 1] = mathOperator;
意味着您要将mathOperator分配给polyArray [i + 1],而不是将polyArray [i + 1]中的任何内容存储给mathOperator。这是您想要的:
mathOperator = polyArray[i + 1];