如何在c ++中从文件流中分离数据

问题描述 投票:0回答:2

我刚刚开始在大学里学习c ++,在我们的第一个项目中,我们被要求创建一个五功能计算器(+,-,*,/,%),该计算器从文件中读取输入,对其进行算术运算并将其输出到屏幕。我得到了一个输入文件,就像所有数字一样:

/   100     10
109.5 +     1.0
25    /     2
25.5  /     2
0     /     0
8     *     8
10    -    10
ab    -    ab
100   -     1
10    *   -10
10.5  +  1000
1000  -  1.05
1000  *  1000
1000  /  1000
1000  /     0

对于我的代码,这是我能想到的最好的:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
const string file_name = "input.txt";


float calculate_number (float first_number, char symbol, float last_number);

int main () {
    ifstream input_file;
    ofstream output_file;

    // Open file to get input_file from
    input_file.open(file_name, ios::in);

    // Error checking
    if (!input_file) {
        cout << "Error: file input.txt does not exists\n";
        exit(1);
    } else {

        string data_from_file;           // Declare string to store data from file
        input_file >> data_from_file;    // Pass file content to data

        while (getline(input_file, data_from_file)) {
            istringstream ss(data_from_file);

            char symbol;
            int first_number, last_number;

            ss >> first_number >> symbol >> last_number;
            cout << first_number << symbol << last_number << endl;
        }
    }

    return 0;
}

上面的代码给出了以下输出,虽然很混乱,但仍然向前迈出了一大步:

10010
109.5
25/2
25.5
0/0
8*8
10-10
0-10
100-1
10*-10
10.5
1000-1
1000*1000
1000/1000
1000/0

我的问题是,它们是一种将每个数字和算术运算符与输入流分离,将它们加在一起并将输出显示在屏幕上的方法。 提前感谢

c++
2个回答
0
投票

我建议实现一个structure,用于存储您的数字和操作。这样,您可以分隔每一行。

struct calc {
    int a, b;
    char o;
};

int main() {
    std::ifstream input("input.txt");
    if (!input) {
        /* Handle error */
    } else {
        while (!input.eof()) {
            calc temp;
            input >> temp.a >> temp.o >> temp.b;
            /* Store it as you like */
        }
    }
}

您可以将最终数据存储在vectorlist中。


0
投票

从输入文件中获取信息的最简单方法是在迭代从输入文件获得的结果的同时提取信息,并将其传递给变量以方便参考。


#include <iostream>
#include <string>
#include <fstream>

using namespace std;
const string INPUT_FILE = "input.txt";
const string OUTPUT_FILE = "result.txt";


// Global variables

float first_number, second_number, answer;
char arithemetic_op;
ifstream input_file;     // File to get input from
ofstream output_file;    // File to put result in

// function prototype

float calculate (float first_number, char arithemetic_op, float second_number);


int main () {

    // Open file to get input_file from
    input_file.open(INPUT_FILE, ios::in);

    // Error if file does not exists
    if (!input_file) {
        cout << "Error: file `input.txt` does not exists\n";
        exit(1);
    }


    while (input_file >> first_number >> arithemetic_op >> second_number) {
        answer = calculate(first_number, arithemetic_op, second_number);

        output_file.open(OUTPUT_FILE, ios::out);
        output_file << first_number << " " << arithemetic_op << " " << second_number << " = " << answer << endl;
        output_file.close();
    }
    return 0;
}

float calculate (float first_number, char arithemetic_op, float second_number) {
    switch (arithemetic_op) {
        case '-':
            answer = first_number - second_number;
            return answer;
        case '+':
            answer = first_number + second_number;
            return answer;
        case '*':
            answer = first_number * second_number;
            return answer;
        case '/':
            answer = first_number / second_number;
            return answer;
    }
}

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.