❶ 用C語言設計演算法完成24點游戲的計算.
這個不適合用C語言實現,撲克牌需要有圖形界面,C語言是字元界面,建議用Java。
❷ C語言算24點
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char op[3], o[5]="+-*/";
float n[4], on[10];
int used[4] = {0}, top=0, tp=0, x;
void chk(float k);
void search24(int d);
float calc(float n1, float n2, char o);
void make(int i, float p, float q, char o, int d);
int main( void )
{
printf("please input 4 card number: ");
scanf("%f%f%f%f", &n[0], &n[1], &n[2], &n[3]);
search24(0);
printf("No answer. ");
return 0;
}
void chk(float k)
{
if( (tp != 3) || ( fabs(k-24.0) > 0.000001 )) //沒有用完3個運算符或者結果不為24就退出.
return;
for(x=0; x<5; x+=2) //這樣設計是為了使3個選中的符號都可以得到輸出.
printf("%g%c%g=%g ", on[x], op[x/2], on[x+1], //分析得到的.
calc(on[x], on[x+1], op[x/2]));
system("pause");
exit(0);
}
float calc(float n1, float n2, char o)
{
switch(o){
case '+': return (n1+n2);
case '-': return (n1-n2);
case '*': return (n1*n2);
case '/': return (n1/n2);
default: exit(0);
}
}
void make(int i, float p, float q, char o, int d)
{
if(fabs(q)>0.000001 || o!='/') //除數不為0,或者為0的時候不能為除數.
n[i] = calc(p, q, o);
op[tp++] = o;
chk(n[i]);
search24(d+1);
tp--; //因為是全是全局變數,所以在做試驗性的循環遞歸問題時,如果失敗,要在遞歸函數後面重新恢復回原來的值
}
void search24(int d)
{
int i, j, k;
float p, q;
if(d>=3) //控制遞歸深度,就是運算符的輸出個數.
return;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
if( (i!=j)&& (used[i]+used[j] == 0) ) //i!=j是防止重復,(used[i]+used[j] == 0)是防止又再匹配已經用過的j,
//但是i可以新來.
{
used[j] = 1; //j得到匹配之後,賦值為1,表示已經使用
p=n[i];
q=n[j];
on[top++] = p;
on[top++] = q;
for(k=0; k<4; k++) //運算符的循環試用.
make(i, p, q, o[k], d);
n[i] = p; //因為是全是全局變數,所以在做試驗性的循環遞歸問題時,
used[j] = 0; //如果失敗,要在遞歸函數後面重新恢復回原來的值
top -= 2; //
}
}
出處:http://blog.sina.com.cn/s/blog_491de9d60100d5er.html
❸ 24點游戲C語言
#include<cstdlib>#include<iostream>#include<ctime>using namespace std;
class CCard{private: int m_Pip[5];//一共五張牌 int m_Number;//發了多少張牌 int m_Dollar;//賭本 int m_Gamble;//賭注 int m_Win;//贏局數 int m_Lose;//輸局數 int m_Draw;//平局數public: CCard();//構造函數。 void FirstPlayTwo();//最初的兩張牌 int GetNumber();//返回牌張 int GetPip();//返回點數 void DisplayPip();//依次全部顯示牌面的點數 void DisplayPip(int);//除了第一張牌,依次顯示全部牌面點數(針對計算機牌的顯示) void TurnPlay();//出一張牌。 void Win();//贏了計算賭注 void Lose();//輸了 void Draw();//平局 int SetGamble(int);//設置賭本,賭本不夠返回-1 int GetMoney();//返回錢數 void DisplayInfo();//列印必要的信息 int GetCurrentCard();//返回當前的牌點};
CCard::GetNumber(){ return m_Number;}
CCard::CCard()//構造函數,初始化{ m_Number = 0; m_Dollar = 100;//初始賭注為100美元 for(int i=0;i<5;i++) m_Pip[i] = 0; m_Gamble = 0; m_Win = m_Lose = m_Draw = 0;}
int CCard::GetMoney(){ return m_Dollar;}
void CCard::DisplayInfo()//列印必要的信息{ cout<<"\n\n\n\t\t\t您一共玩了"<<m_Win+m_Lose+m_Draw<<"局 "<<"贏了"<<m_Win<<"局 "<<"輸了"<<m_Lose<<"局 "<<"平局"<<m_Draw<<"次。"<<endl; cout<<"\n\t\t\t\t您的賭本共計有$"<<m_Draw<<"。\n"<<endl;}
int CCard::SetGamble(int gamble){ if(gamble<0) { cout<<"\n輸入金額有誤"<<endl; return -1; } if(m_Dollar-gamble<0) { cout<<"\n金額不足"<<endl; return -1; } else m_Gamble = gamble; m_Dollar -= gamble; return 0;}
void CCard::FirstPlayTwo()//最初兩張牌{ m_Pip[0] = rand()%13+1; m_Pip[1] = rand()%13+1; m_Number = 2;}
int CCard::GetPip(){ int SumPip = 0; for(int i=0;i<m_Number;i++) { SumPip += m_Pip[i]; } return SumPip;}
void CCard::DisplayPip(){ int i; for(i=0;i<m_Number;i++) { cout<<m_Pip[i]<<'\t'; } cout<<endl;}
void CCard::TurnPlay(){ m_Number++; m_Pip[m_Number-1] = rand()%13+1;}
void CCard::Win(){ cout<<"贏家牌面:"; DisplayPip(); cout<<"\n牌面點數:"<<GetPip()<<endl; m_Dollar = m_Dollar + 2 * m_Gamble; m_Win++; cout<<"\n賭本:$"<<m_Dollar<<" 贏了"<<m_Win<<"次 "<<"輸了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl; cout<<endl;}
void CCard::Lose(){ m_Lose++; cout<<"\n輸家的牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\t\t\t\t\t\t\t\t暴了!"<<endl; else cout<<"牌面點數:"<<GetPip()<<endl; cout<<"\n賭本:$"<<m_Dollar<<" 贏了"<<m_Win<<"次 "<<"輸了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}
void CCard::Draw(){ m_Draw++; m_Dollar += m_Gamble; cout<<"\n平局牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\n暴了!"<<endl; else cout<<"牌面點數:"<<GetPip()<<endl; cout<<"賭本:$"<<m_Dollar<<" 贏了"<<m_Win<<"次 "<<"輸了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}
void DisplayRule(void){ cout<<endl<<endl; cout<<"\t※※※※※※※※※※歡迎進入21點游戲世界!※※※※※※※※※※\n\n"; cout<<"\t\t 游戲規則:\n"; cout<<endl; cout<<"\t\t 1.玩家最多可以要5張牌;\n"; cout<<endl; cout<<"\t\t 2.如果牌點數的總數超過21點則暴點,自動判數;\n"; cout<<endl; cout<<"\t\t 3.贏家可得雙倍的賭注;\n"; cout<<endl; cout<<"\t\t 4.計算機方在大於等於16點時不再要牌。\n"; cout<<endl; cout<<"\t※※※※※※※※※※※※※ 祝您好運! ※※※※※※※※※※\n"; cout<<endl<<endl;}
void Judge(CCard &cpu,CCard &player){ cout<<endl; if((cpu.GetPip()>21&&player.GetPip()>21)||cpu.GetPip()==player.GetPip()) { cout<<"\n\n\t\t\t\t\t\t\t\t平局!\n"; cout<<"計算機數據:\t"; cpu.DisplayPip(); cout<<"牌面點數:"<<cpu.GetPip()<<endl; cout<<"\n您的數據:\t"; player.Draw(); cout<<endl; } else if((cpu.GetPip()>21)||(player.GetPip()>cpu.GetPip()&&player.GetPip()<=21)) { cout<<"\n\n\n\t\t\t\t\t\t\t\t恭喜您贏了!\n\n"; cout<<"計算機數據:\t"; cpu.DisplayPip(); cout<<"牌面點數:"<<cpu.GetPip()<<endl; cout<<"\n您的數據:\t"; player.Win(); cout<<endl; } else { cout<<"\n\n\t\t\t\t\t\t\t\t很遺憾您輸了!\n"; cout<<"計算機數據:\t"; cpu.DisplayPip(); cout<<"牌面點數:"<<cpu.GetPip()<<endl; cout<<"\n您的數據:\t"; player.Lose(); cout<<endl; }}void CCard::DisplayPip(int n){ int i; cout<<"[*]"<<'\t'; for(i=1;i<m_Number;i++) cout<<m_Pip[i]<<'\t'; cout<<endl;}
void PlayTurn(CCard &cpu,CCard & player)//玩一局{ char chChoice; int blCpu = 1;//判斷是否要牌 int blPlayer = 1; cpu.FirstPlayTwo();//計算機和玩家各要兩張牌 player.FirstPlayTwo(); do { cout<<"\n您的牌點為:\t"; player.DisplayPip(); cout<<"\n"; cout<<"您的牌面點數是:"<<player.GetPip()<<endl; cout<<"\n計算機的牌點為:\t"; cpu.DisplayPip(1); if(blPlayer) { cout<<"\n\n\t您是否繼續要牌(Y/N)?\t\t\t"; cin>>chChoice; if((chChoice == 'Y'||chChoice == 'y')) { if(player.GetNumber()<5) { player.TurnPlay(); cout<<"\n您要的這張牌是:"<<player.GetPip()<<endl; if(player.GetPip()>21) blPlayer = 0; } else { cout<<"對不起,您已經要了5張牌,不能再要牌了!"; blPlayer = 0; } } if(chChoice=='n'||chChoice=='N') blPlayer = 0; } if(cpu.GetPip()<16&&cpu.GetNumber()<5) { cpu.TurnPlay(); cout<<"\n計算機要牌,牌點是:"<<cpu.GetPip()<<endl; } else blCpu = 0; if(blCpu&&player.GetNumber()<5&&player.GetPip()<21) blPlayer = 1; }while(blCpu||blPlayer); Judge(cpu,player); return ;}
int main(){ srand((unsigned)time(NULL));//初始化隨機數種子 CCard cpu,player; int blLogic; int nMoney;// DisplayRule();// char chChoice; cout<<"是否現在開始游戲(Y/N)?\t\t"; cin>>chChoice; while(chChoice=='Y'||chChoice=='y') { do { cout<<endl; cout<<"\t\t\t您現在有的賭本:$"<<player.GetMoney(); cout<<"\n\n請下注(賭注不能超過賭本);"; cin>>nMoney; blLogic = player.SetGamble(nMoney); if(blLogic) cout<<"您的賭本不夠,請重新下注!\n"; }while(blLogic); PlayTurn(cpu,player);// cout<<"是否繼續21點游戲(Y/N)?\t\t\t"; cin>>chChoice; } player.DisplayInfo(); cout<<"\t\t\t您的選擇是明智的,賭博有礙家庭和睦!\n"; cout<<"\n\n\t\t\t\t歡迎再次使用此程序!"<<endl<<endl<<endl;
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點的演算法
下面是我自己寫的一個程序:
我的解法是把這個問題分解成了兩個子問題,首先求出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)
主程序為了簡化,省去了對輸入的檢查,樓主可以自己添加。
❻ C語言24點游戲
#include <stdio.h>
#include <math.h>
double num[4];//存儲4個數字
double pre = 1E-6; //精度
int go(int n)
{
int i,j;
if(n==1)
{
if(fabs(num[0]-24)<1E-6)
return 1;
else
return 0;
}
else
{
for(i=0;i<n-1;i++)
{
double a=num[i];
double b=num[i+1];
for(j=i+1;j<n-1;j++)
{
num[j]=num[j+1];
}
num[i]=a+b;
if(go(n-1)) return 1;
num[i]=a-b;
if(go(n-1)) return 1;
num[i]=a*b;
if(go(n-1)) return 1;
if(b!=0)
{
num[i]=a/b;
if(go(n-1)) return 1;
}
for(j=n-1;j>i+1;j--)
{
num[j]=num[j-1];
}
num[i+1]=b;
num[i]=a;
}
}
return 0;
}
int main()
{
double sum;
int i;
do
{
sum=0;
for(i=0;i<4;i++)
{
scanf("%lf",&num[i]);
sum+=num[i];
}
if(sum>1)
{
if(go(4))
printf("YES\n");
else
printf("NO\n");
}
}while(sum>1);
return 0;
}
如上可以實現。
❼ 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點游戲演算法
#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;
}