473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include"stdlib.h"
  2. #include"stdio.h"
  3. #include"conio.h"
  4. #include"string.h"
  5. void main()
  6. {
  7.     char n[20][100]={""};
  8.     char o[100]={""};
  9.     char c[100];
  10.     gets(o);
  11.     strcpy(c,o);
  12.     char* p=NULL;
  13.     int i=0,j=0,len=strlen(o);
  14.     char sep[]="()+-*/";
  15.     p=strtok (o,sep);
  16.     while(p!=NULL)
  17.     {
  18.                 strcpy(n[i],p);    
  19.                 p=strtok (NULL,sep);    
  20.                 i++;
  21.     }
  22.     j=len-1;
  23.     while(c[j]!=NULL)
  24.     {
  25.         if(c[j]!=' ')
  26.         {    
  27.         switch(c[j])
  28.                 {
  29.         case'+':{strcpy(n[i],"+");i++;break;}
  30.         case'-':{strcpy(n[i],"-");i++;break;}
  31.         case'*':{strcpy(n[i],"*");i++;break;}
  32.         case'/':{strcpy([i],"/");i++;break;} 
  33.                     default:break;
  34.  
  35.  
  36.                 }
  37.         }
  38.     j--;
  39.     }
  40.  
  41.  
  42.     for(j=0;j<20;j++)
  43.     {
  44.         printf("%s",n[j]);
  45.  
  46.     }
  47.  
  48. }
and thanks in advance;
Jul 5 '10 #1
9 1834
whodgson
542 512MB
@mido elnaggar
what answer does it give........and what do you think is the correct answer?
Jul 6 '10 #2
@whodgson
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.
Jul 6 '10 #3
Oralloy
988 Expert 512MB
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 * +
Jul 7 '10 #4
whodgson
542 512MB
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.
Jul 7 '10 #5
@Oralloy
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.31880452530554940000 1.16164216570094900000 -0.88462041187226370000
-1.24454812860185360000 0.86719235946699669000 -0.49042926724602698000
0.43587564669195888000 -0.13794804151976536000 0.29201684531801819000

and i face aserious problem in the rpn function
so can you help me wid that?
Jul 8 '10 #6
whodgson
542 512MB
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!
Jul 8 '10 #7
Oralloy
988 Expert 512MB
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?
Jul 8 '10 #8
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:
Expand|Select|Wrap|Line Numbers
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. #include"string.h"
  4. char prefix[50];
  5. int stack[50];
  6. int top;
  7. char infix[50];
  8. char postfix[100];
  9. int pre(char x)
  10. {
  11.     switch(x)
  12.     {
  13.     case'+':
  14.     case'-':
  15.         return 1;
  16.     case'*':
  17.     case'/':
  18.         return 2;
  19.     }
  20. }
  21. void push(int x)
  22. {
  23.     top++;
  24.     stack[top]=x;
  25. }
  26. int pop()
  27. {
  28.     return stack[top--];
  29. }
  30. void main()
  31. {
  32.     int infixprec,len;
  33.     int j=0;
  34.     char next;
  35.  
  36.     int a,b,i,temp=0,result;
  37.     gets(infix);
  38.     len=strlen(infix);
  39.     for(i=0;i<len;i++)
  40.     {
  41.         if(infix[i]!=' ')
  42.         {
  43.             switch(infix[i])
  44.             {
  45.             case'+':
  46.             case'-':
  47.             case'*':
  48.             case'/':
  49.                 infixprec=pre(infix[i]);
  50.                 while(top!=1&&infixprec<=pre(stack[top]))
  51.                 {
  52.                     postfix[j+i]=pop();
  53.                 }
  54.                 push(infix[i]);
  55.                 break;
  56.             case'(':push(infix[i]);break;
  57.             case')':
  58.                 while(next=pop()!='(')
  59.                 {
  60.                     postfix[j++]=next;
  61.                     break;
  62.                 }
  63.  
  64.             default:
  65.                 break;
  66.             }
  67.             postfix[j+i]=infix[i];
  68.         }
  69.     }
  70.     while(top!=-1)
  71.     {
  72.         postfix[j+i]=pop();
  73.  
  74.     }
  75.     postfix[j]='\0';
  76.     for(i=0;i<len;i++)
  77.     {
  78.         if(postfix[i]<='50'&&postfix[i]>='0')
  79.             push(postfix[i]);
  80.         else
  81.         {
  82.             a=pop();
  83.             b=pop();
  84.             switch(postfix[i])
  85.             {
  86.             case'+':
  87.                 temp=a+b;break;
  88.             case'-':
  89.                 temp=b-a;break;
  90.             case'*':
  91.                 temp=a*b;break;
  92.             case'/':
  93.                 temp=b/a;break;
  94.             }
  95.             push(temp);
  96.         }
  97.     }
  98.     result=pop();
  99.  
  100.  
  101.     top=-1;
  102.     printf("enter infix");
  103.  
  104.     printf("value=%d",result);
  105. }
  106.  
Jul 8 '10 #9
Oralloy
988 Expert 512MB
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
Jul 8 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: André Gasser | last post by:
hello newsgroup, I just discovered a weird effect in my php code. here is the flow of my code: 1. upload jepg file to server 2. create new (empty) jpeg file using imagecreatefromjpeg()...
4
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using...
2
by: dorin | last post by:
I wonder if anyone can help me with a problem using the DOM getElementById method. I am using the Xerces Java 2 library to parse an XML file and create and validate the DOM Document. I am able...
4
by: Derek Timothy | last post by:
Hi folks I have strange problem using cmd.exe from asp code I am trying to save the results of an FTP command into a text file. When I run this command from the command line of the web server it...
3
by: Jacky Zhu | last post by:
Hi all, I am having a problem trying to consume a webservice that is developed on ..Net. I can access it without any problem using a .net client, but when I use a java client (based on Axis...
4
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
1
by: sck10 | last post by:
Hello, I am pulling data from a SQL Server table. One field that is (varchar 4000) is used to show notes. I am using a FormView for showing and editing the data. When the form is in Item...
3
by: Geoff Tanaka | last post by:
Hi, I have created a DIB in C# and I am having a problem using it in a C++ DLL. Basically I have a C++ DLL which requires a DIB handle to be passed into it. I create the DIB using: IntPtr myDIB...
3
by: Franky | last post by:
Been having a problem using a treeview. Work great the first time the form containing it is entered but not the second time. Took a while to create a small sample that exhibits the problem, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.