Solved programming problem

Basic infix Calculation in C++.

Source code

#include <bits/stdc++.h>

using namespace std;

string ans;

int marge(string val,string op)

{

    ofstream file;

    file.open("Out.txt",ios_base::app);

    ans = val[0] + op+val[2];

    int flag=0;

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

    {

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

        {

              flag=1;

             // break;

        }

    }

    if(flag==1)

    {

        if(op=="+")

        {

            file<<(val[0]-'0')+(val[2]-'0')<<endl;

        }

        else  if(op=="-")

        {

            file<<(val[0]-'0')-(val[2]-'0')<<endl;

        }

        else  if(op=="*")

        {

            file<<(val[0]-'0')*(val[2]-'0')<<endl;

        }

        else  if(op=="/")

        {

            file<<(val[0]-'0')/(val[2]-'0')<<endl;

        }

         else

        {

          file<< "Error Input"<<endl;

          return 0;

        }

    }

    else

    {

         file<< "Error Input"<<endl;

    }

   // cout<<ans<<endl;

}

int main()

{

    ifstream file;

    ifstream file1;

    file.open("In.txt");

    file1.open("Op.txt");

    while(!file.eof())

    {

        string val;

        getline(file,val);

        string op;

        getline(file1,op);

        marge(val,op);

    }

    return 0;

}

Output

Input:

2 4

4 5

7 5

5 six

another file:

+

-

*

/

Output:

6

-1

Error Input

Error Input
 

Join the discussion

Add your comment