Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c+中从文件流中分离数据+; 我刚开始在大学学习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_C++_Fstream_Sstream - Fatal编程技术网

如何在c+中从文件流中分离数据+; 我刚开始在大学学习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

如何在c+中从文件流中分离数据+; 我刚开始在大学学习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,c++,fstream,sstream,C++,Fstream,Sstream,对于我的代码,这是我能想到的最好的: #包括 #包括 #包括 #包括 使用名称空间std; 常量字符串文件\u name=“input.txt”; 浮点计算_数(浮点第一个_数、字符符号、浮点最后一个_数); int main(){ ifstream输入文件; 流输出文件; //打开文件以从中获取输入文件 input_file.open(文件名,ios::in); //错误检查 如果(!输入_文件){ cout>data\u from_file;//将文件内容传递给数据 while(getlin

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

#包括
#包括
#包括
#包括
使用名称空间std;
常量字符串文件\u name=“input.txt”;
浮点计算_数(浮点第一个_数、字符符号、浮点最后一个_数);
int main(){
ifstream输入文件;
流输出文件;
//打开文件以从中获取输入文件
input_file.open(文件名,ios::in);
//错误检查
如果(!输入_文件){
cout>data\u from_file;//将文件内容传递给数据
while(getline(输入文件、来自文件的数据)){
istringstream ss(来自文件的数据);
字符符号;
int第一个\u编号,最后一个\u编号;
ss>>第一个\u编号>>符号>>最后一个\u编号;

cout我建议您在存储数字和操作的地方实现一个。 这样你就可以把每一行分开

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 */
        }
    }
}

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

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


