numstack numstack;opstack opstack;char *s;int i=0;
numstack.top=0;opstack.top=0;
pushop(&opstack,'#');
printf("\nenter your expression adn end it with #:");scanf("%s",s);
for(i=0;i<strlen(s);i++)
process(&numstack,&opstack,s[i]);
printf("the result is %f",numstack.array[numstack.top-1]);
}
五、测试数据:
六、小结:
这次实验难度较高,我做了几个版本。最初的一个版本由于只用了一个栈,导致不能算小数。第二个版本用的是输入一次处理一次,不能处理括号运算。这一个版本,我个人目前比较满意。代码只有92行,但是功能很完善,能够处理实数的加减乘除运算,乘除法运算无误差,能执行多重括号嵌套运算。并且对错误表达式也有一定的识别能力。
附c语言的源代码:
#define n 50
#include <ctype.h>
#include <string.h>
typedef struct{
int top;
double array[n];
}numstack;
typedef struct{
int top;
char array[n];
}opstack;
int cint(char mychar){
return (mychar-48);
}
void pushnum(numstack *numstack,double num){
numstack->top++;
numstack->array[numstack->top-1]=num;
}
void popnum(numstack *numstack,double *num){