从文件中读取操作符并执行操作C ++

时间:2017-11-17 04:55:46

标签: c++

在这个问题中,我必须要求读取数据的文件名,如果回答'def',请使用提供的默认文件,并计算文件中的行数。 然后一次读取一行数据文件并计算w的值,如下所示: -if w是'add',计算x + y -if w是'sub',计算x-y -if w为'mult',计算x * y -if w是'div',计算x / y

然后一次将结果打印到屏幕上。

文件中的数据示例:

  

mult 4.25 4.56

     

div 7.64 1.01

     

div 6.51 2.46

     

mult 8.90 6.16

     

mult 7.40 8.53

     

sub 3.05 7.15

     

sub 9.51 6.16

     

sub 5.79 1.60

     

添加5.30 8.87

     

sub 8.09 1.65

我的代码:我有前三部分工作,但执行操作的while循环不起作用。我哪里错了?谢谢!

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main(){

    ifstream input;
    string filename;
    cout<<"Enter the file name. Enter 'def' to use default file: ";
    cin>>filename;

    if(filename == "def"){
        input.open("M2-P2-data2Read.dat");
    }
    else{
        input.open(filename.c_str());   
    }

    int number_of_lines = 0;
    string line;

    while (getline(input, line))
        ++number_of_lines;
    cout << "Number of lines in file: " << number_of_lines;

    string w;
    double x, y;

    input >> w;

    while(input) {
    if(w == "add")
        double x, y;
        input >> x >> y;
        cout << "Solution: " << x+y << endl;
    if(w == "sub")
        double x, y;
        input >> x >> y;
        cout << "Solution: " << x-y << endl;
    if(w == "mult")
        double x, y;
        input >> x >> y;
        cout << "Solution: " << x*y << endl;
    if(w == "div")
        double x, y;
        input >> x >> y;
        cout << "Solution: " << x/y << endl;
    input >> w;

   }


}   

2 个答案:

答案 0 :(得分:0)

即使您已表达了在if语句之后创建代码块的意图,但代码实际上并未被阻止。行

if(w == "add")
    double x, y;
    input >> x >> y;
    cout << "Solution: " << x+y << endl;

相当于

if(w == "add")
{ 
    double x, y; // A declaration that does not have any impact on 
                 // the rest of the lines.
}
input >> x >> y; // These variables are the x and y that were declared
                 // before the while statement.
cout << "Solution: " << x+y << endl;

现在,如果您转换下一个if语句,则会得到

if(w == "sub")
{
    double x, y;
 }
 input >> x >> y;
 cout << "Solution: " << x-y << endl;

您的代码尝试在没有读取操作的情况下第二次读入xy,但由于没有要读取的数字,因此操作失败。之后,所有读取操作都会失败。

您需要在if之后阻止语句。

if(w == "add")
{
    input >> x >> y;
    cout << "Solution: " << x+y << endl;
}

您可以通过在x语句之外移动阅读yif的行来简化您的代码。

input >> x >> y;
if(w == "add")
{
    cout << "Solution: " << x+y << endl;
}

else if(w == "sub")  // Use else if instead of just if.
{
    cout << "Solution: " << x-y << endl;
}

答案 1 :(得分:0)

我认为这就是你的目标:

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

int main() {
    std::ifstream input;
    std::string   filename;
    std::cout << "Enter the file name. Enter 'def' to use default file: ";
    std::cin >> filename;

    if ( filename == "def" ) {
        input.open( "Sample.dat" );
    } else {
        input.open( filename.c_str() );
    }

    int number_of_lines = 0;
    std::string line;

    while ( getline( input, line ) ) {
        ++number_of_lines;
    }
    std::cout << "\nNumber of lines in file: " << number_of_lines << "\n\n";

    input.clear();
    input.seekg( 0, input.beg );    

    std::string w;
    double x = 0;
    double y = 0;

    while ( input >> w >> x >> y ) {

        if ( w == "add" ) {
            std::cout << "Solution: " << x + y << std::endl;
        }

        if ( w == "sub" ) {
            std::cout << "Solution: " << x - y << std::endl;
        }

        if ( w == "mult" ) {
            std::cout << "Solution: " << x * y << std::endl;
        }

        if ( w == "div" ) {
            // Should test for denominator = 0
            std::cout << "Solution: " << x / y << std::endl;
        }

    }


    std::cout << "\nPress any key and enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}
相关问题