#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
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<ncount;++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<ncount;++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<ncount;++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<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<ncount;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;
}