严蔚敏版《数据结构 (C语言版)》和《数据结构题集》(五)——栈和队列

栈的顺序表实现

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 1024
 5 using namespace std;
 6 typedef int Elemtype;
 7 typedef struct {
 8 Elemtype data[MAXSIZE];
 9 int top;
10 }Seqstack;
11 void initSeqstack(Seqstack &s){
12 s.top=-1;
13 }
14 int stackEmpty(Seqstack &s){
15 return s.top==-1;
16 }
17 int SeqstackPush(Seqstack &s,Elemtype e){
18 if (s.top>=MAXSIZE-1)
19 return 0;
20 else {
21 s.top++;
22 s.data[s.top]=e;
23 return 1;
24 }
25 }
26 int SeqstackPop(Seqstack &s,Elemtype &e){
27 if (s.top==-1)
28 return 0;
29 else {
30 e=s.data[s.top];
31 s.top--;
32 return 1;
33 }
34 }
35 void getTop(Seqstack &s,Elemtype &e){
36 e=s.data[s.top];
37 }
38 void displaySeqstack(Seqstack &s){
39 for (int i=0;i<=s.top;i++)
40 printf("%d ",s.data[i]);
41 printf ("\n");
42 }
43 int main()
44 {
45     Seqstack s;Elemtype e;
46     initSeqstack(s);
47     for (int i=1;i<6;i++)
48     SeqstackPush(s,i);
49     displaySeqstack(s);
50     getTop(s,e);
51     printf ("%d\n",e);
52     SeqstackPop(s,e);
53     displaySeqstack(s);
54 
55 
56     return 0;
57 }

严蔚敏版 栈的实现

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define STACK_INIT_SIZE 100
 5 #define STACKINCREMENT 10
 6 #define OVERFLOW -2
 7 using namespace std;
 8 
 9 typedef int Elemtype;
10 typedef struct {
11 Elemtype *base;
12 Elemtype *top;
13 int stacksize;
14 }Seqstack;
15 int initStack(Seqstack &s){
16 s.base=(Elemtype *)malloc(sizeof(Elemtype)*STACK_INIT_SIZE);
17 if (!s.base ) exit(OVERFLOW);//存储分配失败
18 s.top=s.base;
19 s.stacksize=STACK_INIT_SIZE;
20 return 1;
21 }
22 int stackEmpty(Seqstack &s){
23 return s.top==s.base;
24 }
25 int stackLength(Seqstack &s){
26 return s.top-s.base;
27 }
28 int SeqstackPush(Seqstack &s,Elemtype e){
29 if (s.top-s.base >=s.stacksize){
30 s.base=(Elemtype *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(Elemtype));
31 if (!s.base) exit(OVERFLOW);
32 s.top=s.base+s.stacksize;
33 s.stacksize+=STACKINCREMENT;
34 //上溢后:
35 //1.分配存储空间给base指针 2、判断是否分配成功 3、top/stacksize重新修改正确
36 }
37 *s.top=e;
38 s.top++;//或者可以合成*s.top++=e,先赋值再++
39 return 1;
40 }
41 int SeqstackPop(Seqstack &s,Elemtype &e){
42 if(s.top==s.base){
43 printf ("null");
44 return 0;
45 }
46 s.top--;
47 e=*s.top;
48 return 1;
49 }
50 void getTop(Seqstack &s,Elemtype &e){
51 if (s.base!=s.top){
52 e=*--s.top;
53 }
54 }
55 void displaySeqstack(Seqstack &s){
56 for (int i=0;i<=s.top-s.base;i++)
57 printf("%d ",s.base[i]);
58 printf ("\n");
59 }
60 
61 int main()
62 {
63     Seqstack s;
64     initStack(s);
65     for (int i=1;i<6;i++)
66     SeqstackPush(s,i);
67     displaySeqstack(s);
68     Elemtype e;
69     getTop(s,e);
70     printf ("%d\n",e);
71     SeqstackPop(s,e);
72     displaySeqstack(s);
73 
74 
75     return 0;
76 }

