您的位置:首页 > 编程语言 > C#

[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# class string list null