Evaluating a postfix expression using expression tree in C - c

I can't seem to get this program to work, it is a doubly linked list representation of expression tree. After creating the tree I need to evaluate the result but I can't seem to figure out how.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
{
char item;
struct node * left;
struct node * right;
}*Btree;
Btree root;
void operandFunc(char);
void operatorFunc(char);
void push(Btree);
Btree pop();
void infix(Btree);
void postfix(Btree);
void prefix(Btree);
int solve(Btree);
int calculate(char,int,int);
int isOperand(char);
char expression[25];
Btree stack[25];
int stackPtr = -1;
int main()
{
int count = 0;
printf("Please enter a postfix expression\n");
while((expression[count++]=getchar())!='\n');
expression[--count] = '\0';
//puts(expression);
for(count = 0;expression[count]!='\0';count++)
{
switch(expression[count])
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '%':
case '$':
operatorFunc(expression[count]);
break;
default:
operandFunc(expression[count]);
}
}
if(stackPtr != 0)
{
printf("Incomplete / Incorrect postfix expression given!\n");
}
else
{
printf("\n\nThe result = %d",solve(stack[stackPtr])+'0');
printf("\n\n");
return 0;
}
}
void prefix(Btree root)
{
Btree temp = root;
if(temp)
{
printf("%c ",temp->item);
prefix(temp->left);
prefix(temp->right);
}
}
void infix(Btree root)
{
Btree temp = root;
if(temp != NULL)
{
infix(temp->left);
printf("%c ",temp->item);
infix(temp->right);
}
}
void postfix(Btree root)
{
Btree temp = root;
if(temp)
{
postfix(temp->left);
postfix(temp->right);
printf("%c ",temp->item);
}
}
void push(Btree root)
{
stack[++stackPtr] = root;
}
Btree pop()
{
return (stack[stackPtr--]);
}
void operandFunc(char var)
{
Btree root = (Btree)malloc(sizeof(struct node));
root->item= var;
root->left= NULL;
root->right= NULL;
push(root);
}
void operatorFunc(char var)
{
Btree root = (Btree)malloc(sizeof(struct node));
root->item = var;
root->right = pop();
root->left = pop();
push(root);
}
int solve(Btree root)
{
Btree temp = root;
char num1,num2;
char operator;
int result;
if(temp)
{
Btree LEFTP = temp->left;
Btree RIGHTP = temp->right;
if(LEFTP)
{
if(isOperand(LEFTP->item))
{
num1 = LEFTP->item;
}
else
{
num1 = solve(LEFTP);
}
}
if(RIGHTP)
{
if(isOperand(RIGHTP->item))
{
num2 = RIGHTP->item;
}
else
{
num2 = solve(RIGHTP);
}
}
operator = temp->item;
printf("Test 1 = %c, num1 = %c, num2 = %c\n",operator,num1,num2);
result = calculate(operator,num1-'0',num2-'0');
printf("Test Result = %d\n",result);
temp->item = (result+'0');
printf("Root Item = %c and %d\n",temp->item,temp->item);
return result;
}
return NULL;
}
int calculate(char operator,int op1,int op2)
{
printf("Operator = %c , num1 = %d, num2 = %d\n",operator,op1,op2);
switch(operator)
{
case '+': return(op1+op2);
break;
case '-': return(op1-op2);
break;
case '*': return(op1*op2);
break;
case '/': return(op1/op2);
break;
case '%': return(op1%op2);
break;
case '$': return pow(op1,op2);
break;
default: printf("\n illegal operation.");
exit;
}
}
int isOperand(char var)
{
switch(var)
{
case '+':
case '-':
case '*':
case '/':
case '$':
case '%':
return 0;
default:
return 1;
}
}
I'm having trouble converting and returning the characters as integers.
UPDATE 1: I was able to make it solve single digit number inputs to recieve a multi digit result. I'm still working on a new data structure to compute multi digit numbers. Here's the updated code below.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
{
char item;
struct node * left;
struct node * right;
}*Btree;
Btree root;
void operandFunc(char);
void operatorFunc(char);
void push(Btree);
Btree pop();
void infix(Btree);
void postfix(Btree);
void prefix(Btree);
int solve(Btree);
int calculate(char,int,int);
int isOperand(char);
char expression[25];
Btree stack[25];
int stackPtr = -1;
int main()
{
int count = 0;
printf("Please enter a postfix expression\n");
while((expression[count++]=getchar())!='\n');
expression[--count] = '\0';
//puts(expression);
for(count = 0;expression[count]!='\0';count++)
{
switch(expression[count])
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '%':
case '$':
operatorFunc(expression[count]);
break;
default:
operandFunc(expression[count]);
}
}
if(stackPtr != 0)
{
printf("Incomplete / Incorrect postfix expression given!\n");
}
else
{
printf("\n\nThe result = %d",solve(stack[stackPtr])-'0');
printf("\n\n");
return 0;
}
}
void prefix(Btree root)
{
Btree temp = root;
if(temp)
{
printf("%c ",temp->item);
prefix(temp->left);
prefix(temp->right);
}
}
void infix(Btree root)
{
Btree temp = root;
if(temp != NULL)
{
infix(temp->left);
printf("%c ",temp->item);
infix(temp->right);
}
}
void postfix(Btree root)
{
Btree temp = root;
if(temp)
{
postfix(temp->left);
postfix(temp->right);
printf("%c ",temp->item);
}
}
void push(Btree root)
{
stack[++stackPtr] = root;
}
Btree pop()
{
return (stack[stackPtr--]);
}
void operandFunc(char var)
{
Btree root = (Btree)malloc(sizeof(struct node));
root->item= var;
root->left= NULL;
root->right= NULL;
push(root);
}
void operatorFunc(char var)
{
Btree root = (Btree)malloc(sizeof(struct node));
root->item = var;
root->right = pop();
root->left = pop();
push(root);
}
int solve(Btree root)
{
Btree temp = root;
char num1,num2;
char operator;
int result;
if(temp)
{
Btree LEFTP = temp->left;
Btree RIGHTP = temp->right;
if(LEFTP)
{
if(isOperand(LEFTP->item))
{
num1 = LEFTP->item;
}
else
{
num1 = solve(LEFTP);
}
}
if(RIGHTP)
{
if(isOperand(RIGHTP->item))
{
num2 = RIGHTP->item;
}
else
{
num2 = solve(RIGHTP);
}
}
operator = temp->item;
printf("Test 1 = %c, num1 = %c, num2 = %c\n",operator,num1,num2);
result = calculate(operator,num1-'0',num2-'0');
printf("Test Result = %d\n",result);
temp->item = (result+'0');
printf("Root Item = %c and %d\n",temp->item,temp->item);
return root->item;
}
return NULL;
}
int calculate(char operator,int op1,int op2)
{
printf("Operator = %c , num1 = %d, num2 = %d\n",operator,op1,op2);
switch(operator)
{
case '+': return(op1+op2);
break;
case '-': return(op1-op2);
break;
case '*': return(op1*op2);
break;
case '/': return(op1/op2);
break;
case '%': return(op1%op2);
break;
case '$': return pow(op1,op2);
break;
default: printf("\n illegal operation.");
exit;
}
}
int isOperand(char var)
{
switch(var)
{
case '+':
case '-':
case '*':
case '/':
case '$':
case '%':
return 0;
default:
return 1;
}
}

