实现:在屏幕上输入一个表达式,然后得出表达式的值。
用双栈做
/* 本程序可用于多位操作符的运算
字符型的函数和结构体名后面都加1
整型的函数和结构体名后面都加2 */
#include "Stdio.h"
#include "Conio.h"
#include "math.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct Stack1
{ /*字符类结构体*/
char *base;
char *top;
int stacksize;
};
struct Stack2
{ /*整形结构体*/
int *base;
int *top;
int stacksize;
};
InitStack1(struct Stack1 *S)
{ /*构造一个空栈*/
S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S->base) printf("Application is failed!");
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
InitStack2(struct Stack2 *S)
{ /*构造一个空栈*/
S->base=(int *)malloc(STACK_INIT_SIZE * sizeof(int));
if (!S->base) printf("Application is failed!");
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
char GetTop1(struct Stack1 S)
{ /*若栈不空,则返回栈顶元素*/
if (S.top==S.base) return 0;
return(*(S.top-1));
}
int GetTop2(struct Stack2 S)
{ /*若栈不空,则返回栈顶元素*/
if (S.top==S.base) return 0;
return(*(S.top-1));
}
Push1(struct Stack1 *S,char e)
{ /*插入新的栈顶元素*/
if ( (S->top-S->base)>=(S->stacksize) )
{
S->base=(char *)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(char));
if (!S->base) return 0;
S->top=S->base+S->stacksize;
}
*S->top++=e;
}
Push2(struct Stack2 *S,int e)
{ /*插入新的栈顶元素*/
if ( (S->top-S->base)>=(S->stacksize) )
{
S->base=(int *)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(int));
if (!S->base) return 0;
S->top=S->base+S->stacksize;
}
*S->top++=e;
}
char Pop1(struct Stack1 *S)
{ /*若栈不空,则删除S栈顶元素,并返回其值*/
if (S->top==S->base) return 0;
return (*(--S->top));
}
int Pop2(struct Stack2 *S)
{ /*若栈不空,则删除S栈顶元素,并返回其值*/
if (S->top==S->base) return 0;
return (*(--S->top));
}
int Compare(char e)
{ /*比较字符是运算符还是操作数*/
if (e>='0' && e<='9')
return 0;
return 1;
}
char Precede(char e1,char e2)
{
int i=0,j=0;
char save[]={ '+', '-', '*', '/', '(', ')', '#'};
char index[][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','$'},
{'>','>','>','>','$','>','>'},
{'<','<','<','<','<','$','='}
};
for (i=0;i<7;i++)
{
if (save[i]==e1)
break;
}
for (j=0;j<7;j++)
{
if (save[j]==e2)
break;
}
return index[i][j];
}
int Operate(int operand2,char operate,int operand1)
{
switch (operate)
{
case '+':
return (operand1+operand2);
case '-':
return (operand1-operand2);
case '*':
return (operand1*operand2);
case '/':
return (operand1/operand2);
}
}
int change(int elem,int j)
{
int i=0,response=1;
for (i=0;i<=j;i++)
{
response*=elem;
}
return response;
}
int Ret(int e)
{
int i=1,temp=1;
if (e>=1)
{
for (i=1;i<=e;i++)
temp=temp*10;
return temp/10;
}
else
return 0;
}
int ret(int e1,int e2)
{
return Ret(e1-e2);
}
int EvaluateExpression(char in[])
{ /*算术表达式求值的算符优先比较,OP为运算符集合*/
int i=1,j=1,count=0,flag;
char temp,*ch=in;
int temp1,temp2,temp3=0;
struct Stack1 S1;
struct Stack2 S2;
InitStack1(&S1); /*S1存放运算符*/
InitStack2(&S2); /*S2存放一位操作数*/
Push1(&S1,*ch);
while ( *(ch+i)!='\0' && S1.base!=S1.top )
{
if (!Compare(*(ch+i)))
{
temp3=0;
while (!Compare(*(ch+i)))
{
Push2(&S2,(*(ch+i)-48));
count++;
i++;
}
i--;
flag=count+1;
while (count>0)
{
temp3=temp3+Pop2(&S2)*ret(flag,count);
count--;
}
count=0;
Push2(&S2,temp3);
}
else
{
switch (Precede(GetTop1(S1),*(ch+i)))
{
case '<':
Push1(&S1,*(ch+i));
break;
case '=':
Pop1(&S1);
break;
case '>':
temp=Pop1(&S1);
temp1=Pop2(&S2);
temp2=Pop2(&S2);
Push2(&S2,Operate(temp1,temp,temp2));
i--;
break;
}
}
i++;
}
return GetTop2(S2);
}
int main(void)
{
int i=1;
char ch[]="#12+121-(36/3+1*2)+5*1#";
printf("\nThe expression is: ");
for (i=1;i<sizeof(ch)-2;i++)
printf("%c ",ch[i]);
printf("\n\nThe answer is: ");
printf("%d",EvaluateExpression(ch));
getch();
getch();
return 0;
}
稍微改下,自己输入表达式就可以了