建立链栈

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define OVERFLOW -2
 5 using namespace std;
 6 typedef int Elemtype;
 7 typedef struct node {
 8 Elemtype data;
 9 struct node *next;
10 }node,*linkstack;
11 void initLinkstack(linkstack &top){
12 top=NULL;//无头节点的链栈
13 }
14 int linkstackEmpty(linkstack &top){
15 return top==NULL;
16 }
17 int linkstackPush(linkstack &top,Elemtype e){
18 linkstack p=(linkstack )malloc (sizeof(node));
19 if (!p) exit(OVERFLOW);
20 p->data=e;
21 p->next=top;
22 top=p;
23 return 1;
24 }
25 int linkstackPop(linkstack &top,Elemtype &e){
26 e=top->data;
27 linkstack p=top;
28 top=top->next;
29 free(p);
30 return 1;
31 }
32 void getTop(linkstack &top,Elemtype &E){
33 E=top->data;
34 }
35 void displaylinkstack(linkstack &top){
36 linkstack p=top;
37 while (p){
38 printf ("%d ",p->data);
39 p=p->next;
40 }
41 printf ("\n");
42 }
43 int main()
44 {
45     linkstack top;
46     initLinkstack(top);
47     for (int i=1;i<6;i++)
48     linkstackPush(top,i);
49     displaylinkstack(top);
50     Elemtype e;
51     getTop(top,e);
52     printf ("%d\n",e);
53     linkstackPop(top,e);
54     displaylinkstack(top);
55 
56 
57     return 0;
58 }

栈的应用
1.数制转换:对于输入的非负十进制整数,打印输出与之等值的八进制数。

 1 //算法3.1
 2 void conversion(){
 3 Seqstack s;
 4 initSeqstack(s);
 5 int n;
 6 cout<<"请输入一个十进制数字"<<endl;
 7 cin>>n;
 8 while (n){
 9 int r=n%8;
10 SeqstackPush(s,r);
11 n=n/8;
12 }
13 displaySeqstack(s);
14 }

2.括号匹配检验
只有【】()两种括号,检查括号是否是匹配的。
输入一个字符:
1、左括号:压栈
2、右括号:如果栈为空,则右括号多了,输出不匹配信息;如果栈不为空,取出栈顶元素进行配对,如果配对成功弹栈,如果不成功则输出括号不匹配信息。
字符输入结束:
如果栈不为空,左括号多了,输出不匹配信息;否则输出终于匹配信息!

 1 int match(){
 2 Seqstack s;
 3 initSeqstack(s);
 4 char c;Elemtype e;
 5 scanf ("%c",&c);
 6 while (c!='#'){
 7     if (c=='['||c=='(')
 8         SeqstackPush(s,c);
 9         else if (c==']'||c==')'){
10         if (!stackEmpty(s)){
11         e=getTop(s);
12         if (e=='['&&c==']'||e=='('&&c==')'){
13         SeqstackPop(s,e);
14         }
15         }else
16         printf ("右括号太多了!\n");
17         }else
18         printf ("输入符号有误!\n");
19 
20 scanf ("%c",&c);
21 }
22 if (!stackEmpty(s))
23 printf ("左括号太多!\n");
24 else
25 printf ("成功匹配");
26 }

3.行编辑程序

 1 void lineEdit(){
 2 Seqstack s;
 3 initSeqstack(s);
 4 char c;
 5 cout<<"输入字符吧!\n"<<endl;
 6 cin>>c;
 7 while (c!='\n'){
 8 switch (c){
 9 case '#':SeqstackPop(s);break;
10 case '@':clearstack(s);break;
11 default:SeqstackPush(s,c);
12 }
13 c=getchar();
14 }
15 displaySeqstack(s);
16 
17 }

顺序队列的基本实现

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 1024
 5 using namespace std;
 6 typedef int Elemtype;
 7 typedef struct{
 8 Elemtype data[MAXSIZE];
 9 int rear,front;
10 }Seqqueue;
11 void initSeqqueue(Seqqueue &q){
12 q.rear=q.front=-1;
13 }
14 int emptySeqqueue(Seqqueue &q){
15 return q.rear==q.front;
16 }
17 int enSeqqueue(Seqqueue &q,Elemtype e){
18     //先判断是否栈满
19     if (q.rear-q.front>=MAXSIZE){
20     printf ("full!\n");
21     return 0;
22     }
23     q.rear++;
24     q.data[q.rear]=e;
25     return 1;
26 }
27 int deSeqqueue(Seqqueue &q,Elemtype &e){
28 if (emptySeqqueue(q)){
29 printf ("null!\n");
30 return 0;
31 }
32 q.front++;
33 e=q.data[q.front];
34 return 1;
35 }
36 Elemtype getFront(Seqqueue &q){
37 if (emptySeqqueue(q)){
38 printf ("null!\n");
39 }
40 else {
41 Elemtype e;
42 e=q.data[q.front++];
43 return e;
44 }
45 }
46 void display(Seqqueue &q){
47 if (emptySeqqueue(q)){
48 printf ("null!\n");
49 }
50 else {
51 int i=1+q.front;
52 while (i<=q.rear){
53 printf ("%d ",q.data[i]);
54 i++;
55 }
56 printf ("\n");
57 }
58 }
59 int main()
60 {
61     Seqqueue q;
62     initSeqqueue(q);
63     for (int i=1;i<6;i++)
64     enSeqqueue(q,i);
65     display(q);
66     Elemtype e;
67     deSeqqueue(q,e);
68     printf ("%d\n",e);
69     e=getFront(q);
70         printf ("%d\n",e);
71 
72 
73     return 0;
74 }

//1.用两个栈,一个s1,一个s2,来模拟一个队列。队列是先进先出,栈是先进后出,用两个栈模拟一个队列

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 1024
 5 using namespace std;
 6 //1.用两个栈,一个s1,一个s2,来模拟一个队列。队列是先进先出,栈是先进后出,用两个栈模拟一个队列
 7 //2.一带头结点的循环链表表示队列,设计出队入队的算法。
 8 //3.用一个数组建两个栈。建两个以上的栈最好用链栈,每一个都是一个栈。
 9 //因为用数组建两个栈可以,不能建多个栈
10 //4.队列的题目已经上传。
11 
12 typedef int Elemtype;
13 typedef struct {
14 Elemtype data[MAXSIZE];
15 int top;
16 }Seqstack;
17 void initSeqstack(Seqstack &s){
18 s.top=-1;
19 }
20 int isempty(Seqstack &s){
21 return s.top==-1;
22 }
23 int push(Seqstack &s,Elemtype e){
24 if (s.top>=MAXSIZE-1){
25 printf ("full\n");
26 return 0;
27 }
28 s.top++;
29 s.data[s.top]=e;
30 return 1;
31 }
32 int pop(Seqstack &s,Elemtype &e){
33 if (s.top==-1){
34 printf ("null\n");
35 return 0;
36 }
37 e=s.data[s.top];
38 s.top--;
39 return 1;
40 }
41 Elemtype gettop(Seqstack &s){
42 return s.data[s.top];
43 }
44 //入队,直接进1号栈;出队,先判断2号栈是否有元素,有元素就直接弹出栈顶即队首,如果2号栈没有元素,则将1号栈的元素顺序弹出并进2号栈。
45 typedef struct {
46 Seqstack s1;//数据输入栈
47 Seqstack s2;//数据缓存栈,便于先存放进去的元素先出来
48 }dulstack;
49 void initDulstack(dulstack &d){
50 initSeqstack(d.s1);
51 initSeqstack(d.s2);
52 }
53 int enterQueue(dulstack &d,Elemtype x){
54     Elemtype e;
55 if (isempty(d.s1)){
56 //如果S1栈为空,应当把S2栈中的元素全部弹出压入该栈
57 while (!isempty(d.s2)){
58 pop(d.s2,e);
59 push(d.s1,e);
60 }
61 }
62 push(d.s1,x);
63 return 1;
64 }
65 int deQueue(dulstack &d,Elemtype &x){
66     Elemtype e;
67     if (!isempty(d.s2)){
68     x=gettop(s2);
69     return 1;
70     }
71 while (!isempty(d.s1)){
72 pop(d.s1,e);
73 push(d.s2,e);
74 }
75 pop(d.s2,x);
76 return 1;
77 }
78 int isemptydulQueue(dulstack d){
79 return isempty(d.s1)&&isempty(d.s2);
80 }
81 
82 
83 int main()
84 {
85 dulstack d;
86 initDulstack(d);
87 Elemtype x;
88 scanf ("%d",&x);
89 while (x!=-999){
90 enterQueue(d,x);
91 scanf ("%d",&x);
92 }
93 while (!isemptydulQueue(d)){
94 deQueue(d,x);
95 printf ("%d ",x);
96 }
97 cout<<endl;
98     return 0;
99 }

