表达式求值的小问题
今天写了一段关于表达式求值小程序,可是通过文件读取后计算结果明显不正确(即使数字都输入10以内的),还请各位大神帮忙看看,哪出来问题,如果是方法上出了问题,希望能告知正确的方法。谢谢啦!
代码如下:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
char *base;
char *top;
int stactsize;
}SqStack_c;
typedef struct{
int *base;
int *top;
int stactsize;
}SqStact_n;
void Push_c(SqStack_c &ope, char c)
{
if(ope.top - ope.base == ope.stactsize){
ope.base = (char*)realloc(ope.base, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(char));
ope.top=ope.base+ope.stactsize;
ope.stactsize += STACKINCREMENT;
}
*ope.top++ = c;
}
void Push_n(SqStact_n &num, int n)
{
if(num.top - num.base == num.stactsize){
num.base = (int*)realloc(num.base, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(int));
num.top=num.base+num.stactsize;
num.stactsize += STACKINCREMENT;
}
*num.top++ = n;
}
void Pop_c(SqStack_c &ope, char &c)
{
if(ope.base != ope.top)
c = *--ope.top;
}
void Pop_n(SqStact_n &num, int &n)
{
if(num.base != num.top)
n = *--num.top;
}
void InitStack(SqStack_c &ope, SqStact_n &num )
{
ope.base = (char*)malloc(STACK_INIT_SIZE * sizeof(char));
ope.top = ope.base;
Push_c(ope, '#');
ope.stactsize = STACK_INIT_SIZE;
num.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
num.top = num.base;
num.stactsize = STACK_INIT_SIZE;
}
int OperatorCompare(int c1,int c2)
{
int c[7]={'+','-','*','/','(',')','#'};
if(c1 == c[0] || c1 == c[1]){
if(c2 == '+'||c2 == '-' || c2 == ')' || c2 == '#')
return 1;
else if(c2 == '*' || c2 == '/' || c2 == '(')
return 0;
}
else if(c1 == c[2] || c1 == c[3]){
if(c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/' || c2 == ')' || c2 == '#')
return 1;
else if(c2 == '(')
return 0;
}
else if(c1 == c[4]){
if(c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/' || c2 == '(')
return 0;
else if(c2 == ')')
return 2;
else if(c2 == '#')
return 3;
}
else if(c1 == c[5]){
if(c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/' || c2 == ')')
return 1;
else if(c2 == '(')
return 3;
else if(c2 == '#')
return 2;
}
else if(c1 == c[6]){
if(c2 == '+' ||c2 == '-' || c2 == '*' || c2 == '/' || c2 == '(')
return 0;
else if(c2 == '#')
return 2;
else if(c2 == ')')
return 3;
}
}
int Calculate(FILE *fp, SqStack_c ope, SqStact_n num)
{
int rus;
char c;
if((c = getc(fp)) == EOF)
printf("Please input your expresion in file espression.txt! And reperfomance this program!");
else{
int i=0;
printf("Please wait for a moment!\n");
printf("The result is:\n");
while((c = getc(fp)) != EOF)
{
++i;
if(i%2==1)
Push_n(num, c);
else{
int a,b;
char theta;
int t=OperatorCompare(*--ope.top, c);
switch (t){
case 0:
Push_c(ope,c);
break;
case 1:
Pop_n(num, b);
Pop_n(num,a);
Pop_c(ope,theta);
if(theta=='+')
Push_n(num,a+b);
else if(theta=='-')
Push_n(num,a-b);
else if(theta=='*')
Push_n(num,a*b);
else if(theta=='/')
Push_n(num,a/b);
break;
case 2:
Pop_c(ope,theta);
break;
case 3:
printf("There is ERROR in your expression!");
break;
}
}
}
Pop_n(num,rus);
return rus;
}
}
void main()
{
int t;
SqStack_c ope;
SqStact_n num;
InitStack(ope,num);
FILE *fp=fopen("D:\\expression.txt","r");
t=Calculate(fp,ope,num);
printf("%d\n",t);
fclose(fp);
}