24点计算器源代码

合集下载

scratch24点计算编程代码

scratch24点计算编程代码

scratch24点计算编程代码以下是一个用Scratch编写的24点计算程序的示例代码:当标志 flag 为2时重置所有变量和列表设置变量 round 为 1设置变量 score 为 0设置列表 cards 为 [1, 2, 3, 4]设置列表 operators 为 ["+", "-", "*", "/"]设置列表 expression 为空列表当标志 flag 为1时如果 round > 3,显示 "游戏结束!你的得分是 " + score,然后停止脚本否则,显示 "第 " + round + " 回合"随机选择一个数字n 从列表cards 中移除并添加到列表expression随机选择一个运算符 op 从列表 operators 中移除并添加到列表expression随机选择一个数字m 从列表cards 中移除并添加到列表expression显示列表 expression等待用户回答如果用户回答为 "24",将 score 增加 10,并显示 "回答正确!你的得分是 " + score否则,显示 "回答错误!正确答案是 " + expression[0] + op + expression[1] + " = 24"将标志 flag 设置为2将 round 增加 1等待 2 秒重置列表 expression重置列表 cards 和 operators将 1、2、3、4 添加回列表 cards将 "+、-、*、/" 添加回列表 operators等待 2 秒将标志 flag 设置为1将标志 flag 设置为1。

数据结构24点游戏源代码

数据结构24点游戏源代码

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#define OPSETSIZE 7#define STACK_INIF_SIZE 50#define STACKINCREMENT 10int 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 forfor (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<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 {printf("%c不是有效的运算符!\n",s[i]);}} //end fori=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);return (check(*l));}int Operate(int a,int theta, int 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 {//printf("不能为小数\n");return -10000;}}default : return 0;}}int ReturnOpOrd(char op,char* TestOp)// precede()函数调用求优先级{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)];}int initstack(sqstack *s){(s)->base = (int*)malloc(STACK_INIF_SIZE*sizeof(int));if((s)->base==NULL) exit(-2);(s)->top=(s)->base;(s)->stacksize = STACK_INIF_SIZE;return 1;}int gettop(sqstack *s){ //取得栈顶元素int e;if(s->top==s->base){printf("栈空,无法取得栈顶元素!\n");return 0;}e=*(s->top-1);return e;}int 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(-2);s->stacksize+= STACKINCREMENT;}*(s->top++)=e;return 1;}int pop(sqstack *s,int *e){ //出栈if(s->top==s->base){printf("栈空,出栈错误!\n");return 0;}*e=*(--s->top);return 1;}int EvaluateExpression(char* MyExpression) { // 算法3.4----计算表达式的值// 算术表达式求值的算符优先算法。

自己编写的算24点c法度模范代码(解答无重复)[整理版]

自己编写的算24点c法度模范代码(解答无重复)[整理版]

