CHECK_IF_int.C
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:20k
源码类别:
控制台编程
开发平台:
C/C++
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- //#include<time.h>
- #define MAX 13
- #define OK 1
- #define TRUE 1
- #define FALSE 0
- #define ERROR 0
- #define OVERFLOW -2
- #define OPSETSIZE 7
- #define STACK_INIF_SIZE 50
- #define STACKINCREMENT 10
- typedef int status;
- typedef struct sqstack{
- int *base;
- int *top;
- int stacksize;
- }sqstack;
- unsigned char Prior[7][7] = { // 表3.1 算符间的优先关系
- '>','>','<','<','<','>','>',
- '>','>','<','<','<','>','>',
- '>','>','>','>','<','>','>',
- '>','>','>','>','<','>','>',
- '<','<','<','<','<','=',' ',
- '>','>','>','>',' ','>','>',
- '<','<','<','<','<',' ','='
- };
- char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};
- int number[2][4]={2,2,1,6};
- enum
- {
- eNumber = 0, //操作数
- eOperator = 1 //算子
- };
- /*enum MyOP
- {
- eAdd = 43,
- eSub = 45,
- eMulti = 42,
- eDiv = 47,
- eLeftParen = 40,
- eRightParen = 41,
- head_end = 35
- };*/
- int oper[7]={43,45,42,47,40,41,35};
- int Operate(int a,int theta, int b) {
- // printf("a=%d,theta=%c,b=%dn",a,theta,b);
- // int max,min;
- switch(theta) {
- case 43: return a+b;
- case 45: return a-b;
- case 42: return a*b;
- case 47:
- {if(b==0){
- printf("除数不能为0!n");
- exit(ERROR);
- }
- if (a%b==0){
- return a/b;
- }
- else {
- //printf("不能为小数n");
- return -1;
- // exit(0);
- }
- }
- default : return 0;
- }
- }
- int ReturnOpOrd(char op,char* TestOp) {
- int i;
- for(i=0; i< OPSETSIZE; i++) {
- if (op == TestOp[i]) return i;
- }
- return 0;
- }
- char precede(char Aop, char Bop) {
- // printf("%cn",Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)]);
- // printf("%c, %cn",Aop,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;
- // printf("initstack is finished!n");
- return OK;
- }
- int gettop(sqstack *s){
- int e;
- if(s->top==s->base){
- printf("NULL1n");
- return 0;
- }
- e=*(s->top-1);
- // printf("gettop %d is OK!n",e);
- 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 OKn",e);
- return OK;
- }
- status pop(sqstack *s,int *e){
- if(s->top==s->base){
- printf("出时栈栈为空!n");
- return ERROR;
- }
- *e=*(--s->top);
- // printf("pop %d is OK!n",*e);
- return OK;
- }
- 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){
- // printf("while start!n");
- if (expression[index][1]!=1) {
- push(&OPND, c);
- index++;
- c=expression[index][0];
- } // 不是运算符则进栈
- else {
- switch (precede(gettop(&OPTR), c)) {
- case '<':
- // printf("case <!n"); // 栈顶元素优先权低
- 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);
- // printf("q->num_ch is %dn",q->num_ch);
- push(&OPND, Operate(a, theta, b));
- /* if (q->num_ch !=35){
- index++;
- c=expression[index][0];
- printf("%dn",c);*/
- // }
- break;
- default :
- printf("have not this oper!n");
- return 0;
- } // switch
- }//else
- } // while
- result=gettop(&OPND);
- printf("the result is %dn",result);
- return result;
- } // EvaluateExpression
- /*status CombineResult(int expression[][2],int n)
- {
- char s[15];
- */
- int Equal24(int n)
- {
- if(n==24){
- printf("the result is %dn",n);
- return TRUE;
- }
- else return FALSE;
- }
- 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))
- {
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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;
- printf("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))
- {
- // printf("ok!n");
- // CombineResult(expression, 7);//组织结果表达式
- // printf("%d,%d,%d,%d,%d,%d,%dn",i,ii,j,jj,k,kk,l);
- // printf("%d%cn",expression[i][0],oper[ii]);
- printf("%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])
- {
- int find=0;
- //括号的几种情况
- //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))
- {
- find=1;
- return TRUE;
- }
- if (CalcArray2(number))
- {
- find=1;
- return TRUE;
- }
- if (CalcArray3(number))
- {
- find=1;
- return TRUE;
- }
- if (CalcArray4(number))
- {
- find=1;
- return TRUE;
- }*/
- if (CalcArray5(number))
- {
- find=1;
- return TRUE;
- }
- /* if (CalcArray6(number))
- {
- find=1;
- return TRUE;
- }
- if (CalcArray7(number))
- {
- find=1;
- return TRUE;
- }*/
- if(find==0)
- {
- printf("These numbers cannot be combined to 24n");
- }
- return FALSE;
- }
- void main (){
- Calc24(number);
- // printf("finished mainn");
- }