DelilahDemented 0 Newbie Poster

The program runs and gives the correct data until it gets to the 4th line of the input. At the 4th line, the calculation is incorrect and when it gets to line 6 of the input, it just stops completely. I can't quite figure it out. Any help is greatly appreciated!

input:
5+7
6^2+1
3*8+6
5^4-6^4
5-3*7/4-9
8*(9/3-2)/4+5*6
5^3*4+(2+(9*8/(2*6*(8/4)))^2*8-5)/5^2-4
5-3*8/2^3
3^3^2*3
8*(6/3-2)/4+5*6+3

output:
Infix : 5+7post fix :57+
Answer : 12

Infix : 6^2+1post fix :62^1+
Answer : 37

Infix : 3*8+6post fix :38*6+
Answer : 30

Infix : 5^4-6^4post fix :54^64^-
Answer : 97

Infix : 5-3*7/4-9post fix :537*4/-9-
Answer : -9

Infix : 8*(9/3-2)/4+5*6post fix :893/2*4/56*+
Unknown operator





const int size =100;
char infix[size],postfix[size],stack[size];
int top=-1;

int precedence(char ch);
char pop();
char topelement();
void push(char ch);
void calculate(char *s);

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
{
    //Open inFile
    inFile.open("infix.txt");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("Results.txt");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

     while(inFile.good())
     {
         char ele,elem;
        int prep,pre,popped,j=0;

         //gets(infix);
         inFile.getline(infix,size,'\n');
         cout<<endl<<"Infix : "<<infix;
         outFile<<endl<<"Infix : "<<infix;

         for(int i=0;infix[i]!=0;i++)
          {
                  if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')
                       postfix[j++]=infix[i];
                  else if(infix[i]=='(')
                      {
                         elem=infix[i];
                         push(elem);
                      }
                  else if(infix[i]==')')
                      {
                         while((popped=pop() != '('))
                             postfix[j++]=popped;
                      }
                  else
                      {
                         elem=infix[i];
                         pre=precedence(elem);
                         ele=topelement();
                         prep=precedence(ele);

                         if(pre > prep)
                           push(elem);

                         else
                           {
                                while(prep >= pre)
                                  {
                                     if(ele=='#')
                                       break;
                                     popped=pop();
                                     ele=topelement();
                                     postfix[j++]=popped;
                                     prep=precedence(ele);
                                   }
                                   push(elem);
                            }
                         }
             }

          while((popped=pop())!='#')
              postfix[j++]=popped;
          postfix[j]='\0';

          cout<<"post fix :"<<postfix<<endl;
          outFile<<"post fix :"<<postfix<<endl;
          calculate(postfix);

     }
          system("pause");
           return 0;
}

int precedence(char ch)
{
       switch(ch)
          {
               case '^' : return 5;
               case '/' : return 4;
               case '*' : return 4;
               case '+' : return 3;
               case '-' : return 3;
               default  : return 0;
          }
}

char pop()                  //function to pop the element from the stack
{
     char ret;
     if(top!=-1)
       {  ret =stack[top];
          top--;
          return ret;
       }
     else
        return '#';
}

char topelement()          // function to return top element from the stack without popping
{
      char ch;
      if(top!=-1)
        ch=stack[top];
      else
         ch='#';
       return ch;
}

void push(char ch)          // function to push an element in the stack
{
     if(top!=size-1)
         {
            top++;
            stack[top]= ch;
         }
}
void calculate(char *s)
{
    int n1, n2, n3 ,nn;
    while ( *s )
    {
        if ( *s == ' ' || *s == '\t' )
        {
            s++ ;
            continue ;
        }
        if ( isdigit ( *s ) )
        {
            nn = *s - '0' ;
            push ( nn ) ;
        }
        else
        {
            n1 = pop( ) ;
            n2 = pop( ) ;
            switch ( *s )
            {
                case '+' :
                    n3 = n2 + n1 ;
                    break ;
                case '-' :
                    n3 = n2 - n1 ;
                    break ;
                case '/' :
                    n3 = n2 / n1 ;
                    break ;
                case '*' :
                    n3 = n2 * n1 ;
                    break ;
                case '%' :
                    n3 = n2 % n1 ;
                    break ;
                case '^' :
                    double temp,res;
                    temp=n2;
                    res = pow (temp, n1 ) ;
                    n3=res;
                    break ;
                default :
                    cout<<*s;
                    cout << "Unknown operator" ;
                    outFile << "Unknown operator" ;
                    exit (0) ;
            }

            push ( n3 ) ;
        }
        s++ ;
    }
    cout<<"Answer : "<<n3 << endl;
    outFile<<"Answer : "<<n3 << endl;
    top=-1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.