首页 技术 正文
技术 2022年11月8日
0 收藏 449 点赞 1,460 浏览 1948 个字

C堆栈实现的表达式求值

//Luangeng#include<stdio.h>#include<conio.h>#include<windows.h>#define SIZE 100char t[10][14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,1,1,1,1,1,0,0,1,1,1,1,0,1,1,1,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,1,0,0,1,1,1,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};typedef struct { short x,y; short d; } step;typedef struct { step *top,*base; } stack;void gotoxy(int x,int y){COORD cd;cd.X = x;cd.Y = y;HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hout,cd);}void creat(stack *s){ s->base=(step *)malloc(SIZE*sizeof(step));s->top=s->base;if(!s->base) printf("failed !\n");}void push(stack *s,short a,short b,short c){ if(s->top>=(s->base+SIZE))printf("stack full !..........");else{ s->top++;s->top->x=a;s->top->y=b;s->top->d=c;gotoxy(b,a);printf("%c",1);}}void pop(stack *s){ if(s->base==s->top) printf("\nunderflow....\n");else { gotoxy(s->top->y,s->top->x); printf(" "); s->top--; }}int main(){ stack path;short i,j,k;creat(&path);for(i=1;i<=10;i++)for(j=1;j<=14;j++){gotoxy(j,i);if(t[i-1][j-1]==0)printf("%s","#");else printf(" ");}k=1;push(&path,2,2,4); t[1][1]=-1;while(!(path.top->x==9&&path.top->y==13)){ switch(path.top->d){case 4:path.top->d--;if(t[path.top->x-2][path.top->y-1]==1){t[path.top->x-2][path.top->y-1]=-1;push(&path,path.top->x-1,path.top->y,4); k++;}break;case 3:path.top->d--;if(t[path.top->x-1][path.top->y]==1){ t[path.top->x-1][path.top->y]=-1;push(&path,path.top->x,path.top->y+1,4); k++;}break;case 2:path.top->d--;if(t[path.top->x][path.top->y-1]==1){ t[path.top->x][path.top->y-1]=-1;push(&path,path.top->x+1,path.top->y,4); k++;}break;case 1:path.top->d--;if(t[path.top->x-1][path.top->y-2]==1){ t[path.top->x-1][path.top->y-2]=-1;push(&path,path.top->x,path.top->y-1,4); k++;}break;case 0: pop(&path); k--; break;}if(path.top==path.base){ gotoxy(17,1);printf("No way out !!");break;}sleep(5);gotoxy(17,10); printf("step=%d; ",k);}system("pause");}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905