//2.一带头结点的循环链表表示队列,设计出队入队的算法。

 1 typedef int Elemtype;
 2 typedef struct node {
 3 Elemtype data;
 4 struct node *next;
 5 }node,*Queueptr;
 6 typedef struct {
 7 Queueptr front ;
 8 Queueptr rear;
 9 }linkQueue;
10 int  initQueue(linkQueue &q){
11 Queueptr lq=(Queueptr)malloc(sizeof(node));
12 if (!lq) exit(OVERFLOW);
13 lq->next=NULL;
14 q.front=q.rear=lq;
15 }
16 int isempty(linkQueue q){
17 return q.front==q.rear;
18 }
19 int enterQueue(linkQueue &q,Elemtype e){
20 Queueptr p=(Queueptr)malloc(sizeof(node));
21 if (!p) exit(OVERFLOW);
22 p->data=e;
23 p->next=q.rear->next;
24 q.rear->next=p;
25 q.rear=p;
26 return 1;
27 }
28 int deQueue(linkQueue &q,Elemtype &e){
29     //出队依旧要判空,入队不需要判满了
30     if (q.rear==q.front){
31     printf("null\n");
32     return 0;
33     }
34 Queueptr p=q.front->next;
35 e=p->data;
36 q.front->next=p->next;
37 //这里要特别注意如果链表中唯一的元素要出队,尾指针必须要重新指向头结点,不然丢失该指针了
38 if (q.front->next==NULL){//或者q.rear==p;
39 q.rear=q.front;
40 }
41 free(p);
42 return 1;
43 }
44 
45 int main()
46 {
47 linkQueue q;
48 initQueue(q);
49 Elemtype e;
50 scanf ("%d",&e);
51 while (e!=-999){
52 enterQueue(q,e);
53 scanf ("%d",&e);
54 }
55 while (!isempty(q)){
56 deQueue(q,e);
57 printf ("%d ",e);
58 }
59 cout<<endl;
60     return 0;
61 }

用两个队列建立一个栈
/两个队列模拟一个堆栈/
/*队列A、B
入栈:将元素依次压入到非空的队列,第一个元素压倒对列A
出栈:把队列A的前n-1个元素倒到队列B,把第n个元素去掉。此时数据在B中,下次操作,则对B操作。
栈顶:把队列A的前n-1个元素倒到队列B,把第n个元素作为栈顶*/

  1 #include <iostream>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #define MAXSIZE 1024
  5 #define OVERFLOW -2
  6 using namespace std;
  7 
  8 typedef int Elemtype;
  9 typedef struct node {
 10 Elemtype data;
 11 struct node *next;
 12 }node,*Queueptr;
 13 typedef struct {
 14 Queueptr front ;
 15 Queueptr rear;
 16 }linkQueue;
 17 int  initQueue(linkQueue &q){
 18 Queueptr lq=(Queueptr)malloc(sizeof(node));
 19 if (!lq) exit(OVERFLOW);
 20 lq->next=NULL;
 21 q.front=q.rear=lq;
 22 }
 23 int isempty(linkQueue q){
 24 return q.front==q.rear;
 25 }
 26 int enterQueue(linkQueue &q,Elemtype e){
 27 Queueptr p=(Queueptr)malloc(sizeof(node));
 28 if (!p) exit(OVERFLOW);
 29 p->data=e;
 30 p->next=q.rear->next;
 31 q.rear->next=p;
 32 q.rear=p;
 33 return 1;
 34 }
 35 int deQueue(linkQueue &q,Elemtype &e){
 36     //出队依旧要判空,入队不需要判满了
 37     if (q.rear==q.front){
 38     printf("null\n");
 39     return 0;
 40     }
 41 Queueptr p=q.front->next;
 42 e=p->data;
 43 q.front->next=p->next;
 44 //这里要特别注意如果链表中唯一的元素要出队,尾指针必须要重新指向头结点,不然丢失该指针了
 45 if (q.front->next==NULL){//或者q.rear==p;
 46 q.rear=q.front;
 47 }
 48 free(p);
 49 return 1;
 50 }
 51 int getlength(linkQueue &lq){
 52 Queueptr p=lq.front->next;
 53 int count=0;
 54 while (p!=NULL){
 55 count++;
 56 p=p->next;
 57 }
 58 return count;
 59 }
 60 typedef struct {
 61 linkQueue q1;
 62 linkQueue q2;
 63 }dulQueue;
 64 void initDulQueue(dulQueue &dq){
 65 initQueue(dq.q1);
 66 initQueue(dq.q2);
 67 }
 68 int isemptyDul(dulQueue dq){
 69 return isempty(dq.q1)&&isempty(dq.q2);
 70 }
 71 int pushDul(dulQueue &dp,Elemtype e){
 72 if (isempty(dp.q2)){
 73 enterQueue(dp.q1,e);
 74 }
 75 if (isempty(dp.q1)){
 76 enterQueue(dp.q2,e);
 77 }
 78 return 1;
 79 }
 80 int popDul(dulQueue &dp,Elemtype &x){
 81     Elemtype e;
 82 if (isempty(dp.q2)){
 83 int count=getlength(dp.q1);
 84 for (int i=0;i<count-1;i++){
 85 deQueue(dp.q1,e);
 86 enterQueue(dp.q2,e);
 87 }
 88 if (isempty(dp.q1)){
 89 //如果这时Q1弹出了最后一个元素
 90 deQueue(dp.q2,x);
 91 }else
 92 deQueue(dp.q1,x);
 93 return 1;
 94 }
 95 if (isempty(dp.q1)){
 96     int count=getlength(dp.q2);
 97 for (int i=0;i<count-1;i++){
 98 deQueue(dp.q2,e);
 99 enterQueue(dp.q1,e);
100 }
101 deQueue(dp.q2,x);
102 return 1;
103 }
104 }
105 int main()
106 {
107 dulQueue dq;
108 initDulQueue(dq);
109 Elemtype e;
110 scanf ("%d",&e);
111 while (e!=-999){
112 pushDul(dq,e);
113 scanf ("%d",&e);
114 }
115 while (!isemptyDul(dq)){
116 popDul(dq,e);
117 printf ("%d ",e);
118 }
119 cout<<endl;
120     return 0;
121 }

用一个数组建立两个栈

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 20
 5 #define OVERFLOW -2
 6 using namespace std;
 7 
 8 typedef int Elemtype;
 9 typedef struct {
10 Elemtype data[MAXSIZE];
11 int top[2];
12 }Seqstack;
13 void initSeqstack(Seqstack &s){
14 s.top[0]=-1;
15 s.top[1]=MAXSIZE;
16 }
17 int isempty(Seqstack s,int i){
18 if (i==0)
19 return s.top[i]==-1;
20 else
21 return s.top[i]==MAXSIZE;
22 }
23 int isfull(Seqstack s){
24 return s.top[0]+1==s.top[1];
25 }
26 int push(Seqstack &s,Elemtype e,int i){
27 //用int i来判断用户想要push进0栈还是1栈
28 if (isfull(s)){
29 printf("full!\n");
30 return 0;
31 }
32 if (i==0)
33 s.top[i]++;
34 else
35 s.top[i]--;
36 s.data[s.top[i]]=e;
37 return 1;
38 }
39 int pop(Seqstack &s,Elemtype &e,int i){
40 if (isempty(s,i)){
41 printf ("null\n");
42 return 0;
43 }
44 e=s.data[s.top[i]];
45 if (i==0)
46 s.top[i]--;
47 else
48 s.top[i]++;
49 return 1;
50 }
51 int main()
52 {
53     Seqstack s;
54     initSeqstack(s);
55     int x,i;
56     scanf ("%d %d",&x,&i);
57     while (x!=-999){
58     push(s,x,i);
59     scanf ("%d %d",&x,&i);
60     }
61     for (i=0;i<2;i++){
62     while (!isempty(s,i)){
63     pop(s,x,i);
64     printf ("%d ",x);
65     }
66     cout<<endl;
67 
68     }
69 
70 
71 
72     return 0;
73 }

