C#项目整理.docx
- 文档编号:23370566
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:61
- 大小:29.01KB
C#项目整理.docx
《C#项目整理.docx》由会员分享,可在线阅读,更多相关《C#项目整理.docx(61页珍藏版)》请在冰豆网上搜索。
C#项目整理
门票销售:
namespace门票销售
{
publicpartialclassForm1:
Form
{
privateconstintticketprice=50;
publicForm1()
{
InitializeComponent();
}
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)
{
txtNum.ReadOnly=false;
txtPayment.ReadOnly=false;
txtReceiving.Text="";
txtBalance.Text="";
groupBox1.Enabled=false;
switch(comboBox1.SelectedIndex)
{
case0:
txtPrice.Text=ticketprice.ToString();
break;
case1:
txtPrice.Text=(ticketprice*50/100).ToString();
break;
case2:
groupBox1.Enabled=true;
radioButton1.Checked=true;
txtPrice.Text=(ticketprice*90/100).ToString();
break;
}
}
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
{
txtPrice.Text=(ticketprice*90/100).ToString();
}
privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)
{
txtPrice.Text=(ticketprice*80/100).ToString();
}
privatevoidradioButton3_CheckedChanged(objectsender,EventArgse)
{
txtPrice.Text=(ticketprice*65/100).ToString();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
doublepayment,receiving,balance,price;
intnum;
num=Int32.Parse(txtNum.Text);
payment=double.Parse(txtPayment.Text);
price=double.Parse(txtPrice.Text);
receiving=num*price;
txtReceiving.Text=receiving.ToString();
balance=payment-receiving;
txtBalance.Text=balance.ToString();
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
Application.Exit();
}
}
}
成绩排序:
namespace成绩排序
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
ints=0,m=0,result=0,f=0;
int[]A;
stringstr;
str=textBox1.Text;
inti;
boolfound=false;
if(str.Length==0)//判断字符串是否为空
{
MessageBox.Show("文本框不能为空!
");
return;
}
for(i=0;i if(! char.IsNumber(str[i]))//判断输入的数是否为数值型数据 { MessageBox.Show("输入的数据应为数值型数据! "); return; } A=newint[listBox1.Items.Count];//将列表框中的数赋给数组 for(i=0;i A[i]=Int32.Parse(listBox1.Items[i].ToString()); if(listBox1.Items.Count==0)//判断列表框是否为空 f=0; else { s=0; result=A.Length-1; while(! found&&s<=result)//折半插入排序 { m=(s+result)/2; if(A[m].Equals(Int32.Parse(str))) { found=true; f=m; } else { if(Int32.Parse(str).CompareTo(A[m])<0) result=m-1; else s=m+1; } } if(! found) { f=s; } } listBox1.Items.Insert(f,Int32.Parse(str));//输出结果 } privatevoidbutton2_Click(objectsender,EventArgse) { Application.Exit(); } } } 贪吃蛇: namespace贪吃蛇 { publicclassBean { publicBean() { // } privatePointorigin;//定义位置字段 publicPointOrigin//定义位置属性 { get { returnorigin; } set { origin=value;//静态成员访问: 类名.成员名 }//非静态成员访问: 对象名.成员名 } publicvoidDisplay(Graphicsg)//显示豆方法 { SolidBrushb=newSolidBrush(Color.Red);//定义红色画刷 g.FillRectangle(b,origin.X,origin.Y,5,5);//画实心矩形 } publicvoidUnDisplay(Graphicsg)//消除豆方法 { SolidBrushb=newSolidBrush(Color.DodgerBlue);//定义背景画刷 g.FillRectangle(b,origin.X,origin.Y,5,5);//用背景颜色覆盖原矩形 } } } namespace贪吃蛇 { publicclassBlock { publicBlock() { // } privateintnumber;//所以序数字段 privatePointorigin;//定义位置字段 publicintNumber//属性 { get { returnnumber; } set { number=value; } } publicPointOrigin { get { returnorigin; } set { origin=value; } } publicvoidDisplay(Graphicsg)//显示块方法 { Penp=newPen(Color.Red); g.DrawRectangle(p,origin.X,origin.Y,5,5); } publicvoidUnDisplay(Graphicsg)//消除块方法 { Penp=newPen(Color.DodgerBlue);//建立背景色笔 g.DrawRectangle(p,origin.X,origin.Y,5,5); } } } namespace贪吃蛇 { classfloor { privatestaticintunit=5;//单位长度 privateintlength=80*unit;//场地长度 privateintwidth=50*unit;//场地宽度 privatePointdot;//场地左上角的位置 publicintscore;//分数 privateSnakes;//蛇对象字段 privateBeanbean1;//豆对象字段 publicfloor(Pointd)//构造函数,参数为场地左上角位置 { dot=d; s=newSnake(d,10); bean1=newBean(); bean1.Origin=newPoint(d.X+30,d.Y+30); } publicSnakeS { get { returns; } } publicvoidDisplay(Graphicsg)//显示场地方法 { Penp=newPen(Color.Red);//创建画笔,红色实例化 g.DrawRectangle(p,dot.X,dot.Y,length,width);//画出一个在dot位置,长宽为别为length和width的矩形 s.Display(g);//显示蛇 CheckBean(g);//检查豆是否被吃掉 bean1.Display(g);//显示豆 } publicvoidCheckBean(Graphicsg)//检查都是否被吃掉 { if(bean1.Origin.Equals(s.getHeadPoint))//判断豆的位置是否与蛇头的位置相同 { score=score+10;//加分数 this.displaybean(g);//显示新豆 s.Growth();//蛇自动增长 } } publicvoiddisplaybean(Graphicsg)//显示新豆 { bean1.UnDisplay(g);//消除原来的豆 bean1=randombean();//产生随机豆 bean1.Display(g);//显示新豆 } privateBeanrandombean()//产生随机豆方法 { Randomrandom=newRandom();//创建伪随机数对象并实例化 intx=random.Next(length/unit-2)+1; inty=random.Next(width/unit-2)+1; Pointd=newPoint(dot.X+x*5,dot.Y+y*5);//由场地位置和x,y随机整数实例化点对象 Beanbb=newBean(); bb.Origin=d; returnbb;//返回新豆 } publicvoidReSet(Graphicsg)//重新设置蛇方法 { s.UnDisplay(g);//消除以前蛇 s.Reset(dot,10);//重设置蛇 } publicboolCheckSnake()//检查蛇是否撞墙 { if((dot.X (dot.Y s.getHitSelf) { returnfalse; } else { returntrue; } } } } namespace贪吃蛇 { classPubClass { publicstaticintkk; } } namespace贪吃蛇 { publicclassSnake { ArrayListblockList;//字段 privateintheadNumber;//蛇头序数或蛇的长度 privatePointheadPoint;//蛇的位置(左上角坐标) privateintdirection=1;//0,1,2,3分别代表上,下,左,右 publicSnake() { // } publicSnake(Pointvertex,intcount) { Blockbb; Pointp=newPoint(vertex.X+25,vertex.Y+25);//定义起始位置 blockList=newArrayList(count);//初始数组长度为count for(inti=0;i { p.X=p.X+5;//x坐标加5 bb=newBlock();//实例化新块 bb.Origin=p;//块的初始位置 bb.Number=i+1;// blockList.Add(bb);//把块添加到blockList中 if(i==count+10)//如果是蛇头就把位置(顶点)赋给headPoint { headPoint=bb.Origin;//给蛇头位置赋值 } } headNumber=count;//给蛇头序数(蛇长度)赋值 } publicPointgetHeadPoint//只读蛇头位置属性 { get { returnheadPoint; } } publicboolgetHitSelf//只读蛇是否碰到自身 { get { IEnumeratormyEnumerator=blockList.GetEnumerator();//定义并实例化枚举接口 try { while(myEnumerator.MoveNext())//通过循环遍历蛇的各块 { Blockb=(Block)myEnumerator.Current;//读取当前块 if(b.Number! =headNumber&&b.Origin.Equals(headPoint))//当前块不是蛇头且与蛇头位置相同 { returntrue;//返回true } } } catch(Exceptione) { System.Console.WriteLine(e.ToString()); } returnfalse;//返回false } } publicintDirdection//蛇的运动方向属性 { get { returndirection; } set { direction=value; } } publicvoidTurnDirection(intpDirection)//蛇的转向方法,参数为蛇要改变的方向 { switch(direction) { case0: //初始方向向上 if(pDirection==3)//如果改变方向向左 { direction=3; } elseif(pDirection==1)//如果改变方向向右 { direction=1; } break; case1: if(pDirection==2) { direction=2; } elseif(pDirection==0) { direction=0; } break; case2: if(pDirection==3) { direction=3; } elseif(pDirection==1) { direction=1; } break; case3: if(pDirection==2) { direction=2; } elseif(pDirection==0) { direction=0; } break; } } publicvoidGrowth()//蛇的增长方法 { Blockbb=newBlock();//定义并实例化新块 IEnumeratormyEnumerator=blockList.GetEnumerator();//定义并实例化枚举接口变量 try { while(myEnumerator.MoveNext())//通过循环遍历当前蛇中块 { Blockb=(Block)myEnumerator.Current;//读取蛇的当前块 if(b.Number==headNumber)//如果蛇的当前块是蛇头 { intx=b.Origin.X;//读取当前块即蛇头的位置坐标 inty=b.Origin.Y; switch(direction)//根据当前运动方向设置新块坐标 { case0: y=y-5;//向上y坐标减5 break; case1: x=x+5; break; case2: y=y+5; break; case3: x=x-5; break; } Pointheadp=newPoint(x,y);//由坐标构造头位置点 bb.Origin=headp;//把点赋给新块的位置属性 bb.Number=b.Number+1;//把当前块的序数加1赋给新块的序数属性 blockList.Add(bb);//把新块添加的blockList中 headNumber++;//头块的序数(蛇的长度)增加 headPoint=headp;//给蛇头位置赋新值 } } } catch(Exceptione) { System.Console.WriteLine(e.ToString()); } } publicvoidDisplay(Graphicsg)//显示蛇的方法,参数为图形对象 { try { Blockb=newBlock();//定义并初始化新块b b=(Block)blockList[0];//取出blockList中的一个元素给b b.UnDisplay(g);//消除b块显示 blockList.RemoveAt(0);//从blockList中移出第一块 Blockbb=newBlock();//定义新块并初始化 IEnumeratormyEnumerator=blockList.GetEnumerator();//定义枚举接口并初始化 while(myEnumerator.MoveNext())//通过循环遍历数组 { b=(Block)myEnumerator.Current;//读取当前块并赋给b b.Number--;//当前块的序数属性值减1 if(b.Number==(headNumber-1))//如果是最后一块,当前增加一块 { intx=b.Origin.X; inty=b.Origin.Y; switch(direction) { case0: y=y-5; break; case1: x=x+5; break; case2: y=y+5; break; case3: x=x-5; break; } PointheadP=newPoint(x,y); bb.Origin=headP; bb.Number=headNumber; bb.Display(g); headPoint=bb.Origin;//重新指定蛇头的位置 } b.Display(g); } blockList.Add(bb); } catch(Exceptione) { System.Console.WriteLine(e.ToString()); } } publicvoidUnDisplay(Graphicsg)//消除蛇的方法 { try { Blockbb=newBlock();//定义并实例化新块 IEnumeratormyEnumerator=blockList.GetEnumerator();//定义枚举接口并初始化 while(myEnumerator.MoveNext())//通过循环遍历数组 { Blockb=(Block)myEnumerator.Current;//读取当前块 b.UnDisplay(g);//消除当前块 } } catch(Exceptione) { System.Console.WriteLine(e.ToString()); } } publicvoidRese
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 项目 整理
![提示](https://static.bdocx.com/images/bang_tan.gif)