读取 .txt 文件中的多行并为每一行输出一个答案 [英] Reading multiple lines in a .txt file and outputting an answer for each line

查看:30
本文介绍了读取 .txt 文件中的多行并为每一行输出一个答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,它接受来自 .txt 文件的输入并输出总和.我已经正确地得到了一行中数字的总和.我需要文件中所有行的输出.

I have written a program that accepts input from a .txt file and outputs the sum. I have correctly gotten the sum of the numbers in one line. I need outputs for all the lines in the file.

对此的要求是在 .txt 文件中包含一个分号来指示行尾.

The requirements for this are to include a semicolon to indicate end of line in the .txt file.

#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main()
{
    std::ifstream ifs{"formula.txt"};
    int sum = 0;
    ifs >> sum;
    //int input = 0;
    {
        for (char a; ifs >> a;)
        {
            int num;
            ifs >> num;
            if(a == '+')
            {
                sum += num;
            }

            if(a == '-')
            {
                sum -= num; 
            }

            if( a== ';')
            {
                sum += num;
                sum -= num;
            } 
        }
        cout << sum << endl;
    }
}

推荐答案

cout <<总和< 放在for 之后,所以你只在最后写一个值,而不是每次都写值';'到达

the line cout << sum << endl; is put after the for, so you write only one value at the end rather than the value each time ';' is reach

if( a== ';') 
{
  sum+= num;
  sum-= num;
} 

sum 不变,除非溢出

必须像

if( a== ';') 
{
   cout << sum << endl;
   sum = 0;
}

因为';'标记公式的结束.

because the ';' marks the end of a formula.

你的循环无法管理像 10 + 3 + 0 + 25 这样的形式,因为每次 for 你总是管理一个运算符然后一个数字

Your loop cannot manage a form like 10 + 3 + 0 + 25, because each turn of for you always manage an operator then a number

输入是 15;10 ... 并且无法管理,你读取 15 并将其保存在 sum 中,然后读取 ';'并分配 a ,然后您读取 10 并分配 num ,因此 10 丢失等

The input is 15;10 ... and than cannot be managed, you read 15 and save it in sum, then you read ';' and assign a with, then you read 10 and assign num with so 10 is lost etc

提案:

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
  ifstream ifs("formula.txt");

  if (!ifs.is_open()) {
    cout << "cannot open formula.txt" << endl;
    return -1;
  }

  int val;

  while (ifs >> val)
  {
    char op;

    while (ifs >> op)
    {
      if (op == ';')
      {
        cout << val << endl;
        break;
      }

      int num;

      if (! (ifs >> num)) {
        cout << "unexpected EOF" << endl;
        return -1;
      }

      if (op == '+')
      {
        val += num;
      }
      else if (op == '-')
      {
        val -= num; 
      }
      else {
        cout <<"invalid operator '" << op << '\'' << endl;
        return -1;
      }
    }
  }

  return 0;
}

编译执行

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra s.cc
pi@raspberrypi:/tmp $ cat formula.txt 
15;10+3+0+25;5+6-7-8+9+10-11; 
pi@raspberrypi:/tmp $ ./a.out
15
38
4
pi@raspberrypi:/tmp $ 

这篇关于读取 .txt 文件中的多行并为每一行输出一个答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