首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++ fstream,从文本中读取数据并执行数学运算?

C++ fstream,从文本中读取数据并执行数学运算?
EN

Stack Overflow用户
提问于 2013-03-09 06:52:26
回答 7查看 6K关注 0票数 0

我在这里束手无策。我正在用C++编写一个程序,它将读取一个文本文件,其中包含以下内容作为一个简短的示例:

代码语言:javascript
复制
+ 23 34
- 9 8
+ 100 1
* 8 7
^ 2 5
/ 45 8

读取器将第一个操作数存储在char类型中,并基于它检索到的char,调用一个函数来对这两个数字执行操作,以下是完成此操作的示例函数。

代码语言:javascript
复制
void doDivision(ifstream &inFile) {
    char ch;
    int num1, num2;

    inFile >> ch >> num1 >> num2;
    cout << "Division     " << num1 << "    " << num2 << "    " << "Quotient " << "    " << num1/num2 << " Remain " << num1%num2  << endl; 
}

一件事是,我不确定为什么这个参数是&,这个函数原型不是我写的,但是它来自一本书,也许这就是为什么我不能让它工作。

下面是我的主要函数:

代码语言:javascript
复制
int main()
{

ifstream inFile;
char ch;
int num1, num2;

inFile.open("math.txt");
if (inFile.fail())
{
cout << ch;
      cout << "The math.txt input file failed to open";
        return -1;
}


 while(inFile)
{
    switch (ch) {

    case '+':
        doAddition(inFile);
        break;
    case '-':
        doSubtraction(inFile);
        break;
    case '*':
        doMultiplication(inFile);
        cout << "debug " << ch;
        break;
    case '/':
        doDivision(inFile);
        break;
    case '!':
        doFactorial(inFile);
        break;
    default:
        cout << "Invalid Operation" << endl;

    }

    inFile >> ch;
}

inFile.close();
return 0;
}

所有这些加在一起会产生以下意想不到的结果

代码语言:javascript
复制
Invalid Operation 
Addition 3 34 sum 37 (wrong data in text file is 23 and 34)
subtraction 0 8 difference 8 (data in textfile is 9 and 8 respectively)

我该如何实现这一点,因为我以前从来没有处理过文件,所以我不知所措。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-03-09 06:57:07

一个简单的错误是,在while()循环中,直到第一次通过循环之后才调用inFile >> ch。试着修复它,看看它是否有帮助。

另外,什么是Aniket said in their answer是你需要注意的另一个问题。

因此,简而言之,您的循环应该大致如下:

代码语言:javascript
复制
inFile >> ch;
while(inFile) {
    switch(ch) {
    case '+':
        ...
    }
    inFile >> ch;
}

您的函数应该类似于:

代码语言:javascript
复制
void doDivision(ifstream &inFile) {
    int num1, num2;

    inFile >> num1 >> num2;

    ...
}
票数 4
EN

Stack Overflow用户

发布于 2013-03-09 06:57:23

由于您已经在main()中执行inFile >> ch,因此在doAddition()和其他方法中再次读取它将是错误的(逻辑上)。

既然您已经在读入ch字符的地方发布了doDivision()方法,我假设您在doAddition()中也是这样做的。

还有你的输出:

加法3 34和37 (文本文件中错误的数据是23和34)

告诉我们-您已经读取了1个字符(找到了+),然后在doAddition()方法中读取了另一个字符,-将2读取到doAddition()ch中,然后读取334

这才是错误的真正所在。

解决方案:将您的doAddition()和所有其他函数更改为如下所示。

代码语言:javascript
复制
void doAddition(ifstream &inFile) {
    char ch;
    int num1, num2;

    inFile >> num1 >> num2;
    cout << "Addition of " << num1 << " and " << num2 << " = "<< (num1+num2) << '\n';
}

另外,在main()函数中:

while循环应该如下所示:

代码语言:javascript
复制
while(inFile)
{
    inFile >> ch;
    switch (ch) {

    case '+':
        doAddition(inFile);
        break;
    case '-':
        doSubtraction(inFile);
        break;
    case '*':
        doMultiplication(inFile);
        cout << "debug " << ch;
        break;
    case '/':
        doDivision(inFile);
        break;
    case '!':
        doFactorial(inFile);
        break;
    default:
        cout << "Invalid Operation" << endl;

    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-03-09 06:57:47

您不需要在第一次迭代时读入ch。实际上,当您检查打开的文件没有问题时,尽管ch未初始化,但仍执行了cout << ch;。您可以简单地将inFile >> ch;移到while循环的顶部。

下一个问题是,在每个doSomething函数中,您再次执行inFile >> ch,这将尝试再次读取操作字符。但是,doSomething函数不需要知道ch,因为函数本身是根据ch的值选择的。

下面是我写这段代码的方法:

代码语言:javascript
复制
ifstream inFile("math.txt"); // You can specify the file name here
char op;
int left_operand, right_operand;

// Use extraction has while condition
while (inFile >> op >> left_operand >> right_operand) {
  switch (op) {
    // Pass operands to the relevant function
    case '+': doAddition(left_operand, right_operand); break;
    // ...
  }
}

// You do not need to close inFile, it will be closed when it goes out of scope
return 0;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15304690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档