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: - #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;
9 1834 @mido elnaggar
what answer does it give........and what do you think is the correct answer?
@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.
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 * +
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.
@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?
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!
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?
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:
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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()...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |