您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)C++怎么實(shí)現(xiàn)24點(diǎn)游戲,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內(nèi)容如下
隨機(jī)生成4個(gè)代表?yè)淇伺婆泼娴臄?shù)字字母,程序自動(dòng)列出所有可能算出24的表達(dá)式,用擅長(zhǎng)的語(yǔ)言(C/C++/Java或其他均可)實(shí)現(xiàn)程序解決問(wèn)題。
程序風(fēng)格良好(使用自定義注釋模板)
列出表達(dá)式無(wú)重復(fù)。
以下為源代碼
#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] << "。" << endl; for (i = 0; i<4; i++) { if (buf[i] == 'A') nums[i] = 1; else if(buf[i] == '2') nums[i] = 2; else if (buf[i] == '3') nums[i] = 3; else if (buf[i] == '4') nums[i] = 4; else if (buf[i] == '5') nums[i] = 5; else if (buf[i] == '6') nums[i] = 6; else if (buf[i] == '7') nums[i] = 7; else if (buf[i] == '8') nums[i] = 8; else if (buf[i] == '9') nums[i] = 9; else if (buf[i] == '10') nums[i] = 10; else if (buf[i] == 'J') nums[i] = 11; else if (buf[i] == 'Q') nums[i] = 12; else if (buf[i] == 'K') nums[i] = 13; } } double calcute(double a, double b, char index) { if (index == '+') return a + b; //若為+,則返回相應(yīng)結(jié)果 else if (index == '-') return a - b; else if (index == '*') return a*b; else if (index == '/') if (b != 0) return a / b; //只有當(dāng)分母不為0時(shí),返回結(jié)果 } void exh()//窮舉計(jì)算 { double temp[3], tem[2]; //第一個(gè)符號(hào)放置后,經(jīng)過(guò)計(jì)算后相當(dāng)于剩下三個(gè)數(shù),這個(gè)數(shù)組用于存儲(chǔ)這三個(gè)數(shù) double sum; //求得的和 int judge = 0; //判斷是否找到一個(gè)合理的解 for (int i = 0; i < 4; i++) //第一次放置的符號(hào) { for (int j = 0; j < 4; j++) //第二次放置的符號(hào) { for (int k = 0; k < 4; k++) //第三次放置的符號(hào) { for (int m = 0; m < 3; m++) //首先計(jì)算的兩個(gè)相鄰數(shù)字,共有3種情況,相當(dāng)于括號(hào)的作用 { if (nums[m + 1] == 0 && ope[i] == '/') break; temp[m] = calcute(nums[m], nums[m + 1], ope[i]); temp[(m + 1) % 3] = nums[(m + 2) % 4]; temp[(m + 2) % 3] = nums[(m + 3) % 4]; //先確定首先計(jì)算的兩個(gè)數(shù)字,計(jì)算完成相當(dāng)于剩下三個(gè)數(shù),按順序儲(chǔ)存在temp數(shù)組中 for (int n = 0; n < 2; n++) //三個(gè)數(shù)字選出先計(jì)算的兩個(gè)相鄰數(shù)字,兩種情況,相當(dāng)于第二個(gè)括號(hào) { if (temp[n + 1] == 0 && ope[j] == '/') break; tem[n] = calcute(temp[n], temp[n + 1], ope[j]); tem[(n + 1) % 2] = temp[(n + 2) % 3]; //先確定首先計(jì)算的兩個(gè)數(shù)字,計(jì)算完成相當(dāng)于剩下兩個(gè)數(shù),按順序儲(chǔ)存在temp數(shù)組中 if (tem[1] == 0 && ope[k] == '/') break; sum = calcute(tem[0], tem[1], ope[k]); //計(jì)算和 if (sum == 24) //若和為24 { judge = 1; //判斷符為1,表示已求得解 if (m == 0 && n == 0) cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 0 && n == 1) cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 1 && n == 0) cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 1 && n == 1) cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; //m=0,1,2 n=0,1表示六種括號(hào)放置可能,并按照這六種可能輸出相應(yīng)的格式的計(jì)算式 } } } } } } if (judge == 0) cout << "這四張撲克牌無(wú)法找到一個(gè)合理的解" << endl; //如果沒(méi)有找到結(jié)果,符號(hào)位為0 } int main() { int i; int select = 1; cout<< " ################################################" << endl << " # #" << endl << " # 歡迎進(jìn)入24點(diǎn)游戲 #" << endl << " # #" << endl << " ################################################" << endl; while (select) { cout<< " ################################################" << endl << " # #" << endl << " # 是否開(kāi)始游戲 #" << endl << " # #" << endl << " # 0.是 1.否 #" << endl << " # #" << endl << " ################################################" << endl; cout << "請(qǐng)輸入你的選擇(0或1):"; cin >> i; switch (i) { case 0: cre(); exh(); break; case 1: select = 0; break; default: cout << "請(qǐng)?jiān)?和1之間選擇!" << endl; } } return 0; }
效果圖1
關(guān)于“C++怎么實(shí)現(xiàn)24點(diǎn)游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。