In operandFunc(expression[count]); you are only processing one character. That means you cannot work with multi-character operands like 10 or 123. If these occur, you push each digit separately. So your language is limited to single digit numbers (OK; your decission).
In solve you say printf("Root Item = %c and %d\n",temp->item,temp->item);
. Here, the second argument must be converted to an int: temp->item-'0'. You have to do that whenever you use %d in the printf format.
The rest of your code looks fine. Maybe set a higher warning level when compiling to find more errors?
EDIT/ADDITION:
How to handle multi-digit numbers?
The following snippet handles multi-digit numbers. You must adapt your struct to make a difference between chars and ints and change operandFunc to handle these numbers:
while((c=getchar())!='\n')
{
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '%':
case '$':
operatorFunc(c);
break;
default: // assume digit(s)
num= c-'0';
while ((c=getchar())!='\n' && isdigit(c)) num= num*10+c-'0';
operandFunc(num);
ungetc(c,stdin);
}
}

We can evaluate the postfix expression using the binary tree by keeping in mind the two conditions
if
eval(root) is an operator we use recursion, eval(root->llink) + eval(root->rlink)
else
we return root->info - '0'
Function for evaluation
float evaluate(NODE root)
{
float num;
switch(root->info)
{
case '+' : return eval(root->llink) + eval(root->rlink);
case '-' : return eval(root->llink) - eval(root->rlink);
case '/' : return eval(root->llink) / eval(root->rlink);
case '*' : return eval(root->llink) * eval(root->rlink);
case $ :
case ^ : return pow( eval(root->llink) ,eval(root->rlink));
default : if(isalpha(root->info))
{
printf("%c" = root->info);
scanf("%f",&num);
return num;
}
else
return root->info - '0';
}
}

