本文介绍了用C ++读取输入文本文件中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个代码来获取输入文本文件中的所有表达式并进行计算。但问题是,它只是计算第一个表达式。而且如果我在文本文件中包含---------来分隔表达式,它会给我编译错误。



我是什么尝试过:



i have have written a code to to take all expressions in an input text file and calculate them. But the problem is, it is only calculating the first expressions. And also if i include --------- in the text file to separate the expression it will give me compiler error.

What I have tried:

int main ()
{
   string inputLine;
   ifstream file ("input.txt");
   {
       while (std::getline (file, inputLine))
       {
           while(!file.eof())
           {
               spaces
               file.ignore(1,'\n');
               inputLine = trim (removeBackspaces (inputLine));
               Parser p (inputLine);
               double value = p.Evaluate ();
               ofstream file;
               file.open("output.txt");
               file <<std::cout << "Result = " << value << std::endl;
               double abc = p ["abc"];
               file<<std::cout << "abc = " << abc << std::endl;
               file<<std::cout<<"----------------------"<<endl;
           }
       }
       file.close();
    }
}



这是input.txt文件

----

sin(0)

----

cos(0)

----

pi = 3.14 radius = 3 sin(pi / 2)+ pi * radius ^ 2

----

1 + 2

----

10 - 5

----

3 * 4

----

12/4

------------

2 ^ 3


And this is the input.txt file
----
sin(0)
----
cos(0)
----
pi = 3.14 radius = 3 sin( pi / 2 ) + pi * radius ^ 2
----
1 + 2
----
10 - 5
----
3 * 4
----
12 / 4
------------
2 ^ 3

推荐答案


double abc = p ["abc"];

它看起来很可疑,但是如果没有看到Parser类的定义就很难知道。

It looks suspicious but it is hard to know without seeing the definition of the Parser class.


这篇关于用C ++读取输入文本文件中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:28
查看更多