/** * * Description: * <br/>Copyright (2),2009-03,Chevi.C * <br/>This program is protected by copyright laws. * <br/>Program Name: console画图程序 * @author Chevi.C [email protected] * @version 1.0 * * */ namespace CheviC { using System; using System.Threading; //形状类(抽象类) public abstract class Shape { private String star = "*"; //画图元素,默认为* //设置画图元素 public void SetStar(String star) { this.star = star; } //取画图元素 public String GetStar() { return star; } private int rows = 0; //图形的行数 private int cols = 0; //图形的列数 //rows的属性 public int Rows { set { rows = value; } get { return rows; } } //cols的属性 public int Cols { set { cols = value; } get { return cols; } } public abstract void DispSolid(); public abstract void DispHollow(); public virtual void Require() { Console.WriteLine("/n/n以下为画出该图形的要求:"); } } //图型类结束 //正方形类 public class Square : Shape { //重写画实体函数 public override void DispSolid() { for (int i = 0; i < Rows; i++) { for (int j = 0; j < Rows; j++) { Console.Write(" "+GetStar()); } Console.WriteLine(); } } //重写画空心函数 public override void DispHollow() { for (int i = 0; i < Rows; i++) { for (int j = 0; j <Rows; j++) { if (j == 0 || i == 0 || j == (Rows - 1) || i == (Rows - 1)) { Console.Write(" " + GetStar()); } else { Console.Write(" "); } } Console.WriteLine(); } } //重写画图规则 public override void Require() { base.Require(); bool trun=true; //设置如果输入错误继续循环的变量。 Console.Write("/n/t/t您选择了画正方形/n/t/t请输入正方形的高(即行数),(注意,高只能小于40):"); while (trun) { Rows = int.Parse(Console.ReadLine()); if (Rows > 40 || Rows <= 0) { Console.Write("/t/t输入的数字超出范围,请重新输入:"); } else trun = false; } trun = true; while (trun) { Console.Write("/n/t/t默认图形元素为/"*/",是否更改?/n/t/t/t1.是/t2.否/t"); int selecter = int.Parse(Console.ReadLine()); if (selecter == 1) { Console.Write("/t/t请输入你想要的图形:"); SetStar(Console.ReadLine()); trun = false; } else if (selecter == 2) { trun = false; } else if (selecter != 2) { Console.WriteLine("/n/t/t提示:/n/t/t输入错误,请重新输入:"); } } trun = true; while (trun) { Console.Write("/n/t/t/t1.画实心正方形./n/t/t/t2.画空心正方形./t"); int chooser = int.Parse(Console.ReadLine()); switch (chooser) { case 1: DispSolid(); trun = false; break; case 2: DispHollow(); trun = false; break; default: Console.WriteLine("/n提示:/n输入错误,请重新输入:"); break; } } } } //正方形类结束 //长方形类 public class Rectangle : Shape { //重载画实心长方形函数 public override void DispSolid() { for (int i = 0; i < Rows; i++) { for (int j = 0; j <Cols; j++) { Console.Write(" "+GetStar()); } Console.WriteLine(); } } //重载画空心长方形函数 public override void DispHollow() { for (int i = 0; i < Rows; i++) { for (int j = 0; j < Cols; j++) { if (j == 0 || i == 0 || j == (Cols - 1) || i == (Rows - 1)) { Console.Write(GetStar()); } else { Console.Write(" "); } } Console.WriteLine(); } } //重载画图规则 public override void Require() { base.Require(); bool trun = true; while (trun) { Console.Write("/n/t/t您选择了画长方形/n/t/t请输入长方形的 高 :"); Rows = int.Parse(Console.ReadLine()); Console.Write("/t/t请输入长方形的宽:(注意,宽只能小于80):"); Cols = int.Parse(Console.ReadLine()); if (Cols >= 80 || Cols < 0 || Rows < 0) { Console.WriteLine("/t输入的高或宽超出指定范围,请重新输入:"); } else trun = false; } trun = true; while (trun) { Console.Write("/n/t/t默认图形元素为/"*/",是否更改?/n/t/t/t1.是/t2.否/t"); int selecter = int.Parse(Console.ReadLine()); if (selecter == 1) { Console.Write("/t/t请输入你想要的图形:"); SetStar(Console.ReadLine()); trun = false; } else if (selecter == 2) { trun = false; } else if (selecter != 2) { Console.WriteLine("/n/t/t提示:/n/t/t输入错误,请重新输入:"); } } trun = true; while (trun) { Console.Write("/n/n/t/t/t1.画实心长方形./n/t/t/t2.画空心长方形./t"); int chooser = int.Parse(Console.ReadLine()); switch (chooser) { case 1: DispSolid(); trun = false; break; case 2: DispHollow(); trun = false; break; default: Console.WriteLine("/n/t/t提示:/n/t/t输入错误,请重新输入:"); trun = true; break; } } } } //长方形类结束 //三角形类 public class Triangle : Shape { //重载画实心三角形函数 public override void DispSolid() { Console.WriteLine("/t/t**********************"); Console.WriteLine("/t/t* 1.输出正等腰三角形.*"); Console.WriteLine("/t/t* 2.输出倒等腰三角形.*"); Console.WriteLine("/t/t* 3.输出正直角三角形.*"); Console.WriteLine("/t/t**********************"); int switcher = int.Parse(Console.ReadLine()); switch (switcher) { //正三角形 case 1: int MaxStar = Rows * 2 - 1; //计算出这么高的三角型的最底层的星星数. for (int i = 0; i < Rows; i++) //画行. { int s = 1; //控制空格的数量,当s=0后,不再输出空格. for (int j = 0; j < MaxStar; j++) //画列 { if (j == ((MaxStar + 1) / 2 - i)) //第一行开始的星星是在第(MaxStart+1)/2的位置,下一行比上一行前一位,所以-i; { for (int k = 0; k < ((2 * i) + 1); k++) //计算该行的星星的个数; { Console.Write(GetStar()); s = 0; } } if (s == 1) //当s=0后,不再输出空格. { Console.Write(" "); } } Console.WriteLine(); } break; //倒三角形 case 2: int Max = Rows * 2 + 1; int trun = 1; //用来处理画完星后不再画空格 for (int i = 0; i < Rows; i++) //画行 { for (int j = 0; j < Max; j++) //画列 { if (j == i) //行是第几行,就缩进几列 { for (int k = (Max - 2 * (i + 1)); k > 0; k--) //画每行的星星数 { Console.Write(GetStar()); } trun = 0; //使画完星星后不再画空格 } if (trun == 1) //未画星星前画空格 { Console.Write(" "); } } trun = 1; Console.WriteLine(); } break; //直角三角形 case 3: for (int i = 0; i < Rows; i++) { for (int j = 0; j < (i * 2); j++) { Console.Write(GetStar()); } Console.WriteLine(); } break; default: break; } } //重载画空心三角形函数 public override void DispHollow() { int Max = Rows * 2 + 1; //一行中最多的星星数 for (int i = 0; i < Rows; i++) //画行 { int count = 0; //计数器,用来如果星星超过两个就不再填充空格,而换行 for (int j = 0; j < Max; j++) //画列 { if (j == (Max / 2 - i) || j == (Max / 2 + i)) //如果j到达该画的位置,位置除了第一行和最后一行每行只有两颗. { if (j == (Max / 2 - Rows + 1)) //如果到了最后一行 { for (int k = 0; k < (Max - 2); k++) //画出最后一行的星星,Max-2我也不知道为什么减二,可是减二是对的 Console.Write(GetStar()); break; } else { Console.Write(GetStar()); count++; //计数器加,防止画多余的空格 } } else if (count < 2) { Console.Write(" "); } } Console.WriteLine(); } } //重载画图规则 public override void Require() { base.Require(); Console.Write("/n/t/t*****您选择了画三角形*****/n/t/t*****请输入三角形的行高(注意,行高只能小于40):"); bool trun = true; while (trun) { Rows = int.Parse(Console.ReadLine()); if (Rows >= 40 || Rows < 0) { Console.WriteLine("/n/t/t提示:/t输入的高超出要求范围,请重新输入:"); } else trun = false; } trun = true; while (trun) { Console.Write("/n/t/t*****默认图形元素为/"*/",是否更改?/n/t/t/t/t1.是/t2.否/t"); int selecter = int.Parse(Console.ReadLine()); if (selecter == 1) { Console.Write("/t/t请输入你想要的图形:"); SetStar(Console.ReadLine()); trun = false; } else if (selecter == 2) { trun = false; } else if (selecter != 2) { Console.WriteLine("/n/t/t提示:/n/t/t输入错误,请重新输入:"); } } trun = true; while (trun) { Console.WriteLine("/n/t*****1.画实心三角形./n/t*****2.画空心三角形."); int chooser = int.Parse(Console.ReadLine()); switch (chooser) { case 1: DispSolid(); trun = false; break; case 2: DispHollow(); trun = false; break; default: Console.WriteLine("/n提示:/n输入错误.请重新输入:"); break; } } } } //画图类(主类) public class Drawer { //显示菜单函数 public void DispManu() { Console.WriteLine("/n/n/t/t*******************************************"); Console.WriteLine("/t/t* 欢迎来到Chevi.C画图形程序 *"); Console.WriteLine("/t/t* 1.画正方形 *"); Console.WriteLine("/t/t* 2.画长方形 *"); Console.WriteLine("/t/t* 3.画三角形 *"); Console.WriteLine("/t/t* 4. 清屏 *"); Console.WriteLine("/t/t* 5. 退出 *"); Console.WriteLine("/t/t*******************************************"); } //选择变量 public int switcher = 0; public void Setter() { Console.Write("请输入你的选择:"); switcher = int.Parse(Console.ReadLine()); switch (switcher) { case 1: Square squarer = new Square(); squarer.Require(); break; case 2: Rectangle rectangle = new Rectangle(); rectangle.Require(); break; case 3: Triangle triangle = new Triangle(); triangle.Require(); break; case 4: Console.Clear(); break; case 5: Console.WriteLine("谢谢你的使用. 3秒后自动退出程序."); Console.Write("/t/t"); for (int i = 3; i > 0; i--) { Console.Write(i); Thread.Sleep(1000); Console.Write("/b"); } //Thread.Sleep(3000); Environment.Exit(0); //退出程序函数 break; default: Console.WriteLine("/n提示:/n输入错误."); break; } } //入口函数 public static void Main() { while (true) { Drawer drawer = new Drawer(); drawer.DispManu(); drawer.Setter(); } } } } 这个比上次写的改进了一些结构上的东西,算法上没怎么变化,我写出来后老师竟然说不要用goto,我看书快点,见书上说C#中可以用goto,这样以为真的可以用了,原来这真的会打乱结构。这次我全改为while形式。还好,对while我更熟练了。