顺序循环队列

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 8
 5 #define OVERFLOW -2
 6 using namespace std;
 7 
 8 typedef int Elemtype;
 9 typedef struct{
10 Elemtype data[MAXSIZE];
11 int rear,front;
12 }Seqqueue;
13 void initSeqqueue(Seqqueue &q){
14 q.rear=q.front=-1;
15 }
16 int emptySeqqueue(Seqqueue &q){
17 return q.rear==q.front;
18 }
19 int enSeqqueue(Seqqueue &q,Elemtype e){
20     //先判断是否队满
21     if ((q.rear+1)%MAXSIZE==q.front){
22     printf ("full!\n");
23     return 0;
24     }
25     q.rear=(q.rear+1)%MAXSIZE;
26     q.data[q.rear]=e;
27     return 1;
28 }
29 int deSeqqueue(Seqqueue &q,Elemtype &e){
30 if (emptySeqqueue(q)){
31 printf ("null!\n");
32 return 0;
33 }
34 q.front=(q.front+1)%MAXSIZE;
35 e=q.data[q.front];
36 return 1;
37 }
38 Elemtype getFront(Seqqueue &q){
39 if (emptySeqqueue(q)){
40 printf ("null!\n");
41 }
42 else {
43 Elemtype e;
44 q.front=(q.front+1)%MAXSIZE;
45 e=q.data[q.front];
46 return e;
47 }
48 }
49 void display(Seqqueue &q){
50 if (emptySeqqueue(q)){
51 printf ("null!\n");
52 }
53 else {
54 int i=(1+q.front)%MAXSIZE;
55 while (i<=q.rear){
56 printf ("%d ",q.data[i]);
57 i=(i+1)%MAXSIZE;
58 }
59 printf ("\n");
60 }
61 }
62 int getlength(Seqqueue &q){
63 return (q.rear-q.front+MAXSIZE)%MAXSIZE;
64 }
65 int main()
66 {
67     Seqqueue s;
68     initSeqqueue(s);
69     int x;
70     scanf ("%d",&x);
71     while (x!=-999){
72     enSeqqueue(s,x);
73     scanf ("%d",&x);
74     }
75 
76     deSeqqueue(s,x);
77     printf ("%d ",x);
78     deSeqqueue(s,x);
79     printf ("%d ",x);
80     scanf ("%d",&x);
81     enSeqqueue(s,x);
82     deSeqqueue(s,x);
83     printf ("%d ",x);
84     cout<<endl;
85     return 0;
86 }

表达式求值

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #define MAXSIZE 100
  5 using namespace std;
  6 typedef char Elemtype;
  7 typedef struct {
  8 Elemtype elem[MAXSIZE];
  9 int top;
 10 }SqStack;
 11 void initSqstack(SqStack &s){
 12 s.top=-1;
 13 }
 14 int isempty(SqStack &s){
 15 
 16 return s.top==-1;
 17 }
 18 int push(SqStack &s,Elemtype e){
 19 if (s.top>=MAXSIZE-1)
 20 return 0;
 21 else {
 22 s.top++;
 23 s.elem[s.top]=e;
 24 return 1;
 25 }
 26 }
 27 int pop(SqStack &s,Elemtype &e){
 28 if (s.top==-1)
 29 return 0;
 30 else {
 31 e=s.elem[s.top];
 32 s.top--;
 33 return 1;
 34 }
 35 }
 36 Elemtype gettop(SqStack &s){
 37 Elemtype e;
 38 e=s.elem[s.top];
 39 return e;
 40 }
 41 int in(Elemtype &c){
 42 if (c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#'||c=='\n')
 43     return 1;
 44 else return 0;
 45 }
 46 char precede(Elemtype a,Elemtype b){
 47 //比较两个运算符的优先级
 48 if((a=='+'&&(b=='+'||b=='-'||b==')'||b=='#'))||(a=='-'&&(b=='+'||b=='-'||b==')'||b=='#'))
 49 ||(a=='*'&&(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#'))||(a=='/'&&(b=='+'||b=='-'
 50 ||b=='*'||b=='/'||b==')'||b=='#'))||(a==')'&&(b=='+'||b=='-'
 51 ||b=='*'||b=='/'||b==')'||b=='#')))
 52     return '>';
 53 if ((a=='+'&&(b=='*'||b=='/'||b=='('))||(a=='-'&&(b=='*'||b=='/'||b=='('))
 54 ||(a=='*'&&b=='(')||(a=='/'&&b=='(')||(a=='('&&(b=='+'||b=='-'
 55 ||b=='*'||b=='/'||b=='('))||(a=='#'&&(b=='+'||b=='-'
 56 ||b=='*'||b=='/'||b=='(')))
 57 return '<';
 58 if((a=='('&&b==')')||(a=='#'&&b=='#'))
 59 return '=';
 60 }
 61 Elemtype operate(Elemtype &a,Elemtype &th,Elemtype &b){
 62 int aa=a-'0';
 63 int bb=b-'0';
 64 char c;
 65 switch (th){
 66 case '+':c=char(aa+bb+'0');
 67     break;
 68 case '-':c=char(bb-aa+'0');
 69     break;
 70 case '*':c=char(aa*bb+'0');
 71     break;
 72 case '/':c=char(aa/bb+'0');
 73     break;
 74 }
 75 return c;
 76 }
 77 Elemtype evaluateExpression(){
 78 SqStack optr,opnd;
 79 initSqstack(optr);initSqstack(opnd);
 80 push(optr,'#');
 81 char c=getchar(),x,theta,a,b;
 82 while (c!='#'||gettop(optr)!='#')
 83 {
 84     //输入#时表达式结束,操作符栈的栈顶元素为#时说明全部弹出并进行运算
 85     //如果C是运算符则1, 比较栈顶元素的优先级大小,选择弹栈或者压栈
 86     //如果C是数字,这里假定全部为十位数以内,则压栈。
 87     //为什么这里是\\,我们想要设置为:两者都为#时循环结束,取非即为a!=#||b!#
 88 if (!in(c))
 89 {
 90     push(opnd,c);c=getchar();
 91 }else {
 92 switch (precede(gettop(optr),c)){
 93 case '>':
 94 pop(optr,theta);
 95 pop(opnd,a);
 96 pop(opnd,b);
 97 push(opnd,operate(a,theta,b));
 98 //这里不用c=getchar()因为c此时仍然为优先级较小的运算符,应当将循环继续,与gettop的运算
 99 //符相比,如果还是比top的运算符小,c还是不能进栈,直到满足<的条件时才能进栈。
100     break;
101 case '=':pop(optr,x);c=getchar();
102     break;
103 case '<':push(optr,c);c=getchar();
104     break;
105 }
106 }
107 }
108 return gettop(opnd);
109 }
110 int main()
111 {
112     Elemtype c=evaluateExpression();
113     cout<<c;
114 
115     return 0;
116 }

这种做法的弊端是:只能运算十以内的数字,而且在运算过程中也不能产生大于等于十的结果。
这里写图片描述

猜你喜欢

转载自www.cnblogs.com/twomeng/p/9476558.html