| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1966 人关注过本帖
标题:有24点游戏的完整程序吗?
只看楼主 加入收藏
lj3217734
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-3-1
收藏
 问题点数:0 回复次数:12 
有24点游戏的完整程序吗?

小弟在作毕业设计,由于没有思路,所以请各位兄弟姐妹们帮下.
email:lijian.cpp@163.com
1210lijian@sohu.com

搜索更多相关主题的帖子: 游戏 
2006-03-13 22:33
小许
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-3-14
收藏
得分:0 
有报酬吗?
2006-03-15 17:53
xinyu
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-3-12
收藏
得分:0 
[原创]RE24点

你重新申请一个邮箱,我发给你。你的邮箱我发不出去。

[此贴子已经被作者于2006-3-15 18:06:27编辑过]

2006-03-15 18:03
lj3217734
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-3-1
收藏
得分:0 
2006-03-16 10:29
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

复习了一下operator overloading,自己写了一遍,中间一段有点惨不忍睹,不过也改不了多好看。拿4,4,10,10试了一下。
输出正确。
10 * 10 = 100
100 - 4 = 96
96 / 4 = 24

程序代码:

#include <iostream>
#include <vector>
using namespace std;

class CFraction
{
public:
CFraction(int intNumerator, int intDenomiator) : m_intNumerator(intNumerator), m_intDenominator(intDenomiator) {}
CFraction(int intNumerator) : m_intNumerator(intNumerator), m_intDenominator(1) {}
CFraction() : m_intNumerator(0), m_intDenominator(1) {}
private:
int m_intNumerator;
int m_intDenominator;
friend CFraction operator + (CFraction &f1, CFraction &f2);
friend CFraction operator - (CFraction &f1, CFraction &f2);
friend CFraction operator * (CFraction &f1, CFraction &f2);
friend CFraction operator / (CFraction &f1, CFraction &f2);
friend bool operator == (CFraction &f1, CFraction &f2);
friend ostream &operator<< (ostream &o, CFraction &f);
};

CFraction operator + (CFraction &f1, CFraction &f2)
{
// handle exceptin for /0
return CFraction(
f1.m_intNumerator*f2.m_intDenominator+f2.m_intNumerator*f1.m_intDenominator
, f1.m_intDenominator*f2.m_intDenominator);
}

CFraction operator - (CFraction &f1, CFraction &f2)
{
// handle exceptin for /0
return CFraction(
f1.m_intNumerator*f2.m_intDenominator-f2.m_intNumerator*f1.m_intDenominator
, f1.m_intDenominator*f2.m_intDenominator);
}

CFraction operator * (CFraction &f1, CFraction &f2)
{
// handle exceptin for /0
return CFraction(
f1.m_intNumerator*f2.m_intNumerator
, f1.m_intDenominator*f2.m_intDenominator);
}

CFraction operator / (CFraction &f1, CFraction &f2)
{
// handle exceptin for /0
return CFraction(
f1.m_intNumerator*f2.m_intDenominator
, f1.m_intDenominator*f2.m_intNumerator);
}

bool operator == (CFraction &f1, CFraction &f2)
{
if (!f1.m_intDenominator || !f2.m_intDenominator)
return false;
return f1.m_intNumerator*f2.m_intDenominator == f1.m_intDenominator*f2.m_intNumerator;
}

ostream &operator<< (ostream &o, CFraction &f)
{
if (f.m_intDenominator == 1)
return o << f.m_intNumerator;
else if (f.m_intNumerator % f.m_intDenominator == 0)
return o << f.m_intNumerator/f.m_intDenominator;
return o << f.m_intNumerator << \"/\" << f.m_intDenominator;
}

class C24
{
public:
C24(CFraction &f0, CFraction &f1, CFraction &f2, CFraction &f3);
void FindFirstSolution();
private:
void Calculate(int intNum1, int intNum2, int intOp, vector<CFraction> &vecRemain);
void PrintSolution(int intNumberCount[3][2], int intOp[3]);
CFraction m_fra[4];
};

