how to write a calculator in c++

how to write a calculator in c++

Page 1 of 1

0 Replies - 2423 Views - Last Post: 29 April 2009 - 10:34 PM Rate Topic: -----

#1 anglin1024  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 09

how to write a calculator in c++

Posted 29 April 2009 - 10:34 PM

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>



const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;


typedef struct
{
int a[100];
int top;
}STACK;
void push(STACK *s,int x)
{
if(s->top==99)
printf("STACK OVERFLOW\n");
else
s->a[++s->top]=x;
}

int pop1(STACK *s)
{
int x;
if(s->top<0)
printf("STACK UNDERFLOW\n");
else
{
x=s->a[s->top--];
return x;
}
}
int operation(int p1,int p2,char op)
{
switch(op)
{
case '+':return p1+p2;
case '*':return p1*p2;
case '-':return p1-p2;
case '/':return p1/p2;
}
}
int evaluate(char pos[])
{
STACK s1;
int p1,p2,result,i;
s1.top=-1;
for(i=0;pos[i]!='\0';i++)
if(isdigit(pos[i]))
push(&s1,pos[i]-'0');/*use to find the integer value of it*/
else
{
p2=pop1(&s1);
p1=pop1(&s1);
result=operation(p1,p2,pos[i]);
push(&s1,result);
}/*end of for loop*/
return pop1(&s1);
}


int precedence(char ch)
{
	   switch(ch)
		  {
			   case '^' : return 5;
			   case '/' : return 4;
			   case '*' : return 4;											
			   case '+' : return 3;
			   case '-' : return 3;
			   default  : return 0;
		  }
}

char pop()				  //function to pop the element from the stack
{
	 char ret;
	 if(top!=-1)
	   {  ret =stack[top];
		  top--;
		  return ret;
	   }
	 else
		return '#';
}
						 
char topelement()		  // function to return top element from the stack without popping
{	 
	  char ch;
	  if(top!=-1)
		ch=stack[top];
	  else
		 ch='#';
	   return ch;
}

void push(char ch)		  // function to push an element in the stack
{
	 if(top!=size-1)
		 {
			top++;
			stack[top]= ch;
		 }
}		 
								

int main()
{
	 char ele,elem,st[2];
	 int prep,pre,popped,j=0,chk=0;
	 strcpy(postfix," ");
		 
	 gets(infix);
	 
	 for(int i=0;infix[i]!=0;i++)
		  {
				  if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')	 
					   postfix[j++]=infix[i];
				  else if(infix[i]=='(')
					  {
						 elem=infix[i];
						 push(elem);
					  }
				  else if(infix[i]==')')
					  {
						 while(popped=pop() != '(')
							 postfix[j++]=popped;
					  }
				  else
					  {
						 elem=infix[i];
						 pre=precedence(elem);//stores the precedence of operator coming frm infix
						 ele=topelement();
						 prep=precedence(ele);//stores the precedence of operator at the top of the stack
						
						 if(pre > prep)
						   push(elem);										 
						   
						 else
						   {
								while(prep >= pre)
								  {
									 if(ele=='#')
									   break;
									 popped=pop();
									 ele=topelement();
									 postfix[j++]=popped;
									 prep=precedence(ele);
								   }
								   push(elem);
							}
						 }
			 } 
			 
		  while((popped=pop())!='#')
			  postfix[j++]=popped;
		  postfix[j]='\0';
		  
		  cout<<"\n post fix :"<<postfix<<endl;
gets(postfix);
printf("The Result is==>%d",evaluate(postfix));
		   
		   system("pause");
		   return 0;
}




I need to write a calculator by infix and postfix form
I change the infix to postfix form and then calculte postfix form, but it keep telling me the number is underflow, I don't know why?

This post has been edited by anglin1024: 29 April 2009 - 10:41 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1