Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Input :
- Enter the postfix expression 84+
- Output :
- The final answer is 12
- Assumptions
- -> Evaluation of only +,-,* and / will take place.
- -> Input expression must be single digit operands.
- */
- #include<stdio.h>
- #include<conio.h>
- #define Stack_Size 50
- int stack[Stack_Size],top=-1;
- void Push(int element){
- if(top == Stack_Size-1){
- printf("\nStack is full");
- }
- else
- stack[++top]=element;
- }
- int Pop(){
- int Poped_value = stack[top];
- stack[top--]=0;
- return Poped_value;
- }
- void Solve(char operator){
- int b = Pop();
- if(operator == '+'){
- Push(Pop() + b);
- }
- else if(operator == '-'){
- Push(Pop() - b);
- }
- else if(operator == '*'){
- Push(Pop() * b);
- }
- else{
- Push(Pop() / b);
- }
- }
- int Evaluation(char exp[]){
- for(int i=0 ; exp[i] != '\0' ; i++){
- if(exp[i]>='0' && exp[i]<='9'){
- Push(exp[i]-'0');
- }
- else{
- Solve(exp[i]);
- }
- }
- return stack[top];
- }
- int main()
- {
- char exp[20];
- printf("Enter the postfix expression ");
- scanf("%s",exp);
- printf("\nThe final answer is %d",Evaluation(exp));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment