📄 24new1.c
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIF_SIZE 50
#define STACKINCREMENT 10
typedef int status;
typedef struct sqstack{
int *base;
int *top;
int stacksize;
}sqstack;
typedef struct sqlist{
int bol;
int num_ch;
struct sqlist *next;
}sqlist;
sqlist *init_sq(){
sqlist *l;
l=(sqlist*)malloc(sizeof(sqlist));
if(l==NULL){
exit(OVERFLOW);
}
l->next=NULL;
return l;
}
status 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;
printf("insert %d is succssed!\n",e);
return OK;
}
sqtack *initstack(){
sqstack *s;
s->base = (int*)malloc(STACK_INIF_SIZE*sizeof(int));
if(s->base==NULL) exit(OVERFLOW);
s->top=s->base;
s->stacksize = STACK_INIF_SIZE;
return s;
}
int *gettop(sqstack *s){
int e;
if(s->top==s->base){
printf("NULL1\n");
return NULL;
}
e=*(s->top-1);
return &e;
}
status push(sqstack *s,int e){
if(s->top-s->base>=s->stacksize){
s->base=(int*)realloc(s->base ,(s->stacksize+STACKINCREMENT)*sizeof(int));
if(!s->base) exit(OVERFLOW);
s->stacksize+= STACKINCREMENT;
}
*(s->(top++))=e;
printf("push %d is OK\n",e);
return OK;
}
status pop(sqstack *s,int *e){
if(s->top==s->base){
printf("NULL2\n");
return ERROR;
}
*e=*((--s)->top);
printf("pop %d is OK!/n",*e);
return OK;
}
status chang(char *s,sqlist *l){
int t=0,temp,i=0;
int bl;
int a1,a2,a,ii;
sqlist *p=l;
for (;i<strlen(s);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;
insert_sq(&p,s[i],bl);
t=0;
}
} //end for
i=strlen(s)-1;
if(s[i]>47&&s[i]<58){
if(s[i-1]>47&&s[i-1]<58){
bl=0;
insert_sq(&p,a,bl);
}
else {
bl=0;
insert_sq(&p,a1,bl);
}
}
printf("chang is OK\n");
return OK;
}
int operate(int a,char c,int b){
if(c=='+') return(a+b);
else if(c=='-') return(a-b);
else if(c=='*') return(a*b);
else if(c=='/'){
if (b==0) {
printf("除数不能为零\n");
exit(ERROR);
}
else return (a/b);
}
}
char precede(char c1,char c2){
int tt=1;
char c3;
if(c1=='+'||c1=='-'){
if(c2=='*'||c2=='/'||c2=='(') c3='<';
else c3='>';
}
else if(c1=='*'||c1=='/'){
if(c2=='(') c3='<';
else c3='>';
}
else if(c1=='('){
if(c2==')') c3='=';
else if(c2=='#'){
printf("ERROR2!\n");
tt=0;
}
else c3='<';
}
else if(c1==')'){
if(c2==')')
{printf("ERROR!2\n"); tt=0; }
else if(c2=='#') c3='=';
else c3='<';
}
else printf("无此字符\n");
if(tt==0) exit(ERROR);
return(c3);
}
int evaluateExpression(sqlist *l){
int a,b,theta,result;
char c,ch;
sqlist *r=l;
sqstack *optr,*opnd;
ch='#';
optr=initstack();
push(optr,ch);
opnd=initstack();
c=r->num_ch;
while(c!=35||gettop(optr)->ch!='#'){
if(c>0&&c<14){
push(opnd,c);
r=r->next;
c=r->num_ch;
}
else {
switch(prcede(gettop(optr),c)){
case'<':
push(optr,c);
r=r->next;
c=r->num_ch;
break;
case'=':
pop(optr,&c);
r=r->next;
c=r->num_ch;
break;
case'>':
pop(optr,&theta);
pop(opnd,&b);
pop(opnd,&a);
result=operate(a,theta,b);
push(opnd,result);
break;
}//end switch
} //end else
} //end while
return *gettop(opnd);
}
void main (){
char *s,ch,tt[41];
int t=1;
int result;
sqlist *l;
l=init_sq();
printf("/***************************************************/\n");
printf(" 24点游戏 dos 版本\n");
printf(" 北京工商大学微机024班 欧阳锦林 版权所有\n");
printf(" 完成于2004年4月15日\n");
printf("/***************************************************/\n")
printf("The four numbers are 3,4,A,Q\nWhen you input the formula,\n");
printf("you should look A as 1,J as 11,Q as 12,k as 13\n");
while(t!=0){
gets(tt);
s=tt;
chang(s,l);
result=evaluateExpression(l);
if(result==24){
printf("Very good!!It's right.");
}
else {
printf("sorry!You are wrong!Thank you!");
}
printf("Contine?Or not??please input Y/N??");
ch=getchar();
if(ch=='n'||ch=='N')
t=0;
else if (ch=='Y'||ch=='y') continue;
else{
printf("your choic is not exact!!");
t=0;
}
}//end while
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -