#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
typedef struct node{
char ch;
int flag;
long m;
struct node*next;}NODE;
NODE* f1(char* point);
NODE* f2(NODE* head);
void main(void)
{int n;
int i;
NODE* HEAD;
char* pointer;
long mm;
printf("Please input BOLAN numbers:\n");
scanf("%ld",&n);
for(i=0;i<n;i++)
{printf("Please input the %d BOLAN :\n",i);
gets(pointer);
HEAD=f1(pointer);
while(HEAD->next)
HEAD=f2(HEAD);
mm=HEAD->m;
printf("%s result is %ld\n",pointer,mm);
}
}
NODE* f1(char* point)
{NODE* head,last,p;
char* temp;
head=(NODE*)malloc(sizeof(NODE));
if(!head)
exit(1);
last=head;
last->next=NULL;
while(*point)
{switch(*point)
{case '+' : {p=(NODE*)malloc(sizeof(NODE));
p->ch='+';
p->flag=0;
p->next=NULL;
last->next=p;
last=p;
point=point+2;
break;}
case '*' : {p=(NODE*)malloc(sizeof(NODE));
p->ch='*';
p->flag=0;
p->next=NULL;
last->next=p;
last=p;
point=point+2;
break;}
case '-' : {if(*(point+1)==' ')
{p=(NODE*)malloc(sizeof(NODE));
p->ch='+';
p->flag=0;
p->next=NULL;
last->next=p;
last=p;
point=point+2;
break;}
else
{while(*point!=' ')
*temp++=*point++;
if(last->flag==0)
last->m=atol(temp);
else
{p=(NODE*)malloc(sizeof(NODE));
p->ch='\0';
p->flag=1;
p->m=atol(temp);
last->next=p;
last=p;p->next=NULL;}
point=point+1;
break;
}
default:{while(*point!=' ')
*temp++=*point++;
if(last->flag==0;)
last->m=atol(temp);
else
{p=(NODE*)malloc(sizeof(NODE));
p->ch='\0';
p->flag=1;
p->m=atol(temp);
last->next=p;
last=p;p->next=NULL;}
point=point+1;
break;
}
}
}
return head;
}
NODE* f2(NODE* head)
{
NODE* p,l,q;
l=p=head;
while(p->next)
{switch(p->ch)
{case '+' : {if(p->flag==1&&!p->next->ch)
{p->m=p->m+p->next->m;
q=p->next;
p->next=q->next;
free(q);}
p=p->next;
break;
}
case '*' : {if(p->flag==1&&!p->next->ch)
{p->m=p->m*p->next->m;
q=p->next;
p->next=q->next;
free(q);}
p=p->next;
break;
}
case '-' : {if(p->flag==1&&!p->next->ch)
{p->m=p->m-p->next->m;
q=p->next;
p->next=q->next;
free(q);}
p=p->next;
break;
}
default:{p=p->next;break;}
}
}
return l;
}