自己编写的算24点c++程序代码(解答无重复)<删除本句话,并将文件后缀改为.cpp>#include <iostream>using namespace std;const double ANS=24;double sourse[4], suan[4];void four(){ if (suan[0]+suan[1]+suan[2]+suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"+"<<suan[2]<<"+"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]*suan[2]*suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"*"<<suan[2]<<"*"<<suan[3]<<"="<<A NS<<endl;}void three(){ if (suan[0]+suan[1]+suan[2]-suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"+"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]*suan[2]+suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"*"<<suan[2]<<"+"<<suan[3]<<"="<<A NS<<endl;if (suan[3]-suan[0]-suan[1]-suan[2]==ANS) cout<<suan[3]<<"-"<<suan[0]<<"-"<<suan[1]<<"-"<<suan[2]<<"="<<A NS<<endl;if (suan[0]*suan[1]*suan[2]-suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"*"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[3]-suan[0]*suan[1]*suan[2]==ANS) cout<<suan[3]<<"-"<<suan[0]<<"*"<<suan[1]<<"*"<<suan[2]<<"="<<A NS<<endl;if (suan[3]/suan[0]/suan[1]/suan[2]==ANS) cout<<suan[3]<<"/"<<suan[0]<<"/"<<suan[1]<<"/"<<suan[2]<<"="<<A NS<<endl;if (suan[0]*suan[1]*suan[2]/suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"*"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;if ((suan[0]+suan[1]+suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"+"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]+suan[1]+suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"+"<<suan[2]<<")/"<<suan[3]<< "="<<ANS<<endl;if (suan[3]/(suan[0]+suan[1]+suan[2])==ANS) cout<<suan[3]<<"/("<<suan[0]<<"+"<<suan[1]<<"+"<<suan[2]<<")="< <ANS<<endl;}void two(){ if (suan[2]-suan[0]*suan[1]-suan[3]==ANS) cout<<suan[2]<<"-"<<suan[0]<<"*"<<suan[1]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[2]/suan[3]+suan[0]+suan[1]==ANS) cout<<suan[2]<<"/"<<suan[3]<<"+"<<suan[0]<<"+"<<suan[1]<<"="<<A NS<<endl;if (suan[2]/suan[0]/suan[1]+suan[3]==ANS) cout<<suan[2]<<"/"<<suan[0]<<"/"<<suan[1]<<"+"<<suan[3]<<"="<<A NS<<endl;if (suan[2]/suan[3]-suan[0]-suan[1]==ANS) cout<<suan[2]<<"/"<<suan[3]<<"-"<<suan[0]<<"-"<<suan[1]<<"="<<ANS<<endl;if (suan[2]/suan[0]/suan[1]-suan[3]==ANS) cout<<suan[2]<<"/"<<suan[0]<<"/"<<suan[1]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[2]-suan[3]/suan[0]/suan[1]==ANS) cout<<suan[2]<<"-"<<suan[3]<<"/"<<suan[0]<<"/"<<suan[1]<<"="<<A NS<<endl;if (suan[2]+suan[0]*suan[1]-suan[3]==ANS) cout<<suan[2]<<"+"<<suan[0]<<"*"<<suan[1]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[0]+suan[1]-suan[2]/suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"-"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]-suan[2]/suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"-"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;if (suan[2]/suan[3]-suan[0]*suan[1]==ANS) cout<<suan[2]<<"/"<<suan[3]<<"-"<<suan[0]<<"*"<<suan[1]<<"="<<A NS<<endl;if (suan[0]*suan[1]/suan[2]-suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"/"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[2]-suan[0]*suan[1]/suan[3]==ANS) cout<<suan[2]<<"-"<<suan[0]<<"*"<<suan[1]<<"/"<<suan[3]<<"="<<A NS<<endl;if ((suan[0]+suan[1])/suan[2]-suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[2]<<"-"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]-suan[3])/suan[0]/suan[1]==ANS) cout<<"("<<suan[2]<<"-"<<suan[3]<<")/"<<suan[0]<<"/"<<suan[1]<< "="<<ANS<<endl;if (suan[2]+(suan[0]+suan[1])/suan[3]==ANS) cout<<suan[2]<<"+("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[2]-(suan[0]+suan[1])*suan[3]==ANS) cout<<suan[2]<<"-("<<suan[0]<<"+"<<suan[1]<<")*"<<suan[3]<<"="< <ANS<<endl;if (suan[2]-(suan[0]+suan[1])/suan[3]==ANS) cout<<suan[2]<<"-("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[2]*(suan[0]+suan[1])+suan[3]==ANS) cout<<suan[2]<<"*("<<suan[0]<<"+"<<suan[1]<<")+"<<suan[3]<<"="< <ANS<<endl;if (suan[2]/(suan[0]+suan[1])+suan[3]==ANS) cout<<suan[2]<<"/("<<suan[0]<<"+"<<suan[1]<<")+"<<suan[3]<<"="< <ANS<<endl;if (suan[2]*(suan[0]+suan[1])-suan[3]==ANS) cout<<suan[2]<<"*("<<suan[0]<<"+"<<suan[1]<<")-"<<suan[3]<<"="< <ANS<<endl;if (suan[2]/(suan[0]+suan[1])-suan[3]==ANS) cout<<suan[2]<<"/("<<suan[0]<<"+"<<suan[1]<<")-"<<suan[3]<<"="< <ANS<<endl;if (suan[2]*(suan[0]+suan[1])/suan[3]==ANS) cout<<suan[2]<<"*("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[2]/(suan[0]+suan[1])/suan[3]==ANS) cout<<suan[2]<<"/("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[0]/(suan[2]-suan[3])*suan[1]==ANS) cout<<suan[0]<<"/("<<suan[2]<<"-"<<suan[3]<<")*"<<suan[1]<<"="< <ANS<<endl;if (suan[0]*(suan[2]-suan[3])*suan[1]==ANS) cout<<suan[0]<<"*("<<suan[2]<<"-"<<suan[3]<<")*"<<suan[1]<<"="< <ANS<<endl;if ((suan[0]+suan[1]-suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"-"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]-suan[0]-suan[1])*suan[3]==ANS) cout<<"("<<suan[2]<<"-"<<suan[0]<<"-"<<suan[1]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]+suan[0]*suan[1])*suan[3]==ANS) cout<<"("<<suan[2]<<"+"<<suan[0]<<"*"<<suan[1]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]-suan[0]*suan[1])*suan[3]==ANS) cout<<"("<<suan[2]<<"-"<<suan[0]<<"*"<<suan[1]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]*suan[1]-suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"*"<<suan[1]<<"-"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]+suan[1]-suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]-suan[0]-suan[1])/suan[3]==ANS) cout<<"("<<suan[2]<<"-"<<suan[0]<<"-"<<suan[1]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]+suan[0]*suan[1])/suan[3]==ANS) cout<<"("<<suan[2]<<"+"<<suan[0]<<"*"<<suan[1]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[2]-suan[0]*suan[1])/suan[3]==ANS) cout<<"("<<suan[2]<<"-"<<suan[0]<<"*"<<suan[1]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]*suan[1]-suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"*"<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<<"="<<ANS<<endl;if (suan[2]/(suan[0]+suan[1]-suan[3])==ANS) cout<<suan[2]<<"/("<<suan[0]<<"+"<<suan[1]<<"-"<<suan[3]<<")="< <ANS<<endl;if (suan[2]/(suan[3]-suan[0]-suan[1])==ANS) cout<<suan[2]<<"/("<<suan[3]<<"-"<<suan[0]<<"-"<<suan[1]<<")="< <ANS<<endl;if (suan[2]/(suan[3]+suan[0]*suan[1])==ANS) cout<<suan[2]<<"/("<<suan[3]<<"+"<<suan[0]<<"*"<<suan[1]<<")="< <ANS<<endl;if (suan[2]/(suan[3]-suan[0]*suan[1])==ANS) cout<<suan[2]<<"/("<<suan[3]<<"-"<<suan[0]<<"*"<<suan[1]<<")="< <ANS<<endl;if (suan[2]/(suan[0]*suan[1]-suan[3])==ANS) cout<<suan[2]<<"/("<<suan[0]<<"*"<<suan[1]<<"-"<<suan[3]<<")="< <ANS<<endl;if ((suan[0]+suan[1])*(suan[2]-suan[3])==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")*("<<suan[2]<<"-"<<suan[3]< <")="<<ANS<<endl;if ((suan[0]+suan[1])/(suan[2]-suan[3])==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")/("<<suan[2]<<"-"<<suan[3]< <")="<<ANS<<endl;}void dou_two(){ if (suan[0]+suan[1]-suan[2]-suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"-"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]+suan[2]+suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"+"<<suan[2]<<"+"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]-suan[2]-suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"-"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]-suan[2]*suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"-"<<suan[2]<<"*"<<suan[3]<<"="<<A NS<<endl;if (suan[0]*suan[1]/suan[2]/suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"/"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;if (suan[0]+suan[1]-suan[2]*suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"-"<<suan[2]<<"*"<<suan[3]<<"="<<A NS<<endl;if ((suan[0]+suan[1])/suan[2]/suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")/"<<suan[2]<<"/"<<suan[3]<< "="<<ANS<<endl;if (suan[2]/(suan[0]+suan[1])*suan[3]==ANS) cout<<suan[2]<<"/("<<suan[0]<<"+"<<suan[1]<<")*"<<suan[3]<<"="< <ANS<<endl;if (suan[2]*(suan[0]+suan[1])*suan[3]==ANS) cout<<suan[2]<<"*("<<suan[0]<<"+"<<suan[1]<<")*"<<suan[3]<<"="< <ANS<<endl;if ((suan[0]+suan[1])/(suan[2]+suan[3])==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")/("<<suan[2]<<"+"<<suan[3]< <")="<<ANS<<endl;}void exp(){if (suan[0]/suan[1]+suan[2]/suan[3]==ANS) cout<<suan[0]<<"/"<<suan[1]<<"+"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;}void ext(){if ((suan[0]-suan[1])*(suan[2]-suan[3])==ANS) cout<<"("<<suan[0]<<"-"<<suan[1]<<")*("<<suan[2]<<"-"<<suan[3]< <")="<<ANS<<endl;}void ex2(){if ((suan[0]+suan[1])*(suan[2]+suan[3])==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<")*("<<suan[2]<<"+"<<suan[3]< <")="<<ANS<<endl;if (suan[0]*suan[1]+suan[2]*suan[3]==ANS) cout<<suan[0]<<"*"<<suan[1]<<"+"<<suan[2]<<"*"<<suan[3]<<"="<<A NS<<endl;}void one(){ if (suan[0]/suan[1]-suan[2]/suan[3]==ANS) cout<<suan[0]<<"/"<<suan[1]<<"-"<<suan[2]<<"/"<<suan[3]<<"="<<A NS<<endl;if (suan[0]+suan[1]/suan[2]-suan[3]==ANS) cout<<suan[0]<<"+"<<suan[1]<<"/"<<suan[2]<<"-"<<suan[3]<<"="<<A NS<<endl;if ((suan[0]-suan[1])/suan[2]-suan[3]==ANS) cout<<"("<<suan[0]<<"-"<<suan[1]<<")/"<<suan[2]<<"-"<<suan[3]<< "="<<ANS<<endl;if (suan[0]-suan[1]/suan[2]-suan[3]==ANS) cout<<suan[0]<<"-"<<suan[1]<<"/"<<suan[2]<<"-"<<suan[3]<<"="<<ANS<<endl;if (suan[0]+(suan[1]-suan[2])/suan[3]==ANS) cout<<suan[0]<<"+("<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[0]*(suan[1]-suan[2])+suan[3]==ANS) cout<<suan[0]<<"*("<<suan[1]<<"-"<<suan[2]<<")+"<<suan[3]<<"="< <ANS<<endl;if (suan[0]/(suan[1]-suan[2])+suan[3]==ANS) cout<<suan[0]<<"/("<<suan[1]<<"-"<<suan[2]<<")+"<<suan[3]<<"="< <ANS<<endl;if (suan[0]*(suan[1]-suan[2])-suan[3]==ANS) cout<<suan[0]<<"*("<<suan[1]<<"-"<<suan[2]<<")-"<<suan[3]<<"="< <ANS<<endl;if (suan[0]/(suan[1]-suan[2])-suan[3]==ANS) cout<<suan[0]<<"/("<<suan[1]<<"-"<<suan[2]<<")-"<<suan[3]<<"="< <ANS<<endl;if (suan[0]*(suan[1]-suan[2])/suan[3]==ANS) cout<<suan[0]<<"*("<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<<"="< <ANS<<endl;if (suan[0]/(suan[1]-suan[2])/suan[3]==ANS) cout<<suan[0]<<"/("<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<<"="< <ANS<<endl;if ((suan[0]+suan[1]/suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"/"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]-suan[1]/suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"-"<<suan[1]<<"/"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]/suan[1]-suan[2])*suan[3]==ANS) cout<<"("<<suan[0]<<"/"<<suan[1]<<"-"<<suan[2]<<")*"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]+suan[1]/suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"+"<<suan[1]<<"/"<<suan[2]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]-suan[1]/suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"-"<<suan[1]<<"/"<<suan[2]<<")/"<<suan[3]<< "="<<ANS<<endl;if ((suan[0]/suan[1]-suan[2])/suan[3]==ANS) cout<<"("<<suan[0]<<"/"<<suan[1]<<"-"<<suan[2]<<")/"<<suan[3]<< "="<<ANS<<endl;if (suan[0]/(suan[1]+suan[2]/suan[3])==ANS) cout<<suan[0]<<"/("<<suan[1]<<"+"<<suan[2]<<"/"<<suan[3]<<")="< <ANS<<endl;if (suan[0]/(suan[1]-suan[2]/suan[3])==ANS) cout<<suan[0]<<"/("<<suan[1]<<"-"<<suan[2]<<"/"<<suan[3]<<")="< <ANS<<endl;if (suan[0]/(suan[1]/suan[2]-suan[3])==ANS) cout<<suan[0]<<"/("<<suan[1]<<"/"<<suan[2]<<"-"<<suan[3]<<")="< <ANS<<endl;if ((suan[0]-suan[1])/(suan[2]-suan[3])==ANS &&(suan[0]-suan[1])*(suan[2]-suan[3])!=0) cout<<"("<<suan[0]<<"-"<<suan[1]<<")/("<<suan[2]<<"-"<<suan[3]< <")="<<ANS<<endl;}int main(){ int i=0, j=0, k=0;int count=0;int tong1; int tong2; int tong3; int butong1; int butong2;int bao1; int bao2; int bao3; int zi1; int zi2;bool flag=false;char ch='y';do{count=0;do {cout <<"请输入四个正整数:";cin >>sourse[0]>>sourse[1]>>sourse[2]>>sourse[3];} while (sourse[0]<1 || sourse[1]<1 || sourse[2]<1 || sourse[3]<1);for (i=0; i<3; i++)for (j=i+1; j<4; j++){ if (sourse[i]==sourse[j]){ count++;if (flag) count++;flag=true;}flag=false;}switch (count){ case 0:for (i=0; i<4; i++){suan[0]=sourse[i];for (j=0; j<4; j++)if (j!=i){suan[1]=sourse[j];for (k=0; k<4; k++)if (k!=i && k!=j){suan[2]=sourse[k];suan[3]=sourse[6-i-j-k]; one();}}}for (i=0; i<6; i++){bao1=i%4; bao2=(i+1+i/4)%4; zi1=(i+2+i/4)%4; zi2=6-bao1-bao2-zi1;suan[0]=sourse[bao1]; suan[1]=sourse[bao2]; suan[2]=sourse[zi1]; suan[3]=sourse[zi2];dou_two(); two();suan[2]=sourse[zi2]; suan[3]=sourse[zi1]; two();}for (i=0; i<3; i++){bao1=i; bao2=(i+1)%3; zi1=(i+2)%3;zi2=6-bao1-bao2-zi1;suan[0]=sourse[bao1]; suan[1]=sourse[bao2]; suan[2]=sourse[zi1]; suan[3]=sourse[zi2];ext(); exp(); ex2();suan[2]=sourse[zi2]; suan[3]=sourse[zi1]; ext(); exp();suan[0]=sourse[bao2]; suan[1]=sourse[bao1]; exp();suan[2]=sourse[zi1]; suan[3]=sourse[zi2];exp();}for (i=0; i<4; i++){bao1=i; bao2=(i+1)%4; bao3=(i+2)%4; zi1=(i+3)%4;suan[0]=sourse[bao1]; suan[1]=sourse[bao2]; suan[2]=sourse[bao3]; suan[3]=sourse[zi1];three();}suan[0]=sourse[0]; suan[1]=sourse[1];suan[2]=sourse[2]; suan[3]=sourse[3];four(); break;case 1:for (i=0; i<3; i++){for (j=i+1; j<4; j++)if (sourse[i]==sourse[j]) break;if (sourse[i]==sourse[j]) break;}tong1=i; tong2=j;for (i=0; i<4; i++)if (i!=tong1 && i!=tong2){butong1=i; butong2=6-tong1-tong2-butong1; break;}suan[0]=sourse[tong1]; suan[1]=sourse[tong2]; suan[2]=sourse[butong1]; suan[3]=sourse[butong2];four(); two(); dou_two(); three(); ext(); exp(); ex2(); one();suan[2]=sourse[butong2]; suan[3]=sourse[butong1]; two(); three(); ext(); exp(); one();suan[0]=sourse[butong1]; suan[1]=sourse[tong1]; suan[2]=sourse[tong2]; suan[3]=sourse[butong2];two(); dou_two(); ext(); exp(); ex2(); one();suan[0]=sourse[butong2]; suan[3]=sourse[butong1]; two(); dou_two(); exp(); one();suan[0]=sourse[butong1]; suan[1]=sourse[butong2]; suan[2]=sourse[tong1]; suan[3]=sourse[tong2];two(); dou_two(); three(); one();suan[0]=sourse[butong2]; suan[1]=sourse[butong1]; one();suan[0]=sourse[tong1]; suan[1]=sourse[butong1]; suan[2]=sourse[butong2]; suan[3]=sourse[tong2];two(); one();suan[1]=sourse[butong2]; suan[2]=sourse[butong1]; two(); one();suan[0]=sourse[tong1]; suan[1]=sourse[butong1]; suan[2]=sourse[tong2]; suan[3]=sourse[butong2];ext(); exp(); one();suan[1]=sourse[butong2]; suan[3]=sourse[butong1]; one();suan[0]=sourse[butong1]; suan[1]=sourse[tong1]; suan[2]=sourse[butong2]; suan[3]=sourse[tong2];exp(); one();suan[0]=sourse[butong2]; suan[2]=sourse[butong1]; one(); break;case 2:for (i=1; i<4; i++)if (sourse[i]==sourse[0]) break;tong1=0; tong2=i;for (i=0; i<4; i++)if (i!=tong1 && i!=tong2){butong1=i; butong2=6-tong1-tong2-butong1; break;}suan[0]=sourse[tong1]; suan[1]=sourse[tong2]; suan[2]=sourse[butong1]; suan[3]=sourse[butong2];four(); two(); dou_two(); ext(); exp(); ex2(); three(); one();suan[0]=sourse[butong1]; suan[1]=sourse[tong1]; suan[2]=sourse[tong2]; suan[3]=sourse[butong2];two(); dou_two(); ext(); exp(); ex2(); one();suan[0]=sourse[butong1]; suan[1]=sourse[butong2]; suan[2]=sourse[tong1]; suan[3]=sourse[tong2];two(); dou_two(); three(); one();suan[0]=sourse[tong1]; suan[1]=sourse[butong1]; suan[2]=sourse[butong2]; suan[3]=sourse[tong2];two(); one();suan[0]=sourse[tong1]; suan[1]=sourse[butong1]; suan[2]=sourse[tong2]; suan[3]=sourse[butong2];ext(); exp(); one();suan[0]=sourse[butong1]; suan[1]=sourse[tong1]; suan[2]=sourse[butong2]; suan[3]=sourse[tong2];exp(); one(); break;case 3:for (i=0; i<4; i++)if(sourse[i]!=sourse[(i+1)%4] &&sourse[i]!=sourse[(i+2)%4]) break;butong1=i; tong1=3-i;for (i=0; i<4; i++)if (i!=butong1 && i!=tong1){tong2=i; tong3=6-tong1-tong2-butong1; break;}suan[0]=sourse[tong1]; suan[1]=sourse[tong2]; suan[2]=sourse[tong3]; suan[3]=sourse[butong1];four(); two(); dou_two(); three(); ext(); exp(); ex2(); one();suan[0]=sourse[tong1]; suan[1]=sourse[tong2]; suan[2]=sourse[butong1]; suan[3]=sourse[tong3];two(); three(); exp(); one();suan[0]=sourse[tong1]; suan[1]=sourse[butong1]; suan[2]=sourse[tong2]; suan[3]=sourse[tong3];two(); dou_two(); one();suan[0]=sourse[butong1]; suan[1]=sourse[tong1]; suan[2]=sourse[tong2]; suan[3]=sourse[tong3];one(); break;case 6:suan[0]=sourse[0]; suan[1]=sourse[1];suan[2]=sourse[2]; suan[3]=sourse[3];four(); two(); dou_two(); three(); ext(); exp(); one();break;}do{cout << "继续?(y/n):"; cin >> ch;} while (ch!='y' && ch!='n' && ch!='Y' && ch!='N');} while (ch=='y' || ch=='Y');return 0;}。

24点纸牌游戏源代码

24点纸牌游戏源代码

