By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,380 Members | 3,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,380 IT Pros & Developers. It's quick & easy.

convert infix to postfix

P: n/a
i've been working on this program forever! now i'm stuck and going
insane because i keep getting a syntax error msg and i just can't see
what the compiler is signaling to!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. /* constants */
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10. /* structure for stack */
  11. typedef struct
  12. {
  13. char data[40];  /* array to hold stack contents */
  14. int tos;        /* top of the stack pointer */
  15. }
  16. STACK;
  17.  
  18. /* function prototypes */
  19. void initStack(STACK *stack);
  20. void get_infix(char infix[]);
  21. void convertToPostfix(char infix[], char postfix[]);
  22. int isOperator(char c);
  23. int precedence(char operator1, char operator2);
  24. int pred_level(char ch);
  25. void push(STACK *stack, char value);
  26. char pop(STACK *stack);
  27. char stackTop(STACK *stack);
  28. int isEmpty(STACK *stack);
  29. int isFull(STACK *stack);
  30. void printResult(char infix[], char postfix[]);
  31. void print_msg(void);
  32.  
  33. /* program entry point */
  34. int main()
  35. {
  36. char infix[40], postfix[40]="";
  37.  
  38. /* convert from infix to postfix main function */
  39. convertToPostfix(infix, postfix);
  40. /* display the postfix equivalent */
  41. infix[strlen(infix)-2] = '\0';
  42. printResult(infix, postfix);
  43.  
  44. return EXIT_SUCCESS;
  45. }
  46.  
  47. /* initalise the stack */
  48. void initStack(STACK *stack)
  49. {
  50. stack->tos = -1;  /* stack is initially empty */
  51. }
  52.  
  53. /* get infix expression from user */
  54. void get_infix(char infix[])
  55. {
  56. int i;
  57.  
  58. printf("Enter infix expression: ");
  59. fflush(stdin);
  60.  
  61. for ( i=0; i<40; )
  62. {
  63. if ( (infix[i] = getchar()) == '\n' )
  64. {
  65. i++;
  66. break;
  67. }
  68. else if ( !(isspace(infix[i])) )
  69. i++;
  70. }
  71.  
  72. infix[i] = '\0';
  73. }
  74.  
  75. /* convert the infix expression to postfix notation */
  76. void convertToPostfix(char infix[], char postfix[])
  77. {
  78. int i, length;
  79. int j=0;
  80. char tos_ch;
  81. STACK stack;
  82.  
  83. initStack(&stack); /* initialise stack */
  84. get_infix(infix);  /* get infix expression from user */
  85. length = strlen(infix);
  86.  
  87. if ( length )
  88. {
  89. push(&stack, '(');
  90. strcat(infix, ")");
  91. length++;
  92.  
  93. for ( i=0; i<length; i++ )
  94. {
  95. /* if current operator in infix is digit */
  96. if ( isdigit(infix[i]) )
  97. {
  98. postfix[j++] = infix[i];
  99. }
  100. /* if current operator in infix is left parenthesis */
  101. else if ( infix[i] == '(' )
  102. {
  103. push(&stack, '(');
  104. }
  105. /* if current operator in infix is operator */
  106. else if ( isOperator(infix[i]) )
  107. {
  108. while ( TRUE )
  109. {
  110. /* get tos */
  111. tos_ch = stackTop(&stack);
  112.  
  113. /* no stack left */
  114. if ( tos_ch == '\0' )
  115. {
  116. printf("\nFull Stack!\n");
  117. print_msg();
  118. exit(1);
  119. }
  120. else
  121. {
  122. if ( isOperator(tos_ch) )
  123. {
  124. if ( pred_level(tos_ch) >=
  125. pred_level(infix[i]) )
  126. postfix[j++] = pop(&stack);
  127. else
  128. break;
  129. }
  130. else
  131. break;
  132. }
  133. }
  134. push(&stack, infix[i]);
  135. }
  136. /* if current operator in infix is right parenthesis */
  137. else if ( infix[i] == ')' )
  138. {
  139. while ( TRUE )
  140. {
  141. /* get tos */
  142. tos_ch = stackTop(&stack);
  143.  
  144. /* no stack left */
  145. if ( tos_ch == '\0' )
  146. {
  147. printf("\nFull Stack!\n");
  148. print_msg();
  149. exit(1);
  150. }
  151. else
  152. {
  153. if ( tos_ch != '(' )
  154. {
  155. postfix[j++] = tos_ch;
  156. pop(&stack);
  157. }
  158. else
  159. {
  160. pop(&stack);
  161. break;
  162. }
  163. }
  164. }
  165. continue;
  166. }
  167. }
  168. }
  169.  
  170. postfix[j] = '\0';
  171. }
  172.  
  173. /* determine if c is an operator */
  174. switch (c) {
  175. case '+':
  176. return TRUE;
  177. break;
  178. case '-':
  179. return TRUE;
  180. break;
  181. case '*':
  182. return TRUE;
  183. break;
  184. case '/':
  185. return TRUE;
  186. break;
  187. default:
  188. return FALSE;
  189. break;
  190. /*if ( c == '+' || c == '-' || c == '*' ||
  191. c == '/' || c == '%' || c == '^' )
  192. {
  193. return TRUE;
  194. }
  195. else
  196. return FALSE; */
  197. }
  198.  
  199. /* determine precedence level */
  200. int pred_level(char ch)
  201. {
  202. if ( ch == '+' || ch == '-' )
  203. return 1;
  204. else
  205. return 2;
  206. }
  207.  
  208. /* determine if the precedence of operator1 is less than,
  209. equal to, greater than the precedence of operator2 */
  210. int precedence(char operator1, char operator2)
  211. {
  212. if ( pred_level(operator1) > pred_level(operator2) )
  213. return 1;
  214. else if ( pred_level(operator1) < pred_level(operator2) )
  215. return -1;
  216. else
  217. return 0;
  218. }
  219.  
  220. /* push a value on the stack */
  221. void push(STACK *stack, char value)
  222. {
  223. if ( !(isFull(stack)) )
  224. {
  225. (stack->tos)++;
  226. stack->data[stack->tos] = value;
  227. }
  228. }
  229.  
  230. /* pop a value off the stack */
  231. char pop(STACK *stack)
  232. {
  233. char ch;
  234.  
  235. if ( !(isEmpty(stack)) )
  236. {
  237. ch = stack->data[stack->tos];
  238. (stack->tos)--;
  239. return ch;
  240. }
  241. else
  242. return '\0';
  243. }
  244.  
  245. /* return the top value of the stack without popping the stack */
  246. char stackTop(STACK *stack)
  247. {
  248. if ( !(isEmpty(stack)) )
  249. return stack->data[stack->tos];
  250. else
  251. return '\0';
  252. }
  253.  
  254. /* determine if stack is empty */
  255. int isEmpty(STACK *stack)
  256. {
  257. /* empty */
  258. if ( stack->tos == -1 )
  259. return TRUE;
  260. /* not empty */
  261. else
  262. return FALSE;
  263. }
  264.  
  265. /* determine if stack is full */
  266. int isFull(STACK *stack)
  267. {
  268. /* full */
  269. if ( stack->tos == 19 )
  270. return TRUE;
  271. /* not full */
  272. else
  273. return FALSE;
  274. }
  275.  
  276. /* display the result postfix expression */
  277. void printResult(char infix[], char postfix[])
  278. {
  279. /*system("cls");*/
  280. printf("\n\n");
  281. printf("Infix notation: %d%s\n", infix);
  282. printf("Postfix notation: %d%s\n\n", postfix);
  283. print_msg();
  284. }
  285.  
  286. /* print exit message */
  287. void print_msg(void)
  288. {
  289. printf("Hit <RETURN> to exit...");
  290. fflush(stdin);
  291. getchar();
  292. }
  293.  
the error msg i get is: syntax error before "switch"

i don't get it :S ... any suggestions?

Nov 19 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
ok i finally fixed the error!!! but the program outputs operators and
numbers, no variables

for example: if i input a*b+c*d/2 , it gives **2/+

so what now?

Nov 19 '05 #2

P: n/a
caramel wrote:
the error msg i get is: syntax error before "switch"

i don't get it :S ... any suggestions?


Which function is the switch statement supposed to be in?

--
pete
Nov 19 '05 #3

P: n/a

pete wrote:
caramel wrote:
the error msg i get is: syntax error before "switch"

i don't get it :S ... any suggestions?


Which function is the switch statement supposed to be in?

--

the switch is in void convertToPostfix(char infix[], char postfix[])
what i want to know is where is 'c'. i've attempted to trace the
program state and can't find out where it is. You don't seem to pass it
as an argument. Also flushing stdin causes undefined behaviour, take
that out.

Nov 19 '05 #4

P: n/a
bitshadow wrote:

pete wrote:
caramel wrote:
the error msg i get is: syntax error before "switch"

i don't get it :S ... any suggestions?


Which function is the switch statement supposed to be in?

--

the switch is in void convertToPostfix(char infix[], char postfix[])
what i want to know is where is 'c'.


Maybe that's where it's supposed to be,
which is what I asked, but that's not where it is.

I'd like to see an example of the output is supposed to be
for a given input.

--
pete
Nov 19 '05 #5

P: n/a
pete wrote:

bitshadow wrote:

pete wrote:
caramel wrote:

> the error msg i get is: syntax error before "switch"
>
> i don't get it :S ... any suggestions?

Which function is the switch statement supposed to be in?

--

the switch is in void convertToPostfix(char infix[], char postfix[])
what i want to know is where is 'c'.


Maybe that's where it's supposed to be,
which is what I asked, but that's not where it is.


I think the switch is supposed to be in isOperator().
Where isOperator is, I don't know.

--
pete
Nov 19 '05 #6

P: n/a
well i fixed the switch, now i need to get it to print variables

it works as intended with numbers and operators, but for some reason it
does not print the variables entered to the console

Nov 19 '05 #7

P: n/a
caramel wrote:

well i fixed the switch, now i need to get it to print variables

it works as intended with numbers and operators,
but for some reason it
does not print the variables entered to the console


Probably has something to do with the current state of your code.

--
pete
Nov 19 '05 #8

P: n/a
true, i was wondering if anyone can point out any mistake(s) or suggest
a fragment of code i can insert to display variables

Nov 19 '05 #9

P: n/a
On 18 Nov 2005 15:42:10 -0800, "caramel" <es********@gmail.com> wrote:
i've been working on this program forever! now i'm stuck and going
insane because i keep getting a syntax error msg and i just can't see
what the compiler is signaling to!
snip/* convert the infix expression to postfix notation */
void convertToPostfix(char infix[], char postfix[])
You did an excellent job of lining up all your braces.
{ snip if ( length )
{ snip for ( i=0; i<length; i++ )
{ snip if ( isdigit(infix[i]) )
{ snip } snip else if ( infix[i] == '(' )
{ snip } snip else if ( isOperator(infix[i]) )
{
while ( TRUE )
{ snip if ( tos_ch == '\0' )
{ snip }
else
{
if ( isOperator(tos_ch) )
{
if ( pred_level(tos_ch) >= pred_level(infix[i]) ) snip else snip }
else snip }
} snip } snip else if ( infix[i] == ')' )
{
while ( TRUE )
{ snip if ( tos_ch == '\0' )
{ snip }
else
{
if ( tos_ch != '(' )
{ snip }
else
{ snip }
}
} snip }
}
} snip}
This brace ends the function.

/* determine if c is an operator */
switch (c) {


You cannot put a switch statement (or most other statements) outside
of a function.

snip
<<Remove the del for email>>
Nov 19 '05 #10

P: n/a
the brace after (c) ?

and thanks for the compliment, i am a neat freak ;)
but u guys the program works fine now, and i get no more error msgs,
just that one problem that it drops out the variables and only displays
numbers and operators... how do i fix that?

Nov 19 '05 #11

P: n/a
caramel wrote:
the brace after (c) ?

and thanks for the compliment, i am a neat freak ;)
but u guys the program works fine now, and i get no more error msgs,
just that one problem that it drops out the variables and only displays
numbers and operators... how do i fix that?


In your for loop in convertToPostfix, you checked for:

1. isdigit
2. the opening bracket '('
3. isOperator
4. the closing bracket ')'

but ignored everything else. That's why you keep droping variables. I
suggest that after the last else if put an else to accept everything
else as variables. If you want to restrict variables to alphabets then
check for isAnAcceptableVariable.

Nov 19 '05 #12

P: n/a
thanks for the input, i'll go through with that and see what happens

many thanks and much appreciation to all who shared their thoughts :)

Nov 19 '05 #13

P: n/a
"caramel" <es********@gmail.com> writes:
the brace after (c) ?

and thanks for the compliment, i am a neat freak ;)
but u guys the program works fine now, and i get no more error msgs,
just that one problem that it drops out the variables and only displays
numbers and operators... how do i fix that?


Some suggestions on posting style:

Don't assume your readers can see the article to which you're
replying. You need to provide some context. Google makes it
unecessarily difficult to do this, but there is a workaround (which
has been posted here over 1000 times):

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Also, please don't use abbreviations like "u" for "you". They just
make your text more difficult to read. Proper capitalization is also
helpful.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 19 '05 #14

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
Don't assume your readers can see the article to which you're
replying. You need to provide some context. Google makes it
unecessarily difficult to do this, but there is a workaround (which
has been posted here over 1000 times):

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
You are wasting your breath. They'll never get it. And I'll tell you why.

