




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.課 程 設(shè) 計(jì) 報(bào) 告 課程名稱:面向?qū)ο蟪绦蛟O(shè)計(jì)C+ 設(shè)計(jì)題目: 24點(diǎn)小游戲 專 業(yè):計(jì)算機(jī)科學(xué)與技術(shù) 姓 名: 學(xué) 號(hào): 指導(dǎo)教師:李 曉 虹2016 年 1 月 4 日課程設(shè)計(jì)報(bào)告模塊內(nèi)容要求:(1) 系統(tǒng)需求分析:基本簡(jiǎn)單小游戲通過(guò)c+實(shí)現(xiàn)(2) 總體設(shè)計(jì):1. 系統(tǒng)自動(dòng)給出4個(gè)數(shù)字(1-k,其中J,Q,K按一點(diǎn)算)2. 由玩家自主計(jì)算,當(dāng)給出數(shù)字不能算出24點(diǎn)時(shí),按任意鍵繼續(xù); 3. 由玩家自行決定是否繼續(xù)游戲; 4. 在任何情況下都可以按Esc鍵結(jié)束游戲; (3)系統(tǒng)調(diào)試:程序?qū)崿F(xiàn)代碼:1. 主函數(shù):2. #include "stdio.h"3. #incl
2、ude "conio.h"4. #include "stdlib.h"5. #include "time.h"6. #include "math.h"7. #include "string.h"8. /*9. 從一副撲克牌中,任取4張。10. 2-10 按其點(diǎn)數(shù)計(jì)算(為了表示方便10用T表示),J,Q,K,A 統(tǒng)一按 1 計(jì)算11. 要求通過(guò)加減乘除四則運(yùn)算得到數(shù)字 24。12. 本程序可以隨機(jī)抽取紙牌,并用試探法求解。13. */14. void GivePuzzle(char* buf)15
3、. 16. int i;17. char card = 'A','2','3','4','5','6','7','8','9','T','J','Q','K'18. for(i=0;i<4;i+)19. 20. bufi=cardrand()%13;21. 22. 23.24. void shuffle(char * buf)25. 26. int i,k;27. char t;28
4、. for(i=0; i<5; i+)29. 30. k = rand() % 4;31. t = bufk;32. bufk = buf0;33. buf0 = t;34. 35. 36.37. int GetCardValue(int c)38. 39. if(c='T') return 10;40. if(c>='0' && c<='9') return c - '0'41. return 1;42. 43. char GetOper(int n)44. 45. switch(n)46. 4
5、7. case 0:48. return '+'49. case 1:50. return '-'51. case 2:52. return '*'53. case 3:54. return '/'55. 56. return ' '57. 58.59. double MyCalcu(double op1, double op2, int oper)60. 61. switch(oper)62. 63. case 0:64. return op1 + op2;65. case 1:66. return op1 -
6、op2;67. case 2:68. return op1 * op2;69. case 3:70. if(fabs(op2)>0.0001)71. return op1 / op2;72. else73. return 100000;74. 75. return 0;76. 77. void MakeAnswer(char* answer, int type, char* question, int* oper)78. 79. int i;80. 81. char p43;82.83. for(i=0; i<4; i+)84. 85. if( questioni='T
7、39;)86. strcpy(pi, "10");87. else88. sprintf(pi, "%c", questioni);89. 90. switch(type)91. 92. case 0:93. sprintf(answer, "%s %c (%s %c (%s %c %s)", 94. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);95. break;96. case 1:97. sprintf(answer, "%s %c (
8、%s %c %s) %c %s)", 98. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);99. break;100. case 2:101. sprintf(answer, "(%s %c %s) %c (%s %c %s)", 102. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);103. break;104. case 3:105. sprintf(answer, "(%s %c %
9、s) %c %s) %c %s", 106. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);107. break;108. case 4:109. sprintf(answer, "(%s %c (%s %c %s) %c %s", 110. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);111. break;112. 113. 114.115.116. int TestResolve(char* qu
10、estion, int* oper, char* answer)117. 118. / 等待考生完成119. int type = 0;120. double a,b,c;121. double cc = 0.00001;122.123. for(type=0;type<5;type+)124. 125. if(type=0)126. 127. c=MyCalcu(double)GetCardValue(question2),(double)GetCardValue(question3),oper2);128. b=MyCalcu(double)GetCardValue(question
11、1),c,oper1);129. a=MyCalcu(double)GetCardValue(question0),b,oper0);130. /printf("n%fn",a);131. if(fabs(a-24.00)<cc)132. break;133. else continue;134. 135. else if(type=1)136. 137. b=MyCalcu(double)GetCardValue(question1),(double)GetCardValue(question2),oper1);138. c=MyCalcu(b,(double)Ge
12、tCardValue(question3),oper2);139. a=MyCalcu(double)GetCardValue(question0),c,oper0);140. /printf("n%fn",a);141. if(fabs(a-24.00)<cc)142. break;143. else continue;144. 145. else if(type=2)146. 147. 148. 149. a=MyCalcu(double)GetCardValue(question0),(double)GetCardValue(question1),oper0);
13、150. c=MyCalcu(double)GetCardValue(question2),(double)GetCardValue(question3),oper2);151. b=MyCalcu(a,c,oper1);152. /printf("n%fn",b);153. if(fabs(b-24.00)<cc)154. break;155. else continue;156. 157. else if(type=3)158. 159. a=MyCalcu(double)GetCardValue(question0),(double)GetCardValue(q
14、uestion1),oper0);160. b=MyCalcu(a,(double)GetCardValue(question2),oper1);161. c=MyCalcu(b,(double)GetCardValue(question3),oper2);162. /printf("n%fn",c);163. if(fabs(c-24.00)<cc)164. break;165. else continue;166. 167. else if(type=4)168. 169. b=MyCalcu(double)GetCardValue(question1),(dou
15、ble)GetCardValue(question2),oper1);170. a=MyCalcu(double)GetCardValue(question0),b,oper0);171. c=MyCalcu(a,(double)GetCardValue(question3),oper2);172. /printf("n%fn",c);173. if(fabs(c-24.00)<cc)174. break;175. else continue;176. 177. 178. /*switch(type)179. 180. case 0:181. MakeAnswer(a
16、nswer,0,question,oper);182. return true;183. /printf("參考:%sn", answer);184. /break;185. case 1:186. MakeAnswer(answer,1,question,oper);187. return true;188. /printf("參考:%sn", answer);189. /break;190. case 2:191. MakeAnswer(answer,2,question,oper);192. return true;193. /printf(&qu
17、ot;參考:%sn", answer);194. /break;195. case 3:196. MakeAnswer(answer,3,question,oper);197. return true;198. /printf("參考:%sn", answer);199. /break;200. case 4:201. MakeAnswer(answer,4,question,oper);202. return true;203. /printf("參考:%sn", answer);204. /break;205. 206. 207.208.
18、return false;*/209.210. if(type<5)211. 212. MakeAnswer(answer,type,question,oper);213. return 1;214. 215. else return 0;216. 217.218.219. /*220. 采用隨機(jī)試探法:就是通過(guò)隨機(jī)數(shù)字產(chǎn)生 加減乘除的 組合,通過(guò)大量的測(cè)試來(lái)命中的解法221. 提示:222. 1. 需要考慮用括號(hào)控制計(jì)算次序的問(wèn)題 比如:( 10 - 4 ) * ( 3 + A ), 實(shí)際上計(jì)算次序的數(shù)目是有限的:223. A*(B*(c*D)224. A*(B*C)*D)225. (
19、A*B)*(C*D)226. (A*B)*C)*D227. (A*(B*C)*D228. 2. 需要考慮計(jì)算結(jié)果為分?jǐn)?shù)的情況:( 3 + (3 / 7) ) * 7229. 3. 題目中牌的位置可以任意交換230. */231. int TryResolve(char *question, char *answer)232. 233. /int oper3; / 存儲(chǔ)運(yùn)算符,0:加法 1:減法 2:乘法 3:除法234. int i,j;235. int oper3;236. for(i = 0; i < 1000 * 1000; i+)237. 238. / 打亂紙牌順序239. sh
20、uffle(question);240. / 隨機(jī)產(chǎn)生運(yùn)算符241. for(j=0; j<3; j+)242. operj = rand() % 4;243.244. if( TestResolve(question,oper,answer)245.246. return 1;247. 248.249. return 0;250. 251.252.253. int main(int argc, char* argv)254. 255. char buf_question4; / 題目256. char buf_answer30; / 解答257.258. int i;259. / 初始化隨機(jī)種子260. srand( (unsigned)time( NULL ) );261. 262. printf("*n");263. printf("速算24游戲n");264. printf("A J Q K 均按1計(jì)算,其它按牌點(diǎn)計(jì)算n");
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 ISO 5834-3:2025 EN Implants for surgery - Ultra-high-molecular-weight polyethylene - Part 3: Accelerated ageing methods after gamma irradiation in air
- 【正版授權(quán)】 ISO 1952:2025 EN Coal - Determination of extractable metals in dilute hydrochloric acid
- 按摩枕的智能化程度分析考核試卷
- 塑料廢料的分類與處理技術(shù)考核試卷
- 醫(yī)療設(shè)備手柄材料成本分析考核試卷
- 綠色化學(xué)在礦提取中的應(yīng)用考核試卷
- 成本控制流程再造考核試卷
- 寄宿生管理制度
- 公司員工打架檢討書(shū)
- 機(jī)器學(xué)習(xí)與深度學(xué)習(xí)在自動(dòng)駕駛中的應(yīng)用
- 公文寫(xiě)作技能題庫(kù)及答案
- 遼寧省“三支一扶”招募考試真題2024
- 2025廣西專業(yè)技術(shù)人員公需科目培訓(xùn)考試答案
- 2024年山東高中學(xué)業(yè)水平合格考試化學(xué)試卷真題(含答案詳解)
- 國(guó)開(kāi)機(jī)考答案-工程力學(xué)(本)(閉卷)
- 高三學(xué)生復(fù)讀第一課-主題班會(huì)課件(共68張PPT)
- 審判流程管理信息表
- 303093 池國(guó)華 《內(nèi)部控制與風(fēng)險(xiǎn)管理(第3版)》思考題和案例分析答案
- 沈陽(yáng)市生產(chǎn)性服務(wù)業(yè)調(diào)研報(bào)告
- C語(yǔ)言程序設(shè)計(jì)-實(shí)驗(yàn)第一次上機(jī)實(shí)驗(yàn)報(bào)告
- 鄰硝基苯甲酸項(xiàng)目可行性研究報(bào)告模板
評(píng)論
0/150
提交評(píng)論