#include <stdio.h>#include <windows.h>#pragma comment ( lib, "Winmm.lib")// 0,1,2,3分别代表+,-,*,/float OperVisual(float num1,float num2,int oper);//双目运算int Show(float num1,float num2,float num3,float num4,int i);//交换率运算并显示void main(){int i1,i2,i3,i4,i=0;unsigned int s1,s2;float num[4];//分别代表输入的4个数puts("Please enter the number 4:");scanf("%f%f%f%f",&num[0],&num[1],&num[2],&num[3]);s1=timeGetTime();for(i1=0;i1<4;i1++){for(i2=0;i2<4;i2++){for(i3=0;i3<4;i3++){for(i4=0;i4<4;i4++){if(i1!=i2&&i1!=i3&&i1!=i4&&i2!=i3&&i2!=i4&&i3!=i4)i=Show(num[i1],num[i2],num[i3],num[i4],i);}}}}if(!i) printf("No answer !");s2=timeGetTime();printf("\ntime consuming:%dms\n",s2-s1);getchar();getchar();}int Show(float num1,float num2,float num3,float num4,int i){int oper1,oper2,oper3;//分别代表3个操作符char OperShow[4]={'+','-','*','/'};//0,1,2,3float end1,end2,end3;for(oper1=0;oper1<4;oper1++){//第一个运算符end1=OperVisual(num1,num2,oper1);//将第一个数进行4次运算for(oper2=0;oper2<4;oper2++){//第二个运算符end2=OperVisual(end1,num3,oper2);//将前两个数的结果和第三个数进行4次运算for (oper3=0;oper3<4;oper3++){//第三个运算符end3=OperVisual(end2,num4,oper3);//将前三个数的结果和第三个数进行4次运算if(end3==24){if(oper1<=1&&oper2>=2)//如果第一个运算符为+或-第二个运算符为*或/printf("(%.f%c%.f)%c%.f%c%.f=24\n",num1,OperShow[oper1],num2,OperShow[oper2],nu m3,OperShow[oper3],num4),i++;else if(oper1<=1&&oper2<=1&&oper3>=2)printf("(%.f%c%.f%c%.f)%c%.f=24\n",num1,OperShow[oper1],num2,OperShow[oper2],nu m3,OperShow[oper3],num4),i++;else if(oper1>=2&&oper2<=1&&oper3>=2)printf("(%.f%c%.f%c%.f)%c%.f=24\n",num1,OperShow[oper1],num2,OperShow[oper2],nu m3,OperShow[oper3],num4),i++;elseprintf("%.f%c%.f%c%.f%c%.f=24\n",num1,OperShow[oper1],num2,OperShow[oper2],num3,Op erShow[oper3],num4),i++;}}}}return i;}float OperVisual(float num1,float num2,int oper){float result;switch(oper){case 0:result=num1+num2;break;case 1:result=num1-num2;break;case 2:result=num1*num2;break;case 3:result=num1/num2;break;}return result;}。

24点生成计算VBA代码

24点生成计算VBA代码

24点生成计算VBA代码Dim iRndCardsNumber(4) As SingleSub Macro1()Dim i As Integer, j As IntegerDim b As BooleanRandomizei = 1b = TrueSheets("sheet1").Cells.SelectSelection.ClearContentsDo While b = TrueiRndCardsNumber(1) = Int(Rnd * 10 + 1)iRndCardsNumber(2) = Int(Rnd * 10 + 1)iRndCardsNumber(3) = Int(Rnd * 10 + 1)iRndCardsNumber(4) = Int(Rnd * 10 + 1)iAnswer = Operator()If iAnswer <> "" ThenSheets("sheet1").Range("a" & i) = iRndCardsNumber(1) & "," & iRndCardsNumber(2) & "," & iRndCardsNumber(3) & "," & iRndCardsNumber(4)i = i + 1If i > 25 Then b = FalseEnd IfLoopi = 1b = TrueDo While b = TrueiRndCardsNumber(1) = Int(Rnd * 10 + 1)iRndCardsNumber(2) = Int(Rnd * 10 + 1)iRndCardsNumber(3) = Int(Rnd * 10 + 1)iRndCardsNumber(4) = Int(Rnd * 10 + 1)iAnswer = Operator()If iAnswer <> "" ThenSheets("sheet1").Range("c" & i) = iRndCardsNumber(1) & "," & iRndCardsNumber(2) & "," & iRndCardsNumber(3) & "," & iRndCardsNumber(4)i = i + 1If i > 25 Then b = FalseEnd IfLoopEnd SubFunction Operator() As StringDim a As IntegerDim b As IntegerDim c As IntegerDim d As IntegerDim op1 As IntegerDim op2 As IntegerDim op3 As IntegerDim answer1 As SingleDim answer2 As SingleDim answer3 As SingleDim t1l As String, t1r As String, t2l As String, t2r As String '四个变量分别表示为:第一个左括号,第一个右括号,第二个左括号,第二个右括号Operator = "" '如果程序执行到此处那么所给的点数将是一种无解的状态For a = 1 To 4 '得For b = 1 To 4 '到If b <> a Then 'abcdFor c = 1 To 4 '的If (c <> a) And (c <> b) Then '组For d = 1 To 4 '合If (d <> a) And (d <> b) And (d <> c) Then '共有4!=24种组合For op1 = 1 To 4 'For op2 = 1 To 4 'For op3 = 1 To 4 '得到运算符的排序共有4*4*4=64种'到处可以得到24*64=1536种表达式的排序顺序而由于运算符的优先级问题,使得每种表达式又有'5种的运算顺序而最终就可以得到1536*5=7680的输出情况,但我们就只需一种结果等于24的。

24点游戏源代码

