前言
这周跟c#打了一周的交道(本周是学校安排的实验周,然后用到了c#实现想要的程序和功能)
一共七个实验,选择三个,我就选择我进步最大的一个来分析一下吧。
效果
先来看一下效果吧
从txt文本中读取数据后展示出来
点击目标后选中,然后点击“修改”,弹出修改界面,然后进行编辑即可
点击“统计”按钮,弹出窗口显示各分数段的信息
点击“查询”后,弹出界面,输入后,点击“确定”即可显示信息
实现
一、准备工作
在写方法之前,首先就是先把界面规划好,就是通过添加按钮和输入框或显示框组成一个符合要求的窗口或多个窗口
在这个程序中我们用到的主要是这几个组件
对文件进行操作要进行引用的声明,即“using”
我们添加的是这两行
然后我们还要写一些代码来实现其他功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public form1() { initializecomponent(); this .listview1.columns.add( "学号" , 100, horizontalalignment.center); this .listview1.columns.add( "姓名" , 100, horizontalalignment.center); this .listview1.columns.add( "数学" , 100, horizontalalignment.center); this .listview1.columns.add( "英语" , 100, horizontalalignment.center); this .listview1.columns.add( "政治" , 100, horizontalalignment.center); this .listview1.columns.add( "总分" , 100, horizontalalignment.center); this .listview1.columns.add( "平均分" , 100, horizontalalignment.center); this .listview1.columns.add( "名次" , 100, horizontalalignment.center); this .listview1.view = system.windows.forms.view.details; this .listview1.fullrowselect = true ; //是否可以选择行 } |
“listview1”就是按钮上方实现显示的控件,“this”指的就是form1这个窗口,“columns”指的是“栏”,也就是上方的内容,“add”指的是把后面的内容作为“columns”的内容,后面的“100”等都是“columns”的属性,可以通过修改它的属性来修改它的大小和位置,还有一种生成“column”的方法是通过属性栏来添加。
点击listview一次选中它,然后右键单击一次,点击属性,会发现有“column”这个属性,点进去后就可以进行编辑和修改了。
不得不说确实挺方便的,不过实验报告手册中给了部分必须的源码,再加上自己第一次接触c#,所以就没使用后面的方法,不过在后面的操作中使用了一下,确实挺爽。
二、读取操作
这里的“读取”按钮读取的是统计之后的内容,并非成绩等信息,双击“读取”按钮后即可进行编辑(在按钮的属性中我修改了name属性为load,所以此处的方法名为“load_click”)
1
2
3
4
|
private void load_click( object sender, eventargs e) { this .load_data(); } |
此处调用了“load_data()”这个方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public void load_data() { string file = file.readalltext( "score.txt" , utf8encoding. default ); //把txt文件中按行存储的信息利用regex.split按行存入string数组中 string [] records = regex.split(file, "\r\n" ); //开始更新视图 this .listview1.beginupdate(); //清空原有视图 this .listview1.items.clear(); // records.length为数组的元素个数 for ( int index = 0; index < records.length; index++) { //分割每行记录的各个字段 string [] components = regex.split(records[index], " " ); //生成listview的一行 listviewitem lvi = new listviewitem(components); //添加背景色 lvi.subitems[0].backcolor = color.red; //把新生成的行加入到listview中 this .listview1.items.add(lvi); } //视图更新结束 this .listview1.endupdate(); } |
这个方法就是以“/r/n”为分界线定义一个数组1,然后再以空格为分界线定义一个数组2,同时生成一个 listviewitem 来显示数组2,然后再设置一下背景色,此处设置的为红色
1
2
3
4
|
//生成listview的一行 listviewitem lvi = new listviewitem(components); //添加背景色 lvi.subitems[0].backcolor = color.red; |
lvi是新生成的listview的命名
三、查询操作
1
2
3
4
5
|
private void search_click( object sender, eventargs e) { form3 f3 = new form3(); f3.show(); } |
在查询的方法中我们调用了一个窗口form3,同form1一样,先规划好窗口的格局,然后再写方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
private void go_click( object sender, eventargs e) { string file = file.readalltext( "score.txt" , utf8encoding. default ); //把txt文件中按行存储的信息利用regex.split按行存入string数组中 string [] records = regex.split(file, "\r\n" ); //开始更新视图 this .listview1.beginupdate(); //清空原有视图 this .listview1.items.clear(); // records.length为数组的元素个数 for ( int index = 0; index < records.length; index++) { //分割每行记录的各个字段 string [] components = regex.split(records[index], " " ); regex r = new regex( this .textbox1.text); // 定义一个regex对象实例 match m = r.match(components[0]); // 在字符串中匹配 if (m.success) { //生成listview的一行 listviewitem lvi = new listviewitem(components); //添加背景色 lvi.subitems[0].backcolor = color.white; //把新生成的行加入到listview中 this .listview1.items.add(lvi); } else if (components.length > 1) { match n = r.match(components[1]); if (n.success) { //生成listview的一行 listviewitem lvi = new listviewitem(components); //添加背景色 lvi.subitems[0].backcolor = color.white; //把新生成的行加入到listview中 this .listview1.items.add(lvi); } } } //视图更新结束 this .listview1.endupdate(); } |
主要的方法,甚至是唯一的方法,就是上方的“go_click()”方法,“score.txt”是存放成绩等信息的,在姓名和学号之中匹配,匹配到了就新建一个listview的item展示信息
这里的查询用到了匹配的函数match() ,在此给出官方的链接
match()
四、删除
删除的思想就是选中要删除的那一行,之后从文件读取所有内容,新建string数组存放每一行,一行一行的遍历,直到遇见选中的那一行,从数组中移除,然后把剩余的数组元素写入文本中,writealllines()方法定义中调用了replace()方法,因此省去了我们删除文件原有内容的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private void delate_click( object sender, eventargs e) //删除 { foreach (listviewitem lvi in listview1.selecteditems) { //把txt文件内容读入到file中,然后对string对象file进行相关处理 string file = file.readalltext( "score.txt" , utf8encoding. default ); list< string > lines = new list< string >(regex.split(file, "\r\n" )); lines.removeat(lvi.index); file.writealllines( "score.txt" , lines.toarray(), utf8encoding. default ); } this .load_data(); } |
五、录入
1
2
3
4
5
|
private void input_click( object sender, eventargs e) //录入 { form2 f = new form2( this ); f.show(); } |
这里的录入方法调用了一个新的窗口form2,与之前不同的是,这里给form2传了一个参数“this”,即form1这个窗口,因为之后我们要在form2的方法中调用form1的方法。
再来看一下form2的界面和方法
1、接收参数
1
2
3
4
5
6
7
8
9
10
|
private form1 form; public form2() { initializecomponent(); } public form2(form1 form){ this .form = form; initializecomponent(); } |
首先进行定义,然后进行赋值,这样的话在之后就可以用定义时使用的名称进行form1的方法的调用了。
2、保存信息
在点击“保存”按钮后,我们要实现文本的写入以及界面的更新,文本的写入我们要写新的方法,界面的更新我们可以调用form1的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
if ( this .textbox1.text != string .empty && this .textbox2.text != string .empty && this .textbox3.text != string .empty && this .textbox4.text != string .empty && this .textbox5.text != string .empty) { //利用string的加法操作构造新纪录,作为一行,写入txt文件中 string newrecord = this .textbox1.text + " " + this .textbox2.text + " " + this .textbox3.text + " " + this .textbox4.text + " " + this .textbox5.text + "\r\n" ; file.appendalltext( "score.txt" , newrecord, utf8encoding. default ); //结束form2的调用 this .dispose( false ); //调用form1加载界面的方法 this .form.load_data(); } else { form5 f5 = new form5(); f5.show(); } |
首先判空,如果都不为空,定义新的字符串,赋值,写入文本,为了保证程序的严谨性,加一个form5窗口进行错误的提示
(由于时间的限制,这里也不算很完善,有能力的可以考虑进行各个条件的判断,例如“学号”为空,则弹出的窗口显示“学号不能为空”)
值得注意的是,这里调用的form1中的方法必须是公有的,即“public”。
六、运算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
private void operate_click( object sender, eventargs e) { //把txt文件内容读入到file中,然后对string对象file进行相关处理 string file = file.readalltext( "score.txt" , utf8encoding. default ); //把txt文件中按行存储的信息利用regex.split按行存入string数组中 string [] records = regex.split(file, "\r\n" ); //开始更新视图 this .listview1.beginupdate(); //清空原有视图 this .listview1.items.clear(); system.io.file.writealltext( "score.txt" , string .empty); double [] score1 = new double [records.length]; double [] score2 = new double [records.length]; int num = 0; // records.length为数组的元素个数 for ( int index = 0; index < records.length; index++) { if (records[index].length != 0) { //分割每行记录的各个字段 string [] components = regex.split(records[index], " " ); score1[index] = convert.todouble(components[2]) + convert.todouble(components[3]) + convert.todouble(components[4]); score2[index] = (convert.todouble(components[2]) + convert.todouble(components[3]) + convert.todouble(components[4])) / 3.0; num++; } else break ; } //冒泡法排序 int temp; for ( int i = 0; i < num; i++) { temp = i; for ( int j = i + 1; j < num; j++) { if (score1[j] > score1[temp]) temp = j; } double t = score1[temp]; score1[temp] = score1[i]; score1[i] = t; } for ( int index = 0; index < records.length; index++) { if (records[index].length != 0) { //分割每行记录的各个字段 string [] components = regex.split(records[index], " " ); for ( int i = 1; i <= num; i++) { if (score1[i - 1] == convert.todouble(components[2]) + convert.todouble(components[3]) + convert.todouble(components[4])) { //利用string的加法操作构造新纪录,作为一行,写入txt文件中 string newrecord = components[0] + " " + components[1] + " " + components[2] + " " + components[3] + " " + components[4] + " " + score1[i - 1] + " " + score2[index] + " " + i + "\r\n" ; file.appendalltext( "score.txt" , newrecord, utf8encoding. default ); } } } else break ; } //视图更新结束 this .listview1.endupdate(); //刷新界面 this .load_data(); } |
题目的要求是计算各学生总分、平均分以及排名,思想就是,先读取文件,存储数据,清空文件,定义总成绩和平均成绩两个数组,分别计算每个人的总成绩与平均成绩,然后用冒泡法对总成绩进行排序,得到排名,然后构造新数组拼接这些内容,再把数组写入文本,更新视图即可。
七、修改
修改的思想很简单,虽说实现了想要的功能,但是也有缺陷,也没想到特别好的解决办法,先来说一下思路吧,首先选中,然后点击“修改”,弹窗,在这里尤其注意得是,弹窗里对应位置要显示选中的内容
在这个图中,我选中的是第二行,那么我就要把它对应的数据显示上去,在实验过程中很多人所谓的修改就是输入新的数据,里面所有的内容都得再写一遍,这样的系统,自己用着指定难受。
(说到这我想插上一件事,吐槽一下最近使用的二课系统吧,是一个微信小程序,这是学校某个团队开发的,总体上还是可以的,最大的不足就是每次打开都得登录,也就是说每次使用都得重新输入账号和密码,用户的体验真的挺差的,但是也不能因为它有不足就否认它,总体上也算挺方便的,值得去学习)
因为以前参与过项目的开发,而且项目的受众就是我们自己,也知道什么样的效果更能使用户满意,所以对修改这个功能也算是有所了解,在实验中,绝大多数人的“修改”功能都没能达到满意的效果,所以说经验积累绝对不是毫无用处的。
继续回到我们的功能实现中来,点击“确定”后,写入文本,那么我们肯定要把之前的数据删掉,在传参后立即删除数据,当然这也就产生了瑕疵,用户不修改数据,那怎么办,先来看代码吧,看完就知道了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
private void modify_click( object sender, eventargs e) //修改 { if (listview1.selecteditems.count != 0 ) { listviewitem all = this .listview1.selecteditems[0]; form6 f6 = new form6(all, this ); f6.show(); //把数据传输过去之后立即删除数据 foreach (listviewitem lvi in listview1.selecteditems) { //把txt文件内容读入到file中,然后对string对象file进行相关处理 string file = file.readalltext( "score.txt" , utf8encoding. default ); list< string > lines = new list< string >(regex.split(file, "\r\n" )); lines.removeat(lvi.index); file.writealllines( "score.txt" , lines.toarray(), utf8encoding. default ); } } else { form4 f4 = new form4(); f4.show(); } } |
如果选中的内容不为空,那就把它的数据存到listviewitem类型的数组中,然后传给form6(修改数据窗口的类),同时再把form1这个类传过去,以便调用它的方法,然后使用一个删除方法,如果内容为空,弹出form4,提示错误。
同样的,在form6这边先定义,然后接收参数,赋值
1
2
3
4
5
6
7
8
9
|
private void form6_load( object sender, eventargs e) { this .textbox1.text = this .content.subitems[0].text; this .textbox2.text = this .content.subitems[1].text; this .textbox3.text = this .content.subitems[2].text; this .textbox4.text = this .content.subitems[3].text; this .textbox5.text = this .content.subitems[4].text; } |
在加载方法中为textbox赋值,以此来显示数据,修改后点击“确定”,确定的方法和“录入”功能的“确定”的方法相同,说白了就是写入,如果“取消”怎么办,数据已经删除了,既然textbox里本来就有数据,那么就把那些数据再写入文本,这样虽然实现了我的功能,但是有瑕疵,就是无论修改数据与否,重新加载后数据都会显示在最后一行。
“确定”方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
private void determine_click( object sender, eventargs e) { if ( this .textbox1.text != string .empty && this .textbox2.text != string .empty && this .textbox3.text != string .empty && this .textbox4.text != string .empty && this .textbox5.text != string .empty) { //利用string的加法操作构造新纪录,作为一行,写入txt文件中 string newrecord = this .textbox1.text + " " + this .textbox2.text + " " + this .textbox3.text + " " + this .textbox4.text + " " + this .textbox5.text + "\r\n" ; file.appendalltext( "score.txt" , newrecord, utf8encoding. default ); var lines = file.readalllines( "score.txt" ).where(arg => ! string .isnullorwhitespace(arg)); file.writealllines( "score.txt" , lines); //结束form6的调用 this .dispose( false ); } this .form.load_data(); } |
八、统计
所谓的统计就是计算各科各分数段人数以及各科平均分
1
2
3
4
5
6
7
|
private void sta_click( object sender, eventargs e) //统计 { this .output(); form7 f7 = new form7(); f7.show(); } |
调用一个output()方法,用form7显示数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public string output() //写入数据 { string add = string .empty; add += "统计情况:" + "\r\n" + "分数段" + "," + "数学" + "," + "英语" + "," + "政治" + "\r\n" ; add += "<60" + "," + getgradesection(1, 0, 59).tostring() + "," + getgradesection(2, 0, 59).tostring() + "," + getgradesection(3, 0, 59).tostring() + "\r\n" ; add += "60~69" + "," + getgradesection(1, 60, 69).tostring() + "," + getgradesection(2, 60, 69).tostring() + "," + getgradesection(3, 60, 69).tostring() + "\r\n" ; add += "70~79" + "," + getgradesection(1, 70, 79).tostring() + "," + getgradesection(2, 70, 79).tostring() + "," + getgradesection(3, 70, 79).tostring() + "\r\n" ; add += "80~89" + "," + getgradesection(1, 80, 89).tostring() + "," + getgradesection(2, 80, 89).tostring() + "," + getgradesection(3, 80, 89).tostring() + "\r\n" ; add += "90~100" + "," + getgradesection(1, 90, 100).tostring() + "," + getgradesection(2, 90, 100).tostring() + "," + getgradesection(3, 90, 100).tostring() + "\r\n" ; add += "平均分" + "," + getave(1).tostring() + "," + getave(2).tostring() + "," + getave(3).tostring() + "\r\n" ; system.io.file.writealltext( "list.txt" , string .empty); file.appendalltext( "list.txt" , add, utf8encoding. default ); return add; } |
output方法调用了getgradesection()和 getave() 两个方法,先生成并定义字符串数组,然后把两个方法返回的数据与原有的信息拼接到数组中,再写入即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public double getgradesection( int m , int low, int high) //分数段 { int count = 0; string file = file.readalltext( "score.txt" , utf8encoding. default ); //把txt文件中按行存储的信息利用regex.split按行存入string数组中 string [] records = regex.split(file, "\r\n" ); //开始更新视图 this .listview1.beginupdate(); //清空原有视图 this .listview1.items.clear(); // records.length为数组的元素个数 for ( int index = 0; index < records.length; index++) { //分割每行记录的各个字段 if (records[index].length != 0) { string [] components = regex.split(records[index], " " ); if (m == 1) //数学 { if (convert.todouble(components[2]) >= low && convert.todouble(components[2]) <= high) { count++; } } if (m == 2) //英语 { if (convert.todouble(components[3]) >= low && convert.todouble(components[3]) <= high) { count++; } } if (m == 3) //政治 { if (convert.todouble(components[4]) >= low && convert.todouble(components[4]) <= high) { count++; } } } else break ; } return count; } |
先循环,然后进行判断,如果在分数段内就把count值加一,最后返回到output()方法中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public double getave( int m) //平均分 { double ave = 0; string file = file.readalltext( "score.txt" , utf8encoding. default ); //把txt文件中按行存储的信息利用regex.split按行存入string数组中 string [] records = regex.split(file, "\r\n" ); int length = records.length; //开始更新视图 this .listview1.beginupdate(); //清空原有视图 this .listview1.items.clear(); // records.length为数组的元素个数 for ( int index = 0; index < records.length; index++) { if (records[index].length != 0) { //分割每行记录的各个字段 string [] components = regex.split(records[index], " " ); if (m == 1) { ave += double .parse(components[2]); } if (m == 2) { ave += double .parse(components[3]); } if (m == 3) { ave += double .parse(components[4]); } } else break ; } return ave/length; } |
把各科对应的所有的分数加在一起,然后除以总人数,即为平均分,返回到output()方法中
拼接好的字符串数组写入list.txt文本中,在form7中读取,在listview中显示即可
九、清除
清除就是清空所有的文本数据,然后就把空字符写入覆盖即可
1
2
3
4
5
6
|
private void clearall_click( object sender, eventargs e) { system.io.file.writealltext( "score.txt" , string .empty); system.io.file.writealltext( "list.txt" , string .empty); this .load_data(); } |
总结
由于之前没接触过c#,所以也不知道怎么搞,总结下来就是这一周的收获并不是很大,毕竟整天忙得不可开交,吃饭、睡觉、敲代码,最后一总结发现并没有学到什么真切的知识,毕竟一点都没学,上来就用,这种方式真的难以理解,在此我想说的是,只要敢钻研,肯钻研,再难的问题也有解决的办法,最后补充一句,忙点真的挺好的,每天都很充实,加油,奋斗在路
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。
原文链接:https://segmentfault.com/a/1190000020886703