24點c語言程序
{
printf("%-5d%-8s%-6s%-13s%-15s%-15s\n",per[i-1].score,per[i-1].name,per[i-1].age,per[i-1].num,per[i-1].adds,per[i-1].email);
if(i>1&&i%10==0)
{
printf("\t-----------------------------------\n");
printf("\t");
system("pause");
printf("\t-----------------------------------\n");
}
}
⑵ 24點 C語言程序
在網上找了個代碼,我改了下,符合你的三個要求了。
#include<stdio.h>
double
fun(double
a1,double
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
main()
{int
i,j,k,l,n,m,r,save[4],flg=1;
double
num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111;
char
sign[5]="+-*/";
printf("輸入四個數:");
for(i=0;i<4;i++)
{scanf("%lf",num+i);
save[i]=num[i];if(save[i]>13)flg=0;}
if(flg)
{
flg=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
{for(k=0;k<4;k++)
if(k!=i&&k!=j)
{for(l=0;l<4;l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0;n<4;n++)
for(m=0;m<4;m++)
for(r=0;r<4;r++)
{tem1=fun(num[i],num[j],n);
tem2=fun(tem1,num[k],m);
tem3=fun(tem2,num[l],r);
if(tem3==24.0)
{printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);return;}
else
if(tem3==-24.0)
{printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l]);return;}
else
if(tem3==1.0/24.0)
{printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]);return;}
else
if(tem3==-1.0/24.0)
{printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]);return;}
else
{tem1=fun(num[i],num[j],n);
tem2=fun(num[k],num[l],r);
tem3=fun(tem1,tem2,m);
if(tem3==24.0)
{printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);return;}
}
}
}
}
}
}
if(!flg)
printf("NO
ANSWER\n");
}
⑶ C語言程序設計24點游戲,能算出24的運演算法則的代碼,很急
#include<iostream>
#include<math.h>
using namespace std;
const double MIN=1E-6;
void Print(int *Rank,double *FourNum)
{
for(int i=0;i<4;i++)
cout<<FourNum[Rank[i]]<<" ";
cout<<endl;
}
void Calculate_24(int *Rank,int *FourNum,char *Oper,int i,int j,int k,bool &def)
{
double res=0;
switch(i)
{
case 0:
res=FourNum[Rank[0]]+FourNum[Rank[1]];
break;
case 1:
res=FourNum[Rank[0]]-FourNum[Rank[1]];
break;
case 2:
res=FourNum[Rank[0]]*FourNum[Rank[1]];
break;
case 3:
res=FourNum[Rank[0]]/FourNum[Rank[1]];
break;
}
switch(j)
{
case 0:
res=res+FourNum[Rank[2]];
break;
case 1:
res=res-FourNum[Rank[2]];
break;
case 2:
res=res*FourNum[Rank[2]];
break;
case 3:
res=res/FourNum[Rank[2]];
break;
}
switch(k)
{
case 0:
res=res+FourNum[Rank[3]];
break;
case 1:
res=res-FourNum[Rank[3]];
break;
case 2:
res=res*FourNum[Rank[3]];
break;
case 3:
res=res/FourNum[Rank[3]];
break;
}
if(fabs(res-24)>MIN)
return;
else
{
def=true;
for(int num=1;num<=7;num++)
{
switch(num)
{
case 1:
cout<<FourNum[Rank[0]];
break;
case 3:
cout<<FourNum[Rank[1]];
break;
case 5:
cout<<FourNum[Rank[2]];
break;
case 7:
cout<<FourNum[Rank[3]];
break;
case 2:
cout<<Oper[i];
break;
case 4:
cout<<Oper[j];
break;
case 6:
cout<<Oper[k];
break;
}
}
cout<<endl;
}
}
void SearchTree(int Depth,int *Rank,int *FourNum,char *Oper,bool &def)
{
int i,j,k;
if(Depth==4)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
Calculate_24(Rank,FourNum,Oper,i,j,k,def);
}
else
{
for(i=0;i<4;i++)
{
int Remember=0;
for(j=0;j<Depth;j++)
{
if(Rank[j]==i)
Remember=1;
}
if(Remember)
continue;
Rank[Depth]=i;
SearchTree(Depth+1,Rank,FourNum,Oper,def);
}
}
}
int main()
{
int a[4],b[4],time;
char c[4]={'+','-','*','/'};
bool def=false;
cin>>time;
while(time--)
{
for(int i=0;i<4;i++)//輸入測試數據
cin>>a[i];
cout<<"所有可能的結果:"<<endl;
SearchTree(0,b,a,c,def);
if(def==false)
cout<<"No"<<endl;
}
return 0;
}
⑷ C語言24點的演算法
下面是我自己寫的一個程序:
我的解法是把這個問題分解成了兩個子問題,首先求出4個數字的無重復全排列,放到一個數組裡面,再對沒一個排列情況,從頭到尾窮舉所有的四則運算情況。注意到除法是特殊的,我用x/y表示x除以y,用x|y表示x分之y。注意到,如果窮舉的解得到-24的話,只需要把有減法的地方調換一下順序就可以了,代碼如下
/***********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int index[4]={0,1,2,3};//used to generate subscription collection
int sub[4]; //used in p() only
float f[4]={8.0f,3.0f,3.0f,8.0f};//the 24 point numbers
float fs[24][4];//all possible permutaions of f
float tmp[4]; //used for buf
int g_number=0; //number of permutations
float RES[4];
char op[3];
void p(int idx){//求全排列的函數
if(idx==4){
for(int i=0;i<4;++i){tmp[i]=f[sub[i]];}
for(int g=0;g<g_number;++g){if(memcmp(fs[g],tmp,sizeof(float)*4)==0)return;}
for(int i=0;i<4;++i){fs[g_number][i]=f[sub[i]];}
g_number++;
return;
}
for(int i=0;i<4;++i){//make subscription collections
bool pflag=false;
for(int j=0;j<idx;++j){if(sub[j]==i)pflag=true;}
if(pflag==true)continue;
sub[idx]=index[i];
p(idx+1);
}
}
void solve(int L){//對某個排列,遞歸求所有四則運算的結果,找到就退出
if(L==3){
if(fabs(fabs(RES[L])-24.0f)<0.01f){
printf("Found solution,RES=%f,((%d%c%d)%c%d)%c%d\n",RES[L],
(int)f[0],op[0],
(int)f[1],op[1],
(int)f[2],op[2],
(int)f[3]);
exit(0);
}
return;
}
for(int j=0;j<5;++j){//j judges for operators
if(j==0){RES[L+1]=RES[L]+tmp[L+1];op[L]='+';solve(L+1);}
if(j==1){RES[L+1]=RES[L]-tmp[L+1];op[L]='-';solve(L+1);}
if(j==2){RES[L+1]=RES[L]*tmp[L+1];op[L]='*';solve(L+1);}
if(j==3&&tmp[L+1]!=0)
{RES[L+1]=RES[L]/tmp[L+1];op[L]='/';solve(L+1);}
if(j==4&&RES[L+1]!=0)
{RES[L+1]=tmp[L+1]/RES[L];op[L]='|';solve(L+1);}
}
}
int main(int argc,char* argv[]){//should avoid 0
f[0]=atoi(argv[1]);
f[1]=atoi(argv[2]);
f[2]=atoi(argv[3]);
f[3]=atoi(argv[4]);
p(0);
for(int i=0;i<g_number;++i){
memcpy(tmp,fs[i],sizeof(float)*4);
RES[0]=tmp[0];
for(int t=0;t<4;++t){ printf("%d,",(int)tmp[t]); }
printf("\n");
solve(0);
}
printf("Found no solution :( \n");
return 0;
}
----------編譯運行,運行時的參數就是4個數字
g++ p.cpp && ./a.out 1 5 5 5
1,5,5,5,
Found solution,RES=-24.000000,((1/5)-5)*5
g++ p.cpp && ./a.out 8 3 3 8
8,3,3,8,
Found solution,RES=-24.000006,((8/3)-3)|8
上面這個解寫出來就是
8
--------- = 24
3-(8/3)
主程序為了簡化,省去了對輸入的檢查,樓主可以自己添加。
⑸ 計算24點 C語言
//24點分析(窮舉法)
//
#include
#include
#include
typedef struct D_NODE
{
int data;
struct D_NODE *left,*right;
} NODE24;
NODE24 head;
int res=24;
int maketree(int *src,int *i, NODE24 *p)
{
int d=src[*i];
(*i)++;
switch (d)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=d;
maketree(src,i,p->left);
maketree(src,i,p->right);
break;
default:
p->data=d;
p->left=NULL;
p->right=NULL;
}
return 0;
}
int cmaketree(NODE24 *p)
{
int c;
c=getch();
putchar(c);
switch (c)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=c;
cmaketree(p->left);
cmaketree(p->right);
break;
default:
p->data=c-'0';
p->left=NULL;
p->right=NULL;
}
return 0;
}
int work(struct D_NODE *d)
{
int res=0;
if (d->left==NULL&&d->right==NULL)
res=d->data;
else
{
int a,b;
a=work(d->left);
b=work(d->right);
switch (d->data)
{
case '+':
res=a+b;//work(d->left)+work(d->right);
break;
case '-':
res=a-b;//work(d->left)-work(d->right);
break;
case '*':
res=a*b;//work(d->left)*work(d->right);
break;
case '/':
if (b!=0)
res=(a%b==0)?a/b:-79;
else
res=-79;
//res=work(d->right)?work(d->left)/work(d->right):-79;
break;
}
}
return res;
}
int destroy(struct D_NODE *d)
{
if (d->left==NULL&&d->right==NULL)
delete d;
else
{
destroy(d->left);
d->left=NULL;
destroy(d->right);
d->right=NULL;
if (d != &head)
delete d;
}
return 0;
}
int show(struct D_NODE *d)
{
if (d->left==NULL && d->right==NULL)
printf("%d",d->data);
else
{
printf("(");
show(d->left);
printf("%c",d->data);
show(d->right);
printf(")");
}
return 0;
}
/* int input()
{
//int buf[30]=,idx=0;
//maketree(buf,&idx,&head);
int buf[20],idx=0;
printf("\nPlease Input:");
for (idx=0;idx<20;idx++)
{
buf[idx]=getch();
printf("%c",buf[idx]);
}
idx=0;
maketree(buf,&idx,&head);
return 0;
} */
#define test(p1,p2,p3,p4,p5,p6,p7) {exp[0]=(p1),exp[1]=(p2), \
exp[2]=(p3),exp[3]=(p4),exp[4]=(p5),exp[5]=(p6),exp[6]=(p7); \
idx=0; \
maketree(exp,&idx,&head); \
if (work(&head)==res) \
{ \
found++;printf("%5d: ",found);show(&head);\
if (!(found%3)) printf("\n");\
} \
destroy(&head);\
}
// printf("%d,%d,%d,%d,%d,%d,%d\n",p1,p2,p3,p4,p5,p6,p7); \
int test24()
{
int num[4],opc[4]=,exp[20];
int i1,i2,i3,i4,ic1,ic2,ic3,idx,found=0;
char prompt[]="24點游戲分析\n易華衛 12/17/2000\n";
printf("%s",prompt);
for (i1=0;i1<4;i1++)
{
printf("請輸入第%d個數字: ",i1+1);
scanf("%d",num+i1);
//num[i1]=num[i1]%13+1;
}
printf("\n你已經輸入了:%d,%d,%d,%d四個數字!\n",num[0],num[1],num[2],num[3]);
printf("\n請輸入要計算的結果值,(當然二十四點就輸入24啦!):");
scanf("%d",&res);
printf("OK! 按任意鍵就可以開始了!\n");
getch();
for (i1=0;i1<4;i1++)
for (i2=0;i2<4;i2++)
if (i2!=i1)
for (i3=0;i3<4;i3++)
if (i3!=i1&&i3!=i2)
for (i4=0;i4<4;i4++)
if (i4!=i1&&i4!=i2&&i4!=i3)
for (ic1=0;ic1<4;ic1++)
for (ic2=0;ic2<4;ic2++)
for (ic3=0;ic3<4;ic3++)
{
test(opc[ic1],opc[ic2],opc[ic3],num[i1],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],num[i2],opc[ic3],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],num[i2],opc[ic3],num[i3],num[i4]);
}
printf("\n共找到了 %d 條正確的計算方法!(很抱歉,我沒有處理交換率*^_^*)\n",found);
return 0;
}
main()
{
// fflush(stdin);
// input();
// cmaketree(&head);
// printf("\n=%d\n",work(&head));
test24();
return 0;
}
⑹ 求算24點的C語言程序
#include <stdio.h>
#include <string.h>
int c24;
char expr3[80];
char expr4[80];
void count2( int a, int b )
{
if ( a + b == 24 )
{
c24 ++;
printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b );
}
if ( a * b == 24 )
{
c24 ++;
printf( "%s\t%s\t%d*%d=24\n", expr4, expr3, a, b );
}
if ( a - b == 24 )
{
c24 ++;
printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, a, b );
}
if ( b - a == 24 )
{
c24 ++;
printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, b, a );
}
if ( (b!=0) && (a==24*b) )
{
c24 ++;
printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, a, b );
}
if ( (a!=0) && (b==24*a) )
{
c24 ++;
printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, b, a );
}
}
void count3( int a, int b, int c )
{
int dd[3];
int i,j,k,nd;
dd[0] = a;
dd[1] = b;
dd[2] = c;
for ( i = 0; i < 3; i++ )
for ( j = i+1; j < 3; j++ )
{
k = 3 - i - j;
nd = dd[i] + dd[j];
sprintf( expr3, "%d+%d=%d", dd[i], dd[j], nd );
count2( nd, dd[k] );
nd = dd[i] * dd[j];
sprintf( expr3, "%d*%d=%d", dd[i], dd[j], nd );
count2( nd, dd[k] );
if ( dd[i]>dd[j] )
{
nd = dd[i] - dd[j];
sprintf( expr3, "%d-%d=%d", dd[i], dd[j], nd );
}
else
{
nd = dd[j] - dd[i];
sprintf( expr3, "%d-%d=%d", dd[j], dd[i], nd );
}
count2( nd, dd[k] );
if ( (dd[j]!=0) && (dd[i]%dd[j]==0) )
{
nd = dd[i] / dd[j];
sprintf( expr3, "%d/%d=%d", dd[i], dd[j], nd );
count2( nd, dd[k] );
}
else if ( (dd[i]!=0) && (dd[j]%dd[i]==0) )
{
nd = dd[j] / dd[i];
sprintf( expr3, "%d/%d=%d", dd[j], dd[i], nd );
count2( nd, dd[k] );
}
}
}
void count4( int a, int b, int c, int d )
{
int dd[4];
int i,j,k,m,nd;
dd[0] = a;
dd[1] = b;
dd[2] = c;
dd[3] = d;
for ( i = 0; i < 4; i++ )
for ( j = i+1; j < 4; j++ )
{
for ( k = 0; (k==i) || (k==j); k++ ) ;
m = 6 - i - j - k;
nd = dd[i] + dd[j];
sprintf( expr4, "%d+%d=%d", dd[i], dd[j], nd );
count3( nd, dd[k], dd[m] );
nd = dd[i] * dd[j];
sprintf( expr4, "%d*%d=%d", dd[i], dd[j], nd );
count3( nd, dd[k], dd[m] );
if ( dd[i]>dd[j] )
{
nd = dd[i] - dd[j];
sprintf( expr4, "%d-%d=%d", dd[i], dd[j], nd );
}
else
{
nd = dd[j] - dd[i];
sprintf( expr4, "%d-%d=%d", dd[j], dd[i], nd );
}
count3( nd, dd[k], dd[m] );
if ( (dd[j]!=0) && (dd[i]%dd[j]==0) )
{
nd = dd[i] / dd[j];
sprintf( expr4, "%d/%d=%d", dd[i], dd[j], nd );
count3( nd, dd[k], dd[m] );
}
else if ( (dd[i]!=0) && (dd[j]%dd[i]==0) )
{
nd = dd[j] / dd[i];
sprintf( expr4, "%d/%d=%d", dd[j], dd[i], nd );
count3( nd, dd[k], dd[m] );
}
}
}
main()
{
int a,b,c,d;
printf( "Input 4 Number for Count 24: " );
scanf( "%d%d%d%d", &a, &b, &c, &d );
c24 = 0;
count4( a, b, c, d );
if ( c24==0 )
printf( "No Answer\n" );
/***/
printf("End of C Program, Press Enter.\n");
getchar();getchar();
return 0;
}
⑺ 如何用C語言編寫一個24點的游戲
(已經調試)
#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"*/
else
text1("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; /*跳出循環*/
else
w=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; /*跳出循環*/
else
w=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*/
else
return 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*/
else
return 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 ; /*返回*/
}
⑻ 用c語言編寫,關於24點的程序
#include<stdio.h>
double fun(double a1,double 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 main()
{int i,j,k,l,n,m,r,save[4];
double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111;
char sign[5]="+-*/";
printf("input 4 numbers:");
for(i=0;i<4;i++)
{scanf("%lf",num+i); save[i]=num[i];}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
{for(k=0;k<4;k++)
if(k!=i&&k!=j)
{for(l=0;l<4;l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0;n<4;n++)
for(m=0;m<4;m++)
for(r=0;r<4;r++)
{tem1=fun(num[i],num[j],n);
tem2=fun(tem1,num[k],m);
tem3=fun(tem2,num[l],r);
if(tem3==24.0)printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);
else if(tem3==-24.0)printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l]);
else if(tem3==1.0/24.0)printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]);
else if(tem3==-1.0/24.0)printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]);
else
{tem1=fun(num[i],num[j],n);
tem2=fun(num[k],num[l],r);
tem3=fun(tem1,tem2,m);
if(tem3==24.0) printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);
}
}
}
}
}
}
⑼ C語言程序 算24點
#include<iostream>
using namespace std;
struct operation
{
int x;
int y;
int o;
}op[3];
bool co=false;
void g(int a[],int b[],int n,int i,int j)
{
int x,y;
for(x=0,y=1;x<n;x++)
{
if(x==i||x==j)continue;
b[y++]=a[x];
}
op[4-n].x=a[i];
op[4-n].y=a[j];
}
void f(int a[],int n)
{
int i,j,t,b[4];
if(n==1&&a[0]==24)co=true;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
for(t=1;t<=6;t++)
{
switch(t)
{
case 1:g(a,b,n,i,j);
b[0]=a[i]+a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 2:g(a,b,n,i,j);
b[0]=a[i]-a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 3:g(a,b,n,i,j);
b[0]=a[i]*a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 4:
if(a[j]!=0&&a[i]%a[j]==0)
{
g(a,b,n,i,j);
b[0]=a[i]/a[j];
op[4-n].o=t;
f(b,n-1);
if(co)return;
}
break;
case 5:g(a,b,n,i,j);
b[0]=a[j]-a[i];
op[4-n].o=t;
f(b,n-1);
if(co)return;
break;
case 6:
if(a[i]!=0&&a[j]%a[i]==0&&a[j]>a[i])
{
g(a,b,n,i,j);
b[0]=a[j]/a[i];
op[4-n].o=t;
f(b,n-1);
if(co)return;
}
break;
}
}
}
}
}
void h(int a[],int n,int x,int y,int h)
{
int i,j,b[4];
bool bx=false,by=false;
for(i=0,j=0;i<n;i++)
{
if(a[i]==x&&bx==false)
{
bx=true;
continue;
}
if(a[i]==y&&by==false)
{
by=true;
continue;
}
b[j++]=a[i];
}
for(i=0;i<n-1;i++)a[i+1]=b[i];
a[0]=h;
}
void main()
{
int a[4],i,j;
cout<<"輸入4個整數:";
for(i=0;i<4;i++)cin>>a[i];
f(a,4);
if(co)
{
for(i=0;i<3;i++)
{
for(j=0;j<4-i;j++)cout<<a[j]<<" ";
cout<<"\n運算 ";
switch(op[i].o)
{
case 1:cout<<op[i].x<<"+"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x+op[i].y);
break;
case 2:cout<<op[i].x<<"-"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x-op[i].y);
break;
case 3:cout<<op[i].x<<"*"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x*op[i].y);
break;
case 4:cout<<op[i].x<<"/"<<op[i].y<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].x/op[i].y);
break;
case 5:cout<<op[i].y<<"-"<<op[i].x<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].y-op[i].x);
break;
case 6:cout<<op[i].y<<"/"<<op[i].x<<endl;
h(a,4-i,op[i].x,op[i].y,op[i].y/op[i].x);
break;
}
}
cout<<"24"<<endl;
}
else cout<<"不能算出24"<<endl;
}
⑽ 求一個C語言24點的程序,一定要能給我解釋的
#ifndef_24_H
#define_24_H
//隨機取4張牌
voidGivePuzzle(char*buf);
//洗牌
voidshuffle(char*buf);
//紙牌數值轉換
intGetCardValue(intc);
//獲取操作符
charGetOper(intn);
//對表達式求值
doubleMyCalcu(doubleop1,doubleop2,intoper);
//輸出
voidMakeAnswer(char*answer,inttype,char*question,int*oper);
//解題
intTestResolve(char*question,int*oper,char*answer);
intTryResolve(char*question,char*answer);
#endif
#include"24.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<time.h>
//隨機取4張牌
voidGivePuzzle(char*buf)
{
charcard[]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
inti;
for(i=0;i<4;i++){
buf[i]=card[rand()%13];
}
}
//洗牌
voidshuffle(char*buf)
{
inti;
chart;
for(i=0;i<5;i++)
{
intk=rand()%4;
t=buf[k];
buf[k]=buf[0];
buf[0]=t;
}
}
//紙牌數值轉換
intGetCardValue(intc)
{
if(c=='T')return10;
if(c>='0'&&c<='9')returnc-'0';
return1;
}
//獲取操作符
charGetOper(intn)
{
switch(n)
{
case0:
return'+';
case1:
return'-';
case2:
return'*';
case3:
return'/';
}
return'';
}
//對表達式求值
doubleMyCalcu(doubleop1,doubleop2,intoper)
{
switch(oper)
{
case0:
returnop1+op2;
case1:
returnop1-op2;
case2:
returnop1*op2;
case3:
if(fabs(op2)>0.0001)
returnop1/op2;
else
return100000;
}
return0;
}
//輸出
voidMakeAnswer(char*answer,inttype,char*question,int*oper)
{
charp[4][3];
inti;
for(i=0;i<4;i++)
{
if(question[i]=='T')
strcpy(p[i],"10");
else
sprintf(p[i],"%c",question[i]);
}
switch(type)
{
case0:
sprintf(answer,"%s%c(%s%c(%s%c%s))",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
break;//A*(B*(c*D))
case1:
sprintf(answer,"%s%c((%s%c%s)%c%s)",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
break;//A*((B*C)*D)
case2:
sprintf(answer,"(%s%c%s)%c(%s%c%s)",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
break;//(A*B)*(C*D)
case3:
sprintf(answer,"((%s%c%s)%c%s)%c%s",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
break;//((A*B)*C)*D
case4:
sprintf(answer,"(%s%c(%s%c%s))%c%s",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
break;//(A*(B*C))*D
}
}
//解題
intTestResolve(char*question,int*oper,char*answer)
{
//等待考生完成
//question=charbuf1[4]
//answer=charbuf2[30]
doubletemp,temp1;
inta=0;
temp=MyCalcu(GetCardValue(question[2]),GetCardValue(question[3]),oper[2]);
temp=MyCalcu(GetCardValue(question[1]),temp,oper[1]);
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0]);
if(temp==24)
{
MakeAnswer(answer,0,question,oper);
a=1;
}
temp=MyCalcu(GetCardValue(question[1]),GetCardValue(question[2]),oper[1]);
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2]);
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0]);
if(temp==24l)
{
MakeAnswer(answer,1,question,oper);
a=1;
}
temp=MyCalcu(GetCardValue(question[0]),GetCardValue(question[1]),oper[0]);
temp1=MyCalcu(GetCardValue(question[2]),GetCardValue(question[3]),oper[2]);
temp=MyCalcu(temp,temp1,oper[1]);
if(temp==24l)
{
MakeAnswer(answer,2,question,oper);
a=1;
}
temp=MyCalcu(GetCardValue(question[0]),GetCardValue(question[1]),oper[0]);
temp=MyCalcu(temp,GetCardValue(question[2]),oper[1]);
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2]);
if(temp==24l)
{
MakeAnswer(answer,3,question,oper);
a=1;
}
temp=MyCalcu(GetCardValue(question[1]),GetCardValue(question[2]),oper[1]);
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0]);
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2]);
if(temp==24l)
{
MakeAnswer(answer,4,question,oper);
a=1;
}
returna;
}
//解題
intTryResolve(char*question,char*answer)
{
inti,j;
intoper[3];//存儲運算符,0:加法1:減法2:乘法3:除法
for(i=0;i<1000*1000;i++)
{
//打亂紙牌順序
shuffle(question);
//隨機產生運算符
for(j=0;j<3;j++)
oper[j]=rand()%4;
if(TestResolve(question,oper,answer))
return1;
}
return0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<time.h>
intmain()
{
intj;
charbuf1[4];//題目
charbuf2[30],ch;//解答
//初始化隨機種子
srand((unsigned)time(NULL));
printf("*************************** ");
printf("計算24 ");
printf("AJQK均按1計算,其它按牌點計算 ");
printf("目標是:通過四則運算組合出結果:24 ");
printf("*************************** ");
for(;;)
{
GivePuzzle(buf1);//出題
printf("題目:");
for(j=0;j<4;j++)
{
if(buf1[j]=='T')//、、、、初始化buf1[];
printf("10");
else
printf("%6c",buf1[j]);
}
printf(" ");
system("pause");
if(TryResolve(buf1,buf2))//解題
{
printf("參考:%s ",buf2);
}
else
printf("可能是無解… ");
printf("按任意鍵出下一題目,x鍵退出… ");
ch=getchar();
if(ch=='x'||ch=='X')
break;
}
return0;
}