24点游戏源代码
u = v - u;
else if(tempp[i] == 60003)
u = u * v;
else if(tempp[i] == 60004)
u = v / u;
tempp[i] = u;
}
}
ssum = u;
}
int checkk(void)
float a[4];
float bb[4];
float ssum;
char temp;
char output[MAX] ;
char stack[MAX] ;
char input[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
float sum(float xx,float yy,char mm);
void test24(float x[],char m[]);
int main(){
float sum(float,float,char);
void test24(float [],char []);
{
if(tempp[j] <=60000)
{
v=tempp[j];
tempp[j] = 60010;
break; Biblioteka } j--; }
if(tempp[i] == 60001)
u = u + v;
else if(tempp[i] == 60002)
}
ssum = 0.0;
SetExpression (input) ;
ConvertToPostfix ( ) ;

C语言计算24点(原创完美版)

C语言计算24点(原创完美版)

#include<stdio.h>char cha[4]="+-*/",c=0;float fun(float a1,float a2,int b){switch(b) //选择四则运算{case 0:return (a1+a2);case 1:return (a1-a2);case 2:return (a1*a2);case 3:return (a1/a2);}}void algebra(int x[4])//计算满足要求的表达式{int i,j,k;float temp1,temp2,temp3;for(i=0;i<4;i++)for(j=0;j<4;j++)for(k=0;k<4;k++){temp1=fun(x[0],x[1],i);temp2=fun(temp1,x[2],j);temp3=fun(temp2,x[3],k);if(temp3==24){printf("\t\t\t\t[(%d%c%d)%c%d]%c%d=24\n",x[0],cha[i],x[1],cha[j],x[2],cha [k],x[3]);c=1;}temp1=fun(x[0],x[1],i);temp2=fun(x[2],x[3],k);temp3=fun(temp1,temp2,j);if(temp3==24){printf("\t\t\t\t(%d%c%d)%c(%d%c%d)=24\n",x[0],cha[i],x[1],cha[j],x[2],cha [k],x[3]);c=1;}temp1=fun(x[1],x[2],j);temp2=fun(temp1,x[0],i);temp3=fun(temp2,x[3],k);if(temp3==24){printf("\t\t\t\t[%d%c(%d%c%d)]%c%d=24\n",x[0],cha[i],x[1],cha[j],x[2],cha [k],x[3]);c=1;}temp1=fun(x[1],x[2],j);temp2=fun(temp1,x[3],k);temp3=fun(temp2,x[0],i);if(temp3==24){printf("\t\t\t\t%d%c[(%d%c%d)%c%d]=24\n",x[0],cha[i],x[1],cha[j],x[2],cha [k],x[3]);c=1;}temp1=fun(x[2],x[3],k);temp2=fun(temp1,x[1],j);temp3=fun(temp2,x[0],i);if(temp3==24){printf("\t\t\t\t%d%c[%d%c(%d%c%d)]=24\n",x[0],cha[i],x[1],cha[j],x[2],cha [k],x[3]);c=1;}}}int arrange(int num[4])//将四个数排列为24种方式{int i,j,k,l;int temp[4];for(i=0;i<4;i++)for(j=0;j<4;j++)for(k=0;k<4;k++)for(l=0;l<4;l++){if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l){temp[0]=num [i];temp[1]=num[j];temp[2]=num[k];temp[3]=num[l];algebra(temp);}}}int main(){int i, num[4];char ch;printf(" **计算24点游戏**\n\n请输入4个整数,然后按“回车”键: \n");for(i=0;i<4;i++)scanf("%d",&num[i]);//将四个数输入到数组中去printf("结果是:\n");arrange(num);if(c==0)printf("无解.\n");printf("是否继续?(y,n): ");fflush(stdin);ch=getchar();fflush(stdin);if(ch=='n'){printf("\t\t\t\t ****再见!****\n\n");return 0;}while(ch!='y'){printf("输入错误,请重输(y或n): ");ch=getchar();fflush(stdin);if(ch=='n')return 0;}while(ch=='y'){printf("请输入四个整数:");for(i=0;i<4;i++)scanf("%d",&num[i]);//将四个数输入到数组中去fflush(stdin);printf("结果是:\n");arrange(num);if(c==0)printf("无解.\n");printf("是否继续?(y,n): ");ch=getchar();fflush(stdin);if(ch=='n'){printf("\t\t\t\t ******再见!****\n\n");return 0;}while(ch!='y'){printf("输入错误,请重输(y或n): ");ch=getchar();fflush(stdin);if(ch=='n'){printf("\t\t\t\t ******再见!****\n\n");return 0;} }}return 0;}。

c#24点源代码

c#24点源代码

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Collections;namespace _24Point{public partial class Form1 : Form{private int A, B, C, D;private int NumberA, NumberB, NumberC, NumberD;private int topCard;// 下载于#region 关于一副牌的生成private card[] Deck; //一副牌struct card{public int face; //牌面大小public int suit; //牌面花色public int count; //牌面点数public bool faceup;//牌面是否向上}private void GetDeck() //生成一副牌{Deck = new card[53];int i;int j;for (i = 0; i < 13; i++){for (j = 1; j <= 4; j++){Deck[j + i * 4].face = i + 1;Deck[j + i * 4].suit = j;if (i < 10){Deck[j + i * 4].count = i + 1;}else{Deck[j + i * 4].count = 10;}Deck[j + i * 4].faceup = false;}}}private void Shuffle() //洗牌{Random random = new Random((int)DateTime.Now.Ticks);card middleCard;int j, k;for (int i = 0; i < 1000; i++){j = (int)random.Next(1, 52);k = (int)random.Next(1, 52);middleCard = Deck[j];Deck[j] = Deck[k];Deck[k] = middleCard;}}private void btnStart_Click(object sender, EventArgs e) //开始游戏的时候发四张牌{lblInput.Text = "";GetDeck();Shuffle();topCard = 0;int imageNum;string path;//画第一张牌topCard = topCard = 1;pictureBox1.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox1.Image = Image.FromFile(path);NumberA = Convert.ToInt32(Deck[topCard].face);btnNumber1.Text = NumberA.ToString();//画第二张牌topCard = topCard + 1;pictureBox2.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox2.Image = Image.FromFile(path);NumberB = Convert.ToInt32(Deck[topCard].face);btnNumber2.Text = NumberB.ToString();//画第三张牌topCard = topCard + 1;pictureBox3.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox3.Image = Image.FromFile(path);NumberC = Convert.ToInt32(Deck[topCard].face);btnNumber3.Text = NumberC.ToString();//画第四张牌topCard = topCard + 1;pictureBox4.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox4.Image = Image.FromFile(path);NumberD = Convert.ToInt32(Deck[topCard].face);btnNumber4.Text = NumberD.ToString();btnStart.Visible = false;groupBox1.Visible = true;lblAnswer.Visible = true;}#endregionpublic Form1(){InitializeComponent();Initial();}private void Initial(){pictureBox1.Visible = false;pictureBox2.Visible = false;pictureBox3.Visible = false;pictureBox4.Visible = false;groupBox1.Visible = false;lblAnswer.Visible = false;lblResult.Visible = false;}private void Form1_Load(object sender, EventArgs e){}private void btnNext_Click(object sender, EventArgs e){btnAdd.Enabled = true;btnMinus.Enabled = true;btnDivide.Enabled = true;btnMulti.Enabled = true;btnNumber1.Enabled = true;btnNumber2.Enabled = true;btnNumber3.Enabled = true;btnNumber4.Enabled = true;btnDelete.Enabled = true;btnClear.Enabled = true;btnLeft.Enabled = true;btnRight.Enabled = true;btnEnter.Enabled = true;lblInput.Text = "";lblAnswer.Text = "答案";lblResult.Visible = false;int imageNum;string path;//画第一张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox1.Image = Image.FromFile(path);NumberA = Convert.ToInt32(Deck[topCard].face);btnNumber1.Text = NumberA.ToString();//画第二张牌if (topCard >= 52){topCard = 0;}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox2.Image = Image.FromFile(path);NumberB = Convert.ToInt32(Deck[topCard].face);btnNumber2.Text = NumberB.ToString();//画第三张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox3.Image = Image.FromFile(path);NumberC = Convert.ToInt32(Deck[topCard].face);btnNumber3.Text = NumberC.ToString();//画第四张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox4.Image = Image.FromFile(path);NumberD = Convert.ToInt32(Deck[topCard].face);btnNumber4.Text = NumberD.ToString();}public void ChangeLoc24(int i){//24种转换switch (i){case 1:A = NumberA;B = NumberB;C = NumberC;D = NumberD;case 2:A = NumberA;B = NumberB; D = NumberC;C = NumberD;break;case 3:A = NumberA; C = NumberB;B = NumberC; D = NumberD;break;case 4:A = NumberA; C = NumberB; D = NumberC;B = NumberD;break;case 5:A = NumberA; D = NumberB;B = NumberC;C = NumberD;break;case 6:A = NumberA; D = NumberB; C = NumberC;B = NumberD;break;case 7:B = NumberA; A = NumberB;C = NumberC;D = NumberD;break;case 8:B = NumberA; A = NumberB; D = NumberC;C = NumberD;break;case 9:B = NumberA;C = NumberB; A = NumberC;D = NumberD;break;case 10:B = NumberA;C = NumberB;D = NumberC; A = NumberD;break;case 11:B = NumberA; D = NumberB; A = NumberC;C = NumberD;break;case 12:B = NumberA; D = NumberB;C = NumberC; A = NumberD;break;case 13:C = NumberA; A = NumberB; B = NumberC;D = NumberD;break;case 14:C = NumberA; A = NumberB;D = NumberC; B = NumberD;break;case 15:C = NumberA; B = NumberB; A = NumberC;D = NumberD;break;C = NumberA; B = NumberB;D = NumberC; A = NumberD;break;case 17:C = NumberA;D = NumberB; A = NumberC; B = NumberD;break;case 18:C = NumberA;D = NumberB; B = NumberC; A = NumberD;break;case 19:D = NumberA; A = NumberB; B = NumberC; C = NumberD;break;case 20:D = NumberA; A = NumberB; C = NumberC; B = NumberD;break;case 21:D = NumberA; B = NumberB; A = NumberC; C = NumberD;break;case 22:D = NumberA; B = NumberB; C = NumberC; A = NumberD;break;case 23:D = NumberA; C = NumberB; A = NumberC; B = NumberD;break;case 24:D = NumberA; C = NumberB; B = NumberC; A = NumberD;break;}}private void btnAnswer_Click(object sender, EventArgs e){btnAdd.Enabled = false;btnMinus.Enabled = false;btnDivide.Enabled = false;btnMulti.Enabled = false;btnNumber1.Enabled = false;btnNumber2.Enabled = false;btnNumber3.Enabled = false;btnNumber4.Enabled = false;btnDelete.Enabled = false;btnClear.Enabled = false;btnLeft.Enabled = false;btnRight.Enabled = false;btnEnter.Enabled = false;lblAnswer.Text = "答案";#region 24种情况的遍历for (int i = 1; i <= 24; i++){ChangeLoc24(i);ArrayList first =new ArrayList();ArrayList firstStr=new ArrayList();first.Add(A.ToString());firstStr.Add("A");cal(ref first, ref firstStr, B,'B');cal(ref first, ref firstStr, C,'C');cal(ref first, ref firstStr, D,'D');for (int j = 0; j < first.Count; j++){if (Convert.ToInt32(Convert.ToDouble(first[j].ToString())) == 24){firstStr[j] = replaceString(firstStr[j].ToString(), 'A', A);firstStr[j] = replaceString(firstStr[j].ToString(), 'B', B);firstStr[j] = replaceString(firstStr[j].ToString(), 'C', C);firstStr[j] = replaceString(firstStr[j].ToString(), 'D', D);lblAnswer.Text = "答案:" + firstStr[j].ToString() + "=24;";return;}}}#endregionif (lblAnswer.Text == "答案"){lblAnswer.Text = "此题无解";}}private string replaceString(string str, char myChar, int num){int loc=str.IndexOf(myChar);string first = str.Substring(0, loc);str = first + Convert.ToInt16(num) + str.Substring(loc + 1);return str;}private void cal(ref ArrayList num,ref ArrayList numStr, int num2,char myChar){ArrayList newNum = new ArrayList();ArrayList newNumStr = new ArrayList();int temp;for (int i = 0; i < num.Count; i++){int num1 = Convert.ToInt32(num[i].ToString());//加法的情况temp = num1 + num2;newNum.Add(temp.ToString());newNumStr.Add(numStr[i].ToString() + "+" + myChar);//减法的情况if (num1 > num2){temp = num1 - num2;newNum.Add(temp.ToString());newNumStr.Add(numStr[i].ToString() + "-" + myChar);}else{temp = num2 - num1;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf('+') != -1 || numStr[i].ToString().IndexOf('-')!=-1){newNumStr.Add(myChar + "-" + "(" + numStr[i].ToString() + ")");}elsenewNumStr.Add(myChar + "-" + numStr[i].ToString());}temp = num1 * num2;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(numStr[i].ToString() + "*" + myChar);}else{newNumStr.Add("(" + numStr[i].ToString() + ")" + "*" + myChar);}#region 除法的情况if (num1 > num2){if (num2 != 0 && num1 % num2 == 0){temp = num1 / num2;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(numStr[i].ToString() + "/" + myChar);}else{newNumStr.Add("(" + numStr[i].ToString() + ")" + "/" + myChar);}}}else{if (num1 != 0 && num2 % num1 == 0){temp = num2 / num1;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(myChar + "/" + numStr[i].ToString());}else{newNumStr.Add(myChar + "/" + "(" + numStr[i].ToString() + ")");}}}#endregion}num = newNum;numStr = newNumStr;}private void btnEnter_Click(object sender, EventArgs e){if (CheckForNumbers(lblInput.Text.ToString().Trim())){int result=Deal(lblInput.Text.ToString().Trim());lblResult.Visible = true;if (result == 24){lblResult.Text = "<<---恭喜您,答对了!--->>";}else{lblResult.Text = "<<--抱歉,您的回答有误!-->>";}}}private void btnNumber1_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber1.Text.ToString().Trim(); }private void btnNumber2_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber2.Text.ToString().Trim(); }private void btnNumber3_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber3.Text.ToString().Trim(); }private void btnNumber4_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber4.Text.ToString().Trim(); }private void btnAdd_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnAdd.Text.ToString().Trim();}private void btnMinus_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnMinus.Text.ToString().Trim();}private void btnMulti_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnMulti.Text.ToString().Trim();}private void btnDivide_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnDivide.Text.ToString().Trim();}private void btnLeft_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnLeft.Text.ToString().Trim();}private void btnRight_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnRight.Text.ToString().Trim();}private void btnDelete_Click(object sender, EventArgs e){string input = lblInput.Text.ToString().Trim();lblInput.Text = input.Substring(0, input.Length - 1);}private void btnClear_Click(object sender, EventArgs e){lblInput.Text = "";}public int Deal(string input)//处理谓词表达式中的括号{int result = 0;while (input.IndexOf(')') != -1){int rightLoc = input.IndexOf(')');string temp = input.Substring(0, rightLoc);int leftLoc = stIndexOf('(');string first = input.Substring(0, leftLoc);string middle = input.Substring(leftLoc + 1, rightLoc - leftLoc - 1);string last = input.Substring(rightLoc + 1);input = first + Formular(middle).ToString() + last;}result = Formular(input);return result;}protected int Formular(string Inputs)//最简式运算{int Len = Inputs.Length;ArrayList OpeLoc = new ArrayList();//记录操作符位置ArrayList Ope = new ArrayList();//记录操作符int i;for (i = 0; i < Len; i++){if (IsOperator(Inputs[i]))//获取算符组信息{OpeLoc.Add(i);Ope.Add(Inputs[i]);}}if (OpeLoc.Count == 0) return int.Parse(Inputs);//处理无算符的情况RebuildOperator(ref OpeLoc, ref Ope);//算符重组,区分负号和减号if (!CheckFunction(OpeLoc, Len)) return 0;//判断算符组是否合法ArrayList Val = new ArrayList();//记录数值内容int j = 0;for (i = 0; i < OpeLoc.Count; i++){V al.Add(int.Parse(Inputs.Substring(j, Convert.ToInt32(OpeLoc[i]) - j)));j = Convert.ToInt32(OpeLoc[i]) + 1;}Val.Add(int.Parse(Inputs.Substring(j, Len - j)));//处理最后一个数据的录入return Calculate(Ope, Val);}protected bool IsOperator(char chr)//判断一个符号是否是基本算符{if (chr == '+' | chr == '-' | chr == '*' | chr == '/')return true;//判断是否是四则混合运算算符else return false;}protected void RebuildOperator(ref ArrayList OpeLoc, ref ArrayList Ope)//对负号的处理和重构{ArrayList DelItem = new ArrayList();if (Convert.ToInt32(OpeLoc[0].ToString()) == 0 & Convert.ToChar(Ope[0]) == '-')//判断第一个符号是否是负号{DelItem.Add(0);}int i;for (i = 1; i < OpeLoc.Count; i++){//判断是否有相邻的算符且后一个是负号if (Convert.ToChar(Ope[i]) == '-' && Convert.ToChar(Ope[i - 1]) != '-' && (Convert.ToInt32(OpeLoc[i]) - Convert.ToInt32(OpeLoc[i - 1])) == 1){DelItem.Add(i);}}for (i = DelItem.Count - 1; i >= 0; i--)//将负号和减号分开处理{Ope.RemoveAt(Convert.ToInt32(DelItem[i]));OpeLoc.RemoveAt(Convert.ToInt32(DelItem[i]));}}protected int Calculate(ArrayList Ope, ArrayList Values)//处理四则混合运算等基础运算{int i;for (i = 0; i < Ope.Count; i++)//处理乘法、除法{switch (Convert.ToChar(Ope[i])){case '*':Values[i] = Convert.ToInt32(Values[i]) * Convert.ToInt32(Values[i+1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;case '/':Values[i] = Convert.ToInt32(Values[i]) / Convert.ToInt32(Values[i + 1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;}}for (i = 0; i < Ope.Count; i++)//处理加法和减法{switch ((char)Ope[i]){case '+':Values[i] = Convert.ToInt32(Values[i]) + Convert.ToInt32(Values[i + 1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;case '-':Values[i] = Convert.ToInt32(Values[i]) - Convert.ToInt32(Values[i+1]);;Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;}}return Convert.ToInt32(Values[0].ToString());}protected bool CheckFunction(ArrayList Loc, int Len)//判断算符组是否合法{if (Convert.ToInt32(Loc[0]) == 0)return false;int i;for (i = 1; i < Loc.Count; i++){if (Convert.ToInt32(Loc[i]) - Convert.ToInt32(Loc[i - 1]) == 1)return false;}if (Convert.ToInt32(Loc[Loc.Count - 1]) == Len - 1)return false;return true;}protected bool CheckForNumbers(string input){bool result = true;string[] str = input.Split(new char[] { '+', '-', '/', '*', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);if (str.Length != 4){MessageBox.Show("抱歉,输入有误!请重新输入");result = false;return result;}int[] numbers=new int[4];int[] nums = { NumberA,NumberB, NumberC, NumberD };Array.Sort(nums);for (int i = 0; i < 4; i++){numbers[i] = Convert.ToInt32(str[i]);}Array.Sort(numbers);for (int i = 0; i < 4; i++){if (nums[i] != numbers[i]){result = false;MessageBox.Show("抱歉,每张牌只能使用一次!");return result;}}return result;}}}。

算24点源代码

算24点源代码

#include<stdio.h>#include<stdlib.h>#define Shujugeshu 10int tiny_main(int Tezhengzhi, FILE* fp_total);int heart(float fours[4], int dingwei[6], int Tezhengzhi); /*核心程序,处理一组数*/void myprint(int xuhao, int* jishu, float fours[4], int dingwei[6], FILE*fp); /*打印结果*/void AAA_maker(float fours[4], float AAA[6][6][3]); /*AAA制造者*/void BBB_maker(float AAA[6][6][3], float BBB[6][6][3][6][2], int x1, int x2); /*BBB制造者*/void CCC_maker(float BBB[6][6][3][6][2], float CCC[6][6][3][6][6], int x1, intx2, int x3, int x4, int dingwei[6], int Tezhengzhi); /*CCC制造者*/void sixs_maker(float m1, float m2, float sixs[6]); /*两个数加减乘(被乘)除(被除)六种运算出结果*/int main(){int Tezhengzhi;FILE* fp_total;if((fp_total=fopen("/Users/shengdian/Documents/C/算24/total.txt","wt"))==NULL){printf("Fail openning file!\n");return -1;}for (Tezhengzhi=-1000; Tezhengzhi<=1000; Tezhengzhi++)tiny_main(Tezhengzhi,fp_total);fclose(fp_total);return1;}int tiny_main(int Tezhengzhi, FILE* fp_total){int m1, m2, m3, m4; /*用于运算的四个数的序号*/int xuhao, i, jishu; /*每种排列的序号,,成功数目*/float QiQi[4*Shujugeshu]; /*1到Shujugeshu共十个数,每个数字出现4次*/float fours[4]; /*存储四个数的数组*/int dingwei[6]; /*定位记号,前5个为坐标,最后一个为1代表有解*/FILE *fp; /*输出文件*/char file_name[50]="/Users/shengdian/Documents/C/算24/11/";char file_name_add[20];sprintf(file_name_add, "%d.txt", Tezhengzhi);strcat(file_name, file_name_add);if((fp=fopen(file_name, "wt"))==NULL){printf("Fail openning file!\n");return -1;}for(i=1;i<=Shujugeshu;i++) /*数据初始化*/QiQi[4*i-4]=QiQi[4*i-3]=QiQi[4*i-2]=QiQi[4*i-1]=i;xuhao=1;jishu=0;for(m1=0; m1<4*Shujugeshu; m1++)for(m2=m1+1; m2<4*Shujugeshu; m2++)for(m3=m2+1; m3<4*Shujugeshu; m3++)for(m4=m3+1; m4<4*Shujugeshu; m4++) /*相当于在4*NN张牌中选择4张牌*/{fours[0]=QiQi[m1];fours[1]=QiQi[m2];fours[2]=QiQi[m3];fours[3]=QiQi[m4];heart(fours, dingwei, Tezhengzhi);myprint(xuhao, &jishu, fours, dingwei, fp);xuhao++;}xuhao--;fprintf(fp, "\n~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n");fprintf(fp,"\nTezhengzhi\t:\t%d\nTotal\t\t:\t%d\nSuccess\t\t:\t%d\nSuccessRate\t:\t%2.2f%%\n", Tezhengzhi, xuhao, jishu,100.0*(float)jishu/(float)xuhao);fclose(fp);fprintf(fp_total, "%d\t\t%d\t\t%2.2f\n", Tezhengzhi, jishu,100.0*(float)jishu/(float)xuhao);return1;}int heart(float fours[4], int dingwei[6], int Tezhengzhi) /*判断一组四个数是否有解,并求具体解*/{float AAA[6][6][3], BBB[6][6][3][6][2], CCC[6][6][3][6][6]; /*4C2=6, 3C2=3, 2C2=1*/int x1, x2, x3, x4; /*四个坐标*/dingwei[5]=-1;AAA_maker(fours, AAA); /*第一步*/for(x1=0; x1<=5; x1++)for(x2=0; x2<=5; x2++)BBB_maker(AAA, BBB, x1, x2); /*第二步*/for(x1=0; x1<=5; x1++)for(x2=0; x2<=5; x2++)for(x3=0; x3<=2; x3++)for(x4=0; x4<=5; x4++) /*第三步*/{dingwei[5]=-1;CCC_maker(BBB, CCC, x1, x2, x3, x4, dingwei, Tezhengzhi);if(dingwei[5]==1)return1; /*算出24,立即返回*/}return -1;}void AAA_maker(float fours[4], float AAA[6][6][3]) /*AAA是存储的第一步运算结果*/{int x2;float sixs[6];/*前两个数不参与运算,后两个数参与运算*/ sixs_maker(fours[0], fours[1], sixs); /*2,3,(0,1)*/for(x2=0; x2<=5; x2++){AAA[0][x2][0]=fours[2];AAA[0][x2][1]=fours[3];AAA[0][x2][2]=sixs[x2];}sixs_maker(fours[0], fours[2], sixs); /*1,3,(0,2)*/for(x2=0; x2<=5; x2++){AAA[1][x2][0]=fours[1];AAA[1][x2][1]=fours[3];AAA[1][x2][2]=sixs[x2];}sixs_maker(fours[0], fours[3], sixs); /*1,2,(0,3)*/for(x2=0; x2<=5; x2++){AAA[2][x2][0]=fours[1];AAA[2][x2][1]=fours[2];AAA[2][x2][2]=sixs[x2];}sixs_maker(fours[1], fours[2], sixs); /*0,3,(1,2)*/for(x2=0; x2<=5; x2++){AAA[3][x2][0]=fours[0];AAA[3][x2][1]=fours[3];AAA[3][x2][2]=sixs[x2];}sixs_maker(fours[1], fours[3], sixs); /*0,2,(1,3)*/for(x2=0; x2<=5; x2++){AAA[4][x2][0]=fours[0];AAA[4][x2][1]=fours[2];AAA[4][x2][2]=sixs[x2];}sixs_maker(fours[2], fours[3], sixs); /*0,1,(2,3)*/for(x2=0; x2<=5; x2++){AAA[5][x2][0]=fours[0];AAA[5][x2][1]=fours[1];AAA[5][x2][2]=sixs[x2];}}void BBB_maker(float AAA[6][6][3], float BBB[6][6][3][6][2], int x1, int x2) /*BBB存储的第二步运算结果*/{int x4;float sixs[6], threes[3];threes[0]=AAA[x1][x2][0];threes[1]=AAA[x1][x2][1];threes[2]=AAA[x1][x2][2];sixs_maker(threes[0], threes[1], sixs); /*2,(0,1)*/for(x4=0; x4<=5; x4++){BBB[x1][x2][0][x4][0]=threes[2];BBB[x1][x2][0][x4][1]=sixs[x4];}sixs_maker(threes[0], threes[2], sixs); /*1,(0,2)*/for(x4=0; x4<=5; x4++){BBB[x1][x2][1][x4][0]=threes[1];BBB[x1][x2][1][x4][1]=sixs[x4];}sixs_maker(threes[1], threes[2], sixs); /*0,(1,2)*/for(x4=0; x4<=5; x4++){BBB[x1][x2][2][x4][0]=threes[0];BBB[x1][x2][2][x4][1]=sixs[x4];}}void CCC_maker(float BBB[6][6][3][6][2], float CCC[6][6][3][6][6], int x1, int x2, int x3, int x4, int dingwei[6], int Tezhengzhi) /*CCC存储的第三步运算结果*/ {int x5;float sixs[6];sixs_maker(BBB[x1][x2][x3][x4][0], BBB[x1][x2][x3][x4][1], sixs);for(x5=0; x5<=5; x5++){CCC[x1][x2][x3][x4][x5]=sixs[x5];if(sixs[x5]<Tezhengzhi+0.0001 && sixs[x5]>Tezhengzhi-0.0001) /*运算成功*/{dingwei[0]=x1;dingwei[1]=x2;dingwei[2]=x3;dingwei[3]=x4;dingwei[4]=x5;dingwei[5]=1;return;}}}void myprint(int xuhao, int* jishu, float fours[4], int dingwei[6], FILE *fp) {char a0[3], a1[3], a2[3], a3[3];char s0[20], s1[20], s2[20], s3[20], t[20];if(dingwei[5]!=1){fprintf(fp, "%5d.\tNO\t\t%3d,%3d,%3d,%3d\n", xuhao, (int)fours[0], (int)fours[1], (int)fours[2], (int)fours[3]);return;}fprintf(fp, "%5d.\tYes\t\t%3d,%3d,%3d,%3d", xuhao, (int)fours[0],(int)fours[1], (int)fours[2], (int)fours[3]);(*jishu)++;sprintf(a0, "%d", (int)(fours[0]));sprintf(a1, "%d", (int)(fours[1]));sprintf(a2, "%d", (int)(fours[2]));sprintf(a3, "%d", (int)(fours[3]));switch (dingwei[0]){case0: {strcpy(s0, a2);strcpy(s1, a3);strcpy(s2, a0);strcpy(s3, a1);break;}case1: {strcpy(s0, a1);strcpy(s1, a3);strcpy(s2, a0);strcpy(s3, a2);break;}case2: {strcpy(s0, a1);strcpy(s1, a2);strcpy(s2, a0);strcpy(s3, a3);break;}case3: {strcpy(s0, a0);strcpy(s1, a3);strcpy(s2, a1);strcpy(s3, a2);break;}case4: {strcpy(s0, a0);strcpy(s1, a2);strcpy(s2, a1);strcpy(s3, a3);break;}case5: {strcpy(s0, a0);strcpy(s1, a1);strcpy(s2, a2);strcpy(s3, a3);break;}}switch (dingwei[1]){case0:{strcat(s2, "+");strcat(s2, s3);break;}case1:{strcat(s2, "-");strcat(s2, s3);break;}case2:{strcat(s3, "-");strcat(s3, s2);strcpy(s2, s3);break;}case3:{strcat(s2, "*");strcat(s2, s3);break;}case4:{strcat(s2, "/");strcat(s2, s3);break;}case5:{strcat(s3, "/");strcat(s3, s2);strcpy(s2, s3);break;} }strcpy(s3, "(");strcat(s3, s2);strcat(s3, ")");strcpy(s2, s3);switch (dingwei[2]){case0:{strcpy(t, s0);strcpy(s0, s1);strcpy(s1, t);strcpy(t,s0);strcpy(s0, s2);strcpy(s2, t);break;}case1:{strcpy(t, s0);strcpy(s0, s1);strcpy(s1, t);break;}case2:break;}switch (dingwei[3]){case0:{strcat(s1, "+");strcat(s1, s2);break;}case1:{strcat(s1, "-");strcat(s1, s2);break;}case2:{strcat(s2, "-");strcat(s2, s1);strcpy(s1, s2);break;}case3:{strcat(s1, "*");strcat(s1, s2);break;}case4:{strcat(s1, "/");strcat(s1, s2);break;}case5:{strcat(s2, "/");strcat(s2, s1);strcpy(s1, s2);break;} }strcpy(s3, "(");strcat(s3, s1);strcat(s3, ")");strcpy(s1, s3);switch (dingwei[4]){case0:{strcat(s0, "+");strcat(s0, s1);break;}case1:{strcat(s0, "-");strcat(s0, s1);break;}case2:{strcat(s1, "-");strcat(s1, s0);strcpy(s0, s1);break;}case3:{strcat(s0, "*");strcat(s0, s1);break;}case4:{strcat(s0, "/");strcat(s0, s1);break;}case5:{strcat(s1, "/");strcat(s1, s0);strcpy(s0, s1);break;} }。

C++24速算寻找解法源代码

C++24速算寻找解法源代码

int main(){int c;int flag=1;int i1,i2,i3,i4,v,n1,n2;int num=0;int a,b;double t1,t2,t3,t4,choice[4],s1[4],s2[4][4],s3[2],t;printf("**************** 二十四点游戏*****************\n"); printf("玩法介绍:\n");printf("1.随即抽取四张牌,\n");printf("2.通过加减乘除运算使式子结果为24。

\n");printf("3.友情提示:A=1,J=11,Q=12,K=13\n");while(flag){printf("请给出你抽到的第一牌:");scanf("%c",&choice[0]);printf("请给出你抽到的第二牌:");scanf("%c",&choice[1]);printf("请给出你抽到的第三牌:");scanf("%c",&choice[2]);printf("请给出你抽到的第四牌:");scanf("%c",&choice[3]);for(b=0;b<=2;b++)for(a=3;a>=1+b;a--)if(choice[a]<choice[a-1]){t=choice[a-1];choice[a-1]=choice[a];choice[a]=t;}for(i1=0;i1<4;i1++){t1=choice[i1];for(i2=0;i2<4;i2++){ if((i1-1)!=0 && t1==choice[i1-1]) break;if(i2!=i1){t2=choice[i2];s1[0]=t1+t2;s1[1]=t1-t2;s1[2]=t1*t2;s1[3]=t1/t2;for(i3=0;i3<4;i3++){if(i3!=i1&&i3!=i2){t3=choice[i3];for(v=0;v<4;v++){s2[v][0]=s1[v]+t3;s2[v][1]=s1[v]-t3;s2[v][2]=s1[v]*t3;s2[v][3]=s1[v]/t3;}for(i4=0;i4<4;i4++){if(i4!=i1&&i4!=i2&&i4!=i3){t4=choice[i4];for(n1=0;n1<4;n1++)for(n2=0;n2<4;n2++)if(s2[n1][n2]+t4==24||s2[n1][n2]-t4==24||s2[n1][n2]*t4==24||s2[n1][n2]/t4==24) {printf("找到一种组合方案:\n");num++;}}}}}}}for(i1=0;i1<4;i1++){t1=choice[i1];for(i2=0;i2<4;i2++){if(i2!=i1){t2=choice[i2];s1[0]=t1*t2;s1[1]=t1/t2;for(i3=0;i3<4;i3++){if(i3!=i1&&i3!=i2){t3=choice[i3]; for(i4=0;i4<4;i4++){if(i4!=i1&&i4!=i2&&i4!=i3){t4=choice[i4];s3[0]=t3*t4;s3[1]=t3/t4; for(n1=0;n1<2;n1++) for(n2=0;n2<2;n2++)if(s1[n1]+s3[n2]==24||s1[n1]-s3[n2]==24){printf("找到一种组合方案:\n");if(s1[n1]==t1*t2)printf(" * ");if(s1[n1]==t1/t2)printf(" / ");num++;}}}}}}printf("总共有种解法。

24点源码

24点源码

24点源码.txt吃吧吃吧不是罪,再胖的人也有权利去增肥!苗条背后其实是憔悴,爱你的人不会在乎你的腰围!尝尝阔别已久美食的滋味,就算撑死也是一种美!减肥最可怕的不是饥饿,而是你明明不饿但总觉得非得吃点什么才踏实。

/*6.3.4 源程序*/#define N 20#define COL 100#define ROW 40#include "stdio.h"#include "time.h" /*系统时间函数*/#include "graphics.h" /*图形函数*/#include "alloc.h"/*动态地址分配函数*/#include "stdlib.h" /*库函数*/#include "string.h" /*字符串函数*/#include "ctype.h" /*字符操作函数*/char p[4][13]={{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},/*扑克牌,10用0来表示*/ {'A','2','3','4','5','6','7','8','9','0','J','Q','K'},{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},{'A','2','3','4','5','6','7','8','9','0','J','Q','K'}};typedef struct node{int data;struct node *link;}STACK1; /*栈1*/typedef struct node2{char data;struct node2 *link;}STACK2; /*栈2*/void init(void);/*图形驱动*/void close(void);/*图形关闭*/void play(void);/*发牌的具体过程*/void rand1(int j);/*随机发牌函数*/void change(char *e,char *a); /*中缀变后缀函数*/int computer(char *s); /*后缀表达式计算函数*/STACK1 *initstack1(STACK1 *top); /*栈1初始化*/STACK1 *push(STACK1 *top,int x); /*栈1入栈运算*/STACK1 *pop(STACK1 *top); /*栈1删除栈顶元素*/int topx(STACK1 *top); /*栈1读栈顶元素*/STACK1 *ptop(STACK1 *top,int *x); /*栈1读出栈顶元素值并删除栈顶元素*/int empty(STACK1 *top); /*判栈1是否为空函数*/STACK2 *initstack2(STACK2 *top); /*栈2初始化*/STACK2 *push2(STACK2 *top,char x); /*栈2入栈运算*/STACK2 *pop2(STACK2 *top); /*栈2删除栈顶元素*/char topx2(STACK2 *top); /*栈2读栈顶元素*/STACK2 *ptop2(STACK2 *top,char *x); /*栈2读出栈顶元素值并删除栈顶元素*/int empty2(STACK2 *top); /*判栈2是否为空函数*int text1(char *s) ; /*显示文本*/main(){char s[N],s1[N],ch;int i,result;int gdriver, gmode;clrscr(); /*清屏*/init(); /*初始化函数*/while(1){setbkcolor(BLACK); /*设置背景颜色*/cleardevice();/*清屏*/play(); /*发牌*/gotoxy(1,15); /*移动光标*/printf("--------------------Note-------------------\n");printf(" Please enter express accroding to above four number\n"); /*提示信息*/printf(" Format as follows:2.*(5.+7.)\n");/*提示输入字符串格式*/printf(" ----------------------------------------------\n");scanf("%s%c",s1,&ch); /*输入字符串压回车键*/change(s1,s); /*调用change函数将中缀表达式s1转换为后缀表达式s*/result=computer(s); /*计算后缀表达式的值,返回结果result */if(result==24) /*如果结果等于24*/text1("very good"); /*调用函数text1显示字符串"very good"*/elsetext1("wrong!!!");/*否则函数text1显示字符串"wrong!!!"*/printf("Continue (y/n)?\n"); /*提示信息,是否继续*/scanf("%c",&ch); /*输入一字符*/if(ch=='n'||ch=='N') /*如果该字符等于n或N*/break; /*跳出循环,程序结束*/} /*否则,开始下一轮循环*/close();return; /*返回*/}void rand1(int j)/*随机发牌函数*/{int kind,num;char str[3],n;randomize();while(1)/*循环直到有牌发*/{kind=random(4); /*花色随机数*/num=random(13); /*大小随机数*/if(p[kind][num]!=-1) /*该数未取过*/{n=p[kind][num]; /*取相应位置的扑克牌数*/p[kind][num]=-1; /*牌发好以后相应位置的元素置-1*/break;}}switch(kind)/*花式的判断*/{case 0:setcolor(RED);sprintf(str,"%c",3);break; /*红桃*/case 1:setcolor(BLACK);sprintf(str,"%c",3);break; /*黑桃*/case 2:setcolor(RED);sprintf(str,"%c",4);break; /*方片*/case 3:setcolor(BLACK);sprintf(str,"%c",5);break; /*草花*/}settextstyle(0,0,2);outtextxy(COL+j*100-30,ROW+100-46,str);/*显示左上角花色*/outtextxy(COL+j*100+16,ROW+100+32,str); /*显示右下角花色*/if(n!='0')/*输出其他牌*/{settextstyle(0,0,3);sprintf(str,"%c",n);outtextxy(COL+j*100-5,ROW+100-5,str);/*显示牌的大小*/}else/*输出10的时候*/{sprintf(str,"%d",10);outtextxy(COL+j*100-6,ROW+100-5,str);}}void play(void)/*发牌的具体过程*/{int j;for(j=0;j<4;j++){bar(COL+j*100-35,ROW+100-50,COL+j*100+35,ROW+1*100+50);/*画空牌*/setcolor(BLUE);rectangle(COL+j*100-32,ROW+100-48,COL+j*100+32,ROW+100+48); /*画矩形框*/ rand1(j); /*随机取牌*/delay(10000); /*延时显示*/}}void init(void)/*图形驱动*/{int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}void close(void)/*图形关闭*/{closegraph();}void change(char *e,char *a) /*中缀字符串e转后缀字符串a函数*/ {STACK2 *top=NULL; /* 定义栈顶指针*/int i,j;char w;i=0;j=0;while(e[i]!='\0') /*当字符串没有结束时*/{if(isdigit(e[i])) /*如果字符是数字*/{do{a[j]=e[i]; /*将数字原样拷贝到数组a中*/i++; /*e数组的下标加1*/j++; /*a数组的下标加1*/}while(e[i]!='.'); /*直到字符为数字结束符“.”为止*/a[j]='.';j++; /*将数字结束符“.”拷贝到a数组依然保持结束标记*/ }if(e[i]=='(') /*如果字符是“(”时*/top=push2(top,e[i]); /*将其压入堆栈*/if(e[i]==')') /*如果字符是“)”时*/{top=ptop2(top,&w); /*取出栈顶元素,并从栈顶删除该元素*/while(w!='(') /*如果字符不是“(”时反复循环*/{a[j]=w; /*将栈顶元素存入a数组*/j++; /*下标加1*/top=ptop2(top,&w) ; /*取出栈顶元素,并从栈顶删除该元素*/}}if(e[i]=='+'||e[i]=='-') /*如果字符是加或减号时*/{if(!empty2(top)) /*如栈不为空*/{w=topx2(top);while(w!='(') /*当栈顶元素不是“(”时反复循环*/{a[j]=w;j++; /*将栈顶元素存入表达式a中,a的下标加1*/top=pop2(top); /*删除栈顶元素*/if(empty2(top)) /*如果栈为空*/break; /*跳出循环*/elsew=topx2(top); /*否则读栈顶元素*/}}top=push2(top,e[i]); /*将当前e的字符元素压入堆栈*/}if(e[i]=='*'||e[i]=='/') /*如果字符是乘或除号时*/{if(!empty2(top)) /*如栈不为空*/{w=topx2(top); /*读栈顶元素存入w*/while(w=='*'||w=='/')/*当栈顶元素是乘或除时反复循环*/{a[j]=w;j++; /*将栈顶元素存入字符串a中,a的下标加1*/top=pop2(top); /*删除栈顶元素*/if(empty2(top)) /*如果栈为空*/break; /*跳出循环*/elsew=topx2(top); /*否则读栈顶元素*/}}top=push2(top,e[i]); /*将当前e字符元素压入堆栈*/}i++; /*e的下标加1*/}while(!empty2(top)) /*当不为空时反复循环*/top=ptop2(top,&a[j++]); /*将栈顶元素存入数组a中*/a[j]='\0'; /*将字符串结束标记写入最后一个数组元素中构成字符串*/ }int computer(char *s) /* 计算函数*/{STACK1 *top=NULL;int i,k,num1,num2,result;i=0;while(s[i]!='\0') /*当字符串没有结束时作以下处理*/{if(isdigit(s[i])) /*判字符是否为数字*/{k=0; /*k初值为0*/do{k=10*k+s[i]-'0'; /*将字符连接为十进制数字*/i++; /*i加1*/}while(s[i]!='.'); /*当字符不为‘.’时重复循环*/top=push(top,k); /*将生成的数字压入堆栈*/}if(s[i]=='+') /*如果为'+'号*/{top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/result=num2+num1; /*将num1和num2相加存入result中*/top=push(top,result); /*将result压入堆栈*/}if(s[i]=='-') /*如果为'-'号*/{top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/result=num1-num2; /*将num1减去num2结果存入result中*/ top=push(top,result); /*将result压入堆栈*/}if(s[i]=='*') /*如果为'*'号*/{top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/result=num1*num2; /*将num1与num2相乘结果存入result中*/ top=push(top,result); /*将result压入堆栈*/}if(s[i]=='/') /*如果为'/'号*/{top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/result=num1/num2; /*将num1除num2结果存入result中*top=push(top,result); /*将result压入堆栈*/}i++; /*i加1*/}top=ptop(top,&result); /*最后栈顶元素的值为计算的结果*/ return result; /*返回结果*/}STACK1 *initstack1(STACK1 *top) /*初始化*/{top=NULL; /*栈顶指针置为空*/return top; /*返回栈顶指针*/STACK1 *push(STACK1 *top,int x) /*入栈函数*/{STACK1 *p; /*临时指针类型为STACK1*/p=(STACK1 *)malloc(sizeof(STACK1)); /*申请STACK1大小的空间*/ if(p==NULL) /*如果p为空*/{printf("memory is overflow\n!!"); /*显示内存溢出*/exit(0); /*退出*/}p->data=x; /*保存值x到新空间*/p->link=top; /*新结点的后继为当前栈顶指针*/top=p; /*新的栈顶指针为新插入的结点*/return top; /*返回栈顶指针*/}STACK1 *pop(STACK1 *top) /*出栈*/{STACK1 *q; /*定义临时变量*/q=top; /*保存当前栈顶指针*/top=top->link; /*栈顶指针后移*/free(q); /*释放q*/return top; /*返回栈顶指针*/}int topx(STACK1 *top) /*读栈顶元素*/{if(top==NULL) /*栈是否为空*/{printf("Stack is null\n"); /*显示栈为空信息*/return 0; /*返回整数0*/}return top->data; /*返回栈顶元素*/}STACK1 *ptop(STACK1 *top,int *x) /*取栈顶元素,并删除栈顶元素*/ {*x=topx(top); /*读栈顶元素*/top=pop(top); /*删除栈顶元素*/return top; /*返回栈顶指针*/}int empty(STACK1 *top) /*判栈是否为空*/{if(top==NULL) /*如果为空*/return 1; /*返回1*/elsereturn 0; /*否则返回0*/STACK2 *initstack2(STACK2 *top) /*初始化*/{top=NULL; /*栈顶指针置为空*/return top; /*返回栈顶指针*/}STACK2 *push2(STACK2 *top,char x) /*入栈函数*/{STACK2 *p; /*临时指针类型为STACK2*/p=(STACK2 *)malloc(sizeof(STACK2)); /*申请STACK2大小的空间*/ if(p==NULL) /*如果p为空*/{printf("memory is overflow\n!!"); /*显示内存溢出*/exit(0); /*退出*/}p->data=x; /*保存值x到新空间*/p->link=top; /*新结点的后继为当前栈顶指针*/top=p; /*新的栈顶指针为新插入的结点*/return top; /*返回栈顶指针*/}STACK2 *pop2(STACK2 *top) /*出栈*/{STACK2 *q; /*定义临时变量*/q=top; /*保存当前栈顶指针*/top=top->link; /*栈顶指针后移*/free(q); /*释放q*/return top; /*返回栈顶指针*/}char topx2(STACK2 *top) /*读栈顶元素*/{if(top==NULL) /*栈是否为空*/{printf("Stack is null\n"); /*显示栈为空信息*/return ''; /*返回空字符*/}return top->data; /*返回栈顶元素*/}STACK2 *ptop2(STACK2 *top,char *x) /*取栈顶元素,并删除栈顶元素*/ {*x=topx2(top); /*读栈顶元素*/top=pop2(top); /*删除栈顶元素*/return top; /*返回栈顶指针*/}int empty2(STACK2 *top) /*判栈是否为空*/if(top==NULL) /*如果为空*/return 1; /*返回1*/elsereturn 0; /*否则返回0*/}int text1(char *s){setbkcolor(BLUE); /*设置背景颜色为蓝色*/cleardevice(); /*清除屏幕*/setcolor(12); /*设置文本颜色为淡红色*/settextstyle(1, 0, 8);/*三重笔划字体, 放大8倍*/outtextxy(120, 120, s); /*输出字符串s*/setusercharsize(2, 1, 4, 1);/*水平放大2倍, 垂直放大4倍*/ setcolor(15); /*设置文本颜色为*白色/settextstyle(3, 0, 5); /*无衬字笔划, 放大5倍*/outtextxy(220, 220, s); /*输出字符串s*/getch(); /*键盘输入任一字符*/return ; /*返回*/}。

扑克24点游戏JAVA源代码

扑克24点游戏JAVA源代码

扑克24点游戏JAVA源代码1.游戏规则简介拿出一副扑克,随机发出四张扑克,要求游戏者随意组合使用+、-、×、÷四种算数方法,寻找能计算出结果为24的方案。

2.源码说明本源码由本人单独完成,使用JAVA作为编码语言,其中使用泛型、枚举,JDK版本你懂的。

下列部分截图:1.类的内部结构图:2.部分方法截图:3.测试方法截图4.结果展示:99478方片7+方片6*方片3-方片1=24.099479方片7+方片5+方片4*方片3=24.099480方片7+方片5*方片4-方片3=24.099481方片7+方片5*方片3+方片2=24.099482方片7*方片4-方片3-方片1=24.099483方片7*方片3+方片2+方片1=24.099484方片6+方片5*方片4-方片2=24.099485方片6*方片5-方片4-方片2=24.099486方片5*方片4+方片3+方片1=24.099487方片4*方片3*方片2*方片1=24.099488方片4*方片3*方片2/方片1=24.013420594276533.源码Puke24Test.java附带正文:package com.test.puke;/**** @author GuanChi* BUG说明:* 1.应该优先计算乘法(10+7/4*8)或(7/4*8+10)两种情况 * 2.组合重复情况是否需要过滤,如上面*/public class Puke24Test{public static void serviceExce(){int count = 0;Puke24Test puke24 = new Puke24Test();PokerContainer PokerContainer = puke24.new PokerContainer();java.util.TreeSet<Poker> sPokers = PokerContainer.getPokers();java.util.TreeMap<String, Poker> mPokers =puke24.getTreeMapPokersByTreeSet(sPokers);Poker[] Pokers4 = new Poker[4];for (int i = 1; i <= mPokers.size(); i++){Pokers4[0] = mPokers.get(""+i);for (int j = i+1; j <= mPokers.size(); j++){Pokers4[1] = mPokers.get(""+j);for (int k = j+1; k <= mPokers.size(); k++){Pokers4[2] = mPokers.get(""+k);for (int l = k+1; l <= mPokers.size(); l++){Pokers4[3] = mPokers.get(""+l);Expression24[] exps = calcBy4Pokers(Pokers4);for (int m = 0; m < exps.length; m++){if (null != exps[m] && 24 == exps[m].getResult()){System.out.println(""+ ++count+exps[m]);}}}}}}// 按次序获取四张牌,轮询所有组合方式,轮询所有计算方式,获取结果为24的}public static Expression24[] calcBy4Pokers(Poker[] pokers4){Expression24[] exps = new Expression24[4*4*4*4*4*4*4];int n=0;// 第一个计算符号for (byte i = 1; i <= 4; i++){// 第二个计算符号for (byte j = 1; j <= 4; j++){// 第三个计算符号for (byte k = 1; k <= 4; k++){// 第一个计算扑克牌for (int a = 1; a <= 4; a++){// 第二个计算扑克牌for (int b = a+1; b <= 4; b++){// 第二张拍的脚码不能大于4,并且不能和前面牌重复if (b>4){b=-4;}// 第三个计算扑克牌for (int c = b+1; c <= 4; c++){// 第三张拍的脚码不能大于4,并且不能和前面牌重复if (c>4){c=-4;}// 第四个计算扑克牌for (int d = c+1; d <= 4; d++){// 第四张拍的脚码不能大于4,并且不能和前面牌重复if (d>4){d=-4;}// 计算单个算式的值exps[n++] = calcByExp(pokers4[a-1],Math.convertByteType2CS(i),pokers4[b-1],Math.convertByteType2CS(j),pokers4[c-1],Math.convertByteType2CS(k),pokers4[d-1]);}}}}}}}return exps;}/*** 给定三个计算符号的顺序,把四张牌轮流各个位置,计算出结果* @param poker 第一张扑克* @param j 第一个计算符号* @param poker2 第二张扑克* @param k 第二个计算符号* @param poker3 第三张扑克* @param l 第三个计算符号* @param poker4 第四张扑克* @return 表达式* @attention ?代表未知符号在该方法注释里面*/private static Expression24 calcByExp(Poker poker, Math.CountSymbol j, Poker poker2,Math.CountSymbol k, Poker poker3, Math.CountSymbol l, Poker poker4) {double d = 0;// 当第一个计算符号为乘号或者除号的时候,先计算乘除法(如,1*2?3?4)if (j==Math.CountSymbol.MUL || j==Math.CountSymbol.DIV){d = Math.calc(poker.getNumber(),j,poker2.getNumber());//当第二个符号也为乘除法的时候,用第一次计算的值和第三个数相乘除,然后直接和第四个数字计算(如,1*2*3?4)if (k==Math.CountSymbol.MUL || k==Math.CountSymbol.DIV){d = Math.calc(d,k,poker3.getNumber());d = Math.calc(d,l,poker4.getNumber());}// 当第二个符号不为乘除法,第三个符号为乘除法的时候,先计算第三、第四个数,然后把第一次计算和这次计算结果相运算(如,1*2-3*4)else if (l==Math.CountSymbol.MUL || l==Math.CountSymbol.DIV){double d2 = Math.calc(d,l,poker4.getNumber());d = Math.calc(d,l,d2);}// 当第二个符号不为乘除法,第三个符号也不为乘除法的时候,一口气计算完毕(如,1*2+3-4)else{d = Math.calc(d,k,poker3.getNumber());d = Math.calc(d,l,poker4.getNumber());}}// 第一个符号不为乘除法的时候,第二个符号为乘除法的时候(如,1+2*3?4) else if (k==Math.CountSymbol.MUL || k==Math.CountSymbol.DIV){d = Math.calc(poker2.getNumber(),k,poker3.getNumber());d = Math.calc(d,l,poker4.getNumber());d = Math.calc(poker.getNumber(),j,d);}// 第一个符号不为乘除法的时候,第二个符号不为乘除法的时候,第三个符号为乘除法的时候(如,1+2-3*4)else if (l==Math.CountSymbol.MUL || l==Math.CountSymbol.DIV){d = Math.calc(poker3.getNumber(),l,poker4.getNumber());double d2 = Math.calc(poker.getNumber(),j,poker2.getNumber());d = Math.calc(d,k,d2);}// 第一个符号不为乘除法的时候,第二个符号不为乘除法的时候,第三个符号不为乘除法的时候(如,1+2-3+4)else{d = Math.calc(poker.getNumber(),j,poker2.getNumber());d = Math.calc(d,k,poker3.getNumber());d = Math.calc(d,l,poker4.getNumber());}Expression24 exp24 = newExpression24(poker,j,poker2,k,poker3,l,poker4);exp24.setResult(d);return 24 == d ? exp24: null;// TODO Auto-generated method stub}/*** 加减乘除24的表达式* @author GuanChi* 包含4张扑克以及中间的三个运算符号,还有计算结果*/static class Expression24{/** 第一张扑克 **/private Poker poker1;/** 第一个运算符号 **/private Math.CountSymbol countSymbols1;/** 第二张扑克 **/private Poker poker2;/** 第二个运算符号 **/private Math.CountSymbol countSymbols2;/** 第三张扑克 **/private Poker poker3;/** 第三个运算符号 **/private Math.CountSymbol countSymbols3;/** 第四张扑克 **/private Poker poker4;/** 结果 **/private double result;/*** 构造方法,初始化时必须提供4张扑克和三个运算符号* @param poker1 第一张扑克* @param countSymbols1 第一个运算符号* @param poker2 第二张扑克* @param countSymbols2 第二个运算符号* @param poker3 第三个张扑克* @param countSymbols3 第三个运算符号* @param poker4 第四个张扑克*/public Expression24(Poker poker1, Math.CountSymbol countSymbols1, Poker poker2,Math.CountSymbol countSymbols2, Poker poker3, Math.CountSymbol countSymbols3, Poker poker4){super();this.poker1 = poker1;this.countSymbols1 = countSymbols1;this.poker2 = poker2;this.countSymbols2 = countSymbols2;this.poker3 = poker3;this.countSymbols3 = countSymbols3;this.poker4 = poker4;}public void setResult(double result){this.result = result;}public double getResult(){return result;}public Poker getPoker1(){return poker1;}public void setPoker1(Poker poker1){this.poker1 = poker1;}public Math.CountSymbol getCountSymbols1(){return countSymbols1;}public void setCountSymbols1(Math.CountSymbol countSymbols1) {this.countSymbols1 = countSymbols1;}public Poker getPoker2(){return poker2;}public void setPoker2(Poker poker2){this.poker2 = poker2;}public Math.CountSymbol getCountSymbols2()return countSymbols2;}public void setCountSymbols2(Math.CountSymbol countSymbols2){this.countSymbols2 = countSymbols2;}public Poker getPoker3(){return poker3;}public void setPoker3(Poker poker3){this.poker3 = poker3;}public Math.CountSymbol getCountSymbols3(){return countSymbols3;}public void setCountSymbols3(Math.CountSymbol countSymbols3){this.countSymbols3 = countSymbols3;}public Poker getPoker4(){return poker4;}public void setPoker4(Poker poker4){this.poker4 = poker4;}/*** 打印时候需要把字符拼接成一般能看懂的格式,包含+、-、*、/的格式*/public String toString(){return new StringBuilder().append(null==this.poker1?null:""+this.poker1.getColor()+this.poker1.ge tNumber()).append(""+(this.countSymbols1==Math.CountSymbol.ADD?"+":(this.countSym bols1==Math.CountSymbol.SUB?"-":(this.countSymbols1==Math.CountSymbol.MUL?".append(null==this.poker2?null:""+this.poker2.getColor()+this.poker2.ge tNumber()).append(""+(this.countSymbols2==Math.CountSymbol.ADD?"+":(this.countSym bols2==Math.CountSymbol.SUB?"-":(this.countSymbols2==Math.CountSymbol.MUL?" *":"/")))).append(null==this.poker3?null:""+this.poker3.getColor()+this.poker3.ge tNumber()).append(""+(this.countSymbols3==Math.CountSymbol.ADD?"+":(this.countSym bols3==Math.CountSymbol.SUB?"-":(this.countSymbols3==Math.CountSymbol.MUL?" *":"/")))).append(null==this.poker4?null:""+this.poker4.getColor()+this.poker4.ge tNumber()).append("="+this.result).toString();}}/*** 从TreeSet里面获取TreeMap* @param sPokers 扑克牌TreeSet容器* @return 排列好的扑克牌TreeMap容器*/public java.util.TreeMap<String, Poker>getTreeMapPokersByTreeSet(java.util.TreeSet<Poker> sPokers){java.util.Iterator<Poker> iPokers = sPokers.iterator();java.util.TreeMap<String, Poker> mPokers = newjava.util.TreeMap<String, Poker>();int i = 0;while (iPokers.hasNext()){Poker poker = iPokers.next();// System.out.println(""+Poker.getColor()+Poker.getNumber());// 按顺序把扑克塞进map容器里面,方便拿取mPokers.put(""+ ++i, poker);}return mPokers;}/*** 扑克牌容器-牌盒子* @author GuanChi* 成员变量Pokers里面放置了一副扑克,共52张,会在第一次实例化时初始化 */class PokerContainer{/** 扑克牌容器 **/private java.util.TreeSet<Poker> tpokers = newjava.util.TreeSet<Poker>();/*** 构造方法,第一次实例化时候初始化扑克牌容器*/public PokerContainer(){super();if (0 == this.tpokers.size()){init();}}/*** 初始化扑克牌容器,把52张扑克放入牌盒子*/private void init(){// 选择1~13,分别对应扑克牌里面的A23~JQK.for (int i = 1;i <= 13; i++){// 选择四种不同的花色for (int j = 1;j <= 4; j++){Poker.Color color = null;switch (j){case 1:color = Poker.Color.黑桃;break;case 2:color = Poker.Color.红桃;break;case 3:color = Poker.Color.梅花;break;case 4:color = Poker.Color.方片;break;default:System.err.println("Error Occur!!");break;}// 给容器里面添加一张扑克牌this.tpokers.add(new Poker(color,i));}}}public java.util.TreeSet<Poker> getPokers(){return tpokers;}}/*** 扑克牌类型* @author GuanChi* 该类型包含两个成员变量:花色color和数字number**/static class Poker implements parable<Poker> {/*** 枚举类型-花色* @author GuanChi* 包含黑红梅方四种类型*/static enum Color{/** 黑桃 **/黑桃,/** 红桃 **/红桃,/** 梅花 **/梅花,/** 方片 **/方片}/** 扑克牌的大小点数 **/private int number;/** 扑克牌的花色 **/private Color color = null;/*** 初始化一张扑克牌,包含颜色和点数* @param color 颜色* @param number 点数*/public Poker(Color color, int number) {super();this.color = color;this.number = number;}public int getNumber(){return number;}public void setNumber(int number) {this.number = number;}public Color getColor(){return color;}public void setColor(Color color) {this.color = color;}public boolean equals(Object obj) {Poker Poker = (Poker)obj;return (Poker.getColor().equals(this.color)) && (Poker.getNumber() == this.number);}public int compareTo(Poker Poker){return Poker.number - this.number + Poker.color.hashCode() - this.color.hashCode();}}/*** 数学运算类型* @author GuanChi* 该类型提供基本的加减乘除运算静态方法*/static class Math{static enum CountSymbol{/** 加法 **/ADD,/** 减法 **/SUB,/** 乘法 **/MUL,/** 除法 **/DIV,/** 错误类型 **/ERROR,}static byte convertCSType2Byte(CountSymbol cs){// 初始化结果byte b = 0;// 根据符号来用Math中不同的方法switch (cs){// 加法case ADD:b = 1;break;// 减法case SUB:b = 2;break;// 乘法case MUL:b = 3;break;// 除法case DIV:b = 4;break;default:b = 0;}return b;}static CountSymbol convertByteType2CS(byte b) {// 初始化结果CountSymbol cs;// 根据符号来用Math中不同的方法switch (b){// 加法case 1:cs = Math.CountSymbol.ADD;break;// 减法case 2:cs = Math.CountSymbol.SUB;break;// 乘法case 3:cs = Math.CountSymbol.MUL;break;// 除法case 4:cs = Math.CountSymbol.DIV;break;default:return cs = CountSymbol.ERROR;}return cs;}static double calc(double d1,CountSymbol cs,double d2) {// 初始化结果double d = 0;// 根据符号来用Math中不同的方法switch (cs){// 加法case ADD:d = Math.add(d1, d2);break;// 减法case SUB:d = Math.sub(d1, d2);break;// 乘法case MUL:d = Math.mul(d1, d2);break;// 除法case DIV:d = Math.div(d1, d2);break;default:d = 0;}return d;}static double calc(int d1,CountSymbol cs,int d2){// 初始化结果double d = 0;// 根据符号来用Math中不同的方法switch (cs){// 加法case ADD:d = Math.add(d1, d2);break;// 减法case SUB:d = Math.sub(d1, d2);break;// 乘法case MUL:d = Math.mul(d1, d2);break;// 除法case DIV:d = Math.div(d1, d2);break;default:d = 0;}return d;}static double calc(double d1,CountSymbol cs,int d2) {// 初始化结果double d = 0;// 根据符号来用Math中不同的方法switch (cs){// 加法case ADD:d = Math.add(d1, d2);break;// 减法case SUB:d = Math.sub(d1, d2);break;// 乘法case MUL:d = Math.mul(d1, d2);break;// 除法case DIV:d = Math.div(d1, d2);break;default:d = 0;}return d;}static double calc(int d1,CountSymbol cs,double d2) {// 初始化结果double d = 0;// 根据符号来用Math中不同的方法switch (cs){// 加法case ADD:d = Math.add(d1, d2);break;// 减法case SUB:d = Math.sub(d1, d2);break;// 乘法case MUL:d = Math.mul(d1, d2);break;// 除法case DIV:d = Math.div(d1, d2);break;default:d = 0;}return d;}/*** 加法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 加法运算结果*/static double add(double number1, double number2) {}/*** 加法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 加法运算结果*/static int add(int number1, int number2){return number1 + number2;}/*** 加法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 加法运算结果*/static double add(int number1, double number2) {return number1 + number2;}/*** 加法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 加法运算结果*/static double add(double number1, int number2) {return number1 + number2;}/*** 减法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 减法运算结果*/static double sub(double number1, double number2) {}/*** 减法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 减法运算结果*/static int sub(int number1, int number2){return number1 - number2;}/*** 减法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 减法运算结果*/static double sub(int number1, double number2) {return number1 - number2;}/*** 减法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 减法运算结果*/static double sub(double number1, int number2) {return number1 - number2;}/*** 乘法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 乘法运算结果*/static double mul(double number1, double number2) {return number1 * number2;}/*** 乘法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 乘法运算结果*/static int mul(int number1, int number2){return number1 * number2;}/*** 乘法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 乘法运算结果*/static double mul(int number1, double number2) {return number1 * number2;}/*** 乘法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 乘法运算结果*/static double mul(double number1, int number2) {return number1 * number2;}/*** 除法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 除法运算结果*/static double div(double number1, double number2) {double d = 1;return 0 == number2 ? 0 : d * number1/ number2;}/*** 除法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 除法运算结果*/static double div(int number1, int number2){double d = 1;return 0 == number2 ? 0 : d * number1/ number2;}/*** 除法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 除法运算结果*/static double div(double number1, int number2){double d = 1;return 0 == number2 ? 0 : d * number1/ number2;}/*** 除法* @param number1 第一个运算数字* @param number2 第二个运算数字* @return 除法运算结果*/static double div(int number1, double number2){double d = 1;return 0 == number2 ? 0 : d * number1/ number2;}}/*** 卡片工厂* @author GuanChi* 提供一个工厂实例创建卡片*/// static class PokerFactory// {// private static PokerFactory instance = null;// public synchronized static PokerFactory getInstance()// {// if (null == instance)// {// instance = new PokerFactory();// }// return instance;// }//// public Poker createPoker(Color color, int number)// {// return new Poker(color, number);// }// }public static void testAdd24(){int no = 1;final byte[] PUKE_NUMBERS = {1,2,3,4,5,6,7,8,9,10,11,12,13};for (int i = 0;i < 13; i++){for (int j = i;j < 13; j++){for (int k = j;k < 13; k++){for (int l = k;l < 13; l++){int result = PUKE_NUMBERS[i] + PUKE_NUMBERS[j]+ PUKE_NUMBERS[k]+ PUKE_NUMBERS[l];if (result == 24){System.out.println(""+no+"→1:"+PUKE_NUMBERS[i] + "; 2:" + PUKE_NUMBERS[j] + "; 3:" + PUKE_NUMBERS[k] + "; 4:" + PUKE_NUMBERS[l]);no++;}}}}}}public static void testPoker52(){PokerContainer c = new Puke24Test().new PokerContainer();java.util.TreeSet<Poker> s = c.getPokers();System.out.println(s.size());}public static void testEqualsPoker(){System.out.println(new Poker(Poker.Color.方片,1).equals(new Poker(Poker.Color.黑桃,1)));}public static void testDiv(){System.out.println(Math.div(5, 3));}public static void main(String[] args){System.out.println(System.currentTimeMillis());// Puke24Test.testAdd24();// Puke24Test.testPoker52();// Puke24Test.testEqualsPoker();// Puke24Test.testDiv();Puke24Test.serviceExce();System.out.println(System.currentTimeMillis());}}。

24点算法的Java和C语言源码

24点算法的Java和C语言源码

24点算法的Java版和C语言版源码Java版共2个类:和== ==package num24;public class Application {double num[] = new double[4];boolean mark[] = new boolean[4];/*** 取得k个数运算,结果为result的第一个Node,比如get(24,4) * @param result* @param n* @return*/public Node get(double result, int n) {Node left = null, right = null;if (n <= 0) {return null;} .mark[i]=false;} .return null;}public void output(Node node) {if (node == null) {return;}if != null) {if ( "+") || "-")) && "*") || "/"))) { "(");output;")");}else {output;}}if (!"")) {}else {}if != null) {if ("+") || "-")) && "*") || "/"))||"+") || "-")) && "-")) ||(!"")) && "/")) ) {"(");output;")");}else {output;}}}/**** @param args*/public static void main(String[] args) { Application app=new Application();[0]=(args[0]);[1]=(args[1]);[2]=(args[2]);[3]=(args[3]);double result;Node tree=null;//while(true){result=;// if(result==break;[0]=false;[1]=false;[2]=false;[3]=false;tree=(result,4);if(tree!=null){(tree);"="+result);}else{"No!");//}}}======package num24;public class Node {public double num;String name;Node left, right = null;public Node() {num = ;name = " ";left = right = null;}public Node(double aNum){this(aNum," ",null,null);}public Node(double aNum, String aName, Node aLeft, Node aRight) { num = aNum;name = aName;left = aLeft;right = aRight;}public void delete(){if(left!=null){();}if(right!=null){}left=null; right=null; }}C 语言版//用二叉树保留表达式,输出无多余括号。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
goto begin;
}
if((a*b)%c==0)
{if(a*b/c+d==24)
{printf("%d*%d/%d+%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b/c-d==24)
getchar();
goto begin;
}
if(a+b+c-d==24)
{printf("%d+%d+%d-%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b+c+d==24)
{printf("%d*%d+%d+%d=24\n",a,b,c,d);
SetScreen(0);
printf("请输入4个数字(1-13)");
for(num[aaa=0]=0;num[aaa=0]<1||num[aaa=0]>13;num[aaa=0]=inputn(20));
for(num[aaa=1]=0;num[aaa=1]<1||num[aaa=1]>13;num[aaa=1]=inputn(25));
long value;
char numberc[11];
char string[12];
row=start/20;
col=start%20;
strcpy(numberc,"0bnmghjtyu");
memset(string,0,12);
sign=1;
n=0;
value=0;
{memmove(string+1,string,10);
string[aaa=0]=ch;
}
else if(sign==-1)
{memmove(string,string+1,10);
string[n-1]=0;
getchar();
goto begin;
}
if(a*b*c*d==24)
{printf("%d*%d*%d*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b+c*d==24)
{printf("%d*%d+%d*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b+c-d==24)
{printf("%d*%d+%d-%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b-c-d==24)
{printf("%d*%d-%d-%d=24\n",a,b,c,d);
}
if((a-b)*c-d==24)
{printf("(%d-%d)*%d-%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a+b)*c*d==24)
{printf("(%d+%d)*%d*%d=24\n",a,b,c,d);
for(i=0;i<=3;i++)
{a=num[i];
for(j=0;j<=3;j++)
{if(j==i) continue;
b=num[j];
for(k=0;k<=3;k++)
{if(k==i||k==j) continue;
c=num[k];
getchar();
goto begin;
}
if((a-b*c)*d==24)
{printf("(%d-%d*%d)*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a-b)*(c-d)==24)
{printf("(%d-%d)*(%d-%d)=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a-b)*(c+d)==24)
{printf("%d/%d+%d+%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(c%d==0)
if(a+b-c/d==24)
{printf("%d+%d-%d/%d=24\n",a,b,c,d);
getchar();
for(num[aaa=2]=0;num[aaa=2]<1||num[aaa=2]>13;num[aaa=2]=inputn(30));
for(num[aaa=3]=0;num[aaa=3]<1||num[aaa=3]>13;num[aaa=3]=inputn(35));
printf("\n");
{printf("%d*%d/%d-%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a*b/c)%d==0)
if(a*b/c/d==24)
{printf("%d*%d/%d/%d=24\n",a,b,c,d);
{printf("(%d-%d)*(%d+%d)=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a+b+c)*d==24)
{printf("(%d+%d+%d)*%d=24\n",a,b,c,d);
getchar();
for(l=0;l<=3;l++)
{if(l==i||l==j||l==k) continue;
d=num[l];
/*以下是45种算法*/
if(a+b+c+d==24)
{printf("%d+%d+%d+%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a-b)*c*d==24)
{printf("(%d-%d)*%d*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a+b)*(c+d)==24) {printf("(%d+%d)*(%d+%d)=24\n",a,b,c,d);
{ch=i+'0';
break;
}
else if(ch==20)
ch='-';
}
if(isdigit(ch)&&n<10)
string[n]=ch;
else if (ch=='-')
{if(sign==1)
}
sign=-sign;
}
else if(ch==LT_KEY&&n)
string[n-1]=0;
else if (ch==ENTER_KEY&&n)
{string[n]=0;
memset(_TEXT+start+n,0,1);
break;
}
}
if(sign==1)
i=0;
else
i=1;
for(;i<n;i++)
{ch=string[i];
value=value*10+(ch&0xf);
}
return sign*value;
}
void main()
}
if(c%d==0)
{if(a*b+c/d==24)
{printf("%d*%d+%d/%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b-c/d==24)
{printf("%d*%d-%d/%d=24\n",a,b,c,d);
goto begin;
}
if((a+b-c)*d==24)
{printf("(%d+%d-%d)*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if((a-b-c)*d==24)
{printf("(%d-%d-%d)*%d=24\n",a,b,c,d);
getchar();
goto begin;
}
}
if((a+b)*c+d==24)
{printf("(%d+%d)*%d+%d=24\n",a,b,c,d);
相关文档
最新文档