#include <stdio.h>
char infix[200];
char stack[200];
char queue[200];
int count_stack = 0;
int count_queue = 0;
int precedence(char x)
{
switch (x)
{
case '^':
return 2;
case '/':
return 1;
case '*':
return 1;
case '+':
return 0;
case '-':
return 0;
}
}
int isChar(char x)
{
if (x == '(' || x == ')' || x == '^' || x == '*' || x == '/' || x == '+' || x == '-')
{
return 1;
}
else
{
return 0;
}
}
int isNum(char x){
if (x >= '0' && x <= '9'){
return 1;
}
else{
return 0;
}
}
void pushtoStack(char x){
if (count_stack >= 200 ) {
printf("stack over flow");
return;
}
else {
stack[count_stack] = x;
count_stack++;
}
}
void pop()
{
if (count_stack < 0) {
printf("stack under flow");
}
else {
//push to queue
queue[count_queue] = stack[count_stack];
count_queue++;
count_stack--;
int i = count_stack;
while(i!=0)
{
stack[i] = stack[i-1]; // assign arr[i-1] to arr[i]
i--;
}
// return item;
}
}
void pushToQueue(char x){
queue[count_queue] = x;
count_queue++;
}
int main(){
scanf("%s", infix);
int i = 0;
while (infix[i] != '\0'){
if (count_stack==0 && isChar(infix[i]) == 1){
pushtoStack(infix[i]);
i++;
}
else if (isNum(infix[i]) == 1){
pushToQueue(infix[i]);
i++;
}
else if(count_stack !=0 && infix[i]=='(')
{
pushtoStack(infix[i]);
i++;
}
else if(count_stack !=0 && infix[i]==')')
{
int j = count_stack;
while(stack[j]!='('){
pushToQueue(stack[j]);
count_stack--;
j--;
}
pop(infix[i]);
pop(stack[count_stack]);
i++;
}
else if (count_stack !=0 && isChar(infix[i]) == 1 && precedence(infix[i]) <= precedence(stack[count_stack]))
{
while(precedence(stack[count_stack]) >= precedence(infix[i])){
pushToQueue(stack[count_stack]);
count_queue++;
pop();
i++;
}
pushtoStack(infix[i]);
i++;
}
}
for (int i = 0; i < 100;i++){
printf("%c", queue[i]);
}
}
Trying to do: Storing input in infix, reading chars and storing the postfix in queue. Queue will be evaluated later using precedence rules
Program is stuck after receiving input E.g. 5-6*9 NO OUTPUT (program keeps running)
NOTE: The evaluation of postfix is not included in the code.
This is for an assignment and I am restricted to using only the std lib of C <stdio.h>
If this problem can be solved in some other way, kindly edify me
There are several small errors throughout your code, but one big problem is in the implementation of
pop. To pop an item from a LIFO stack, you can just return the item currently at the top of the stack and decrement the stack index. You shouldn't need a while loop to shift any elements because only the top element is actually affected.This is exactly what the program does in this section:
By pushing the top of the stack to the queue and then decrementing
count_stack, the program is essentially popping the top of the stack, but without using apopfunction.Another issue is that later in the program
popis called with an argument, but thepopfunction definition doesn't define any arguments.Calling a function with more arguments than it is defined to have is an example of undefined behavior. This means that the C standard doesn't specify how the code should execute and the compiler is allowed to do essentially whatever it chooses. Undefined behavior should be avoided because the result is unpredictable.
It looks like you are trying to implement the Shunting Yard Algorithm. This is the pseudo-code from the Wikipedia article (slightly modified for brevity):