C24::C24(CFraction &f0, CFraction &f1, CFraction &f2, CFraction &f3)
{
m_fra[0]=f0, m_fra[1]=f1, m_fra[2]=f2, m_fra[3]=f3;
}

void C24::Calculate(int intNum1, int intNum2, int intOp, vector<CFraction> &vecRemain)
{
int intErase1, intErase2;
char chararOperator[] = \"+-*/\";
if (intNum1 < 0 || intNum2 < 0 || intNum1 >= (int)vecRemain.size() || intNum2 >= (int)vecRemain.size())
return;
if (intOp < 0 || intOp > 3)
return;

CFraction f1 = vecRemain[intNum1], f2 = vecRemain[intNum2], fReturn;
intErase1 = max(intNum1, intNum2);
intErase2 = min(intNum1, intNum2);
vecRemain.erase(&vecRemain[intErase1]);
vecRemain.erase(&vecRemain[intErase2]);
switch(chararOperator[intOp])
{
case '+':
fReturn = f1+f2;
break;
case '-':
fReturn = f1-f2;
break;
case '*':
fReturn = f1*f2;
break;
case '/':
fReturn = f1/f2;
break;
default:
return;
}
vecRemain.push_back(fReturn);
return;
}

void C24::PrintSolution(int intNumberCount[][2], int intOp[])
{
char chararOperator[] = \"+-*/\";
vector<CFraction> vecRemain;
for (int intCount = 0; intCount < 4; intCount++)
vecRemain.push_back(m_fra[intCount]);
for (int intRound = 0; intRound < 3; intRound++)
{
cout << vecRemain[intNumberCount[intRound][0]];
cout << \" \";
cout << chararOperator[intOp[intRound]];
cout << \" \";
cout << vecRemain[intNumberCount[intRound][1]];
cout << \" = \";
Calculate(intNumberCount[intRound][0], intNumberCount[intRound][1], intOp[intRound], vecRemain);
cout << vecRemain[vecRemain.size()-1];
cout << endl;
}
}

void C24::FindFirstSolution()
{
int intNumberCount[3][2], intOp[3];
vector<CFraction> vecRemain;

for (intNumberCount[0][0] = 0; intNumberCount[0][0] < 4; intNumberCount[0][0]++)
{
for (intNumberCount[0][1] = 0; intNumberCount[0][1] < 4; intNumberCount[0][1]++)
{
if (intNumberCount[0][0] == intNumberCount[0][1])
continue;
for (intOp[0] = 0; intOp[0] < 4; intOp[0]++)
{
// second round
for (intNumberCount[1][0] = 0; intNumberCount[1][0] < 3; intNumberCount[1][0]++)
{
for (intNumberCount[1][1] = 0; intNumberCount[1][1] < 3; intNumberCount[1][1]++)
{
if (intNumberCount[1][0] == intNumberCount[1][1])
continue;
for (intOp[1] = 0; intOp[1] < 4; intOp[1]++)
{
// final round
for (intNumberCount[2][0] = 0; intNumberCount[2][0] < 2; intNumberCount[2][0]++)
{
for (intNumberCount[2][1] = 0; intNumberCount[2][1] < 2; intNumberCount[2][1]++)
{
if (intNumberCount[2][0] == intNumberCount[2][1])
continue;
for (intOp[2] = 0; intOp[2] < 4; intOp[2]++)
{
for (int intCount = 0; intCount < 4; intCount++)
vecRemain.push_back(m_fra[intCount]);
Calculate(intNumberCount[0][0], intNumberCount[0][1], intOp[0], vecRemain);
Calculate(intNumberCount[1][0], intNumberCount[1][1], intOp[1], vecRemain);
Calculate(intNumberCount[2][0], intNumberCount[2][1], intOp[2], vecRemain);
if (vecRemain[0] == CFraction(24))
{
PrintSolution(intNumberCount, intOp);
return;
}
vecRemain.clear();
}
}
}
}
}
}
}
}
}
}

int main(int argc, char* argv[])
{
C24 c24(CFraction(4), CFraction(4), CFraction(10), CFraction(10));

c24.FindFirstSolution();
return 0;
}


http://myajax95./
2006-03-30 10:09
wonglaye
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-3-26
收藏
得分:0 
十分佩服楼上的"曾经ajax"

freedom is not free to gain
2006-03-30 11:04
zyhhy_x
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-11-23
收藏
得分:0 
everajax也太强了,做我师父算了
2006-04-17 12:41
love_me
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2005-12-29
收藏
得分:0 


1年前用C#写的


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 算24点
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(24, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(48, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.Window;
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox2.Location = new System.Drawing.Point(88, 8);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(48, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox3.Location = new System.Drawing.Point(24, 40);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(48, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// textBox4
//
this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox4.Location = new System.Drawing.Point(88, 40);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(48, 21);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.ForeColor = System.Drawing.SystemColors.ControlDark;
this.button1.Location = new System.Drawing.Point(40, 72);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "计算";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(152, 102);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.ForeColor = System.Drawing.SystemColors.GrayText;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "算24点";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
int[] x=new int [4];
int[] q=new int [4];
string str="",str2="",str3="";
bool me=false;
public double add(double x,double y)
{
return x+y;
}
public double sub(double x,double y)
{
return x-y;
}
public double mul(double x,double y)
{
return x*y;
}
public double vid(double x,double y)
{
return x/y;
}
public void jj(int a,int b,int c,int d)
{
double x=1,y=1,z=1;
string jj1="",jj2="";
for (int i=0;i<4;i++)
{
if(i==0) x=add(c,d);
else if(i==1) x=sub(c,d);
else if(i==2) x=mul(c,d);
else if(i==3) x=vid(c,d);
for(int j=0;j<4;j++)
{
if(j==0) y=add(b,x);
else if(j==1) y=sub(b,x);
else if(j==2) y=mul(b,x);
else if(j==3) y=vid (b,x);
for(int k=0;k<4;k++)
{
if(k==0) z=add(a,y);
else if(k==1) z=sub(a,y);
else if(k==2) z=mul(a,y);
else if(k==3) z=vid(a,y);
if(z.ToString ()=="24")
{
string f="",m="",n="";

if(i==0) f="+";
else if(i==1) f="-";
else if(i==2) f="*";
else if(i==3) f="/";
if(j==0) m="+";
else if(j==1) m="-";
else if(j==2) m="*";
else if(j==3) m="/";
if(k==0) n="+";
else if(k==1) n="-";
else if(k==2) n="*";
else if(k==3) n="/";
jj1=""+a+n+"["+b+m+"("+c+f+d+")]";
if(jj1==jj2) continue;
if(jj1==str) continue;
//MessageBox.Show (jj1,"24点");
if(me==true) goto end;
if (MessageBox.Show(jj1+"\n\n按取消终止显示","24点",MessageBoxButtons.OKCancel)==DialogResult.Cancel)
{
me=true;
goto end;
}
jj2=jj1;
str=jj1;

}
}
}
}
end:;
}
public void jc(int a,int b,int c,int d)
{
double x=1,y=1,z=1;
string[] jc24=new string [2];
for (int i=0;i<4;i++)
{
if(i==0) x=add(c,d);
else if(i==1) x=sub(c,d);
else if(i==2) x=mul(c,d);
else if(i==3) x=vid(c,d);
for(int j=0;j<4;j++)
{
if(j==0) y=add(a,b);
else if(j==1) y=sub(a,b);
else if(j==2) y=mul(a,b);
else if(j==3) y=vid (a,b);
for(int k=0;k<4;k++)
{
if(k==0) z=add(y,x);
else if(k==1) z=sub(y,x);
else if(k==2) z=mul(y,x);
else if(k==3) z=vid(y,x);
if(z.ToString ()=="24")
{
string f="",m="",n="";

if(i==0) f="+";
else if(i==1) f="-";
else if(i==2) f="*";
else if(i==3) f="/";
if(j==0) m="+";
else if(j==1) m="-";
else if(j==2) m="*";
else if(j==3) m="/";
if(k==0) n="+";
else if(k==1) n="-";
else if(k==2) n="*";
else if(k==3) n="/";
jc24[0]="("+a+m+b+")"+n+"("+c+f+d+")";

if(jc24[0]==jc24[1]) continue;
if(jc24[0]==str2) continue;
if(me==true) goto end;
if (MessageBox.Show(jc24[0]+"\n\n按取消终止显示","24点",MessageBoxButtons.OKCancel)==DialogResult.Cancel)
{
me=true;
goto end;
}

jc24[1]=jc24[0];
str2=jc24[0];
}
}
}
}
end:;
}
public void jd(int a,int b,int c,int d)
{
double x=1,y=1,z=1;
string jj1="",jj2="";
for (int i=0;i<4;i++)
{
if(i==0) x=add(a,b);
else if(i==1) x=sub(a,b);
else if(i==2) x=mul(a,b);
else if(i==3) x=vid(a,b);
for(int j=0;j<4;j++)
{
if(j==0) y=add(x,c);
else if(j==1) y=sub(x,c);
else if(j==2) y=mul(x,c);
else if(j==3) y=vid (x,c);
for(int k=0;k<4;k++)
{
if(k==0) z=add(y,d);
else if(k==1) z=sub(y,d);
else if(k==2) z=mul(y,d);
else if(k==3) z=vid(y,d);
if(z.ToString ()=="24")
{
string f="",m="",n="";

if(i==0) f="+";
else if(i==1) f="-";
else if(i==2) f="*";
else if(i==3) f="/";
if(j==0) m="+";
else if(j==1) m="-";
else if(j==2) m="*";
else if(j==3) m="/";
if(k==0) n="+";
else if(k==1) n="-";
else if(k==2) n="*";
else if(k==3) n="/";
jj1="[("+a+f+b+")"+m+c+"]"+n+d;
if(jj1==jj2) continue;
if(jj1==str3) continue;
if(me==true) goto end;
//MessageBox.Show (jj1,"24点");
if (MessageBox.Show(jj1+"\n\n按取消终止显示","24点",MessageBoxButtons.OKCancel)==DialogResult.Cancel)
{
me=true;
goto end;
}
jj2=jj1;
str3=jj1;

}

}

}

}
end:;
}
public void js()
{
str="";str2="";str3="";//来自全局变量
me=false;
try
{
q[0]=Convert.ToUInt16 (this.textBox1.Text) ;
q[1]=Convert.ToUInt16 (this.textBox2.Text);
q[2]=Convert.ToUInt16 (this.textBox3.Text);
q[3]=Convert.ToUInt16 (this.textBox4.Text) ;
for(int i=0;i<4;i++)
{
x[i]=q[i];
}
for(int i=0;i<4;i++)
{
x[0]=q[i];
for(int j=0;j<4;j++)
{

x[1]=q[j];
if(j==i) continue;
for(int m=0;m<4;m++)
{
x[2]=q[m];
if(m==j||m==i) continue;
for(int n=0;n<4;n++)
{
if(n==i||n==j||n==m) continue;
x[3]=q[n];

jj(x[0],x[1],x[2],x[3]);
jc(x[0],x[1],x[2],x[3]);
jd(x[0],x[1],x[2],x[3]);


}

//MessageBox.Show (" "+x[0]+" "+x[1]+" "+x[2]+" "+x[3]);

}
}
}
}
catch (Exception e)
{

MessageBox.Show(e.Message,"警告");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
js();
}
}
}


灌水无罪! 顶贴有理! <0_0>
2006-04-19 22:24
oヤ偽妳變壞
Rank: 2
等 级:新手上路
威 望:4
帖 子:2251
专家分:0
注 册:2006-3-19
收藏
得分:0 

强~!

2006-06-03 10:45
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 
感觉是挺强的呀!呵呵,顶一个了!

2006-06-03 12:26
快速回复:有24点游戏的完整程序吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018988 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved