problem using rpn

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mido elnaggar
    New Member
    • Apr 2010
    • 5

    problem using rpn

    i 've done this code for converting a normal entry by user into rpn ,however this case doesn't work
    1+(2+3)/5->1235/++ which gives wrong answer
    the code is as follows:
    Code:
    #include"stdlib.h"
    #include"stdio.h"
    #include"conio.h"
    #include"string.h"
    void main()
    {
    	char n[20][100]={""};
    	char o[100]={""};
    	char c[100];
    	gets(o);
    	strcpy(c,o);
    	char* p=NULL;
    	int i=0,j=0,len=strlen(o);
    	char sep[]="()+-*/";
    	p=strtok (o,sep);
    	while(p!=NULL)
    	{
    				strcpy(n[i],p);	
    				p=strtok (NULL,sep);	
    		        i++;
    	}
    	j=len-1;
    	while(c[j]!=NULL)
    	{
    		if(c[j]!=' ')
    		{	
    		switch(c[j])
    				{
    		case'+':{strcpy(n[i],"+");i++;break;}
    		case'-':{strcpy(n[i],"-");i++;break;}
    		case'*':{strcpy(n[i],"*");i++;break;}
    		case'/':{strcpy([i],"/");i++;break;} 
    					default:break;
    					
    			
    				}
    		}
    	j--;
    	}
    
    	
    	for(j=0;j<20;j++)
    	{
    		printf("%s",n[j]);
    	  
    	}
    
    }
    and thanks in advance;
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Originally posted by mido elnaggar
    i 've done this code for converting a normal entry by user into rpn ,however this case doesn't work
    1+(2+3)/5->1235/++ which gives wrong answer
    the code is as follows:
    Code:
    #include"stdlib.h"
    #include"stdio.h"
    #include"conio.h"
    #include"string.h"
    void main()
    {
    	char n[20][100]={""};
    	char o[100]={""};
    	char c[100];
    	gets(o);
    	strcpy(c,o);
    	char* p=NULL;
    	int i=0,j=0,len=strlen(o);
    	char sep[]="()+-*/";
    	p=strtok (o,sep);
    	while(p!=NULL)
    	{
    				strcpy(n[i],p);	
    				p=strtok (NULL,sep);	
    		        i++;
    	}
    	j=len-1;
    	while(c[j]!=NULL)
    	{
    		if(c[j]!=' ')
    		{	
    		switch(c[j])
    				{
    		case'+':{strcpy(n[i],"+");i++;break;}
    		case'-':{strcpy(n[i],"-");i++;break;}
    		case'*':{strcpy(n[i],"*");i++;break;}
    		case'/':{strcpy([i],"/");i++;break;} 
    					default:break;
    					
    			
    				}
    		}
    	j--;
    	}
    
    	
    	for(j=0;j<20;j++)
    	{
    		printf("%s",n[j]);
    	  
    	}
    
    }
    and thanks in advance;
    what answer does it give........and what do you think is the correct answer?

    Comment

    • mido elnaggar
      New Member
      • Apr 2010
      • 5

      #3
      Originally posted by whodgson
      what answer does it give........and what do you think is the correct answer?
      the correct answer is 2 but it gives 3.6
      cause it didn't take the precidence of those between brackets
      all i want is amodification to operate on between brackets first and then continue.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Without running your code, all I can say is that I see no code here supporting operator precedence.

        Can you process 5*1+1*6 properly?
        • (incorrect, result = 35) 5 1 1 6 * + *
        • (incorrect, result = 36) 5 1 * 1 + 6 *
        • (correct, result = 11) 5 1 * 1 6 * +


        Or, perhaps simpler, 1 * 2 + 3
        • (correct, result = 5) 1 2 * 3 +
        • (incorrect, result = 7) 1 2 3 * +

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          What result is given when you input 1+((2+3)/5)?
          If 2 it is possible that the original parenthesis arrangement was meaningless to your code.

          Comment

          • mido elnaggar
            New Member
            • Apr 2010
            • 5

            #6
            Originally posted by Oralloy
            Without running your code, all I can say is that I see no code here supporting operator precedence.

            Can you process 5*1+1*6 properly?
            • (incorrect, result = 35) 5 1 1 6 * + *
            • (incorrect, result = 36) 5 1 * 1 + 6 *
            • (correct, result = 11) 5 1 * 1 6 * +


            Or, perhaps simpler, 1 * 2 + 3
            • (correct, result = 5) 1 2 * 3 +
            • (incorrect, result = 7) 1 2 3 * +
            look;my assignment project is to make a code that performs matrix calculations while the entry is as in the following example:
            A = [4+3-2*3+3/2 (1+2*(2+3)/(5-4*5)-3) 4*5*2/(19+3); 2.1+3.2 2.6-6/4.1 3.2+1.5; 2 4 5]
            B = [1 2 5; 5 2 2.1; 3 4 1]
            C = A+B
            D = A*B
            E = C/D
            The Calculated Value for E is:
            E=
            -1.3188045253055 4940000 1.1616421657009 4900000 -0.8846204118722 6370000
            -1.2445481286018 5360000 0.8671923594669 9669000 -0.4904292672460 2698000
            0.4358756466919 5888000 -0.1379480415197 6536000 0.2920168453180 1819000

            and i face aserious problem in the rpn function
            so can you help me wid that?

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Your switch statement does not include cases for ( or ).
              You may wish to refer to Bjarne Stroustrup`s Programming Principles and Practice Using C++. In particular chapters #6 & #7 which deal with some methods which can be applied to crude arithmetical calculators. You can also download these 2 calculators which have some bugs!

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Mido,

                We're trying to help. You still have to do your own homework.

                Good interpreters and parsers aren't trivial to write. They take thought and careful coding. And from what I see, you're not ready to use higher level tools like lex and yacc.

                That said, you may have done the first part in your code - breaking the input stream into tokens. I don't see anything in your code supporting equal-signs, square-brackets, or semicolons, as you show in your input example above, though. Spaces can wash out, but they are important, as they delineate the elements of a matrix row. Still, it looks like you've got a basic method going.

                Make that part work first. Once it does, then you can take that output and parse it.

                My suggestion is that you use a recursive descent parse of the token string. Each function will look at the next few tokens in the input stream and then decide what to do with them.

                BTW, Line 7 of your posted code block implies a token stream length of 20. The first line of your example input has well over that number of tokens.

                Also, Line 32 of your posted code block looks like it shouldn't compile.

                Will you please upload what you're really using?

                Comment

                • mido elnaggar
                  New Member
                  • Apr 2010
                  • 5

                  #9
                  ya i've only made the first level of c language in my field of study and here is the whole code i've made till now:
                  Code:
                  #include"stdio.h"
                  #include"stdlib.h"
                  #include"string.h"
                  char prefix[50];
                  int stack[50];
                  int top;
                  char infix[50];
                  char postfix[100];
                  int pre(char x)
                  {
                  	switch(x)
                  	{
                  	case'+':
                  	case'-':
                  		return 1;
                  	case'*':
                  	case'/':
                  		return 2;
                  	}
                  }
                  void push(int x)
                  {
                  	top++;
                  	stack[top]=x;
                  }
                  int pop()
                  {
                  	return stack[top--];
                  }
                  void main()
                  {
                  	int infixprec,len;
                  	int j=0;
                  	char next;
                  	
                  	int a,b,i,temp=0,result;
                  	gets(infix);
                  	len=strlen(infix);
                  	for(i=0;i<len;i++)
                  	{
                  		if(infix[i]!=' ')
                  		{
                  			switch(infix[i])
                  			{
                  			case'+':
                  			case'-':
                  			case'*':
                  			case'/':
                  				infixprec=pre(infix[i]);
                  				while(top!=1&&infixprec<=pre(stack[top]))
                  				{
                  					postfix[j+i]=pop();
                  				}
                  				push(infix[i]);
                  				break;
                  			case'(':push(infix[i]);break;
                  			case')':
                  				while(next=pop()!='(')
                  				{
                  					postfix[j++]=next;
                  					break;
                  				}
                  				
                  			default:
                  				break;
                  			}
                  			postfix[j+i]=infix[i];
                  		}
                  	}
                  	while(top!=-1)
                  	{
                  		postfix[j+i]=pop();
                  
                  	}
                  	postfix[j]='\0';
                  	for(i=0;i<len;i++)
                  	{
                  		if(postfix[i]<='50'&&postfix[i]>='0')
                  			push(postfix[i]);
                  		else
                  		{
                  			a=pop();
                  			b=pop();
                  			switch(postfix[i])
                  			{
                  			case'+':
                  				temp=a+b;break;
                  			case'-':
                  				temp=b-a;break;
                  			case'*':
                  				temp=a*b;break;
                  			case'/':
                  				temp=b/a;break;
                  			}
                  			push(temp);
                  		}
                  	}
                  	result=pop();
                  	
                  	
                  	top=-1;
                  	printf("enter infix");
                  	
                  	printf("value=%d",result);
                  }

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    A few quick comments from reading your code:
                    initialize top
                    shouldn't line 67 be on the default branch of your switch?
                    shouldn't the switch at line 43 just discard spaces, eliminating the extra if?
                    line 70 is a potentially infinite loop
                    line 78 has a syntax error

                    Comment

                    Working...