#包括
#包括
#包括
使用名称空间std;
const string INPUT_FILE=“INPUT.txt”;
常量字符串输出\u FILE=“result.txt”;
//全局变量
浮动第一个号码,第二个号码,回答;
字符算术运算;
ifstream input_file;//从中获取输入的文件
ofstream output_file;//要将结果放入的文件
//功能原型
浮点计算(浮点第一个数、字符算术运算、浮点第二个数);
int main(){
//打开文件以从中获取输入文件
input_file.open(input_file,ios::in);
//如果文件不存在,则出错
如果(!输入_文件){
cout>第一个数字>>算术运算>>第二个数字){
答案=计算(第一个数字、算术运算、第二个数字);
output_file.open(output_file,ios::out);

output_file你的方法基本上还不错。但是,你需要做更多的错误检查。还有一些语义错误

这是一个非常重要的规则,您总是检查输入/输出操作的结果

我首先向您展示基于您的原始草稿的更正版本:

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

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


float calculate_number(float first_number, char symbol, float last_number) {
    float result{ 0 };
    switch (symbol) {
    case '+':
        result = first_number + last_number;
        break;
    case '-':
        result = first_number - last_number;
        break;
    case '*':
        result = first_number * last_number;
        break;
    case '/':
        result = first_number / last_number;
        break;
    case '%':
        result = fmod(first_number, last_number);
        break;
    default:
        break;
    }
    return result;
}

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;
            float first_number, last_number;
            // Rad data and check, if that worked
            if (ss >> first_number >> symbol >> last_number) {
                // Check for a valid symbol
                if ((symbol == '+') || (symbol == '-') || (symbol == '*') || (symbol == '/') || (symbol == '%')) {
                    cout << first_number << " " << symbol << " " << last_number << " = "
                        << calculate_number(first_number, symbol, last_number) << endl;
                }
            }
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常量字符串文件\u name=“r:\\input.txt”;
浮点数计算浮点数(浮点数第一个、字符符号、浮点数最后一个){
浮点结果{0};
开关(符号){
格“+”:
结果=第一个\u编号+最后一个\u编号;
打破
案例'-':
结果=第一个\u编号-最后一个\u编号;
打破
案例“*”:
结果=第一个\u编号*最后一个\u编号;
打破
案例“/”:
结果=第一个\u编号/最后一个\u编号;
打破
案例“%”:
结果=fmod(第一个编号、最后一个编号);
打破
违约:
打破
}
返回结果;
}
int main(){
ifstream输入文件;
流输出文件;
//打开文件以从中获取输入文件
input_file.open(文件名,ios::in);
//错误检查
如果(!输入_文件){
cout>data\u from_file;//将文件内容传递给数据
while(getline(输入文件、来自文件的数据)){
istringstream ss(来自文件的数据);
字符符号;
浮动第一个\u编号、最后一个\u编号;
//无线电数据和检查,如果有效
如果(ss>>第一个\u编号>>符号>>最后一个\u编号){
//检查是否有有效的符号
如果((symbol='+')| |(symbol='-')| |(symbol='*')| |(symbol='/'))| |(symbol='%')){

很好,根据输出,您已经将它们分开了。现在您需要对它们应用适当的操作。您可以使用
switch
语句来区分这些操作。如下所示:
int result=0;switch(symbol){case'+':result=first_number+last_number;break;case'-':…}
回答您提出的确切问题:是的,有。但如果您真的想问为什么程序不工作,这就是调试器的作用,这是一个很好的机会,可以学习如何使用调试器一次运行一行程序,监视所有变量及其值的变化,并分析程序的逻辑执行flo知道如何使用调试器是每个C++开发者所需的技能,没有例外。在调试程序的帮助下,你应该能够快速找到你编写的所有程序中的所有bug,而不必向任何人求助。是的,有一种方法。在互联网上搜索“C++计算器解析”。也可以在因特网上搜索。“c++表达式计算器”。@SamVarshavchik这是我目前用来逐步检查while循环的每个输出的工具,谢谢你的建议
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>

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


float calculate_number(float first_number, char symbol, float last_number) {
    float result{ 0 };
    switch (symbol) {
    case '+':
        result = first_number + last_number;
        break;
    case '-':
        result = first_number - last_number;
        break;
    case '*':
        result = first_number * last_number;
        break;
    case '/':
        result = first_number / last_number;
        break;
    case '%':
        result = fmod(first_number, last_number);
        break;
    default:
        break;
    }
    return result;
}

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;
            float first_number, last_number;
            // Rad data and check, if that worked
            if (ss >> first_number >> symbol >> last_number) {
                // Check for a valid symbol
                if ((symbol == '+') || (symbol == '-') || (symbol == '*') || (symbol == '/') || (symbol == '%')) {
                    cout << first_number << " " << symbol << " " << last_number << " = "
                        << calculate_number(first_number, symbol, last_number) << endl;
                }
            }
        }
    }

    return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <numeric>
#include <map>
#include <utility>
#include <functional>

int main() {

    // All possible operations, defined in one place
    std::map<char, std::function<double(double,double)>> operation {
        {'+', std::plus<double>()},
        {'-', std::minus<double>()},
        {'*', std::multiplies<double>()},
        {'/', std::divides<double>()},
        {'%', static_cast<double(*)(double,double)>(std::fmod) }
    };

    // The file with the source data
    const std::string fileName{ "r:\\input.txt" };

    // Open the source file and check if it could be opened
    if (std::ifstream sourceStream(fileName); sourceStream) {

        // Read all lines of the source file
        for (std::string line{}; std::getline(sourceStream, line); ) {

            // For easier processing, put the line in a std::stringstream
            std::istringstream iss(line);

            // The operands and the operator
            double lhs{ 0.0 }, rhs{ 0.0 };
            char operationSymbol{};

            // Read the parts of the line and check, if read was OK
            if (iss >> lhs >> operationSymbol >> rhs) {

                // Now check, if we have read a valid operator
                if (operation.find(operationSymbol) != operation.end()) {

                    // Show the result
                    std::cout << lhs << " " << operationSymbol << " " << rhs << " = " << operation[operationSymbol](lhs, rhs) << "\n";
                }
                else std::cerr << "\n*** Error: Invalid operator '" << operationSymbol << "'\n\n";
            }
            else std::cerr << "\n*** Error: Problem while reading line:\n'" << line << "'\n\n";
        }
    }
    else std::cerr << "\n*** Error: Source file '" << fileName << "' could not be opened\n\n";

    return 0;
}