Related

Infix to postfix conversion using linked list in c

Can anyone please help me with this code? I'm trying to convert from infix to postfix using a linked list. With an array, I can easily solve this, that solution is also similar to this one. But I'm not getting why this is not working. My guess is I've done anything wrong in this part -
if (precedence(ch) > precedence(top->data))
{
push(ch);
}
else
{
while (precedence(ch) <= precedence(top->data))
{
postfix = insert(postfix, pop());
}
push(ch);
}
Here is the complete code. Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
struct Node
{
char data;
struct Node *next;
};
struct Node *top = NULL;
struct Node *postfix = NULL;
void push(char ch);
char pop();
int is_operator(char ch);
int precedence(char ch);
void convert(char *infix);
struct Node *insert(struct Node *head, char ch);
void print(struct Node *head);
int main()
{
char infix[MAX_SIZE] = "(A+B^C)*D+E^5";
printf("Infix: %s\n", infix);
convert(infix);
printf("Postfix: ");
print(postfix);
}
void convert(char *infix)
{
int i, len = strlen(infix);
char ch;
for (i = 0; i < len; i++)
{
ch = infix[i];
if (is_operator(ch) == 0)
{
postfix = insert(postfix, ch);
}
else
{
if (ch == '(')
{
push(ch);
}
else
{
if (ch == ')')
{
while (top->data != '(')
{
postfix = insert(postfix, pop());
}
pop();
}
else
{
if (precedence(ch) > precedence(top->data))
{
push(ch);
}
else
{
while (precedence(ch) <= precedence(top->data))
{
postfix = insert(postfix, pop());
}
push(ch);
}
}
}
}
}
while (top != NULL)
{
postfix = insert(postfix, pop());
}
}
int is_operator(char ch)
{
switch (ch)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
default:
return 0;
}
}
int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
case '^':
return 4;
default:
return 1; // if (, ), or empty
}
}
void push(char ch)
{
struct Node *newP = (struct Node *)malloc(sizeof(struct Node));
newP->data = ch;
newP->next = top;
top = newP;
}
char pop()
{
char ch = top->data;
struct Node *temp = top;
top = temp->next;
free(temp);
temp = NULL;
return ch;
}
struct Node *insert(struct Node *head, char ch)
{
struct Node *newP = (struct Node *)malloc(sizeof(struct Node));
newP->data = ch;
newP->next = NULL;
if (head == NULL)
{
head = newP;
}
else
{
struct Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newP;
}
return head;
}
void print(struct Node *head)
{
while (head != NULL)
{
printf("%c", head->data);
head = head->next;
}
printf("\n");
}

RPN with C language

I have problem with this code, It works fine with small values but with big values it crash.
for Example.
input:
1+2+3
output:
Expression:
1+2+3
Reverse Polish Notation:
12+3+
Result:
6
but with this example it crash
input:
100+2*100/50
output:
runtime-error
This code is converting infix to postfix and this calculate the RPN.
// C program to convert infix expression to postfix
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack
{
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)
malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*)malloc(stack->capacity *
sizeof(int));
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
int isOperand(char ch)
{
return (ch >= '0' && ch <= '9');
}
int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int infixToPostfix(char* exp)
{
int i, k;
struct Stack* stack = createStack(strlen(exp));
if (!stack)
return -1;
for (i = 0, k = -1; exp[i]; ++i)
{
if (isOperand(exp[i]))
exp[++k] = exp[i];
else if (exp[i] == '(')
push(stack, exp[i]);
else if (exp[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
exp[++k] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1;
else
pop(stack);
}
else
{
while (!isEmpty(stack) &&
Prec(exp[i]) <= Prec(peek(stack)))
exp[++k] = pop(stack);
push(stack, exp[i]);
}
}
while (!isEmpty(stack))
exp[++k] = pop(stack);
exp[++k] = '\0';
printf("Reverse Polish Notation:\n");
char str[10];
strcpy(str, exp);
for (int i = 0; str[i]; i++)
{
printf("%c ", str[i]);
}
printf("\n");
}
int evaluatePostfix(char* exp)
{
struct Stack* stack = createStack(strlen(exp));
int i;
if (!stack) return -1;
for (i = 0; exp[i]; ++i)
{
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2 / val1); break;
}
}
}
return pop(stack);
}
int main()
{
//char exp[] = "1+2+3";
char exp[10];
scanf("%14s", exp);
printf("Expression:\n%s\n", exp);
infixToPostfix(exp);
printf("Result:\n%d", evaluatePostfix(exp));
return 0;
}

