Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
4K views

C Program To Implement Evaluation of Postfix Expression Using Stack

This C program evaluates a postfix expression using a stack. It takes in a postfix expression as a string, pushes operands onto a stack and performs operations based on operators. For each character, if it is a number it is pushed onto the stack, if it is an operator like + or - then the top two elements are popped, operated on and pushed back. Finally it pops and returns the result of evaluating the full expression. The key functions are push to add to the stack, pop to remove from top and traceStack to print the stack values.

Uploaded by

Saiyasodharan
Copyright
© Public Domain
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
4K views

C Program To Implement Evaluation of Postfix Expression Using Stack

This C program evaluates a postfix expression using a stack. It takes in a postfix expression as a string, pushes operands onto a stack and performs operations based on operators. For each character, if it is a number it is pushed onto the stack, if it is an operator like + or - then the top two elements are popped, operated on and pushed back. Finally it pops and returns the result of evaluating the full expression. The key functions are push to add to the stack, pop to remove from top and traceStack to print the stack values.

Uploaded by

Saiyasodharan
Copyright
© Public Domain
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program: #include<stdio.h> #include<stdlib.h> #include<string.

h> struct node { double element; struct node *next; } *head; void push(int c); // function to push a node onto the stack int pop(); // function to pop the top node of stack void traceStack(); // function to //print the stack values int main() { int i = 0, j = 0; // indexes to keep track of current position for input output strings char *exp = (char *)malloc(sizeof(char)*100); double res = 0; char tmp; head = NULL; printf("Enter the postfix expression: "); scanf("%s", exp); while( (tmp=exp[i++]) != '\0') { // repeat till the last null terminator // if the char is operand, pust it into the stack if(tmp >= '0' && tmp <= '9') { int no = tmp - '0'; push(no); continue; } if(tmp == '+') { int no1 = pop(); int no2 = pop(); push(no1 + no2); } else if (tmp == '-') { int no1 = pop(); int no2 = pop(); push(no1 - no2); } else if (tmp == '*') { int no1 = pop(); int no2 = pop(); push(no1 * no2); } else if (tmp == '/') { int no1 = pop(); int no2 = pop(); push(no1 / no2); } }

printf("Result of the evalution is %d", pop()); return 0; } void push(int c) { if(head == NULL) { head = malloc(sizeof(struct node)); head->element = c; head->next = NULL; } else { struct node *tNode; tNode = malloc(sizeof(struct node)); tNode->element = c; tNode->next = head; head = tNode; } } int pop() { struct node *tNode; tNode = head; head = head->next; return tNode->element; } Sample Output: Enter the postfix expression: 65*3+ Result of the evalution is 33 For more programs visit http://www.gethugames.in/blog

You might also like