Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Problem in stack(data structure in C)

Posted on 1998-11-19
Medium Priority
387 Views
How to write a program using stack (in C language)which functions almost like a calculator. the program will receive inputs like for example : 12 * (15 - 6) + 388
The program will calculate +, -, *, /, (), operations.
The priority operations are :

1st. ( )
2nd. *, /
3rd. +, -

The program will print the operation for example like The question above :

15 - 6
12 * 9
108 + 388
= 496
0
Question by:tagheuer
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 10

Expert Comment

ID: 1254427
Strange some question. 2 different users. Are you both in the same school. Sorry not homework will be done in EE. Post what you have done already and we will help you the solve your problems.
0

LVL 4

Accepted Solution

emmons earned 20 total points
ID: 1254428
#include "stdio.h"

#define Err(n)       printf(errm[n-1])
#define Guard(n)     if(sp<n-1){ Err(1); break; }

char errm[3][40] = { "Insufficient stack elements\n",
"Stack full\n",
"Invalid input\n"
};

char base = 'd',buf[160];
int cp=0;

main()

{       long stk[10],mem,sp;
int i;
char c,num[20];

sp=-1;
mem=0;

do
{       c=getnext();
switch(c)
{       case 'x':
base='x';
if(sp>=0)
{       printf("\n");
show(stk[sp]);
}
break;
case 'd':
base='d';
if(sp>=0)
{       printf("\n");
show(stk[sp]);
}
break;
case '+':
Guard(2);
stk[sp-1]+=stk[sp];
--sp;
printf("\n");
show(stk[sp]);
break;
case '-':
Guard(2);
stk[sp-1]-=stk[sp];
--sp;
printf("\n");
show(stk[sp]);
break;
case '*':
Guard(2);
stk[sp-1]*=stk[sp];
--sp;
printf("\n");
show(stk[sp]);
break;
case '/':
Guard(2);
stk[sp-1]/=stk[sp];
--sp;
printf("\n");
show(stk[sp]);
break;
case 't':
printf("\n");
if(sp>=0)show(stk[sp]);
break;
case 'a':
printf("\n");
for(i=sp; i>=0; --i)show(stk[i]);
break;
case 'p':
Guard(1);
--sp;
break;
case 's':
mem=stk[sp];
break;
case 'r':
stk[sp]=mem;
break;
case 'm':
printf("\n");
show(mem);
break;
case 'q':
case '\n':
case '\r':
case ' ':
break;
default:
if('0'<=c && c<='9')
{       if(sp<9)
{       backup();
getnum(num);
++sp;
if(base=='d')sscanf(num,"%ld",stk+sp);
else sscanf(num,"%lx",stk+sp);
}
else Err(2);
}
else Err(3);
}
} while(c!='q');
}

show(a)
long a;

{       if(base=='d')printf("%ld\n",a);
else printf("%lx\n",a);
return;
}

getnext()

{       if(buf[cp]=='\0')getline();
return(buf[cp++]);
}

backup()

{       if(cp>0)--cp;
return;
}

getnum(num)
char *num;

{       int dp;

dp=0;
while( ('0'<=buf[cp] && buf[cp]<='9') || (base=='x' && 'a'<=buf[cp] && buf[cp]<='f') )num[dp++]=buf[cp++];
num[dp]='\0';
return;
}

getline()

{       char *sp,*dp;

scanf("%s",buf);
dp=buf;
for(sp=buf; *sp!='\0'; ++sp)
{       if(*sp!=8)*dp++=*sp;
else dp=dp>buf?dp-1:dp;
}
*dp='\0';
cp=0;
return;
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month7 days, 19 hours left to enroll