对应用程序结构进行组织的程序示例2007-11-10 本站 上面我们介绍了对应用程序结构进行组织的方法,接下来我们举一个综合应用的例子。程序实现的功能非常简单,但其中用到了我们讲解的各方面的知识。在程序中,我们利用名字空间把应用程序功能进行分割,并且用到了在一个名字空间中包含多个类、在一个可执行文件中调用多个动态链接库的方法。程序中还用到了异常处理、类的继承、派生类对虚方法的重载、多态性的实现等概念,希望读者能够在阅读时认真注意这些用法,来加深对基本概念的理解。该程序是一个小游戏,游戏中随机产生矩形、正方形、直角三角形、等腰直角三角形四种图形。游戏开始给每个用户1000分,然后由用户自行押注,根据押注的多少和随机产生的图形面积计算用户赢取的分数。程序中用到了名字空间System中提供的类Random,该类提供了用于产生一个随机数的方法。程序清单16-3:
//MyShape.cs---源文件,用于定义图形类,作为其它图形的基类using System;namespace MyShape{ public class Shape { public virtual void Draw() {;} //虚方法,用于图形绘制 public virtual int GetArea(){ return 0; //虚方法,用于计算图形面积 } }}//rect.cs---源文件,用于定义矩形类和正方形类using System;namespace MyShape{ public class Rectangle:Shape //定义矩形类 {protected int a;protected int b; //矩形的边长public Rectangle(int va,int vb){ a=va; b=vb;}public override int GetArea() //重载虚方法,计算矩形面积{ int area=a*b; return area;}public overide void Draw() //重载虚方法,在屏幕上绘制矩形{ Console.WriteLine("Rectangle:"); Console.WriteLine("* * * * *"); Console.WriteLine("**"); Console.WriteLine("**"); Console.WriteLine("* * * * *"); } } public class Square:Rectangle //定义正方形类 {public Square(int va):base(va,va){;}public override void Draw() //重载,绘制正方形{ Console.WriteLine("Square"); Console.WriteLine("* * * * *"); Console.WriteLine("**"); Console.WriteLine("**"); Console.WriteLine("**"); Console.WriteLine("* * * * *"); } }}//triangle.cs---源文件,用于三角形using System;namespace MyShape{ //定义普通三角形,作为其它三角形的基类 public class Triangle:Shape {protected int a;protected int b;protected int c;public Triangle(int va,int vb,int vc){ a=va; b=vb; c=vc;}public override int GetArea(){ int s=(a+b+c)/2; int area=(int)(Math.Sqrt(s*(s-a)*(s-b)*(s-c))); return area;} } //定义直角三角形 public class RectTriangle:Triangle {new protected int a;new protected int b;public RectTriangle(int va,int vb):base(va,vb,(int)(Math.Sqrt(va*va+vb*vb))){ a=va; b=vb;}public override int GetArea(){ int area=(int)(a*b/2); return area;}public override void Draw(){ Console.WriteLine("RectTriangle"); Console.WriteLine("*"); Console.WriteLine("* *"); Console.WriteLine("**"); Console.WriteLine("* **); } } //定义等腰直角三角形 public class RectEqualTriangle:RectTriangle {new protected int a;public RectEqualTriangle(int va):base(va,va){ a=va;}public override int GetArea(){ int area=(int)(a*a/2); return area;}public override void Draw(){ Console.WriteLine("RectEqualTriangle"); Console.WriteLine("*"); Console.WriteLine("* *"); Console.WriteLine("**"); Console.WriteLine("**"); Console.WriteLine("* * * * *"); }}}//Mymessage.cs----源文件,用于定义程序显示的一些信息using System;namespace MyMessage{ public class Message {public void Begin(){ Console.WriteLine("**********************"); Console.WriteLine("****"); Console.WriteLine("**********************"); Console.WriteLine("* **** *"); Console.WriteLine("******"); Console.WriteLine("* **** *"); Console.WriteLine("** * ***"); Console.WriteLine("* ** * *"); Console.WriteLine("****"); Console.WriteLine("* ** *"); Console.WriteLine("** *"); Console.WriteLine("* SHAPE GAME *"); Console.WriteLine("**********************************"); } public bool Ask() {Console.WriteLine("Press 0 to exit the game");Console.WriteLine("Press any other key to continue the game");Console.WriteLine();int c=Console.Read();if(c==48)return false;return true;}}}//client.cs---客户程序using System;using MyShape;using MyMessage;class ClientTest{ public static void Main() {int score=1000; //总分int win; //每一局赢取的分数int choice; //随机获得的图形号int bet; //每一局下的注string s;Shape sp=new Shape();Random ran=new Random();Message msg=new Message();msg.Begin();while(true){ if(!msg.Ask()) break; Console.WriteLine("Your Score:{0}",score); Console.WriteLine("Enter your bet:"); Console.ReadLine(); s=Console.ReadLine();//如果押注的输入不正确,进行异常处理,并默认下注为100分 try{ bet=s.ToInt32(); } catch{ bet=100; } if(bet<score) score-=bet; else{ bet=score; score=0; } Console.WriteLine("Remain Score:{0}",score); win=0; for(int i=0;i<3;i++) {choice=ran.Next()%4; //随机数发生器switch(choice){ case 0: sp=new RectTriangle(5,4); goto end; case 1: sp=new RectEqualTriangle(5); goto end; case 2: sp=new Rectangle(5,4); goto end; case 3: sp=new Square(5); } end: //利用多态性,计算得分sp.Draw(); win+=sp.GetArea()*(i+1)*bet/100; Console.WriteLine("Your win:{0}",win);} score+=win; Console.WriteLine("Your Score:{0}",scroe); if(score<100) {Console.WriteLine("Your remain score is not enough to play");break;} } }}
对源代码进行编译的命令为:csc/target:library /out:MyShape.dll MyShape.cs rect.cs triangle.cscsc/target:library /out:MyMessage.dll MyMessage.cscsc/reference:MyShape.dll;MyMessage.dll client.cs