数字为个位数,不考虑语法错误
#include<stdio.h>
#include<stdlib.h>
#define add 43
#define subs 45
#define mult 42
#define div 47
#define MAXSIZE 100
typedef struct stack
{
int stkdata[MAXSIZE];
int top;
};
int init_stack(struct stack *s)
{
s->top=-1;
return 1;
}
int push(struct stack *s,char e)
{
if(s->top==100)
{
printf("over flow\n");
return 0;
}
s->stkdata[++(s->top)]=e;
return 1;
}
char pop(struct stack *s)
{
char e;
if(s->top==-1)
{
printf("under flow \n");
return 0;
}
e=s->stkdata[(s->top)--];
return e;
}
void main()
{
struct stack *s1;
int length,i,num1,num2,result,sum;
char s[100];
printf("please input \n");
printf("warning:must less than 100\n");
gets(s);
length=strlen(s);
for(i=0;i<=length-1;i++)
{
if(s[i]>='1'&&s[i]<='9')
push(s1,s[i]-48);
else
{
num1=pop(s1);
num2=pop(s1);
switch(s[i])
{
case add:
{
sum=num1+num2;
push(s1,sum);
break;
}
case subs:
{
sum=num2-num1;
push(s1,sum);
break;
}
case div:
{
sum=num2/num1;
push(s1,sum);
break;
}
case mult:
{
sum=num1*num2;
push(s1,sum);
break;
}
}
}
}
result=s1->stkdata[s1->top];
printf("result is %d\n",result);
}