数据结构24点游戏源代码
- 格式:doc
- 大小:103.00 KB
- 文档页数:21
#include
#include
#include
#include
#define OPSETSIZE 7
#define STACK_INIF_SIZE 50
#define STACKINCREMENT 10
int number[2][4];
enum
{
eNumber = 0, //操作数
eOperator = 1 //算子
};
int oper[7]={43,45,42,47,40,41,35};
char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};
typedef struct sqlist{
int bol;//bol 是0 时,num-ch是一个数字;bol 是1 时num_ch 运算符int num_ch;
struct sqlist *next;
}sqlist;//线性表
typedef struct sqstack{
int *base;
int *top;
int stacksize;
}sqstack;//栈的定义
unsigned char Prior[7][7] = {// 课本表3.1 算符间的优先关系
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=',' ',
'>','>','>','>',' ','>','>',
'<','<','<','<','<',' ','='
};
int init_sq(sqlist *l){//初始化链表
l=(sqlist*)malloc(sizeof(sqlist));
if(l==NULL){
exit(-2);
}
l->next=NULL;
return 1;
}
int insert_sq(sqlist **p,int e,int bl){//链表插入操作
sqlist *q;
q=(sqlist*)malloc(sizeof(sqlist));
q->num_ch=e;
q->bol=bl;
q->next=NULL;
(*p)->next=q;
(*p)=(*p)->next;
return 1;
}
int check(sqlist l)//保证输入的数字是给出的四个数字
{
int right=1,find=0,i;
sqlist *q=&l;
q=q->next ;
for (;q->next!=NULL;q=q->next){
if(q->bol==1){
if(q->num_ch <=39||q->num_ch>57||q->num_ch==44||q->num_ch==46){ right=0;
printf("%c不是有效的运算符!\n");
}
}
else {
find=0;
for(i=0;i<4;i++){
if(number[1][i]==0&&number[0][i]==q->num_ch ){
number[1][i]=1;
find=1;
break;
}
}
if(find==0){
printf("%d 不在给出的四个数字中!\n",q->num_ch );
right=0;
}
}
}//end for
for (i=0;i<4;i++){
if(number[1][i]==0){
printf("%d没有用上!\n",number[0][i]);
right=0;
}
}
return right;
}
int chang(char *s,sqlist *l){//将用户的输入转化为单链表int t=0;
unsigned int i=0;
int bl,ch;
int a1,a2,a;
sqlist *p=l;
for (;i { if(s[i]>47&&s[i]<58&&t==0){ a1=(int)s[i]-48; t++; } else if(s[i]>47&&s[i]<58&&t==1){ a2=(int)s[i]-48; a=a1*10+a2; t++; } else if(s[i]<48&&s[i]>39&&s[i]!=44&&s[i]!=46){ if(t==1){ bl=0; insert_sq(&p,a1,bl); t=0; } else if(t==2){ bl=0; insert_sq(&p,a,bl); t=0; } bl=1; ch=(int)s[i]; insert_sq(&p,ch,bl); t=0; } else { printf("%c不是有效的运算符!\n",s[i]); }