24game.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:25k
源码类别:
控制台编程
开发平台:
C/C++
- #include"24.h"
- int oper[7]={43,45,42,47,40,41,35};//七种字符的ASCII码
- unsigned char Prior[7][7] = { // 表3.1 算符间的优先关系
- '>','>','<','<','<','>','>',
- '>','>','<','<','<','>','>',
- '>','>','>','>','<','>','>',
- '>','>','>','>','<','>','>',
- '<','<','<','<','<','=',' ',
- '>','>','>','>',' ','>','>',
- '<','<','<','<','<',' ','='
- };
- char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};
- status init_sq(sqlist *l){//初始化链表
- l=(sqlist*)malloc(sizeof(sqlist));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return OK;
- }
- status insert_sq(sqlist **p,int e,int bl){
- //链表插入操作,*p一直指向链表的最后一个结点
- //由于这里要求修改外部指针,因此要用指向指针的指针
- //将插入到链表的末尾
- sqlist *q;
- q=(sqlist*)malloc(sizeof(sqlist));
- q->num_ch=e;
- q->bol=bl;
- q->next=NULL;
- (*p)->next=q;
- (*p)=(*p)->next;
- // PR("insert %d ,%dis succssed!n",e,bl);
- return OK;
- }
- 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;
- PR("%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){
- PR("%d 不在给出的四个数字中!n",q->num_ch );
- right=0;
- }
- }
- }//end for
- for (i=0;i<4;i++){
- if(number[1][i]==0){
- PR("%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<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;
- ch=(int)s[i];
- insert_sq(&p,ch,bl);
- t=0;
- }
- else {
- PR("%c不是有效的运算符!n",s[i]);
- }
- } //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);
- }
- }
- bl=1;
- a=35;
- insert_sq(&p,a,bl);
- // PR("chang is OKn");
- return (check(*l));//转化完成了之后判断是否合法
- }
- int Operate(int a,int theta, int b) {//计算a theta b
- // PR("a=%d,theta=%c,b=%dn",a,theta,b);
- switch(theta) {
- case 43: return a+b;
- case 45: return a-b;
- case 42: return a*b;
- case 47:
- {
- if(b==0){
- return -2000;
- }
- if (a%b==0){
- return a/b;
- }
- else {//PR("不能为小数n");
- return -10000;
- }
- }
- default : return 0;
- }
- }
- int ReturnOpOrd(char op,char* TestOp) {
- //被char precede(char Aop, char Bop)所调用来求优先级
- int i;
- for(i=0; i< OPSETSIZE; i++) {
- if (op == TestOp[i]) return i;
- }
- return 0;
- }
- char precede(char Aop, char Bop) {//返回优先级
- return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)];
- }
- status 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;
- // PR("栈初始化完成!n");
- return OK;
- }
- int gettop(sqstack *s){//取栈顶元素
- int e;
- if(s->top==s->base){
- PR("栈空,无法取得栈顶元素!n");
- return 0;
- }
- e=*(s->top-1);
- // PR("取得栈顶元素: %d n",e);
- return e;
- }
- status push(sqstack *s,int e){//把 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;
- // PR("把 %d 压栈 is OKn",e);
- return OK;
- }
- status pop(sqstack *s,int *e){//出栈,用e 保存
- if(s->top==s->base){
- PR("栈空,出栈错误!n");
- return ERROR;
- }
- *e=*(--s->top);
- // PR(" %d出栈成功 !n",*e);
- return OK;
- }
- int EvaluateExpression(char* MyExpression) { // 算法3.4
- // 算术表达式求值的算符优先算法。
- // 设OPTR和&&OPND分别为运算符栈和运算数栈,OP为运算符集合。
- int result;
- sqstack OPTR; // 运算符栈,字符元素
- sqstack OPND; // 运算数栈,实数元素
- int c,bl,a,b,theta,top;
- sqlist *q,l;
- char *s=MyExpression;
- init_sq(&l);
- if(chang(s,&l)!=0){
- q=&l;
- initstack(&OPTR);
- push(&OPTR, 35);
- initstack (&OPND);
- q=q->next;
- c=q->num_ch;
- bl=q->bol;
- while ((c!= 35 || gettop(&OPTR)!=35)){
- if (bl!=1) {
- push(&OPND, c);
- q=q->next;
- c=q->num_ch;
- bl=q->bol;
- } // 不是运算符则进栈
- else {
- top=gettop(&OPTR);
- // PR("top %c",top);
- switch (precede(top, c)) {
- case '<': // 栈顶元素优先权低
- push(&OPTR, c);
- q=q->next;
- c=q->num_ch;
- bl=q->bol;
- break;
- case '=': // 脱括号并接收下一字符
- pop(&OPTR, &c);
- q=q->next;
- c=q->num_ch;
- bl=q->bol;
- break;
- case '>': // 退栈并将运算结果入栈
- pop(&OPTR, &theta);
- pop(&OPND, &b);
- pop(&OPND, &a);
- // PR("q->num_ch is %dn",q->num_ch);
- push(&OPND, Operate(a, theta, b));
- break;
- default :
- PR("没有这个运算符!n");
- return 0;
- } // switch
- }//else
- } // while
- result=gettop(&OPND);
- return result;
- }
- else {
- PR("你的输入有错误!n");
- return 0;
- }
- } // EvaluateExpression
- int randomm()//产生四个随机数
- {
- int i=0;
- srand((unsigned)time(NULL));
- for (;i<4;i++){
- number[0][i]=0;
- number[0][i]=rand();
- number[0][i]%=13;
- number[0][i]++;
- number[1][i]=0;
- }
- return number[2][4];
- }
- int CalcOneExpress(int expression[][2])
- {
- // 算术表达式求值的算符优先算法。
- // 设OPTR和&&OPND分别为运算符栈和运算数栈,OP为运算符集合。
- int index=0,result,c,theta,a,b;
- sqstack OPTR; // 运算符栈,字符元素
- sqstack OPND; // 运算数栈,实数元素
- initstack(&OPTR);
- push(&OPTR, 35);
- initstack (&OPND);
- c=expression[index][0];
- while (c!= 35 || gettop(&OPTR)!=35){
- if (expression[index][1]!=1) {
- push(&OPND, c);
- index++;
- c=expression[index][0];
- } // 不是运算符则进栈
- else {
- switch (precede(gettop(&OPTR), c)) {
- case '<':
- // 栈顶元素优先权低
- push(&OPTR, c);
- index++;
- c=expression[index][0];
- break;
- case '=': // 脱括号并接收下一字符
- pop(&OPTR, &c);
- index++;
- c=expression[index][0];
- break;
- case '>': // 退栈并将运算结果入栈
- pop(&OPTR, &theta);
- pop(&OPND, &b);
- pop(&OPND, &a);
- push(&OPND, Operate(a, theta, b));
- break;
- default :
- PR("没有这个运算符n");
- return 0;
- } // switch
- }//else
- } // while
- result=gettop(&OPND);
- return result;
- } // EvaluateExpression
- /****************计算机计算模块*******************************/
- int Equal24(int n)
- {
- if(n==24){
- // PR("the result is %dn",n);
- return TRUE;
- }
- else return FALSE;
- }
- //括号的几种情况
- //1 无括号
- //2 (a b) c d 同a b (c d), 下省略
- //3 (a b c) d
- //4 a (b c) d
- //5 (a b) (c d)
- //6 ((a b) c) d
- //7 (a (b c)) d
- int CalcArray1(int iNumInput[2][4])
- {
- // a * b * c * d //7 number
- int expression[8][2],ii,jj,kk;
- int i,j,k,l,dRes;
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[0][0]=iNumInput[0][i];
- expression[2][0]=iNumInput[0][j];
- expression[4][0]=iNumInput[0][k];
- expression[6][0]=iNumInput[0][l];
- expression[0][1]=eNumber;
- expression[2][1]=eNumber;
- expression[4][1]=eNumber;
- expression[6][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[1][0] = oper[ii];
- expression[1][1] = eOperator;
- expression[3][0] = oper[jj];
- expression[3][1] = eOperator;
- expression[5][0] = oper[kk];
- expression[5][1] = eOperator;
- expression[7][0] = oper[6];
- expression[7][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%d%c%d%c%d%c%dn",expression[0][0],oper[ii],expression[2][0],oper[jj],expression[4][0],oper[kk],expression[6][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray2(int iNumInput[2][4])
- {
- // (a * b) * c * d //9 number
- int expression[10][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray2n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[1][0]=iNumInput[0][i];
- expression[3][0]=iNumInput[0][j];
- expression[6][0]=iNumInput[0][k];
- expression[8][0]=iNumInput[0][l];
- expression[1][1]=eNumber;
- expression[3][1]=eNumber;
- expression[6][1]=eNumber;
- expression[8][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[2][0] = oper[ii];
- expression[2][1] = eOperator;
- expression[5][0] = oper[jj];
- expression[5][1] = eOperator;
- expression[7][0] = oper[kk];
- expression[7][1] = eOperator;
- expression[9][0] = oper[6];
- expression[9][1] = eOperator;
- expression[0][0] = oper[4];
- expression[0][1] = eOperator;
- expression[4][0] = oper[5];
- expression[4][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%c%d%c%d%c%c%d%c%dn",oper[4],expression[1][0],oper[ii],expression[3][0],oper[5],oper[jj],expression[6][0],oper[kk],expression[8][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray3(int iNumInput[2][4])
- {
- // (a * b * c) * d //9 number
- int expression[10][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray3n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[1][0]=iNumInput[0][i];
- expression[3][0]=iNumInput[0][j];
- expression[5][0]=iNumInput[0][k];
- expression[8][0]=iNumInput[0][l];
- expression[1][1]=eNumber;
- expression[3][1]=eNumber;
- expression[5][1]=eNumber;
- expression[8][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[2][0] = oper[ii];
- expression[2][1] = eOperator;
- expression[4][0] = oper[jj];
- expression[4][1] = eOperator;
- expression[7][0] = oper[kk];
- expression[7][1] = eOperator;
- expression[9][0] = oper[6];
- expression[9][1] = eOperator;
- expression[0][0] = oper[4];
- expression[0][1] = eOperator;
- expression[6][0] = oper[5];
- expression[6][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%c%d%c%d%c%d%c%c%dn",oper[4],expression[1][0],oper[ii],expression[3][0],oper[jj],expression[5][0],oper[5],oper[kk],expression[8][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray4(int iNumInput[2][4])
- {
- // (a * b * c) * d //9 number
- int expression[10][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray4n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[0][0]=iNumInput[0][i];
- expression[3][0]=iNumInput[0][j];
- expression[5][0]=iNumInput[0][k];
- expression[8][0]=iNumInput[0][l];
- expression[0][1]=eNumber;
- expression[3][1]=eNumber;
- expression[5][1]=eNumber;
- expression[8][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[1][0] = oper[ii];
- expression[1][1] = eOperator;
- expression[4][0] = oper[jj];
- expression[4][1] = eOperator;
- expression[7][0] = oper[kk];
- expression[7][1] = eOperator;
- expression[9][0] = oper[6];
- expression[9][1] = eOperator;
- expression[2][0] = oper[4];
- expression[2][1] = eOperator;
- expression[6][0] = oper[5];
- expression[6][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%d%c%c%d%c%d%c%c%dn",expression[0][0],oper[ii],oper[4],expression[3][0],oper[jj],expression[5][0],oper[5],oper[kk],expression[8][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray5(int iNumInput[2][4])
- {
- // (a * b) * (c * d) //11 number
- int expression[12][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray5n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[1][0]=iNumInput[0][i];
- expression[3][0]=iNumInput[0][j];
- expression[7][0]=iNumInput[0][k];
- expression[9][0]=iNumInput[0][l];
- expression[1][1]=eNumber;
- expression[3][1]=eNumber;
- expression[7][1]=eNumber;
- expression[9][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[2][0] = oper[ii];
- expression[2][1] = eOperator;
- expression[5][0] = oper[jj];
- expression[5][1] = eOperator;
- expression[8][0] = oper[kk];
- expression[8][1] = eOperator;
- expression[11][0] = oper[6];
- expression[11][1] = eOperator;
- expression[0][0] = oper[4];
- expression[0][1] = eOperator;
- expression[6][0] = oper[4];
- expression[6][1] = eOperator;
- expression[4][0] = oper[5];
- expression[4][1] = eOperator;
- expression[10][0] = oper[5];
- expression[10][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%c%d%c%d%c%c%c%d%c%d%cn",oper[4],expression[1][0],oper[ii],expression[3][0],oper[5],oper[jj],oper[4],expression[7][0],oper[kk],expression[9][0],oper[5]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray6(int iNumInput[2][4])
- {
- // ((a * b) * c) * d //11 number
- int expression[12][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray6n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[2][0]=iNumInput[0][i];
- expression[4][0]=iNumInput[0][j];
- expression[7][0]=iNumInput[0][k];
- expression[10][0]=iNumInput[0][l];
- expression[2][1]=eNumber;
- expression[4][1]=eNumber;
- expression[7][1]=eNumber;
- expression[10][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[3][0] = oper[ii];
- expression[3][1] = eOperator;
- expression[6][0] = oper[jj];
- expression[6][1] = eOperator;
- expression[9][0] = oper[kk];
- expression[9][1] = eOperator;
- expression[11][0] = oper[6];
- expression[11][1] = eOperator;
- expression[0][0] = oper[4];
- expression[0][1] = eOperator;
- expression[1][0] = oper[4];
- expression[1][1] = eOperator;
- expression[5][0] = oper[5];
- expression[5][1] = eOperator;
- expression[8][0] = oper[5];
- expression[8][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%c%c%d%c%d%c%c%d%c%c%dn",oper[4],oper[4],expression[2][0],oper[ii],expression[4][0],oper[5],oper[jj],expression[7][0],oper[5],oper[kk],expression[10][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int CalcArray7(int iNumInput[2][4])
- {
- // (a * b * c) * d //9 number
- int expression[12][2];
- int ii,jj,i,j,k,l,kk;
- int dRes;
- // PR("CalcArray7n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(j==i)
- {
- continue;
- }
- for(k=0;k<4;k++)
- {
- if(k==i||k==j)
- {
- continue;
- }
- for(l=0;l<4;l++)
- {
- if(l==i||l==j||l==k)
- {
- continue;
- }
- expression[1][0]=iNumInput[0][i];
- expression[4][0]=iNumInput[0][j];
- expression[6][0]=iNumInput[0][k];
- expression[10][0]=iNumInput[0][l];
- expression[1][1]=eNumber;
- expression[4][1]=eNumber;
- expression[6][1]=eNumber;
- expression[10][1]=eNumber;
- for (ii=0;ii<4;ii++)
- {
- for (jj=0;jj<4;jj++)
- {
- for (kk=0;kk<4;kk++)
- {
- expression[2][0] = oper[ii];
- expression[2][1] = eOperator;
- expression[5][0] = oper[jj];
- expression[5][1] = eOperator;
- expression[9][0] = oper[kk];
- expression[9][1] = eOperator;
- expression[11][0] = oper[6];
- expression[11][1] = eOperator;
- expression[0][0] = oper[4];
- expression[0][1] = eOperator;
- expression[3][0] = oper[4];
- expression[3][1] = eOperator;
- expression[7][0] = oper[5];
- expression[7][1] = eOperator;
- expression[8][0] = oper[5];
- expression[8][1] = eOperator;
- dRes = CalcOneExpress(expression);
- if(Equal24(dRes))
- {
- PR("可以这样运算:%c%d%c%c%d%c%d%c%c%c%dn",oper[4],expression[1][0],oper[ii],oper[4],expression[4][0],oper[jj],expression[6][0],oper[5],oper[5],oper[kk],expression[10][0]);
- return TRUE;
- }
- }
- }
- }//end of for oper
- }
- }
- }
- }
- return FALSE;
- }
- int Calc24(int number[2][4])
- {
- //括号的几种情况
- //1 无括号
- //2 (a b) c d 同a b (c d), 下省略
- //3 (a b c) d
- //4 a (b c) d
- //5 (a b) (c d)
- //6 ((a b) c) d
- //7 (a (b c)) d
- if (CalcArray1(number))//计算不含括号的情况
- return TRUE;
- if (CalcArray2(number))
- {
- return TRUE;
- }
- if (CalcArray3(number))
- {
- return TRUE;
- }
- if (CalcArray4(number))
- {
- return TRUE;
- }
- if (CalcArray5(number))
- {
- return TRUE;
- }
- if (CalcArray6(number))
- {
- return TRUE;
- }
- if (CalcArray7(number))
- {
- return TRUE;
- }
- PR("这四个数字不能算出24点.n");
- return FALSE;
- }
- /****************计算机计算模块*******************************/
- void game_24_main(){
- char s[40],ch,mood;
- int result,t=1,t0=1,nall=0,nright=0,t1=1;
- double right;
- while(t==1){
- PR("你想采用什么模式?n");
- PR(" ****************************************************n");
- PR(" *-------24点游戏菜单-------------------------------*n");
- PR(" * 计算机给出四个数字,你来算直到你想退出:-----0 *n");
- PR(" *自己给出四个数字,要计算机来算,直到想退出:-----1 *n");
- PR(" * 退出24点游戏:-----2 *n");
- PR(" ****************************************************n");
- PR("请选择模式:");
- mood=getch();
- PR("%cn",ch);
- // scanf( " %c",&mood);
- if (mood=='0'){
- nall=0;
- nright=0;
- t0=1;
- while(t0==1){
- number[2][4]=randomm();
- PR("这四个数字是: %d %d %d %dn",number[0][0],number[0][1],number[0][2],number[0][3]);
- PR("请输入算式n做上5道看看会有什么结果……n");
- PR("如果你认为这四个数算不出24点,请输入 ?n");
- PR("计算机将会给出答案,算不出也是一种答案!n");
- PR("你的算式是:");
- scanf ("%s",s);
- if(s[0]=='?'){
- if (Calc24(number)==1){
- nall++;
- }
- }
- else {
- result=EvaluateExpression(s);
- PR("你输入的算式的结果是: %d n",result);
- if(result==24){
- PR("恭喜您,你算对了!n");
- nright++;
- nall++;
- }
- else {
- PR("对不起,你算错了,谢谢!n");
- Calc24(number);
- nall++;
- }
- }//else
- PR("你共做了%d道,做对了%d道n",nall,nright);
- if (nall%5==0&&nall!=0){
- right=(double)nright/nall;
- PR("你的正确率为: %fn",right);
- if (right<0.5){
- PR("训练尚未成功,同志仍须努力!n");
- }
- else if(right>=0.5&&right<0.7){
- PR("不错不错,假以时日,必成良材!n");
- }
- else if(right>=0.7&&right<0.8){
- PR("将遇良材,非大战三百回合不可!n");
- }
- else if(right>=0.8&&right<0.96){
- PR("对你的敬佩之情,如滔滔江水,绵绵不绝---^^n");
- }
- else {
- PR("如此才智,老夫自愧不如n");
- }
- }
- PR("继续这个模式吗?请选择: 'y':继续 'n':退出?n");
- ch=getch();
- // scanf(" %c",&ch);
- if(ch=='n'||ch=='N'){
- t0=0;
- }
- else if (ch=='Y'||ch=='y') t0=1;
- else{
- PR("你的选择(输入)有误!n");
- // t0=0;
- }
- }//while mood 0
- }//end if mood 0
- else if (mood=='1'){
- t1=1;
- while (t1==1){
- PR("请输入四个正整数1-13:(用逗号隔开) ");
- scanf("%d,%d,%d,%d",&number[0][0],&number[0][1],&number[0][2],&number[0][3]);
- Calc24(number);
- PR("继续这个模式吗?请选择: 'y':继续 'n':退出?n");
- ch=getch();
- // scanf(" %c",&ch);
- if(ch=='n'||ch=='N'){
- t1=0;
- }
- else if (ch=='Y'||ch=='y') t1=1;
- else{
- PR("你的选择(输入)有误!n");
- t1=0;
- }
- }
- }//end mood 1
- else if (mood=='2'){
- // PR(" 再见!n");
- t=0;
- }
- else {
- PR("mood =%cn",mood);
- PR("你的选择(输入)有误!n");
- // t=0;
- }
- }//end big while
- }