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! -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <ctype.h>
-
-
/* constants */
-
#define TRUE 1
-
#define FALSE 0
-
-
/* structure for stack */
-
typedef struct
-
{
-
char data[40]; /* array to hold stack contents */
-
int tos; /* top of the stack pointer */
-
}
-
STACK;
-
-
/* function prototypes */
-
void initStack(STACK *stack);
-
void get_infix(char infix[]);
-
void convertToPostfix(char infix[], char postfix[]);
-
int isOperator(char c);
-
int precedence(char operator1, char operator2);
-
int pred_level(char ch);
-
void push(STACK *stack, char value);
-
char pop(STACK *stack);
-
char stackTop(STACK *stack);
-
int isEmpty(STACK *stack);
-
int isFull(STACK *stack);
-
void printResult(char infix[], char postfix[]);
-
void print_msg(void);
-
-
/* program entry point */
-
int main()
-
{
-
char infix[40], postfix[40]="";
-
-
/* convert from infix to postfix main function */
-
convertToPostfix(infix, postfix);
-
/* display the postfix equivalent */
-
infix[strlen(infix)-2] = '\0';
-
printResult(infix, postfix);
-
-
return EXIT_SUCCESS;
-
}
-
-
/* initalise the stack */
-
void initStack(STACK *stack)
-
{
-
stack->tos = -1; /* stack is initially empty */
-
}
-
-
/* get infix expression from user */
-
void get_infix(char infix[])
-
{
-
int i;
-
-
printf("Enter infix expression: ");
-
fflush(stdin);
-
-
for ( i=0; i<40; )
-
{
-
if ( (infix[i] = getchar()) == '\n' )
-
{
-
i++;
-
break;
-
}
-
else if ( !(isspace(infix[i])) )
-
i++;
-
}
-
-
infix[i] = '\0';
-
}
-
-
/* convert the infix expression to postfix notation */
-
void convertToPostfix(char infix[], char postfix[])
-
{
-
int i, length;
-
int j=0;
-
char tos_ch;
-
STACK stack;
-
-
initStack(&stack); /* initialise stack */
-
get_infix(infix); /* get infix expression from user */
-
length = strlen(infix);
-
-
if ( length )
-
{
-
push(&stack, '(');
-
strcat(infix, ")");
-
length++;
-
-
for ( i=0; i<length; i++ )
-
{
-
/* if current operator in infix is digit */
-
if ( isdigit(infix[i]) )
-
{
-
postfix[j++] = infix[i];
-
}
-
/* if current operator in infix is left parenthesis */
-
else if ( infix[i] == '(' )
-
{
-
push(&stack, '(');
-
}
-
/* if current operator in infix is operator */
-
else if ( isOperator(infix[i]) )
-
{
-
while ( TRUE )
-
{
-
/* get tos */
-
tos_ch = stackTop(&stack);
-
-
/* no stack left */
-
if ( tos_ch == '\0' )
-
{
-
printf("\nFull Stack!\n");
-
print_msg();
-
exit(1);
-
}
-
else
-
{
-
if ( isOperator(tos_ch) )
-
{
-
if ( pred_level(tos_ch) >=
-
pred_level(infix[i]) )
-
postfix[j++] = pop(&stack);
-
else
-
break;
-
}
-
else
-
break;
-
}
-
}
-
push(&stack, infix[i]);
-
}
-
/* if current operator in infix is right parenthesis */
-
else if ( infix[i] == ')' )
-
{
-
while ( TRUE )
-
{
-
/* get tos */
-
tos_ch = stackTop(&stack);
-
-
/* no stack left */
-
if ( tos_ch == '\0' )
-
{
-
printf("\nFull Stack!\n");
-
print_msg();
-
exit(1);
-
}
-
else
-
{
-
if ( tos_ch != '(' )
-
{
-
postfix[j++] = tos_ch;
-
pop(&stack);
-
}
-
else
-
{
-
pop(&stack);
-
break;
-
}
-
}
-
}
-
continue;
-
}
-
}
-
}
-
-
postfix[j] = '\0';
-
}
-
-
/* determine if c is an operator */
-
switch (c) {
-
case '+':
-
return TRUE;
-
break;
-
case '-':
-
return TRUE;
-
break;
-
case '*':
-
return TRUE;
-
break;
-
case '/':
-
return TRUE;
-
break;
-
default:
-
return FALSE;
-
break;
-
/*if ( c == '+' || c == '-' || c == '*' ||
-
c == '/' || c == '%' || c == '^' )
-
{
-
return TRUE;
-
}
-
else
-
return FALSE; */
-
}
-
-
/* determine precedence level */
-
int pred_level(char ch)
-
{
-
if ( ch == '+' || ch == '-' )
-
return 1;
-
else
-
return 2;
-
}
-
-
/* determine if the precedence of operator1 is less than,
-
equal to, greater than the precedence of operator2 */
-
int precedence(char operator1, char operator2)
-
{
-
if ( pred_level(operator1) > pred_level(operator2) )
-
return 1;
-
else if ( pred_level(operator1) < pred_level(operator2) )
-
return -1;
-
else
-
return 0;
-
}
-
-
/* push a value on the stack */
-
void push(STACK *stack, char value)
-
{
-
if ( !(isFull(stack)) )
-
{
-
(stack->tos)++;
-
stack->data[stack->tos] = value;
-
}
-
}
-
-
/* pop a value off the stack */
-
char pop(STACK *stack)
-
{
-
char ch;
-
-
if ( !(isEmpty(stack)) )
-
{
-
ch = stack->data[stack->tos];
-
(stack->tos)--;
-
return ch;
-
}
-
else
-
return '\0';
-
}
-
-
/* return the top value of the stack without popping the stack */
-
char stackTop(STACK *stack)
-
{
-
if ( !(isEmpty(stack)) )
-
return stack->data[stack->tos];
-
else
-
return '\0';
-
}
-
-
/* determine if stack is empty */
-
int isEmpty(STACK *stack)
-
{
-
/* empty */
-
if ( stack->tos == -1 )
-
return TRUE;
-
/* not empty */
-
else
-
return FALSE;
-
}
-
-
/* determine if stack is full */
-
int isFull(STACK *stack)
-
{
-
/* full */
-
if ( stack->tos == 19 )
-
return TRUE;
-
/* not full */
-
else
-
return FALSE;
-
}
-
-
/* display the result postfix expression */
-
void printResult(char infix[], char postfix[])
-
{
-
/*system("cls");*/
-
printf("\n\n");
-
printf("Infix notation: %d%s\n", infix);
-
printf("Postfix notation: %d%s\n\n", postfix);
-
print_msg();
-
}
-
-
/* print exit message */
-
void print_msg(void)
-
{
-
printf("Hit <RETURN> to exit...");
-
fflush(stdin);
-
getchar();
-
}
-
the error msg i get is: syntax error before "switch"
i don't get it :S ... any suggestions? 19 11304
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?
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
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 convertToPostfi x(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.
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 convertToPostfi x(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
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 convertToPostfi x(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
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
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
true, i was wondering if anyone can point out any mistake(s) or suggest
a fragment of code i can insert to display variables
On 18 Nov 2005 15:42:10 -0800, "caramel" <es********@gma il.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 convertToPostfi x(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(infi x[i]) ) { while ( TRUE ) {
snip if ( tos_ch == '\0' ) {
snip } else { if ( isOperator(tos_ ch) ) { if ( pred_level(tos_ ch) >= pred_level(infi x[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>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: KidLogik |
last post by:
Hello!
I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier ->
(5*(((9+8)*(4*6))+7)) == 598+46**7+*
I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"
|
by: Tony Johansson |
last post by:
Hello Experts!
I'm reading i a book about C++ and they mention infix with telling what it
is.
I hope you out there can do so.
Many thanks!
//Tony
|
by: stesia |
last post by:
which is the programm of converting infix to postfix in c++ by using stacks?
please help me...
|
by: Xah Lee |
last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully
Functional Notations
Xah Lee, 2006-03-15
In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”.
Likewise, they write “(if test this that)” to mean “if (test) {this}
else {that}”. LISP codes are all of the form “(a b c ...)”, where the
|
by: ostorat_elwafa2 |
last post by:
program that uses a stack to convert a given infix expression to a
postfix expression and then to evaluate it.
program should first check if the infix expression entered has
balanced brackets ( (, , } ) and if not should produce an
error. The program should handle at least the following operators (+,
-, *, /, %). Assume the operands are single digit integers.
thank you
| |
by: jackikay |
last post by:
how do i write a program that converts infix to postfix.?
|
by: jimmuel001 |
last post by:
pls help me fo my assignment that will allow the user to input infinite equations in infix type. and will output in postfix.
thanks!
|
by: aitia |
last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help?
import java.io.*;
import java.util.*;
public class Postfix
{
private static Stack operators = new Stack();
private static Stack operands = new Stack();
|
by: zeroeight |
last post by:
Hi guys,
I'm a newbie here, I just want to seek for help regarding my program. I want to implement an infix to postfix conversion only accepting numbers. Right now, I already debugged the errors and encountering loop everytime I execute it.
Here's my sample code:
********************************************************************
#include<stdio.h>
#include<conio.h>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |