Example of a program that organizes an application structure

Source: Internet
Author: User
Tags exception handling goto readline

The above we introduce the method of organizing the application structure, and then we give an example of a comprehensive application. The function of the program implementation is very simple, but it uses the knowledge of all aspects that we explain. In the program, we use namespaces to split the application functionality, and we use a method that contains multiple classes in one namespace and calls multiple dynamic link libraries in an executable file. In the program, we also use the concepts of exception handling, inheritance of classes, overloading of virtual methods and implementation of polymorphism, and hope that readers can pay attention to these usages when reading the program to deepen their understanding of basic concepts.

The program is a small game, the game randomly produced rectangular, square, right-angled triangle, isosceles right-angled triangle four kinds of graphics. The game starts with 1000 points for each user, then the user bets on the number of bets and the randomly generated graphic area to calculate the user's winning score. The program uses the class random provided in the namespace system, which provides a method for generating a random number.

Program Listing 16-3:

MyShape.cs---Source file that defines the graphic class as the base class using System for other graphics; Namespace MyShape {public class Shape {public virtual void Draw () {;}//virtual method, for drawing public virtual int Getarea

() {return 0;//virtual method, used to compute the graphics area}}//rect.cs---Source file, used to define rectangular class and square class using System;
  Namespace MyShape {public class Rectangle:shape//Definition Rectangle class {protected int A; protected int B;
   The rectangle's edges public Rectangle (int va,int vb) {A=va;
  B=VB;
   public override int Getarea ()//overloaded virtual method, compute rectangle area {int area=a*b;
  return area;
   Public overide void Draw ()//overloaded virtual method, drawing rectangle on screen {Console.WriteLine ("Rectangle:");
   Console.WriteLine ("* * * * * *");
   Console.WriteLine ("* * *");
   Console.WriteLine ("* * *");
   Console.WriteLine ("* * * * * *");
  } public class Square:rectangle//Definition square class {public square (int va): base (Va,va) {;}
   public override void Draw ()//overload, Draw square {Console.WriteLine ("Square");
   Console.WriteLine ("* * * * * *"); Console.WriteLine ("* *"));
   Console.WriteLine ("* * *");
   Console.WriteLine ("* * *");
   Console.WriteLine ("* * * * * *");
//triangle.cs---Source file for the triangle using System;
  namespace MyShape {//defines a common triangle, as the base class of other triangles 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;
  }//define the right-angled triangle 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 ("* * * *)"; }///define Isosceles Right AngleTriangle 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----source file to define some of the information that the program displays 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 Boolean 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---client using System;
Using MyShape;
Using Mymessage; Class Clienttest {public static void Main () {int score=1000;//total int win;//per innings winning score int choice;//randomly obtained graphic number I NT bet;
  Note String s under each inning;
  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 ();
   If the input of the bet is incorrect, the exception is handled, and the default bet is 100 minutes 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;
       Random number generator 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://Use polymorphism to compute the score 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 isn't enough to play");  Break }
     }
   }
}

The command to compile the source code is:

Csc/target:library/out:myshape.dll MyShape.cs Rect.cs Triangle.cs

Csc/target:library/out:mymessage.dll MyMessage.cs

Csc/reference:myshape.dll; MyMessage.dll Client.cs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.