欢迎来到天天文库
浏览记录
ID:58850910
大小:38.50 KB
页数:14页
时间:2020-09-23
《栈的应用--计算器雏形.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、//本计算器的缺陷在于操作数只能为(0到9),符号为+-*/()=2012.8.31#include#include#include/******************************括号匹配结构体********************************/typedefstructnode{charelem;structnode*next;}*PNODE,NODE;typedefstructhead{PNODEtop;PNOD
2、Ebase;}*PHEAD,HEAD;/******************************操作数结构************************************/typedefstructnode1{intelem;structnode1*next;}*PNODE1,NODE1;typedefstructhead1{PNODE1top;PNODE1base;}*PHEAD1,HEAD1;/******************************符号结构体***********
3、*************************/typedefstructnode2{charelem;structnode2*next;}*PNODE2,NODE2;typedefstructhead2{PNODE2top;PNODE2base;}*PHEAD2,HEAD2;/******************************括号匹配栈相关函数*****************************///初始化栈voidinitstack(PHEADph){ph->top=(PNOD
4、E)malloc(sizeof(NODE));if(!(ph->top)){printf("内存分配失败!");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)
5、);if(!pnew){printf("内存分配失败!");exit(-1);}else{pnew->elem=e;pnew->next=ph->top;ph->top=pnew;}return;}//出栈voidpop(PHEADph){PNODEp;if(empty(ph)){printf("栈3为空!");return;}else{p=ph->top;ph->top=p->next;free(p);return;}}//清空栈voidclear(PHEADph){PNODEp;while
6、(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("内存分配失败!");exit(
7、-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("内存分配失败!");exit(-1);}else{pnew->elem
8、=e;pnew->next=ph->top;ph->top=pnew;}return;}//元素出栈到*memorry里voidpop1(PHEAD1ph,int*pmemorry1){PNODE1p;if(empty1(ph)){printf("栈1为空!");return;}else{*pmemorry1=ph->top->elem;p=ph->top;ph->top=p->next;free(p);return;}}//清空栈voidclear
此文档下载收益归作者所有