Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am assigned to make a calculator that evaluates and convert the equation from infix to postfix while using a stack (push, pop functions).
I've finished most of the code (it runs but still need some work)

but I have no idea how to make it push 2 or more digits in the stack and evaluate them Like 234+15*12


C
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>



int InfixToPostfix();
void push(char);
char pop();
void calculate();
int priority(char);
char stack[20];
int top=-1;
char postfix[20];

int main()
{
	while(1)
		calculate();
	getch();
	return 0;
}

int InfixToPostfix()
{	
	char infix[20],ch;
	int k=0,size;
    printf("----------------------------------------------\nPlease enter the value you wish to calculate\n");
	gets(infix);
	size=strlen(infix);
	for(int i =0;i<size;i++)
	{
		ch=infix[i];
		if(isdigit(ch))
			postfix[k++]=ch;
		else if(ch=='(')
			push(ch);
		else if (ch==')')
		{
			while((stack[top] != '('))
				postfix[k++]= pop();
			char remove = pop();
		}
		else 
		{
			while(priority(stack[top]) >= priority(ch))
				postfix[k++]=pop();
			   push(ch);
		}
	}
	while(top != -1)
		postfix[k++]=pop();
	    postfix[k] = '\0'; 
        printf("\nGiven Infix Expn: %s  Postfix Expn: %s\n", infix, postfix);
		return postfix[k];
}

void push(char x)
{
	stack[++top]=x;
}

char pop()
{
	return stack[top--];
}
void calculate()
	{
		int i=0;
		int op1,op2;
		char z;
		InfixToPostfix();
		do{

		    z=postfix[i++];
			if(isdigit(z))
				push(z - 48); 
				

			else{
	    switch(z)
	  {
	    case '+':
			op1=pop();
	        op2=pop();
	        
	        push(op2+op1);
	        break;

	    case '-':

	        op1=pop();
	        op2=pop();
	        
	        push(op2-op1);
	        break;

	    case '*':

	        op1=pop();
	        op2=pop();
	        
	        push(op2*op1);
	        break;

	    case '/':
			
	        op1=pop();
	        op2=pop();
	        
	        push(op2/op1);
	        break;
		case '^':
		    op1=pop();
			op2=pop();
			push((int)pow((double)op2,op1));
			break;
	      }
    	}
	}while(z != '\0');
		 
  printf("\n Result after Evaluation: %d\n", stack[top]);
}

int priority(char x)
{
	if(x=='(')
		return 0;
	if(x== '+'|| x=='-')
		return 1;
	if(x=='*' || x=='/')
		return 2;
	if(x=='^')
	return 3;
	return -1;
	
}


What I have tried:

Looking on the internet i guess
Posted
Updated 5-May-17 8:22am
v3
Comments
PIEBALDconsult 5-May-17 14:06pm    
Maybe read a few articles to get ideas.
Perhaps even mine: https://www.codeproject.com/Articles/12804/A-Simple-Infix-to-Reverse-Polish-Notation-Transfor

1 solution

You don't push digits: you convert them to numbers and push those.
Basically, what you are doing is "tokenizing" the user input:
234+15*12
Becomes 5 tokens:
Number  :  234
Operator:    +
Number  :   15
Operator:    *
Number  :   12
So there are five items you need to push onto your stack.
Have a look at this: 3.9. Infix, Prefix and Postfix Expressions — Problem Solving with Algorithms and Data Structures[^] - it shows the algorithm for handling this once you have tokenized the input.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900