对非法表达式的判断你再自己加些,
#include "stdio.h"
#include "stdlib.h"
int INITSIZE = 20;
int ADDSIZE = 10;
/*
用一个堆栈存储数据,用一个堆栈存储符合(预存一个'#')。
读到数据时入栈,读到运算符A是判断其优先级与栈定的运算
符B高低,若A高则A入栈,若A低则取出B,取出两栈定数据,进行运算,再A入栈。
输入格式: 3-8*(2-7)=
*/
struct stackData {
float *top;
float *bottom;
float *data;
int size;
};
struct stackFlag {
char *top;
char *bottom;
char *data;
int size;
};
void initStackData(struct stackData *st)
{
float *p=(float*) malloc( (sizeof(float) * INITSIZE));
if(p!=NULL)
{
st->data=p;
st->size=INITSIZE;
}
else
exit(1);
st->top=st->data;
st->bottom=st->data;
}
void pushData(struct stackData *st,float element)
{
float *p;
if((st->top-st->bottom)>=st->size)
{
p=(float*)realloc(st->data , (((st->size+ADDSIZE)*sizeof(float)) ));
if(p!=NULL)
{
st->data=p;
st->size +=ADDSIZE;
}
else
exit(0);
}
*((st->top)++) = element;
}
float popData(struct stackData *st)
{
if(st->top==st->bottom)
return -1;
return *(--(st->top));
}
float getData(struct stackData *st)
{
if(st->top==st->bottom)
return -1;
return *(st->top-1);
}
void destoryStackData(struct stackData *st)
{
if(st->data!=NULL)
free(st->data);
}
void initStackFlag(struct stackFlag *st)
{
char *p=(char*)malloc(INITSIZE*sizeof(char));
if(p!=NULL)
{
st->data=p;
st->size=INITSIZE;
}
else
exit(1);
st->top=st->data;
st->bottom=st->data;
}
void pushFlag(struct stackFlag *st,char element)
{
char *p;
if((st->top-st->bottom)>=st->size)
{
p=(char*)realloc(st->data,(st->size+ADDSIZE)*sizeof(char));
if(p!=NULL)
{
st->data=p;
st->size +=ADDSIZE;
}
else
exit(0);
}
*((st->top)++) = element;
}
char popFlag(struct stackFlag *st)
{
if(st->top==st->bottom)
return -1;
return *(--(st->top));
}
char getFlag(struct stackFlag *st)
{
if(st->top==st->bottom)
return -1;
return *(st->top-1);
}
void destoryStackFlag(struct stackFlag *st)
{
if(st->data!=NULL)
free(st->data);
}
int flagLevel(char flag)
{
switch(flag)
{
case '#':
return 0;
break;
case '(':
return 4;
break;
case ')':
return 3;
break;
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
default : return -1;
};
}
float compute(float num1,float num2,char flag)
{
switch(flag)
{
case '*':
// printf("%f*%f=%f\n",num1,num2,num1*num2);
return num1*num2;
break;
case '/':
// printf("%f/%f=%f\n",num1,num2,num1/num2);
if(num2 == 0)
{
printf("除数为零");
exit(0);
}
return num1/num2;
break;
case '+':
// printf("%f+%f=%f\n",num1,num2,num1+num2);
return num1+num2;
break;
case '-':
// printf("%f-%f=%f\n",num1,num2,num1-num2);
return num1-num2;
break;
default : return 0;
};
}
int check(char *str)
{
char a = *(str++);
if(a != '(' && !('0' {
printf("第一个字符不合规范");
exit(0);
}
else
{
while((a=*(str++)) !='=')
{
printf("%c\n",a);
if( !(('0' <=a && a<='9') || a=='+'|| a=='-'|| a=='*'|| a=='/'|| a==')'|| a=='('))
{
printf("表达式不符合四则运算规范5");
exit(0);
}
}
if((a=*(str++)) != '\0')
{
printf("最后一个字符不是‘=’");
exit(0);
}
}
return 1;
}
int main(int argc,int * argv[])
{
float num=0;
int flag;
int flag2;
int flagNum = 0;
int dataNum = 0;
char *p;
char *str=(char*)malloc(INITSIZE*sizeof(char));
struct stackData sd ;
struct stackFlag sf;
initStackData(&sd);
initStackFlag(&sf);
pushFlag(&sf,'#');
p = str;
scanf("%s",str);
if(check(str) == 0)
{
printf("表达式不符合四则运算规范4");
exit(0);
}
while(*str!='\0')
{
if('0'<=*str && *str<='9')//数字
{
num = num*10 + (float)(*str)-48;
dataNum ++;
}
else if(*str == '=')//等号
{
flag = 0;
if(dataNum != 0)
{
pushData(&sd,num);
num=0;
dataNum = 0;
flagNum = 0; //运算符计数器清零
}
if(flag<=flagLevel(getFlag(&sf) ) && !(flag==0 && 0==flagLevel(getFlag(&sf))))
{
while(flag<=flagLevel(getFlag(&sf)) && !(flag==0&& 0==flagLevel(getFlag(&sf))))
{
pushData(&sd, compute(popData(&sd),popData(&sd),popFlag(&sf)) );
}
pushFlag(&sf,*str);
}
else
{
pushFlag(&sf,*str);
}
}
else if(*str ==')' ) //')'
{
if(dataNum != 0)
{
pushData(&sd,num);
num=0;
dataNum = 0;
flagNum = 0;
}
flag2 = flagLevel(getFlag(&sf) );
while(flag2 != 4)
{
pushData(&sd, compute(popData(&sd),popData(&sd),popFlag(&sf)) );
flag2 = flagLevel(getFlag(&sf) );
}
popFlag(&sf);
}
else if((flag = flagLevel(*str)) != -1)//运算符和‘(’
{
if(dataNum != 0)
{
pushData(&sd,num);
num=0;
dataNum = 0;
flagNum = 0; //运算符计数器清零
}
flag2 = flagLevel(getFlag(&sf) );
if(flag2 == 4) //'('
{
pushFlag(&sf,*str);
}
else if(flag<=flag2&& !(flag==0&& 0==flag2))
{
while(flag<=flag2&& !(flag==0&& 0==flag2))
{
pushData(&sd, compute(popData(&sd),popData(&sd),popFlag(&sf)) );
flag2 = flagLevel(getFlag(&sf) );
}
pushFlag(&sf,*str);
flagNum++; //运算符计数 (不能连续两个)
}
else
{
pushFlag(&sf,*str);
if(*str != '(')
flagNum++;
}
}
else
{
printf("表达式不符合四则运算规范2");
exit(0);
}
if(flagNum >= 2)
{
printf("表达式不符合四则运算规范3");
exit(0);
}
str++;
}
system("CLS");
printf("%s%.2f\n",p,popData(&sd));
return 0;
}