Solved programming problem

Use 2 txt file parallel in C++.

Source code

#include <bits/stdc++.h>

using namespace std;

int a;

void findAns(string r)

{

    int t = r[0]-'0';

    // cout<<t<<endl;

    // cout<<"T"<<t-a<<endl;

    int ans = abs(t-a);

    cout<<pow(ans,3)<<endl;

}

int marge(string val)

{

    int flag=0;

    //cout<<val.size();

    if(val[0]>='0' &&val[0]<='9')

    {

        if(val[2]>='0' &&val[2]<='9')

        {

            flag=1;

            // break;

        }

    }

    if(flag==1)

    {

        if(val[1]=='+')

        {

            a=(val[0]-'0')+(val[2]-'0');

        }

        else  if(val[1]=='-')

        {

            a=(val[0]-'0')-(val[2]-'0');

        }

        else  if(val[1]=='*')

        {

            a=(val[0]-'0')*(val[2]-'0');

        }

        else  if(val[1]=='/')

        {

            a=(val[0]-'0')/(val[2]-'0');

        }

        else

        {

            cout<< "Error Input"<<endl;

            return 0;

        }

    }

    else

    {

       cout<<"Error Input"<<endl;

    }

}

int main()

{

    ifstream file;

    ifstream file1;

    file.open("expression.txt");

    file1.open("re.txt");

    while(!file.eof())

    {

        string val;

        getline(file,val);

        if(val.size()==3)

        {

            marge(val);

            string r;

            getline(file1,r);

            findAns(r);

        }

        else

        {

            cout<<"Error INput"<<endl;

        }

        marge(val);

    }

    return 0;

}

Output

Input inside a file:

2+4

4+5

6+6+           

another file:

5

6

7

output:

1

343

Error INput

    

Join the discussion

Add your comment