24点游戏程序设计报告Word格式.docx
《24点游戏程序设计报告Word格式.docx》由会员分享,可在线阅读,更多相关《24点游戏程序设计报告Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
3
●4
核心源程序模块·
4
●5
操作方法简介及实验结果
·
●6
设计体会·
附录·
6
一、程序功能简介
题目要求在输入4个数后,程序对这个4个数进行运算,若能计算出结
果等于24,即输出运算过程。
接到题目的时候,手上并无其他资料,需要自己全动手设计才能到达目标,而目标是在输入四个数之后,先进行全排列,然后进行全运算,从而最终得到结果以输出。
二、本人完成的主要工作
●程序思路的构建
●源代码的编写
●程序运行及调试
●程序总结
三、设计方案
1、设计分析
关于这个计算24游戏的设计,首先我将题目看成两部分,一部分为运算数,
一部分为运算符。
首先由于有四个数,则只有三个运算符,由于涉及了括号,个人认为用穷举法才能全面地进行说有的运算,我们可以将运算符和括号这部分归为主运算部分,在这部分中,运算数的位置固定,但是运算符和括号的位置会改变,而且程序主要就是在这部分运算,直接将数值引入,然后计算判断是否等于24,若等于24,则输出数值和这些运算符、括号的位置构成的运算式;
然而,这是不够全面的,因为还要考虑运算数的排列位置,于是有了第二部分,运算数的位置全排列,同样是采用穷举法,将运算数全排列,在每个排列后面调用主运算的方法,从而达到全面不遗漏地计算。
在MAIN函数中,我们仅需要记录输入的4个数字,然后将数值用在全排列函数中即可。
整个程序的设计看起来很繁琐,但在运用上很简单,能简明扼要地表现出来。
2、模块的功能及程序说明
本程序的主要功能就是把输入的四个数用简单的四则运算和括号计算得出24,然后输出其算法。
本程序所做的就是把四个数能进行的所有运算做一遍,然后把其中结果为24的算法输出。
3、核心算法流程图
4、核心源程序模块
#include<
stdio.h>
voidmain()
{
floata,b,c,d;
A:
printf("
请输入4个整数:
\n"
);
scanf("
%f%f%f%f"
&
a,&
b,&
c,&
d);
运算结果如下:
arrangement(a,b,c,d);
gotoA;
}
5、操作方法简介及实验结果
1)首先输入四个数字:
2)然后得出结果,并进行下一次计算:
6、设计体会
因为整个程序的算法比较简单,只运用了枚举法然后嵌入使用,在编写的前期只是较为繁琐而没有出现编译上的问题,只是有些计算情况开始没想到,在自己和同学的帮助下进行了完善。
但在初始设计的时候,并没有想到要进行运算数的位置排列,在考虑之后,决定要加入运算数的位置排列从而全面地进行运算。
设计运算数的位置排列,最早是希望用递归调用的方法来完成,但是由于时间和个人的原因,未能成功,最终仍是采取了穷举法,虽然未能成功,但是也让我对递归算法的调用有了一定的理解。
附录:
自定义函数及其功能
一、枚举所有算法
voidcalculation(floata,floatb,floatc,floatd)
if(a+b+c+d==24){printf("
%1.0f+%1.0f+%1.0f+%1.0f=24\n"
a,b,c,d);
if(a+b+c-d==24){printf("
%1.0f+%1.0f+%1.0f-%1.0f=24\n"
if(a+b+c*d==24){printf("
%1.0f+%1.0f+%1.0f*%1.0f=24\n"
if(a+(b+c)*d==24){printf("
%1.0f+(%1.0f+%1.0f)*%1.0f=24\n"
if((a+b+c)*d==24){printf("
(%1.0f+%1.0f+%1.0f)*%1.0f=24\n"
if(a+b+c/d==24){printf("
%1.0f+%1.0f+%1.0f/%1.0f=24\n"
if(a+(b+c)/d==24){printf("
%1.0f+(%1.0f+%1.0f)/%1.0f=24\n"
if((a+b+c)/d==24){printf("
(%1.0f+%1.0f+%1.0f)/%1.0f=24\n"
if(a+b-c+d==24){printf("
%1.0f+%1.0f-%1.0f+%1.0f=24\n"
if(a+b-c-d==24){printf("
%1.0f+%1.0f-%1.0f-%1.0f=24\n"
if(a+b-c*d==24){printf("
%1.0f+%1.0f-%1.0f*%1.0f=24\n"
if(a+(b-c)*d==24){printf("
%1.0f+(%1.0f-%1.0f)*%1.0f=24\n"
if((a+b-c)*d==24){printf("
(%1.0f+%1.0f-%1.0f)*%1.0f=24\n"
if(a+b-c/d==24){printf("
%1.0f+%1.0f-%1.0f/%1.0f=24\n"
if(a+(b-c)/d==24){printf("
%1.0f+(%1.0f-%1.0f)/%1.0f=24\n"
if((a+b-c)/d==24){printf("
(%1.0f+%1.0f-%1.0f)/%1.0f=24\n"
if(a+b*c+d==24){printf("
%1.0f+%1.0f*%1.0f+%1.0f=24\n"
if((a+b)*c+d==24){printf("
(%1.0f+%1.0f)*%1.0f+%1.0f=24\n"
if(a+b*(c+d)==24){printf("
%1.0f+%1.0f*(%1.0f+%1.0f)=24\n"
if((a+b)*(c+d)==24){printf("
(%1.0f+%1.0f)*(%1.0f+%1.0f)=24\n"
if(a+b*c-d==24){printf("
%1.0f+%1.0f*%1.0f-%1.0f=24\n"
if((a+b)*c-d==24){printf("
(%1.0f+%1.0f)*%1.0f-%1.0f=24\n"
if(a+b*(c-d)==24){printf("
%1.0f+%1.0f*(%1.0f-%1.0f)=24\n"
if((a+b)*(c-d)==24){printf("
(%1.0f+%1.0f)*(%1.0f-%1.0f)=24\n"
if(a+b*c*d==24){printf("
%1.0f+%1.0f*%1.0f*%1.0f=24\n"
if((a+b)*c*d==24){printf("
(%1.0f+%1.0f)*%1.0f*%1.0f=24\n"
if(a+b*c/d==24){printf("
%1.0f+%1.0f*%1.0f/%1.0f=24\n"
if((a+b)*c/d==24){printf("
(%1.0f+%1.0f)*%1.0f/%1.0f=24\n"
if((a+b)*(c/d)==24){printf("
(%1.0f+%1.0f)*(%1.0f/%1.0f)=24\n"
if(a+b/c+d==24){printf("
%1.0f+%1.0f/%1.0f+%1.0f=24\n"
if((a+b)/c+d==24){printf("
(%1.0f+%1.0f)/%1.0f+%1.0f=24\n"
if(a+b/(c+d)==24){printf("
%1.0f+%1.0f/(%1.0f+%1.0f)=24\n"
if((a+b)/(c+d)==24){printf("
(%1.0f+%1.0f)/(%1.0f+%1.0f)=24\n"
if(a+b/c-d==24){printf("
if((a+b)/c-d==24){printf("
(%1.0f+%1.0f)/%1.0f-%1.0f=24\n"
if(a+b/(c-d)==24){printf("
%1.0f+%1.0f/(%1.0f-%1.0f)=24\n"
if((a+b)/(c-d)==24){printf("
(%1.0f+%1.0f)/(%1.0f-%1.0f)=24\n"
if(a+b/c*d==24){printf("
%1.0f+%1.0f/%1.0f*%1.0f=24\n"
if((a+b)/c*d==24){printf("
(%1.0f+%1.0f)/%1.0f*%1.0f=24\n"
if((a+b)/(c*d)==24){printf("
(%1.0f+%1.0f)/(%1.0f*%1.0f)=24\n"
if(a+b/c/d==24){printf("
%1.0f+%1.0f/%1.0f/%1.0f=24\n"
if((a+b)/c/d==24){printf("
(%1.0f+%1.0f)/%1.0f/%1.0f=24\n"
if((a+b)/(c/d)==24){printf("
(%1.0f+%1.0f)/(%1.0f/%1.0f)=24\n"
if(a-b+c+d==24){printf("
%1.0f-%1.0f+%1.0f+%1.0f=24\n"
if(a-b+c-d==24){printf("
%1.0f-%1.0f+%1.0f-%1.0f=24\n"
if(a-b+c*d==24){printf("
%1.0f-(%1.0f+%1.0f)*%1.0f=24\n"
if(a-(b+c)*d==24){printf("
%1.0f-%1.0f+%1.0f*%1.0f=24\n"
if((a-b+c)*d==24){printf("
(%1.0f-%1.0f+%1.0f)*%1.0f=24\n"
if(a-b+c/d==24){printf("
%1.0f-%1.0f+%1.0f/%1.0f=24\n"
if(a-(b+c)/d==24){printf("
%1.0f-(%1.0f+%1.0f)/%1.0f=24\n"
if((a-b+c)/d==24){printf("
(%1.0f-%1.0f+%1.0f)/%1.0f=24\n"
if(a-b-c+d==24){printf("
%1.0f-%1.0f-%1.0f+%1.0f=24\n"
if(a-b-c-d==24){printf("
%1.0f-%1.0f-%1.0f-%1.0f=24\n"
if(a-b-c*d==24){printf("
%1.0f-%1.0f-%1.0f*%1.0f=24\n"
if(a-(b-c)*d==24){printf("
%1.0f-(%1.0f-%1.0f)*%1.0f=24\n"
if((a-b-c)*d==24){printf("
(%1.0f-%1.0f-%1.0f)*%1.0f=24\n"
if(a-b-c/d==24){printf("
%1.0f-%1.0f-%1.0f/%1.0f=24\n"
if(a-(b-c)/d==24){printf("
%1.0f-(%1.0f-%1.0f)/%1.0f=24\n"
if((a-b-c)/d==24){printf("
if(a-b*c+d==24){printf("
%1.0f-%1.0f*%1.0f+%1.0f=24\n"
if((a-b)*c+d==24){printf("
(%1.0f-%1.0f)*%1.0f+%1.0f=24\n"
if(a-b*(c+d)==24){printf("
%1.0f-%1.0f*(%1.0f+%1.0f)=24\n"
if((a-b)*(c+d)==24){printf("
(%1.0f-%1.0f)*(%1.0f+%1.0f)=24\n"
if(a-b*c-d==24){printf("
%1.0f-%1.0f*%1.0f-%1.0f=24\n"
if((a-b)*c-d==24){printf("
(%1.0f-%1.0f)*%1.0f-%1.0f=24\n"
if(a-b*(c-d)==24){printf("
%1.0f-%1.0f*(%1.0f-%1.0f)=24\n"
if((a-b)*(c-d)==24){printf("
(%1.0f-%1.0f)*(%1.0f-%1.0f)=24\n"
if(a-b*c*d==24){printf("
%1.0f-%1.0f*%1.0f*%1.0f=24\n"
if((a-b)*c*d==24){printf("
(%1.0f-%1.0f)*%1.0f*%1.0f=24\n"
if(a-b*c/d==24){printf("
%1.0f-%1.0f*%1.0f/%1.0f=24\n"
if((a-b)*c/d==24){printf("
(%1.0f-%1.0f)*%1.0f/%1.0f=24\n"
if((a-b)*(c/d)==24){printf("
(%1.0f-%1.0f)*(%1.0f/%1.0f)=24\n"
if(a-b/c+d==24){printf("
%1.0f-%1.0f/%1.0f+%1.0f=24\n"
if((a-b)/c+d==24){printf("
(%1.0f-%1.0f)/%1.0f+%1.0f=24\n"
if(a-b/(c+d)==24){printf("
%1.0f-%1.0f/(%1.0f+%1.0f)=24\n"
if((a-b)/(c+d)==24){printf("
(%1.0f-%1.0f)/(%1.0f+%1.0f)=24\n"
if(a-b/c-d==24){printf("
if((a-b)/c-d==24){printf("
(%1.0f-%1.0f)/%1.0f-%1.0f=24\n"
if(a-b/(c-d)==24){printf("
%1.0f-%1.0f/(%1.0f-%1.0f)=24\n"
if((a-b)/(c-d)==24){printf("
(%1.0f-%1.0f)/(%1.0f-%1.0f)=24\n"
if(a-b/c*d==24){printf("
%1.0f-%1.0f/%1.0f*%1.0f=24\n"
if((a-b)/c*d==24){printf("
(%1.0f-%1.0f)/%1.0f*%1.0f=24\n"
if((a-b)/(c*d)==24){printf("
(%1.0f-%1.0f)/(%1.0f*%1.0f)=24\n"
if(a-b/c/d==24){printf("
%1.0f-%1.0f/%1.0f/%1.0f=24\n"
if((a-b)/c/d==24){printf("
(%1.0f-%1.0f)/%1.0f/%1.0f=24\n"
if((a-b)/(c/d)==24){printf("
(%1.0f-%1.0f)/(%1.0f/%1.0f)=24\n"
if(a*b+c+d==24){printf("
%1.0f*%1.0f+%1.0f+%1.0f=24\n"
if(a*(b+c)+d==24){printf("
%1.0f*(%1.0f+%1.0f)+%1.0f=24\n"
if(a*(b+c+d)==24){printf("
%1.0f*(%1.0f+%1.0f+%1.0f)=24\n"
if(a*b+c-d==24){printf("
%1.0f*%1.0f+%1.0f-%1.0f=24\n"
if(a*(b+c)-d==24){printf("
%1.0f*(%1.0f+%1.0f)-%1.0f=24\n"
if(a*(b+c-d)==24){printf("
%1.0f*(%1.0f+%1.0f-%1.0f)=24\n"
if(a*b+c*d==24){printf("
%1.0f*%1.0f+%1.0f*%1.0f=24\n"
if(a*(b+c)*d==24){printf("
%1.0f*(%1.0f+%1.0f)*%1.0f=24\n"
if(a*b+c/d==24){printf("
%1.0f*%1.0f+%1.0f/%1.0f=24\n"
if(a*(b+c)/d==24){printf("
%1.0f*(%1.0f+%1.0f)/%1.0f=24\n"
if(a*b-c+d==24){printf("
%1.0f*%1.0f-%1.0f+%1.0f=24\n"
if(a*(b-c)+d==24){printf("
%1.0f*(%1.0f-%1.0f)+%1.0f=24\n"
if(a*(b-c+d)==24){printf("
%1.0f*(%1.0f-%1.0f+%1.0f)=24\n"
if(a*b-c-d==24){printf("
%1.0f*%1.0f-%1.0f-%1.0f=24\n"
if(a*(b-c)-d==24){printf("
%1.0f*(%1.0f-%1.0f)-%1.0f=24\n"
if(a*(b-c-d)==24){printf("
%1.0f*(%1.0f-%1.0f-%1.0f)=24\n"
if(a*b-c*d==24){printf("
%1.0f*%1.0f-%1.0f*%1.0f=24\n"
if(a*(b-c)*d==24){printf("
%1.0f*(%1.0f-%1.0f)*%1.0f=24\n"
if(a*b-c/d==24){printf("
%1.0f*%1.0f-%1.0f/%1.0f=24\n"
if(a*(b-c)/d==24){printf("
%1.0f*(%1.0f-%1.0f)/%1.0f=24\n"
if(a*b*c+d==24){printf("
%1.0f*%1.0f*%1.0f+%1.0f=24\n"
if(a*b*(c+d)==24){printf("
%1.0f*%1.0f*(%1.0f+%1.0f)=24\n"
if(a*b*c-d==24){printf("
%1.0f*%1.0f*%1.0f-%1.0f=24\n"
if(a*b*(c-d)==24){printf("
%1.0f*%1.0f*(%1.0f-%1.0f)=24\n"
if(a*b*c*d==24){printf("
%1.0f*%1.0f*%1.0f*%1.0f=24\n"
if(a*b*c/d==24){printf("
%1.0f*%1.0f*%1.0f/%1.0f=24\n"
if(a*b*(c/d)==24){printf("
%1.0f*%1.0f*(%1.0f/%1.0f)=24\n"
if(a*b/c+d==24){printf("
%1.0f*%1.0f/%1.0f+%1.0f=24\n"
if(a*b/(c+d)==24){printf("
%1.0f*%1.0f/(%1.0f+%1.0f)=24\n"
if(a*b/c-d==24){printf("
%1.0f*%1.0f/%1.0f-%1.0f=24\n"
if(a*b/(c-d)==24){printf("
%1.0f*%1.0f/(%1.0f-%1.0f)=24\n"
if(a*b/c*d==24){printf("
%1.0f*%1.0f/