数据结构24点游戏源代码

合集下载

24点游戏代码

24点游戏代码
{
int i,j,k;
float sum1,sum2,sum3;
for (i=0;i<4;i++)
for (j=0;j<6;j++)
for (k=0;k<6;k++)
{
if ((!(i==3 && b==0)) && (!(j==3 && c==0)) && (!(k==3 && d==0)))
{
sum1=myF(i,a,b);
//printf ("sum2:myF(%d,%2.0f,%2.0f) sum2=%f\n",j,c,d,sum2);
//printf ("2:myF(%d,myF(%d,%2.0f,%2.0f),myF(%d,%2.0f,%2.0f)) sum3=%f\n\n",k,i,a,b,j,c,d,sum3);
sum2=myF(j,sum1,c);
sum3=myF(k,sum2,d);
if (fabs(sum3-24)<0.1)
{
temp++;
myPrint(1,i,j,k,a,b,c,d);
//printf ("sum1:myF(%d,%2.0f,%2.0f) sum1=%f\n",i,a,b,sum1);
}
}
if (k==2)
{
sum1=myF(i,a,b);
sum2=myF(j,c,d);
sum3=sum1*sum2;
if (fabs(sum3-24)<0.1)
{
temp++;

24点游戏程序

24点游戏程序

24点程序编译环境:dev c++//24点游戏#include<iostream>#define plus 1#define minus 2#define mul 3#define div 4using namespace std;float num[4];char str[5]={' ','+','-','*','/'};float op(int sym,float a,float b){if(sym==plus) return a+b;if(sym==minus) return a-b;if(sym==mul) return a*b;else return a/b;}void swap(int i,int j){float temp=num[i];num[i]=num[j];num[j]=temp;}bool search_ope(){for(int i=1;i<=4;++i)for(int j=1;j<=4;++j)for(int k=1;k<=4;++k){float sum=op(i,num[0],num[1]);sum=op(j,sum,num[2]);sum=op(k,sum,num[3]);if(sum==24){int bracket1=false;int bracket2=false;if(i<=2&&j>=3) bracket1=true;if(j<=2&&k>=3) bracket2=true;for(int count=0;count<bracket1+bracket2;++count)cout<<"(";cout<<(int)num[0]<<str[i]<<num[1];if(bracket1)cout<<")";cout<<str[j]<<(int)num[2];if(bracket2)cout<<")";cout<<str[k]<<(int)num[3]<<endl;return true;}float temp=op(i,num[0],num[1]);sum=op(k,num[2],num[3]);sum=op(j,temp,sum);if(sum==24){int bracket1=false;int bracket2=false;if(i<=2&&j>=3) bracket1=true;if(k<=2) bracket2=true;if(bracket1)cout<<"(";cout<<(int)num[0]<<str[i]<<(int)num[1];if(bracket1)cout<<")";cout<<str[j];if(bracket2) cout<<"(";cout<<num[2]<<str[k]<<num[3];if(bracket2)cout<<")";cout<<endl;return true;}}return false;}bool search(int n){if(n>3) return false;if(search_ope()) return true;if(search(n+1)) return true;for(int i=n+1;i<4;++i){swap(i,n);if(search_ope()) return true;if(search(n+1)) return true;swap(i,n);}return false;}int main(){char flag='Y';loop:cout<<"请输入四个数(1-13):"<<endl;for(int i=0;i<4;++i){cin>>num[i];if(num[i]<=0||num[i]>13){cout<<"错误输入,";goto loop;}}if(!search(0)) cout<<"不能形成24点"<<endl;cout<<"是否继续(Y/N)"<<endl;cin>>flag;while(flag!='Y'&&flag!='N'){cout<<"错误输入,请重新输入(Y/N):"<<endl;cin>>flag;}if(flag=='Y')goto loop;if(flag=='N') return 0;return 0;}本程序仅仅搜索一种答案,如果要搜索全部答案可通过修改递归函数bool search(int n)中的return即可。

24点游戏C语言代码及报告

24点游戏C语言代码及报告

24点游戏C语言代码及报告24点游戏一,问题分析二十四点游戏规则:给出4个数字,所给数字均为有理数,用加、减、乘、除(可加括号)把给出的数算成24(每个数必须用一次且只能用一次,先算出结果者获胜。

步骤:比如下面的例子:例:3、8、8、9,答案1:(9—8)×8×3答案2: 3×8 ?(9—8)答案3:(9—8?8)×3利用3×8,24、4×6,24求解我的主要想法是首先穷举的可行性问题。

我把表达式如下分成三类—— 1、无括号的简单表达式。

2、有一个括号的简单表达式。

3、有两个括号的较复4、杂表达式。

穷举的开始我对给出的四个数进行排列,其可能的种数为4*3*2*1=24。

我利用一个嵌套函数实现四个数的排列二,源程序及注释//首先穷举的可行性问题。

我把表达式如下分成三类——//1、无括号的简单表达式。

//2、有一个括号的简单表达式。

//3、有两个括号的较复4、杂表达式。

//穷举的开始我对给出的四个数进行排列,其可能的种数为4*3*2*1=24。

我利用一个嵌套函数实现四个数的排列#include <iostream>#include <string>using namespace std;int number=0;string sss[5000];float cal(float a,float b,int p) {switch(p){case 1:return a+b;case 2:return a-b;case 3:return a*b;case 4:return a/b;case 5:return b-a;case 6:return b/a;}}string link(string a,string b,int p) {string st=a;switch(p){case 1:st+=("+"+b);break;case 2:st+=("-"+b);break;case 3:st+=("*"+b);break;case 4:st+=("/"+b);break;case 5:st=b+"-"+st;break;case 6:st=b+"/"+st;break;}st="("+st+")";return st;}void putout1(string c[],int p[]) { string st;st=link(c[1],c[2],p[1]);st=link(st,c[3],p[2]);st=link(st,c[4],p[3]);st=st.erase(0,1);st=st.erase(st.length()-1,1); number++;sss[number]=st;}void putout2(string c[],int p[]) { string st1,st2,st;st1=link(c[1],c[2],p[1]);st2=link(c[3],c[4],p[3]);st=link(st1,st2,p[2]);st=st.erase(0,1);st=st.erase(st.length()-1,1);number++;sss[number]=st;}void suan(float a[],string c[]) {int p[4];int i;float sum,sum1,sum2;for(p[1]=1;p[1]<=6;p[1]++)for(p[2]=1;p[2]<=6;p[2]++)for(p[3]=1;p[3]<=6;p[3]++){sum=cal(a[1],a[2],p[1]);sum=cal(sum,a[3],p[2]);sum=cal(sum,a[4],p[3]);if((sum-24>=0&&sum-24<1e-5)||(sum-24<=0&&sum-24>-1e-5)) putout1(c,p); sum1=cal(a[1],a[2],p[1]);sum2=cal(a[3],a[4],p[3]);sum=cal(sum1,sum2,p[2]);if((sum-24>=0&&sum-24<1e-5)||(sum-24<=0&&sum-24>-1e-5)) putout2(c,p); }}void disorder(float a[],string c[]){float b[5];string d[5];int i,j,k,l;for(i=1;i<=4;i++)for(j=1;j<=4;j++){if(j==i)continue;for(k=1;k<=4;k++){if(k==i||k==j)continue;for(l=1;l<=4;l++){if(l==i||l==j||l==k)continue;b[1]=a[i];b[2]=a[j];b[3]=a[k];b[4]=a[l]; d[1]=c[i];d[2]=c[j];d[3]=c[k];d[4]=c[l]; suan(b,d);}}}}int main(){cout<<"请输入四个数字";float a[5];int i,j;string c[5];for(i=1;i<=4;i++){cin>>a[i];if(a[i]==10)c[i]="10" ;else c[i]=int(a[i])+'0';}disorder(a,c);int v=1,u=0;for(i=1;i<=number;i++){v=1;for(j=1;j<i;j++)if(sss[i]==sss[j])v=0;if(v==1){cout<<sss[i]<<endl;u=1;}}if(u==0)cout<<"无解"<<endl;system("pause");return 0;}三,运行结果显示四,调试和运行程序过程中产生的问题及采取的措施:。

数据结构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----计算表达式的值// 算术表达式求值的算符优先算法。

c++24点游戏

c++24点游戏

c++24点游戏正文第一篇:c++24点游戏 c++24点游戏 #include iostream#include stringusing namespace std; //定义Stack类const maxsize=2022 enum Error_code { success, overflow, underflow };templateclass Stack {public:Stack();bool empty() const;bool full() const;int size() const;void clear();Error_code top(T &item) const;Error_code pop();Error_code push(const T &item);private:int count;T entry[maxsize];};templateStack::Stack() {count=0;}templatebool Stack::empty () const { return count==0;}templatebool Stack::full () const { return count==maxsize;}templateint Stack::size() const {return count;}templatevoid Stack::clear() {count=0;}templateError_code Stack::top (T &item) const { if (empty()) return underflow;item= entry[count-1];return success;}templateError_code Stack::pop () {if (empty()) return underflow;count--;return success;}templateError_code Stack::push (const T &item) {if (full()) return overflow;entry[count++]=item;return success;} Stack sign;Stack num;int set; // 判断程序中的异常,以便适时退出?//void process(char c) //计算两个数的 + - * / 运算// { int k=0;double a,b;sign.pop();if (num.top(b)==success) {num.pop();if (num.top(a)==success) {num.pop();k=1;}}if (k) {switch (c) {case '+': num.push(a+b); break; case '-': num.push(a-b); break; case '*': num.push(a*b); break; case '/':if (b==0) { set=4;num.push(-1);}elsenum.push(a/b);break;}}else {set=1;num.push(-1);}}void get_command(string &str) {coutstr;}double do_command(const string &str) {string s=;double outcome=-1;char c;for (int i=0;str[i]!='\0';i++){if (set!=0) break; //例外则停止运行while (1) { //分离数据与运算符if (str[i]='0' || str[i]=='.') {s+=str[i];i++;}else {if(s!=) {if (num.push(atof(s.c_str ()))==overflow)set=3;s=;break;}}char ch= str[i];switch (ch) { //处理运算的优先级,并注意例外抛出case '*':case '/':if (sign.top(c)==success)if(c=='*'||c=='/') process(c);if (sign.push(ch)==overflow)set=3;break;case '+':case '-':while (sign.top(c)==success) {if (c!='(') process(c);else break;if (sign.push(ch)==overflow) set=3;break;case '(':if (sign.push(ch)==overflow) set=3;break;case ')':while (sign.top(c)==success) { if (c!='(') process(c);else break;}sign.pop();break;case '=':while (sign.top(c)==success) { if (c!='(') process(c);else break;}break;default: set=2;break;}}if (num.size()==1 && sign.size()==0)num.top(outcome);else set=1;if (set==0) coutout; cout>jj;cout13 || a[i]!=int(a[i])) 在输出方面,运算结果等于 24就输出,利用调用的参数判断输出形式,有5种:(a+b)*(c+d),a*(b*(c+d)) ,(a*(b+c))*d ,a*((b+c)*d) ,((a+b)*c)*d。

24点游戏

24点游戏

//初始化数字组合和符号组合 初始化数字组合和符号组合 Cal24(String inputnum) { //生成所输入 生成所输入abcd四个数字的全排列(不可重复使用,如aaaa), 四个数字的全排列( 生成所输入 四个数字的全排列 不可重复使用, ), 去掉重复的, 去掉重复的,保存在 num 字符串集合 for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { for (int k=0;k<4;k++) { if (i==j || i==k || k==j) //去掉重复使用同一数字的情况 去掉重复使用同一数字的情况 continue; int z=6-i-j-k; //计算出剩下的第四个数。 i+j+k+z=6 计算出剩下的第四个数。 计算出剩下的第四个数 num.add(new String(inputnum.substring(i,i+1)+inputnum.substring(j,j+1) +inputnum.substring(k,k+1)+inputnum.substring(z,z+1)向对象基础实例
游戏描述
每次给您4张牌,您用这4张牌的点数和加、 每次给您4张牌,您用这4张牌的点数和加、减、 除符号,还有括号构成一个算术式子, 乘、除符号,还有括号构成一个算术式子,使 式子计算结果为24. 式子计算结果为24.
代码: 代码:
import java.util.*; public class Cal24 { String[] op; Set num = new HashSet(); public static void main(String[] args) { Cal24 cal24; if (args.length<1) { System.out.println("Usage Cal24 abcd"); return; } cal24 = new Cal24(args[0]); cal24.pEx(); }

C++实现简单24点游戏

C++实现简单24点游戏

C++实现简单24点游戏本⽂实例为⼤家分享了C++实现简单24点游戏的具体代码,供⼤家参考,具体内容如下随机⽣成4个代表扑克牌牌⾯的数字字母,程序⾃动列出所有可能算出24的表达式,⽤擅长的语⾔(C/C++/Java或其他均可)实现程序解决问题。

程序风格良好(使⽤⾃定义注释模板)列出表达式⽆重复。

以下为源代码#include<iostream>#include<string>#include <stdlib.h>#include<time.h>using namespace std;char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' };char buf[4];double nums[4];char ope[4] = { '+','-','*','/' };void cre()//⽣成{int i = 0;int j;cout << "⽣成的四张牌⾯为:";srand((unsigned)time(0));for (i = 0; i<4; i++){j =rand() % 13;buf[i] = card[j];}cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。

c语言计算得到2424点游戏

c语言计算得到2424点游戏

#include<stdio.h>char mark[4]={'+','-','*','/'};float cal(float x,float y,int mark){switch(mark){case 0:return x+y;case 1:return x-y;case 2:return x*y;case 3:return x/y;}}float calculate_A(float a,float b,float c,float d,int mark1,int mark2,int mark3) {float r1,r2,r3;r1=cal(a,b,mark1);r2=cal(r1,c,mark2);r3=cal(r2,d,mark3);return r3;}float calculate_B(float a,float b,float c,float d,int mark1,int mark2,int mark3) {float r1,r2,r3;r1=cal(b,c,mark2);r2=cal(a,r1,mark1);r3=cal(r2,d,mark3);return r3;}float calculate_C(float a,float b,float c,float d,int mark1,int mark2,int mark3) {float r1,r2,r3;r1=cal(c,d,mark3);r2=cal(b,r1,mark2);r3=cal(a,r2,mark1);return r3;}float calculate_D(float a,float b,float c,float d,int mark1,int mark2,int mark3) {float r1,r2,r3;r1=cal(b,c,mark2);r2=cal(r1,d,mark3);r3=cal(a,r2,mark1);return r3;}float calculate_E(float a,float b,float c,float d,int mark1,int mark2,int mark3){float r1,r2,r3;r1=cal(a,b,mark1);r2=cal(c,d,mark3);r3=cal(r1,r2,mark2);return r3;}float get(int a,int b,int c,int d){int mark1,mark2,mark3;float flag=0;for(mark1=0;mark1<4;mark1++){for(mark2=0;mark2<4;mark2++){for(mark3=0;mark3<4;mark3++){if(calculate_A(a,b,c,d,mark1,mark2,mark3)==24){printf("((%d%c%d)%c%d)%c%d=24\n",a,mark[mark1],b,mark[mark2],c,mark[mark3],d);flag=1;}if(calculate_B(a,b,c,d,mark1,mark2,mark3)==24){printf("(%d%c(%d%c%d))%c%d=24\n",a,mark[mark1],b,mark[mark2],c,mark[mark3],d);flag=1;}if(calculate_C(a,b,c,d,mark1,mark2,mark3)==24){printf("%d%c(%d%c(%d%c%d))=24\n",a,mark[mark1],b,mark[mark2],c,mark[mark3],d);flag=1;}if(calculate_D(a,b,c,d,mark1,mark2,mark3)==24){printf("%d%c((%d%c%d)%c%d)=24\n",a,mark[mark1],b,mark[mark2],c,mark[mark3],d);flag=1;}if(calculate_E(a,b,c,d,mark1,mark2,mark3)==24){printf("(%d%c%d)%c(%d%c%d)=24\n",a,mark[mark1],b,mark[mark2],c,mark[mark3],d);flag=1;}}}}return flag;}main(){int a,b,c,d;loop: printf("Please input 4 numbers(1~10):\n");scanf("%d%d%d%d",&a,&b,&c,&d);if((a>=1&&a<=10)&&(b>=1&&b<=10)&&(c>=1&&c<=10)&&(d>=1&&d<=10)){if(0!=get(a,b,c,d));elseprintf("Sorry!can not find!\n");goto loop;}else{printf("Input illegal(违法),please input again(1~10)\n");goto loop;}system("pause");}。

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;}。

C语言版24点游戏

C语言版24点游戏

#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<time.h>#include<windows.h>struct s_node{int date;struct s_node *next;};typedef struct s_node s_list;typedef s_list *link;link OPER=NULL;link OPND=NULL;char m;link creat(link stack) //创建空栈的函数{stack=(link)malloc(sizeof(s_list));if (!stack){printf("\nERROR");return NULL;}return stack;}link push(link stack,int value) //添加新的元素value为stack的栈顶元素{link newnode;newnode=(link)malloc(sizeof(s_list));if (!newnode){printf("\nERROR");return NULL;}newnode->date=value;newnode->next=stack;stack=newnode;return stack;}link pop(link stack) //栈顶出栈的函数,m表示出栈的栈顶元素{link top;if (stack!=NULL){top=stack;stack=stack->next;m=top->date;free(top);return stack;}elsem=-1;}int is_oper(char oper) //判断是否为算符的函数{switch (oper){case'+':case'-':case'*':case'/':case'(':case')':case'#':return 1;default:return 0;}}int compare(char q1,char q2) //比较算符优先程度的函数{if (q1=='+') //‘+’的比较{switch (q2){case '+':return 1;case '-':return 1;return 0;case '/':return 0;case '(':return 0;case ')':return 1;case '#':return 1;}}else if (q1=='-') //‘-’的比较{switch (q2){case '+':return 1;case '-':return 1;case '*':return 0;case '/':return 0;case '(':return 0;case ')':return 1;case '#':return 1;}}else if (q1=='*') //‘*’的比较{switch (q2){case '+':return 1;case '-':return 1;case '*':return 1;case '/':return 1;return 0;case ')':return 1;case '#':return 1;}}else if (q1=='/') //‘/’的比较{switch (q2){case '+':return 1;case '-':return 1;case '*':return 1;case '/':return 1;case '(':return 0;case ')':return 1;case '#':return 1;}}else if (q1=='(') //‘(’的比较{switch (q2){case '+':return 0;case '-':return 0;case '*':return 0;case '/':return 0;case '(':return 0;case ')':return 2;return -1;}}else if (q1==')') //‘)’的比较{switch (q2){case '+':return 1;case '-':return 1;case '*':return 1;case '/':return 1;case '(':return -1;case ')':return 1;case '#':return 1;}}else if (q1=='#') //‘#’的比较{switch (q2){case '+':return 0;case '-':return 0;case '*':return 0;case '/':return 0;case '(':return 0;case ')':return -1;case '#':return 2;}}}int operate(int p1,int p2,char q) //二元运算的函数,q代表运算符,p1为运算符的左边数值,p2为运算符的右边数值{int p;if (q=='+'){p=p1+p2;return p;}else if (q=='-'){p=p1-p2;return p;}else if (q=='*'){p=p1*p2;return p;}else if (q=='/'){p=p1/p2;return p;}}int main(){char c,e,u;int n,a,b,t,v;int i=0;int f1,f2,f3,f4,flag=0;int h[5]={0};OPER=creat(OPER);OPND=creat(OPND);while (1){OPER->date=0;OPND->date=0;printf("------------------------------------------------------------------------------\n\n\n");printf(" 24点游戏系统\n\n");printf(" 您得到的四个随机数\n\n");srand( (unsigned)time( NULL ) );f1=rand()%13+1;f2=rand()%13+1;f3=rand()%13+1;f4=rand()%13+1;printf(" 【%d】【%d】【%d】【%d】\n\n",f1,f2,f3,f4);printf(" 请输入表达式'#'号结束,无法解答请输入? 直接结束请输入* \n\n");printf("------------------------------------------------------------------------------\n\n\n");printf(" ");c=getchar();if (c=='?'){system("cls");getchar();continue;}else if (c=='*')break;while (OPER->date!='#'){if (flag==1)break;if (is_oper(c)){if (OPER->date==0) //判断OPER是否为空{OPER=push(OPER,c); //若为空则为第一次输入,进栈.c=getchar();continue;}else{n=compare(OPER->date,c);switch (n){case 0:{OPER=push(OPER,c);c=getchar();break;}case 1:{OPER=pop(OPER);e=m;OPND=pop(OPND);if((t!=f1)&&(t!=f2)&&(t!=f3)&&(t!=f4)&&(t!=h[0])&&(t!=h[1])&&(t!=h[2])&&(t!=h[3])&&(t!=h[4])& &(t!=h[5])){printf("\n\n-----------------------------没有使用给出的数字!-------------------------------\n\n");flag=1;}b=m;OPND=pop(OPND);if((t!=f1)&&(t!=f2)&&(t!=f3)&&(t!=f4)&&(t!=h[0])&&(t!=h[1])&&(t!=h[2])&&(t!=h[3])&&(t!=h[4])& &(t!=h[5])){printf("\n\n-----------------------------没有使用给出的数字!-------------------------------\n\n");flag=1;}a=m;t=operate(a,b,e);h[i]=t;i++;OPND=push(OPND,t);break;}case 2:{OPER=pop(OPER);c=getchar();break;}}}}else{OPND=push(OPND,c-48);c=getchar();if (is_oper(c))continue;else{OPND=pop(OPND);v=c-'0';t=m*10+v;if((t!=f1)&&(t!=f2)&&(t!=f3)&&(t!=f4)&&(t!=h[0])&&(t!=h[1])&&(t!=h[2])&&(t!=h[3])&&(t!=h[4])& &(t!=h[5])){printf("\n\n-----------------------------没有使用给出的数字!-------------------------------\n\n");flag=1;}OPND=push(OPND,t);h[i]=t;i++;c=getchar();continue;}}}if (OPND->date==24&&flag==0){printf("----------------------------------------------------------------------------\n\n\n");printf(" **【Congratulation!】** \n\n\n");printf("----------------------------------------------------------------------------\n\n\n");}else if (flag==0){printf("----------------------------------------------------------------------------\n\n\n");printf(" **【Incorrect!】** \n\n\n");printf("----------------------------------------------------------------------------\n\n\n");}system("pause");printf(" 是否继续游戏?y:继续.n:结束\n\n\n");flag=0;u=getchar();if (u=='n'){break;}else{c=getchar();system("cls");continue;}}printf("\n\n\n-----------------------------24点游戏系统-------------------------------\n\n\n");printf("\n\n\n **谢谢使用!**\n");system("pause");return 0;}。

java实现24点游戏代码

java实现24点游戏代码

java实现24点游戏代码import java.util.Arrays;import java.util.Scanner;public class Test07 {public static void main(String[] args) {Scanner scn = new Scanner(System.in);int shu[] = new int[4];for (int i = 0; i < 4; i++) {shu[i] = scn.nextInt();}// int shu[] = { 5, 9, 9, 4 };char op[] = { '+', '-', '*', '/' };A : for (int i = 0; i < 4; i++) {for (int n1 = 0; n1 < op.length; n1++) {for (int j = 0; j < 4; j++) {for (int n2 = 0; n2 < op.length; n2++) {for (int k = 0; k < 4; k++) {for (int n3 = 0; n3 < op.length; n3++) {for (int m = 0; m < 4; m++) {// 选的这4个数不能重复if (isAllTheOrgiNum(shu[i], shu[j], shu[k], shu[m], shu)) {double temp = 0;switch (op[n1]) {case '+':temp = shu[i] + shu[j];break;case '-':temp = shu[i] - shu[j];break;case '*':temp = shu[i] * shu[j];break;case '/':temp = (double) shu[i] / shu[j];break;}switch (op[n2]) {case '+':temp = temp + shu[k];break;case '-':temp = temp - shu[k];break;case '*':temp = temp * shu[k];break;case '/':temp = (double) temp / shu[k];break;}switch (op[n3]) {case '+':temp = temp + shu[m];break;case '-':temp = temp - shu[m];break;case '*':temp = temp * shu[m];break;case '/':temp = (double) temp / shu[m];break;}if (temp == 24) {System.out.println("(" + "(" + shu[i] + " " + op[n1] + " " + shu[j] + ")" + " " + op[n2] + " " + shu[k] + ")" + " " + op[n3] + " " + shu[m]); break A;}}}}}}}}}}private static boolean isAllTheOrgiNum(int i, int j, int k, int l, int[] shu) {// TODO Auto-generated method stubint[] a = { i, j, k, l };Arrays.sort(a);Arrays.sort(shu);if(a[0]==shu[0] && a[1]==shu[1] &&a[2]==shu[2] && a[3]==shu[3]) {return true;}return false;}}。

扑克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点游戏源代码

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 ( ) ;

算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("总共有种解法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#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----计算表达式的值// 算术表达式求值的算符优先算法。

// 设OPTR和&&OPND分别为运算符栈和运算数栈int result;sqstack OPTR; // 运算符栈,字符元素sqstack OPND; // 运算数栈,实数元素int c,bl,a,b,theta,top;sqlist *q,l;char *s=MyExpression;init_sq(&l);if(chang(s,&l)!=0){q=&l;initstack(&OPTR);push(&OPTR, 35);initstack (&OPND);q=q->next;c=q->num_ch;bl=q->bol;while ((c!= 35 || gettop(&OPTR)!=35)){if (bl!=1){push(&OPND, c);q=q->next;c=q->num_ch;bl=q->bol;} // 不是运算符则进栈else{top=gettop(&OPTR);switch (precede((char)top, (char)c)){case '<': // 栈顶元素优先权低push(&OPTR, c);q=q->next;c=q->num_ch;bl=q->bol;break;case '=': // 脱括号并接收下一字符pop(&OPTR, &c);q=q->next;c=q->num_ch;bl=q->bol;break;case '>': // 退栈并将运算结果入栈pop(&OPTR, &theta);pop(&OPND, &b);pop(&OPND, &a);push(&OPND, Operate(a, theta, b));break;default :printf("没有这个运算符!\n");return 0;} // switch}//else} // whileresult=gettop(&OPND);return result;}else {printf("你的输入有错误!\n");return 0;}}int randomm()//产生四个随机数{int i=0;srand((unsigned)time(NULL));for (;i<4;i++){number[0][i]=0;number[0][i]=rand();number[0][i]%=13;number[0][i]++;number[1][i]=0;}return number[2][4];}int CalcOneExpress(int expression[][2]) // 计算表达式{// 算术表达式求值的算符优先算法。

相关文档
最新文档