[c#作业] 9.24
2011-09-24 22:17
120 查看
//1.p179_13
using System; namespace p179_13 { class Program { static void Main(string[] args) { Rectangle rec = null; My_list ml = new My_list(); int count = 0; string str1, str2; double len, wid; bool flag = true; Console.WriteLine("是否要输入数据?"); str1 = Console.ReadLine(); if (str1[0] == 'n') { flag = false; } while (flag) { Console.WriteLine("请输入矩形的长和宽"); str1 = Console.ReadLine(); len = Convert.ToDouble(str1); str2 = Console.ReadLine(); wid = Convert.ToDouble(str2); rec = new Rectangle(len, wid); ml[count] = rec; count++; Console.WriteLine("是否要输入数据?"); str1 = Console.ReadLine(); if (str1[0] == 'n') { flag = false; } } for (int i = 0; i < count; i++) { Console.WriteLine("the length and width of ml[{0}] is {1}, {2}", i, ml[i].Length, ml[i].Width); Console.WriteLine("the area of ml[{0}] is {1}", i, ml[i].get_area()); Console.WriteLine("********************"); } } } class Rectangle { private double length, width; public Rectangle() { } public Rectangle(double l, double w) { length = l; width = w; } public double Length { get { return length; } set { length = value; } } public double Width { get { return width; } set { width = value; } } public double get_area() { return length * width; } } class My_list { Rectangle[] rec_list = new Rectangle[100]; static int size = 100; public My_list() { for (int i = 0; i < size; i++) rec_list[i] = new Rectangle(); } public Rectangle this[int index] { get { Rectangle rec = null; if (index >= 0 && index <= size - 1) { rec = rec_list[index]; } return rec; } set { if (index >= 0 && index <= size) { rec_list[index] = value; } } } } }
//2.p225_25
using System; namespace P225_25 { class Meteorologist { public Meteorologist(int[] rf, int[] po) { for (int i = 0; i < 12; i++) { rainfall[i] = rf[i]; pollution[i] = po[i]; } } public int[] rainfall = new int[12]; public int GetRainfall(int index) { int temp = 0; try { temp = rainfall[index]; } catch(IndexOutOfRangeException e) { Console.WriteLine(e.ToString()); } return temp; } public int[] pollution = new int[12]; public double GetAveragePollution(int index) { double averagePollution = 0; try { averagePollution = (double)pollution[index] / rainfall[index]; } catch (IndexOutOfRangeException e) { e.ToString(); } catch (DivideByZeroException e) { e.ToString(); } finally { Console.WriteLine("Closing WeatherXYZ file"); } return averagePollution; } } class Program { static void Main(string[] args) { //int[] arr = new int[10]; //Console.WriteLine(arr[10]); int[] arr1 = new int[12]; int[] arr2 = new int[12]; for (int i = 0; i < 12; i++) { arr1[i] = 100 * i; arr2[i] = 10 * i; } //double.pa Meteorologist me = new Meteorologist(arr1, arr2); double temp = me.GetAveragePollution(2); Console.WriteLine(temp); } } }
//3.p290_21
using System; namespace p290_21 { public delegate void Del(); public class BigCat { public void Cry() { Console.WriteLine("肥猫看见老鼠后大叫!"); } } public class Host { public static void awake() { Console.WriteLine("主人醒了!"); } } public class Mouse { public static void awake() { Console.WriteLine("老鼠受到惊吓!"); } } class Program { static void Main(string[] args) { Del d = null; BigCat bc = new BigCat(); Console.WriteLine("肥猫是否发现老鼠?"); string str; str = Console.ReadLine(); if (str[0] == 'y') { bc.Cry(); //Del d = null; d += Host.awake; d += Mouse.awake; d(); } } } }
//修改后的如下,用事件完成。。 using System; namespace p290_21 { public delegate void Del(); public class BigCat { event Del E; public void Cry() { Console.WriteLine("肥猫看见老鼠后大叫!"); } static void Main(string[] args) { //Del d = null; BigCat bc = new BigCat(); Console.WriteLine("肥猫是否发现老鼠?"); string str; str = Console.ReadLine(); if (str[0] == 'y') { bc.E += new Del(bc.Cry); bc.E += new Del(Host.awake); bc.E += new Del(Mouse.awake); //bc.Cry(); //Del d = null; //d += Host.awake; //d += Mouse.awake; //d(); bc.E(); Console.ReadLine(); } } } public class Host { public static void awake() { Console.WriteLine("主人醒了!"); } } public class Mouse { public static void awake() { Console.WriteLine("老鼠受到惊吓!"); } } class Program { } }
相关文章推荐
- C#第三次作业
- 软件工程第四周作业之四则运算-C#实现
- 大一下C#五子棋大作业
- C#第六次作业:猜猜看
- c#第三次作业——Excel数据读取 及 HTML文件初步(目标三)
- 第三次C#作业-Excel数据读取及HTML文件初步
- c# 作业1
- C#第三次作业 Excel数据读取 及 HTML文件初步
- 《C#程序设计》第三次作业 C#读取Excel文件,并保存为文本文件
- 作业:C#二维数组取最大值
- C#作业补充(6)
- C# 第九次作业
- 作业四:SQL数据库及C#操作SQL数据库——Winform程序
- c#小作业 解一元二次方程
- C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount)
- C#第四次作业
- c#第一次作业
- 【C#基础】【语法03】课后作业题 01-06
- c#第三次作业 C#读取Excel文件,并保存成HTML格式
- C# 第八次作业