栈的数据结构,包括实现文本编辑,迷宫求解,汉诺塔,多项式求解等。其中多项式求解因为所发设计的缺陷,所以只能计算10以内的数。
define.h
#include<iostream>
#include<stdlib.h>
using namespace std;
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
//typedef int SElemType;
typedef char SElemType;
typedef int status;
//typedef char status;
#include"define.h"
#include<string>
int c=0;//ººÅµËþ¼ÆÊýÆ÷
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
int x;
int y;
}PosType;
typedef int MazeType;
/*typedef struct
{
int ord;//ͨµÀ¿éÔÚ·¾¶Éϵġ°ÐòºÅ¡±
PosType seat;//ͨµÀ¿éÔÚÃÔ¹¬ÖеÄ×ø±êλÖÃ
int di;//´Ó´ËͨµÀ¿é×ßÏòÏÂһͨµÀ¿éµÄ·½Ïò
}SElemType;//Çó½âÃÔ¹¬Ê±ÓÃ*/
typedef struct
{
SElemType *base;//ÔÚÕ»¹¹Ôì֮ǰºÍÏú»ÙÖ®ºó£¬baseµÄֵΪNULL
SElemType *top;
int stackSize;//µ±Ç°ÒÑ·ÖÅäµÄ´æ´¢¿Õ¼ä£¬ÒÔÔªËØÎªµ¥Î»
}SqStack;
status InitStack(SqStack &s)
{
s.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s.base)
exit(OVERFLOW);
s.top=s.base;
s.stackSize=STACK_INIT_SIZE;
return OK;
}
status DestroyStack(SqStack &s)
{
free(s.base);
s.base=s.top=NULL;
s.stackSize=0;
return OK;
}
status stackEmpty(SqStack s)
{
if(s.base==s.top)
return true;
else
return false;
}
status Pop(SqStack &s,SElemType &e)
{
//ɾ³ýÕ»¶¥ÔªËØ£¬²¢ÓÃe·µ»Ø£¬ÈôΪ¿Õ£¬Ôò·µ»ØERROR
if(s.top==s.base)
{
printf("Empty stack!");
return ERROR;
}
e=*--s.top;
return OK;
}
status ClearStack(SqStack &s)
{
SElemType e;
while(!stackEmpty(s))
Pop(s,e);
if(s.top==s.base)
return OK;
else
return ERROR;
}
int stackLength(SqStack s)
{
int i;
i=0;
SElemType *T;
T=s.top;
while(T!=s.base)
{
T--;
i++;
}
return i;
}
status GetTop(SqStack s,SElemType &e)
{
//ÓÃe·µ»ØÕ»¶¥ÔªËØ£¬ÈôΪ¿ÕÔò·µ»ØERROR
if(s.top==s.base)
{
printf("Empty stack!");
return ERROR;
}
e=*(s.top-1);
return OK;
}
status Push(SqStack &s,SElemType e)
{
//²åÈëÔªËØeΪеÄÕ»¶¥ÔªËØ
if(s.top-s.base>=s.stackSize)
{
s.base=(SElemType *)realloc(s.base,(s.stackSize+STACKINCREMENT)*sizeof(SElemType));
if(!s.base)
exit(OVERFLOW);
s.top=s.base+s.stackSize;
s.stackSize=s.stackSize+STACKINCREMENT;
}
*s.top++=e;
return OK;
}
status PrintStack(SElemType e)
{
//printf("%d ",e);
printf("%c",e);
return OK;
}
status StackTraverse(SqStack s,status(* visit)(SElemType))
{
//±éÀú,Õ»µ×µ½Õ»¶¥
SElemType *T;
T=s.base;
while(T!=s.top)
{
if((* visit)(*T))
T++;
else
{
return ERROR;
printf("Something is wrong!");
}
}
return OK;
}
/*void conversion()
{
//ÊäÈë·Ç¸ºÊ®½øÖÆÊý£¬×ª»»³É°Ë½øÖÆ
SqStack s;
int N;
InitStack(s);
printf("please input data(positive number):");
scanf("%d",&N);
while(N)
{
Push(s,N%8);
N=N/8;
}
printf("The Octal number is:");
while(!stackEmpty(s))
{
Pop(s,N);
printf("%d",N);
}
printf("/n");
}*/
/*void LineEdit()
{
//ÀûÓÃ×Ö·ûÕ»s£¬´ÓÖն˽ÓÊÕÒ»ÐÐÊý¾Ý£¬ÔÙ´«ËÍÖÁµ÷Óùý³ÌµÄÊý¾ÝÇø
SqStack s;
SElemType ch,c;
InitStack(s);
printf("please input file(ctr+z expresse the end,# express backspace,@ express regression):");
ch=getchar();
while(ch!=EOF)//EOFΪȫÎĽáÊø·û
{
while(ch!=EOF&&ch!='/n')
{
switch(ch)
{
case '#'://Í˸ñ
Pop(s,c);
break;
case '@'://ÍËÐÐ
ClearStack(s);
break;
default:
Push(s,ch);
}
ch=getchar();
}
StackTraverse(s,PrintStack);
ClearStack(s);
if(ch!=EOF)
ch=getchar();
}
DestroyStack(s);
}*/
status Pass(MazeType m[4][4],PosType a)
{
//Åжϵ±Ç°Â·¾¶ÊÇ·ñ¿ÉÒÔͨ¹ý,0±íʾ¿ÉÒÔͨ¹ý
if(m[a.x][a.y]==0)
return OK;
else
return ERROR;
}
void FootPrint(PosType a)
{
printf("(%d,%d) ",a.x,a.y);
}
PosType NextPos(PosType a,int i)
{
//i±íʾ·½Ïò£¬1£º¶«£¬2£ºÄÏ£¬3£ºÎ÷£¬4£º±±
PosType c;
switch(i)
{
case 1:
a.x+=1;
break;
case 2:
a.y+=1;
break;
case 3:
a.x-=1;
break;
case 4:
a.y-=1;
break;
}
c=a;
return c;
}
void MarkPrint(PosType a,MazeType maze[4][4])
{
//ÁôÏÂ×߹ʵĺۼ£
maze[a.x][a.y]=1;
}
/*status MazePath(MazeType maze[4][4],PosType start,PosType end)
{
//ÈôÃÔ¹¬mazeÖдæÔÚ´ÓÈë¿Úµ½³ö¿ÚendµÄͨµÀ£¬ÔòÇóµÃÒ»Ìõ´æ·ÅÔÚÕ»ÖУ¬²¢·µ»Øtrue
//·ñÔò·µ»Øfalse
PosType curpos;//µ±Ç°Î»ÖÃ
SElemType e;
int curstep;//²½Êý
SqStack s;
InitStack(s);
curpos=start;
curstep=1;
do
{
if(Pass(maze,curpos))
{
FootPrint(curpos);
e.ord=curstep;
e.seat=curpos;
e.di=1;//µ±Ç°·½Ïò£¬Ä¬ÈÏΪ¶«
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y)
{
printf("Successful!");
printf("/n");
return true;
}
curpos=NextPos(curpos,1);//ÏÂÒ»²½
curstep++;
}
else
{
if(!stackEmpty(s))
{
Pop(s,e);
while(e.di==4&&!stackEmpty(s))
{
MarkPrint(e.seat,maze);//Áôϲ»ÄÜͨ¹ýµÄºÛ¼£
Pop(s,e);
}
if(e.di<4)
{
e.di++;
Push(s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!stackEmpty(s));
return false;
}*/
char Precede(char t1,char t2)
{
//ÅжÏÔËËã·ûµÄÓÅÏȼ¶
char f;
switch(t2)
{
case '+':
/*if(t1=='('||t1=='#')
f='<';
else
f='>';
break;*/
case '-':
if(t1=='('||t1=='#')
f='<';
else
f='>';
break;
case '*':
/*if(t1=='*'||t1=='/'||t1==')')
f='>';
else
f='<';
break;*/
case '/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else
f='<';
break;
case '(':
if(t1==')')
{
printf("ERROR1/n");
exit(ERROR);
}
else
f='<';
break;
case ')':
if(t1=='(')
f='=';
else if(t1=='#')
{
printf("ERROR1/n");
exit(ERROR);
}
else
f='>';
break;
case '#':
if(t1=='(')
{
printf("ERROR1/n");
exit(ERROR);
}
else if(t1=='#')
f='=';
else
f='>';
break;
}
return f;
}
bool In(SElemType c)
{
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '#':
case '(':
case ')':
return true;
default:
return false;
}
}
SElemType Operate(SElemType a,SElemType theta,SElemType b)
{
float pa,pb,r;
char R[1];
char ch;
pa=a-48;
pb=b-48;
switch(theta)
{
case '+':
r=pa+pb;
break;
case '-':
r=pa-pb;
break;
case '*':
r=pa*pb;
break;
case '/':
r=pa/pb;
break;
}
sprintf(R,"%f",r);
ch=R[0];
return ch;
}
SElemType EvaluateExpression()
{
//¼ÆËã0--9Ö®¼äµÄÖµ
SqStack OPTR;//·ûºÅ
SqStack OPND;//²Ù×÷Êý
SElemType e,c,x,theta,a,b;
InitStack(OPTR);
InitStack(OPND);
Push(OPTR,'#');//ÒÔ¡°#¡±×÷Ϊ½áÊø·û£¬ÔÚÕ»µ×ÊäÈë¡°#¡°×öÅä¶Ô
printf("please input Expression(0~~9,# express the end):/n");
c=getchar();
GetTop(OPTR,e);
while(c!='#'||e!='#')
{
if(!In(c))
{
Push(OPND,c);
c=getchar();
}
else
{
GetTop(OPTR,e);
switch(Precede(e,c))
{
case '<':
Push(OPTR,c);
c=getchar();
break;
case '=':
Pop(OPTR,x);//ÍÑÀ¨ºÅ
c=getchar();
break;
case '>'://ÍËÕ»£¬²¢½«ÔËËã½á¹¹ÈëÕ»
Pop(OPTR,theta);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));//ÔËËã½á¹ûÈëÕ»
break;
}
}
GetTop(OPTR,e);
}
GetTop(OPND,e);
return e;
}
void move(char x,int n,char z)
{
printf("%i move disk %i from %c to %c/n",++c,n,x,z);
}
void hanoi(int n,char x,char y,char z)
{
//Çó½âººÅµËþ,½«xÉϵÄÔ²Å̰ᵽzÉÏ£¬y×ö¸¨ÖúËþ
if(n==1)
move(x,1,z);
else
{
hanoi(n-1,x,z,y);
move(x,n,z);
hanoi(n-1,y,x,z);
}
}
void main()
{
SElemType s;
char x,y,z;
x='x';
y='y';
z='z';
/*MazeType maze[4][4]={{1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};
PosType start,end;
start.x=1;
start.y=1;
end.x=2;
end.y=2;
//maze[8][10]={{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,1,0,1,0,1,1},{1,1,0,0,1,1,0,0,0,1},{1,0,0,0,0,0,0,1,1,1},{1,1,1,0,1,1,0,0,0,1},{1,0,0,0,0,0,1,0,1,1},{1,1,0,1,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}};
MazePath(maze,start,end);
/*SqStack s;
SElemType e;
int i;
InitStack(s);
int a[]={1,2,3};
for(i=0;i<3;i++)
Push(s,a[i]);
StackTraverse(s,PrintStack);
Pop(s,e);
StackTraverse(s,PrintStack);
GetTop(s,e);
PrintStack(e);
conversion();
LineEdit();
s=EvaluateExpression();
cout<<s;
cout<<endl; */
hanoi(3,x,y,z);
}