Wrong answer from a program (infix notation->postfix notation) in C

I made a program which changes infix notation to postfix notation with using stack in C.
I made a function which prints the result of the postfix notation(from infix notaton).
But, the result of a notation is wrong. This should be '195' but its result is '-61'.
The results of the other notations are right but only the notation(op2) has this problem.
What should I do to fix this problem?
This is my code:
typedef char element;
typedef struct {
element stack[MAX_STACK_SIZE];
int top;
} StackType;
void init(StackType *s) {
s->top = -1;
}
int is_empty(StackType *s) {
return (s->top == -1);
}
int is_full(StackType *s) {
return (s->top == (MAX_STACK_SIZE - 1));
}
void push(StackType *s, element item) {
if (is_full(s)) {
fprintf(stderr, "FULL STACK ERROR\n");
return;
}
else s->stack[++(s->top)] = item;
}
element pop(StackType *s) {
if (is_empty(s)) {
fprintf(stderr, "EMPTY STACK ERROR\n");
exit(1);
}
else return s->stack[(s->top)--];
}
int eval(char exp[]) {
int op1, op2, value, i = 0;
int len = strlen(exp);
char ch;
StackType s;
init(&s);
for (i = 0; i < len; i++) {
ch = exp[i];
if (ch != '+' && ch != '-' && ch != '*' && ch != '/') {
value = ch - '0';
push(&s, value);
}
else {
op2 = pop(&s);
op1 = pop(&s);
switch (ch) {
case '+': push(&s, op1 + op2); break;
case '-': push(&s, op1 - op2); break;
case '*': push(&s, op1 * op2); break;
case '/': push(&s, op1 / op2); break;
}
}
}
return pop(&s);
}
char* infix_to_postfix(char exp[]) {
int i = 0, j = 0;
char ch, top_op;
int len = strlen(exp);
char *ex = (char *)malloc(sizeof(char)*(len + 1));
StackType s;
init(&s);
for (i = 0; i < len; i++) {
ch = exp[i];
switch (ch) {
case '+': case '-': case '*': case '/':
while (!is_empty(&s) && (prec(ch) <= prec(peek(&s)))) {
ex[j++] = pop(&s);
}
push(&s, ch);
break;
case '(':
push(&s, ch);
break;
case ')':
top_op = pop(&s);
while (top_op != '(') {
ex[j++] = top_op;
top_op = pop(&s);
}
break;
default:
ex[j++] = ch;
break;
}
}
while (!is_empty(&s)) {
ex[j++] = pop(&s);
}
ex[j] = NULL;
return ex;
}
void main() {
char *op1 = "(9-(3+2))*3+4*((4+2)/3)-1";
char *op2 = "(4*5-3)/3+((2+5*7)-8+9)*5";
char *op3 = "7*3-7-4+1/3+6-8*2";
char *pos1, *pos2, *pos3;
pos1 = infix_to_postfix(op1);
pos2 = infix_to_postfix(op2);
pos3 = infix_to_postfix(op3);
printf(" Result : %d\n", eval(pos1));
printf(" Result : %d\n", eval(pos2));
printf(" Result : %d\n", eval(pos3));
}
[RESULT]
Result : 19
Result : -61 // This should be '195'.
Result : 0
The clue is 61+195 = 256. Which means that somewhere, your computations are being saved to chars. Looks like it is element.
typedef element int;

