您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 栈的应用--计算器雏形
//本计算器的缺陷在于操作数只能为(0到9),符号为+-*/()=2012.8.31#includestdio.h#includemalloc.h#includestdlib.h/******************************括号匹配结构体********************************/typedefstructnode{charelem;structnode*next;}*PNODE,NODE;typedefstructhead{PNODEtop;PNODEbase;}*PHEAD,HEAD;/******************************操作数结构************************************/typedefstructnode1{intelem;structnode1*next;}*PNODE1,NODE1;typedefstructhead1{PNODE1top;PNODE1base;}*PHEAD1,HEAD1;/******************************符号结构体************************************/typedefstructnode2{charelem;structnode2*next;}*PNODE2,NODE2;typedefstructhead2{PNODE2top;PNODE2base;}*PHEAD2,HEAD2;/******************************括号匹配栈相关函数*****************************///初始化栈voidinitstack(PHEADph){ph-top=(PNODE)malloc(sizeof(NODE));if(!(ph-top)){printf(内存分配失败!\n);exit(-1);}else{ph-base=ph-top;ph-top-next=NULL;}return;}//判断栈是否为空intempty(PHEADph){if(ph-top==ph-base)return1;elsereturn0;}//元素进栈voidpush(PHEADph,chare){PNODEpnew=(PNODE)malloc(sizeof(NODE));if(!pnew){printf(内存分配失败!\n);exit(-1);}else{pnew-elem=e;pnew-next=ph-top;ph-top=pnew;}return;}//出栈voidpop(PHEADph){PNODEp;if(empty(ph)){printf(栈3为空!\n);return;}else{p=ph-top;ph-top=p-next;free(p);return;}}//清空栈voidclear(PHEADph){PNODEp;while(1){if(empty(ph))return;else{p=ph-top;ph-top=p-next;free(p);}}return;}/******************************操作数栈相关函数*******************************///初始化栈voidinitstack1(PHEAD1ph){ph-top=(PNODE1)malloc(sizeof(NODE1));if(!(ph-top)){printf(内存分配失败!\n);exit(-1);}else{ph-base=ph-top;ph-top-next=NULL;}return;}//判断栈是否为空intempty1(PHEAD1ph){if(ph-top==ph-base)return1;elsereturn0;}//元素进栈voidpush1(PHEAD1ph,inte){PNODE1pnew=(PNODE1)malloc(sizeof(NODE1));if(!pnew){printf(内存分配失败!\n);exit(-1);}else{pnew-elem=e;pnew-next=ph-top;ph-top=pnew;}return;}//元素出栈到*memorry里voidpop1(PHEAD1ph,int*pmemorry1){PNODE1p;if(empty1(ph)){printf(栈1为空!\n);return;}else{*pmemorry1=ph-top-elem;p=ph-top;ph-top=p-next;free(p);return;}}//清空栈voidclear1(PHEAD1ph){PNODE1p;while(1){if(empty1(ph))return;else{p=ph-top;ph-top=p-next;free(p);}}return;}/******************************符号栈相关函数********************************///初始化栈voidinitstack2(PHEAD2ph){ph-top=(PNODE2)malloc(sizeof(NODE2));if(!(ph-top)){printf(内存分配失败!\n);exit(-1);}else{ph-base=ph-top;ph-top-next=NULL;}return;}//判断栈是否为空intempty2(PHEAD2ph){if(ph-top==ph-base)return1;elsereturn0;}//元素进栈voidpush2(PHEAD2ph,chare){PNODE2pnew=(PNODE2)malloc(sizeof(NODE2));if(!pnew){printf(内存分配失败!\n);exit(-1);}else{pnew-elem=e;pnew-next=ph-top;ph-top=pnew;}return;}//元素出栈到*memorry里voidpop2(PHEAD2ph,char*pmemorry2){PNODE2p;if(empty2(ph)){printf(栈2为空!\n);return;}else{*pmemorry2=ph-top-elem;p=ph-top;ph-top=p-next;free(p);return;}}//清空栈voidclear2(PHEAD2ph){PNODE2p;while(1){if(empty2(ph))return;else{p=ph-top;ph-top=p-next;free(p);}}return;}/*************************************表达式字符串前期处理算法函数***********///括号匹配intcharge_kuohao(PHEADph,charch[]){inti=0;intflag=0;while(ch[i]!='\0'){if(ch[i]=='(')push(ph,'(');if(ch[i]==')'){if(empty(ph)==1){flag=1;break;}elsepop(ph);}i++;}if(empty(ph)==0)flag=1;returnflag;}//格式配对判断,如果符合返回1,若不符合返回0intcharge_stye(charch[],inti,intj){switch(ch[i]){case'+':case'-':case'*':case'/':case'(':if(ch[j]=='+'||ch[j]=='-'||ch[j]=='*'||ch[j]=='/'||ch[j]==')'||ch[j]=='=')return0;elsereturn1;case')':if(ch[j]=='+'||ch[j]=='-'||ch[j]=='*'||ch[j]=='/'||ch[j]==')'||ch[j]=='=')return1;elsereturn0;case'=':return0;default:if(ch[j]=='(')return0;elsereturn1;}}//字符串判断函数intcharge_zfc(charch[]){HEADHZFC;intflag1=0;intflag2=0;intflag3=0;inti=0;initstack(&HZFC);clear(&HZFC);//置为原始状态if(charge_kuohao(&HZFC,ch)==1){printf(表达式有误\n);return0;}else{if(ch[1]=='\0')flag1=1;while(1){if(ch[i+1]=='\0')break;if(ch[i+1]=='=')flag2=1;if(!charge_stye(ch,i,i+1)){flag3=1;break;}i++;}if(flag1==1||flag2==0||flag3==1){printf(表达式有误!\n);return0;}}return1;}/*************************************表达式计算算法函数********************///辨别是操作数,还是运算符intin(charch[],inti){switch(ch[i]){case'+':case'-':case'*':case'/':case'(':case')':case'=':return1;default:return0;}}//运算符优先级比较函数charcharge(charch[],inti,charmemorry2){switch(ch[i]){case'+':case'-':if(memorry2=='('||memorry2=='=')return'';elsereturn'';case'*':case'/':if(memorry2=='*'||memorry2=='/')return'';elsereturn'';case'(':return'';case')':switch(memorry2){case'(':return'=';default:return'';}case'=':switch(memorry2){case'=':return'=';default:return'';}}}//表达式局部运算函数intoperate(intmemorry,charmemorry2,intmemorry1){switch(memorry2){case'+':return(memorry+memorry1);case'-':return(memorry-memorry1);case'*':return(memorry*memorry1);case'/':return(memorry/memorry1);}}//字符型转化为整形,如字符‘1’转化为整形1intchange_int(charch[],inti){intt;intsum=0;switch(ch[i])//还原为十进制数{case48:t=0;break;case49:t=1;break;case50:t=2;break;case51:t=3;break;case52:t=4;break;case53:t=5;break;case54:t=6;break;case55:t=7;break;case56:t=8;break;case57:t=9;break;}sum=sum*10+t;return(sum);}/************************************主函数**********************************/intmain(){HEAD1OPND;/
本文标题:栈的应用--计算器雏形
链接地址:https://www.777doc.com/doc-2359815 .html