四则运算小程序 c语言,c编的四则运算小程序,请指出漏洞和缺点,谢谢!

c编的四则运算小程序,请指出漏洞和缺点,谢谢!

#include

#include

#include

#include

bool judge(char *input,int ncount);//判断式子的合法性

bool legal(char *input,int ncount);//判断式子的合法性

int calculate(struct number *phead);//计算只有+法和-法的式子最终结果

struct number *detach(char *input,int ncount);//将数字和符号分离生成链表

struct number *muldiv(struct number *phead);//把所有*法和/法的结果先计算出来生成链表

struct number *headdiv(struct number *phead);//把前面有*法和/法的计算生成开始只有+法或-法的链表

struct number *pend;

struct number

{

int multiply;

char operation;

struct number *next;

};

int main()

{

char instr[100]={'\0'};

int result=0;

int ncount=0;

int index=0;

struct number *pheaddetach=NULL;

struct number *phead=NULL;

struct number *p=NULL;

printf("please input the express(must be shorter than 100 bytes) you want to calculate:\n");

scanf("%s",instr);

ncount=strlen(instr);

printf("%d\n",ncount);

while(!(judge(instr,ncount)))

{

printf("please input again:\n");

scanf("%s",instr);

}

printf("input legal:\n");

pheaddetach=detach(instr,ncount);

for(p=pheaddetach;p;p=p->next)//输出分离后的链表信息

{

printf("%d\n",p->multiply);

printf("%c\n",p->operation);

}

phead=headdiv(pheaddetach);

for(p=phead;p;p=p->next)//输出计算过头指针有/法和*法的生成的链表,如果没有,和分离后的输出相同

{

printf("%d\n",p->multiply);

printf("%c\n",p->operation);

}

phead=muldiv(phead);//输出计算了*法和/法生成的链表

for(p=phead;p;p=p->next)

{

printf("%d\n",p->multiply);

printf("%c\n",p->operation);

}

result=calculate(phead);//计算最后+法和-法得出结果

printf("%d",result);

system("pause");

return 1;

}

bool judge(char *input,int ncount)

{

int num=0;

if((!isdigit(input[0]))||(!isdigit(input[ncount-1])))

{

printf("input error\n");

return false;

}

for(;num

{

if(!(isdigit(input[num])||input[num]=='*'||input[num]=='-'||input[num]=='+'||input[num]=='/'))

{

printf("input error\n");

return false;

}

}

return legal(input,ncount);

}

bool legal(char *input,int ncount)

{

int num=0;

int index;

for(;num

{

if(!isdigit(input[num]))

{

if(!isdigit(input[num+1]))

{

printf("input error\n");

return false;

}

}

}

return true;

}

int calculate(struct number *phead)

{

struct number *p=NULL;

int ncount=0;

int result=0;

for(p=phead;p->next;p=p->next)

{

if((p->operation=='+')||(p->operation=='-'))

++ncount;

}

while(ncount--)

{

if(phead->operation=='+')

{

p=(struct number*)malloc(sizeof(struct number));

p->multiply=(phead->multiply)+(phead->next->multiply);

p->next=phead->next->next;

p->operation=phead->next->operation;

phead=p;

result=phead->multiply;

}

if(phead->operation=='-')

{

p=(struct number*)malloc(sizeof(struct number));

p->multiply=(phead->multiply)-(phead->next->multiply);

p->next=phead->next->next;

p->operation=phead->next->operation;

phead=p;

result=phead->multiply;

}

}

return result;

}

struct number *detach(char *input,int ncount)

{

int num=0;

struct number *phead=NULL;

struct number *pcur=NULL;

struct number *p;

p->next=NULL;

char copy[100]={'\0'};;

int ind[50]={0};

int i=0;

int j;

for(num=0;num

{

if(!(isdigit(input[num])))

{

p=(struct number*)malloc(sizeof(struct number));

p->next=NULL;

++i;

ind[i]=num+1;

p->operation=input[num];

for(j=ind[i-1];j

copy[j-ind[i-1]]=input[j];

p->multiply=atoi(copy);

if(phead==NULL)

phead=pcur=p;

else

pcur->next=p;

pcur=p;

for(j=0;j<100;++j)

copy[j]='\0';

}

}

p=(struct number*)malloc(sizeof(struct number));

for(j=ind[i];j

copy[j-ind[i]]=input[j];

p->multiply=atoi(copy);

p->operation='\0';

pcur->next=p;

pend=pcur=p;

pend->next=NULL;

return phead;

}

struct number *headdiv(struct number *phead)

{

struct number *pcur;

if(phead->next!=NULL)

while((phead->operation=='/')||(phead->operation=='*'))

{

if(phead->operation=='*')

{

pcur=(struct number*)malloc(sizeof(struct number));

pcur->multiply=(phead->multiply)*(phead->next->multiply);

pcur->operation=phead->next->operation;

pcur->next=phead->next->next;

phead=pcur;

}

else

{

pcur=(struct number*)malloc(sizeof(struct number));

pcur->multiply=(phead->multiply)/(phead->next->multiply);

pcur->operation=phead->next->operation;

pcur->next=phead->next->next;

phead=pcur;

}

continue;

}

printf("phead muldiv successful\n");

return phead;

}

struct number *muldiv(struct number *phead)

{

int ncount=0;

struct number *p=NULL;

for(p=phead;p;p=p->next)

{

if((p->operation=='/')||(p->operation=='*'))

++ncount;

}

struct number *pcur=NULL;

do

{

for(p=phead;p->next->next;p=p->next)

{

if((p->next->operation=='/')||(p->next->operation=='*'))

{

if(p->next->operation=='/')

{

pcur=(struct number*)malloc(sizeof(struct number));

pcur->multiply=(p->next->multiply)/(p->next->next->multiply);

pcur->operation=p->next->next->operation;

pcur->next=p->next->next->next;;

p->next=pcur;

break;

}

else

{

pcur=(struct number*)malloc(sizeof(struct number));

pcur->multiply=(p->next->multiply)*(p->next->next->multiply);

pcur->operation=p->next->next->operation;

pcur->next=p->next->next->next;

p->next=pcur;

break;

}

}

}

}while(--ncount);

printf("muldiv successful\n");

return phead;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值