c#实验三

c#实验三
c#实验三

实验三 WinForm程序设计

2012329620050

一、实验目的

1.掌握WinForm程序设计方法;

2.熟悉各种常用控件的使用;

3.掌握事件处理机制;

4.掌握绘制图形的基本过程和工具。

二、实验内容

(实验过程中编写的程序打包,下课整理后上交。)

1.泛型类的编写

编写一个泛型类Stats,用于一组数值(可以是整数、浮点数)的统计,包括平均数、均方根平均值。要求对泛型类型进行约束。这个类的使用如下:

static void Main(string[] args)

{

int[] inums = {1,2,3,4,5};

Stats iobj = new Stats(inums);

Console.WriteLine("平均值为:"+iobj.average());

double[] dnums = {1.1,2.2,3.3,4.4,5.5};

Stats dobj = new Stats(dnums);

Console.WriteLine("均方根平均值为:" + dobj.rms());

string[] snums = {"1","2","3","4","5"};

Stats sobj = new Stats(snums);

Console.WriteLine("平均值为:" + sobj.average());

}

提示:泛型类的约束如下:

public class DocumentManaget

where TDocument : IDocument { }//where子句指定泛型类型必需从接口IDocument继承本题中,可约束泛型类型从IConvertible继承。

程序:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace FanXing

{

class Stats

where T:IConvertible

{

T[] data;

public Stats(T[] a)

{

data = a;

}

public double average()

{

double all=0;

for (int i = 0; i < data.Length; i++)

{

double d = Convert.ToDouble(data[i]);

all += d;

}

return all / data.Length;

}

public double rms()

{

double all = 0;

for (int i = 0; i < data.Length; i++)

{

double d = Convert.ToDouble(data[i]);

all += d * d;

}

return Math.Sqrt(all/data.Length);

}

}

class Program

{

static void Main(string[] args)

{

int[] inums = { 1, 2, 3, 4, 5 };

Stats iobj = new Stats(inums);

Console.WriteLine("平均值为:" + iobj.average());

double[] dnums = { 1.1, 2.2, 3.3, 4.4, 5.5 };

Stats dobj = new Stats(dnums);

Console.WriteLine("均方根平均值为:" + dobj.rms());

string[] snums = { "1", "2", "3", "4", "5" };

Stats sobj = new Stats(snums);

Console.WriteLine("平均值为:" + sobj.average());

}

}

}

2.熟悉泛型集合类List的使用

1)随机产生20 个整数(10以内的),放入一个List中,遍历并输出这个List 2)删除其中为5 的数

3)再产生3 个整数,先放入一个新List,插入到位置4 处

4)把所有值为1 的数都变成10

程序:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace FanXing

{

class Listtest

{

public static void print(List a)

{

foreach (int b in a)

Console.Write(b + " ");

Console.WriteLine();

}

public static void deletFive(List a)

{

a.RemoveAll(x => x == 5);

}

public static void insert(List a)

{

List temp = new List();

Random ran = new Random();

for (int i = 0; i < 3; i++)

temp.Add(ran.Next(10));

a.InsertRange(4,temp);

}

public static void change(List a)

{

int count=0;

foreach (int c in a)

if (c == 1) count++;

for (int x = 0; x < count; x++)

{

int i = a.IndexOf(1);

a[i] = 10;

}

} }

class Program

{

static void Main(string[] args)

{

List data = new List();

Random ran = new Random();

for (int i = 0; i < 20; i++)

data.Add(ran.Next(10));

Console.WriteLine("初始List:");

Listtest.print(data);

Console.WriteLine("删除5元素后List:");

Listtest.deletFive(data);

Listtest.print(data);

Console.WriteLine("插入3个随机数后List:");

Listtest.insert(data);

Listtest.print(data);

Console.WriteLine("将1改成10后List:");

Listtest.change(data);

Listtest.print(data);

}

}

}

3.设计一个BatchForm类,从System.Windows.Forms.Form类中继承

(1)在该类中定义一个泛型方法AddBatchControl,用于向窗体加入一组T 类型的控件,要求类型参数T从Control类继承,并在泛型方法的各个参数中指定控件的文本、尺寸、位置、间隔,以及摆放的列数。方法声明为:public void AddBatchControl(string[] texts, int width, int height, int left, int top, int hSpan, int vSpan, int iCols);

提示:Form中有控件集合Controls,将新生成的控件加入即可。

(2)创建一个窗体应用程序,基类改为BatchForm。

(3)在主窗体构造函数中,分别通过AddBatchControl方法批量加入一组筛选框、单选框和按钮控件。

public Form1()

{

InitializeComponent();

string[] ss1 = new string[]{"足球", "蓝球", "短跑", "长跑", "登山", "游泳", "自行车", "羽毛球", "乒乓球", "举重"};

this.AddBatchControl(ss1, 60, 20, 10, 10, 5, 5, 4);

string[] ss2 = new string[]{"生产", "销售", "研发", "服务", "金融", "教育", "政府", "其他"};

this.AddBatchControl(ss2, 60, 20, 10, 100, 5, 5, 3);

string[] ss3 = new string[]{"确定", "放弃", "取消", "重试"};

this.AddBatchControl

相关文档
最新文档