Imagine that there's a mouse - and the mouse is the Usenet. You and I can
see that it is a mouse and we behave accordingly. But now there is a class
of users (we'll call them "googlers") that are wearing these funny weird
glasses that make them see not a mouse, but an elephant. Seeing an
elephant (i.e., the Usenet as a web page), they also behave accordingly.
And no amount of verbiage from us is going to convince them that it's not
an elephant - that it is only a mouse.

To make this more clear, to a googler, it doesn't make any sense to "quote"
(whatever the heck that is...), in fact, to do so would be absurd, when all
the rest of the articles in the thread are right there in front of their
faces (just as clear as the trunk on that mouse, er, elephant). And no
amount of verbiage from us is going to convince them not to believe what
they see. The point is you can *never* convince someone that what they see
isn't reality. The only way you can address the problem is to help them
fix their eyesight (or help them remove their funny glasses).
Also, please don't use abbreviations like "u" for "you". They just
make your text more difficult to read. Proper capitalization is also
helpful.


Good advice. tnk u.

Nov 19 '05 #15

P: n/a

Kenny McCormack wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
...
Don't assume your readers can see the article to which you're
replying. You need to provide some context. Google makes it
unecessarily difficult to do this, but there is a workaround (which
has been posted here over 1000 times):

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.


You are wasting your breath. They'll never get it. And I'll tell you why.

Imagine that there's a mouse - and the mouse is the Usenet. You and I can
see that it is a mouse and we behave accordingly. But now there is a class
of users (we'll call them "googlers") that are wearing these funny weird
glasses that make them see not a mouse, but an elephant. Seeing an
elephant (i.e., the Usenet as a web page), they also behave accordingly.
And no amount of verbiage from us is going to convince them that it's not
an elephant - that it is only a mouse.

To make this more clear, to a googler, it doesn't make any sense to "quote"
(whatever the heck that is...), in fact, to do so would be absurd, when all
the rest of the articles in the thread are right there in front of their
faces (just as clear as the trunk on that mouse, er, elephant). And no
amount of verbiage from us is going to convince them not to believe what
they see. The point is you can *never* convince someone that what they see
isn't reality. The only way you can address the problem is to help them
fix their eyesight (or help them remove their funny glasses).
Also, please don't use abbreviations like "u" for "you". They just
make your text more difficult to read. Proper capitalization is also
helpful.


Good advice. tnk u.


Hmmm, well yes, you're right. I thought this Google board thing works
like normal forums do. Guess I was wrong! I still don't really
understand why you can't refer to previous posts, but nonetheless I
will do my best to adhere to your rules. Sorry for any inconvenience!

Nov 19 '05 #16

P: n/a
On 19 Nov 2005 08:02:01 -0800, in comp.lang.c , "caramel"
<es********@gmail.com> wrote:

Hmmm, well yes, you're right. I thought this Google board thing works
like normal forums do.
Google doesn't work like anything. Its a web interface to usenet, and
the majority of people reading usenet do not use google, but proper
newsreaders which display the information in a very different format.
I still don't really
understand why you can't refer to previous posts,
Because the servers other people are getting the posts from may not
carry all the old messages, or the reader may not want to have to
fetch additional material, or may be on dialup where every second
downloading costs money.

Another way to think about this: each message you send is a sort of
email, sent to dozens, possibly thousands of people who are reading
the newsgroup. You can't realistically expect them all to have kept
all the email they recieved, just in case they needed to refer back to
it.
but nonetheless I will do my best to adhere to your rules.


Thanks
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #17

P: n/a
"caramel" <es********@gmail.com> writes:
[...]
Hmmm, well yes, you're right. I thought this Google board thing works
like normal forums do. Guess I was wrong! I still don't really
understand why you can't refer to previous posts, but nonetheless I
will do my best to adhere to your rules. Sorry for any inconvenience!


Google Groups is an interface to Usenet, a system that has been in
existence for decades longer than Google has. Unfortunately, the
folks at Google screwed up the interface, making things difficult for
those of us who use other interfaces. Our attempts to get them to fix
there broken interface have so far been unsuccessful.

(Incidentally, Kenny McCormack is a self-proclaimed troll. Please
ignore him.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 19 '05 #18

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
(Incidentally, Kenny McCormack is a self-proclaimed troll. Please
ignore him.)


But at least he knows how to spell 'their' - as in 'their interface'.

Nov 19 '05 #19

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"caramel" <es********@gmail.com> writes:
[...]
Hmmm, well yes, you're right. I thought this Google board thing works
like normal forums do. Guess I was wrong! I still don't really
understand why you can't refer to previous posts, but nonetheless I
will do my best to adhere to your rules. Sorry for any inconvenience!
Google Groups is an interface to Usenet, a system that has been in
existence for decades longer than Google has. Unfortunately, the
folks at Google screwed up the interface, making things difficult for
those of us who use other interfaces. Our attempts to get them to fix
there broken interface have so far been unsuccessful.

(Incidentally, Kenny McCormack is a self-proclaimed troll. Please
ignore him.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org

<http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.


And I thought I was the only one who thinks Kenny McCormack is an asswipe.

Nov 19 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.