前不久,我的一个朋友托我给他搞一个课程设计--计算器,并且是用TC写,要求能够完成一般四则运算。
我花了一天的时间给他完成,用队列来写的,程序如下:#include#include#include#include#defineMaxQSize80floatqlist[MaxQSize],qlist1[MaxQSize],qlist2[MaxQSize];//定义三个全局队列intfront=0,rear=0,front1=0,front2=0,rear1=0,rear2=0,count=0,count1=0,count2=0;//定义三个队列头结点、尾结点、数据个数main(){charc[80];voidEnter(float),pop(),ClearStack();printf("inputstring:(n:sin,o:cos,s:sqrt)\n");while(gets(c),*c!='q'||*c!='Q'){switch(*c){case'+':case'*':case's':case'n'://求SINcase'o'://求COScase'/'://将符号入第一个队列{if(count==MaxQSize){printf("Quereoverflow!");exit(1);}count++;qlist[rear]=*c;rear=(rear+1)%MaxQSize;break;}case'-':{if(strlen(c)>1)//如果是负号,则将负号和数一起入第二个队列Enter(atof(c));else//否则,说明是减号,入第一队列if(count==MaxQSize){printf("Quereoverflow!");exit(1);}count++;qlist[rear]=*c;rear=(rear+1)%MaxQSize;break;}case'=':pop();//显示结果break;case'c':ClearStack();break;case'q':ClearStack();exit(1); (atof(c));break;}}return0;}voidEnter(floatnum)//将数据入第二个队列{if(count1==MaxQSize){printf("Queueoverflow!\n");exit(1);}count1++;qlist1[rear1]=num;rear1=(rear1+1)%MaxQSize;}voidClearStack()//将三个队列清空{count=0;count1=0;count2=0;front=0;rear=0;front1=0;rear1=0;front2=0;rear2=0;}voidpop()//显示结果{floatQFront(floatnum1,floatnum2,char*temp);floatnum1,num2;floatsum;chartemp;temp=qlist[front];//将第一个队列中的头结点读出来while(count1!=0)//判断第二个队列是否有数{temp=qlist[front];//将队列中头结点读出来sum=QFront(num1,num2,&temp);count--;front=(front+1)%MaxQSize;//将第一个队列头指针加1}if(count!=0&&temp=='s')sum=sqrt(qlist2[front2]);if(count!=0&&temp=='n')sum=sin(qlist2[front2]);if(count!=0&&temp=='o')sum=cos(qlist2[front2]);printf("%f",sum);}floatQFront(floatnum1,floatnum2,char*temp){floatnumber,numb1;floatQFront1();voidQInsert(float);num1=QFront1();//先读出一个数switch(*temp){case'+':if(count2==0){num2=QFront1();//读出第二个数number=num1+num2;QInsert(number);//将结果存入第三个队列}else{number=num1+qlist2[front2];front2=(front2+1)%MaxQSize;QInsert(number);}break;case'-':if(count2==0){num2=QFront1();number=num1-num2;printf("number=%f\n",number);QInsert(number);}else{number=qlist2[front2]-num1;printf("number=%f\n",number);front2=(front2+1)%MaxQSize;QInsert(number);}break;case'*':if(count2==0)//如果第三个队列中没有数据,则从第二个队列中读出一个数据{num2=QFront1();number=num1*num2;QInsert(number);}else//否则,从第三个队列中读出数据{number=qlist2[front2]*num1;front2=(front2+1)%MaxQSize;QInsert(number);}break;case'/':if(count2==0){num2=QFront1();if(num2==0){printf("diviedby0");ClearStack();exit(1);}else{number=num1/num2;QInsert(number);}}else{if(num1==0){printf("divideby0");ClearStack();exit(1);}else{number=qlist2[front2]/num1;front2=(front2+1)%MaxQSize;QInsert(number);}}break;case's':if(count2==0){number=sqrt(num1);QInsert(number);}else{number=sqrt(qlist2[front2]);printf("number=%d\n",number);front2=(front2+1)%MaxQSize;QInsert(number);}break;case'n':if(count2==0){number=sin(num1);QInsert(number);}else{number=sin(qlist2[front2]);front2=(front2+1)%MaxQSize;QInsert(number);}break;case'o':if(count2==0){number=cos(num1);printf("number=%d\n",number);QInsert(number);}else{number=cos(qlist2[front2]);printf("number=%d\n",number);front2=(front2+1)%MaxQSize;QInsert(number);}break;}returnnumber;}floatQFront1()//从第一个数据读数据{floattemp;if(count1==0){printf("Deletingfromanemptyqueue!\n");exit(1);}temp=qlist1[front1];count1--;front1=(front1+1)%MaxQSize;returntemp;}voidQInsert(floatnumber)//将结果存入第三个队列{if(count2==MaxQSize){printf("Queueoverflow!\n");exit(1);}count2++;qlist2[rear2]=number;rear2=(rear2+1)%MaxQSize;}其他。
全部