Using a stack to do an infix to postfix

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "stack.h"
#define MAX_EQU_LEN 100
static int prec(char operator)
{
switch (operator)
{
case '*':
return 5;
case '/':
return 4;
case '%':
return 3;
case '+':
return 2;
case '-':
return 1;
default:
break;
}
return 0;
}
static int isNumeric(char* num)
{
if(atoi(num) == 0)
{
return 0;
}
return 1;
}
char* infix_to_postfix(char* infix)
{
char* postfix = malloc(MAX_EQU_LEN);
stack* s = create_stack();
s->size = strlen(infix);
node* tempPtr = s->stack;
unsigned int i;
char symbol,next;
char temp[2] = {0};
for(i = 0; i < s->size ; i++)
{
symbol = *((infix + i));
temp[0] = symbol;
tempPtr = s->stack;
if(isNumeric(temp) != 1)
{
strcat(postfix, temp);
}
else if(symbol == '(')
{
push(s, symbol);
}
else if(symbol == ')')
{
while(s->size != 0 && top(s) != '(')
{
next = tempPtr->data;
temp[0] = next;
pop(s);
strcat(postfix, temp);
tempPtr = s->stack;
if(tempPtr->data == '(')
{
pop(s);
}
}
}
else
{
while(s->size != 0 && prec(top(s)) > prec(symbol))
{
next = tempPtr->data;
temp[0] = next;
pop(s);
strcat(postfix,temp);
push(s,next);
}
}
while(s->size != 0)
{
next = tempPtr->data;
temp[0] = next;
pop(s);
strcat(postfix, temp);
}
}
return postfix;
}
int evaluate_postfix(char* postfix) {
//For each token in the string
int i,result;
int right, left;
char ch;
stack* s = create_stack();
node* tempPtr = s->stack;
for(i=0; i < strlen(postfix); i++){
//if the token is numeric
ch = postfix[i];
if(isNumeric(&ch)){
//convert it to an integer and push it onto the stack
atoi(&ch);
push(s, ch);
}
else
{
pop(&s[0]);
pop(&s[1]);
//apply the operation:
//result = left op right
switch(ch)
{
case '+': push(&s[i],right + left);
break;
case '-': push(&s[i],right - left);
break;
case '*': push(&s[i],right * left);
break;
case '/': push(&s[i],right / left);
break;
}
}
}
tempPtr = s->stack;
//return the result from the stack
return(tempPtr->data);
}
This code is part of a bigger program that is designed to convert simple math from infix to postfix and then evaluate it. This will be aided by a stack to hold the numbers and symbols. However when I run the program it seg faults and all the debugger says is that it is in the infix_to_postfix function and I cannot figure out what part of the code makes it seg fault.

Evaluate postfix notation

I was doing my exercise but i stucked at the last step.
I have to evaluate postfix notation using stacks in C.
Code:
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
int info;
}tipElement;
typedef struct
{
tipElement magacin [MAX];
int vrv;
}tipMagacin;
tipMagacin postfixStack;
void Inicijalizacija(tipMagacin *Mag)
{
(*Mag).vrv = -1; //warehouse is empty
}
int Prazen(tipMagacin Mag)
{
return(Mag.vrv == -1); //returns true if the warehouse is empty
}
int Poln(tipMagacin Mag)
{
return(Mag.vrv == MAX-1); //returns true if the warehouse is full
}
void Push(tipMagacin *Mag, tipElement Element)
{
if (Poln(*Mag)) printf("Warehouse is full\n"); //if it is full report an error
else
{
(*Mag).vrv++; //next position in the warehouse
(*Mag).magacin[(*Mag).vrv].info = Element.info; //put an element
}
}
void Pop(tipMagacin *Mag, tipElement *Element)
{
if (Prazen(*Mag)) printf("Warehouse is empty\n"); //if it is empty report an error
else
{
(*Element).info = (*Mag).magacin[(*Mag).vrv].info; //read the last element
(*Mag).vrv--; // delete it
}
}
int evaluate(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
int evaluatePostfix (char *izraz, int n)
{
tipMagacin *Mag;
tipElement element;
tipElement go,z;
int i=0;
int op2;
char ch;
int value;
while (i < n) {
element.info = izraz[i];
if(isdigit(element.info))
{
Push(&postfixStack, element);
}
else
{
z=Pop(&postfixStack, &go);
op2=1;
value = evaluate(z,op2,ch);
Push(Mag,value);
}
i++;
}
return value;
}
my code works fine until here:
else
{
op1=Pop(&postfixStack, &go);
op2=Pop(&postfixStack, &go);
value = evaluate(op1,op2,element.info);
Push(&postfixStack, value);
}
i++;
}
return value;
}
the problem is: error: void value not ignored as it ought to be
my Push and Pop functions are void and i need to put the value that I remove from my stack to some int variable then i calculate them. put it doesnt put it for some reason i dont know.
Anyone help ?

Resources