2023年C#实验报告解析_第1页
2023年C#实验报告解析_第2页
2023年C#实验报告解析_第3页
2023年C#实验报告解析_第4页
2023年C#实验报告解析_第5页
已阅读5页,还剩50页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

学生实验报告(理工类)

课程名称:C#程序设计实验专业班级:11计算机科学与技术(单)学生学号:学生姓名:姜飞所属院部:信息技术学院指导教师:蔡群2013——2014学年第1学期金陵科技学院教务处制实验项目名称:C#概述实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:批改时间:实验目的熟悉VisualStudio.NET2023的基本操作方法。掌握了C#应用程序的基本操作过程。掌握简朴窗体控件:Label、TextBox和Button的基本用法。初步理解C#程序的特点。实验规定熟悉Windows系统的基本操作。认真阅读本章相关内容,特别是案例。实验前进行程序设计,完毕源程序的编写任务。反复操作,直到不需要参考教材、能纯熟操作为止。实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023实验任务1、设计一个简朴的C#控制台应用程序,逐行显示自己的学号、姓名、专业等信息。2、设计一个C#Windows窗体应用程序,实现个人信息(涉及学号、姓名、性别、年龄、专业等)的输入操作。核心代码1、namespace11{classProgram{staticvoidMain(string[]args){Console.WriteLine("mynumberis:");Console.Write("\n");Console.WriteLine("mynameis:姜飞");Console.Write("\n");Console.WriteLine("mymajoris:计算机");Console.Write("\n");Console.Read();}}}实验结果:mynumberis:mynameis:姜飞mymajoris:计算机2、privatevoidbtnConfirm_Click(objectsender,EventArgse){MessageBox.Show(”学号:”+txtNumber.Text+”,姓名:”+txtName.Text+”,性别:”+txtSex.Text+”,年龄:”+txtAge.Text+”,专业:”+txtMajor.Text):}实验结果:学号:姓名:姜飞性别:女年龄:21专业:计算机科学与技术实验体会本次实验是初次接触c#程序设计,感觉很陌生,但在实验过程中,却也体会到了很多乐趣。例如对c#中输入输出的方法不纯熟,经常把write和writeline等用错地方,但随着语句越写越多,也会发现其实C#编程还是很方便很故意思的。此外本次实验,我掌握了简朴窗体控件:label、textbox、button的基本用法等,在以后的实验中,我觉得会更加有趣,学到的东西也会越来越丰富。实验项目名称:C#程序设计基础实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:批改时间:一、实验目的1、理解C#的值类型、常量和变量的概念。2、掌握C#常用运算符以及表达式的运营规则。3、了解C#的引用类型,理解数据类型转换、装箱和拆箱的区别。实验规定熟悉VisualStudio.NET2023的基本操作方法。认真阅读本章相关内容,特别是案例。实验前进行程序设计,完毕源程序的编写任务。反复操作,直到不需要参考教材、能纯熟操作为止。实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023实验任务1、设计一个简朴的Windows应用程序,在文本框中随意输入一个日期,单击“拟定”按钮时显示“这一天是星期几”。2、设计一个简朴的计算器,实现两个数的加、减、乘、除、求幂等计算。3、设计一个简朴的Windows程序,输入多个数字,然后排序并输出。核心代码1、namespace_21ﻫ{publicpartialclassForm1:Formﻫ{publicForm1(){InitializeComponent();}

enumWeekday{星期天,星期一,星期二,星期三,星期四,星期五,星期六};privatevoidbutton1_Click(objectsender,EventArgse){

DateTimedt=Convert.ToDateTime(textDate.Text);ﻫWeekdaywd=(Weekday)dt.DayOfWeek;ﻫlab.Text="这一天是"+wd+".";}}}实验结果:在文本框中输入2023-11-14这一天是星期四。2、privatevoidbutton1_Click(objectsender,EventArgse){doublea=Convert.ToDouble(textBox1.Text);doubleb=Convert.ToDouble(textBox2.Text);label3.Text+=a+"加"+b+"的和是";label3.Text+=a+b+".";}privatevoidbutton2_Click(objectsender,EventArgse){doublea=Convert.ToDouble(textBox1.Text);doubleb=Convert.ToDouble(textBox2.Text);label3.Text+=a+"减"+b+"的差是";label3.Text+=a-b+".";}privatevoidbutton3_Click(objectsender,EventArgse){doublea=Convert.ToDouble(textBox1.Text);doubleb=Convert.ToDouble(textBox2.Text);label3.Text+=a+"乘"+b+"的积是";label3.Text+=a*b+".";}privatevoidbutton4_Click(objectsender,EventArgse){doublea=Convert.ToDouble(textBox1.Text);doubleb=Convert.ToDouble(textBox2.Text);label3.Text+=a+"除"+b+"的商是";label3.Text+=a/b+".";}privatevoidbutton5_Click(objectsender,EventArgse){doublea=Convert.ToDouble(textBox1.Text);doubleb=Convert.ToDouble(textBox2.Text);label3.Text+=a+"的"+b+"次方是";label3.Text+=Math.Pow(a,b)+".";}实验结果:a:3b:33加3的和是63减3的差是03乘3的积是93除3的商是13的3次方是273、float[]a=newfloat[5];inti=0;privatevoidbtnAdd_Click(objectsender,EventArgse){a[i]=Convert.ToSingle(txtNumber.Text);lblShow.Text+=a[i]+“”;i++;}privatevoidbtnSort_Click(objectsender,EventArgse){Array.Sort(a);lblShow.Text+=”\n排序之后为:”;lblShow.Text+=a[0]+””+a[1]+””+a[2]+””+a[3]+””+a[4];}实验结果:输入5个数据:2.54.81.73.34.1,则输出:排序之前为:2.54.81.73.34.1排序之后为:1.72.53.34.14.8实验体会通过本次C#实验,我对C#Windows应用程序有了进一步的了解,比上次实验更加纯熟地运用窗体中的label、button和txtBox等控件。并且了解了C#的值类型、常量和变量的概念,掌握了C#常用运算符以及表达式的运营规则等。并且自己设计窗体界面,也让我对C#的实验更加感爱好。实验项目名称:C#程序的流程控制实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:批改时间:一、实验目的1、理解分支和循环的逻辑意义。2、掌握C#的if、switch分支语句的使用方法。3、掌握C#的while、do/while、for、foreach等循环语句的使用方法。实验规定1、熟悉VisualStudio.NET2023的基本操作方法。2、认真阅读本章相关内容,特别是案例。3、实验前进行程序设计,完毕源程序的编写任务。4、反复操作,直到不需要参考教材、能纯熟操作为止。实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023实验任务1、修改上机实验2的第三个实验任务,将输入的n个数字,通过for语句排序并输出。注意,不允许使用Array.Sort()方法排序。2、设计一个Windows应用程序,实现如下功能。(1)输入学生姓名和考试成绩并保存到结构体数组中。(2)使用foreach语句最高分并输出相应的姓名。3、设计一个Windows应用程序,输入一行字符,检索是否存在反复的双字词汇,输出反复的次数。五、核心代码1、namespace_31{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}double[]a=newdouble[50];inti=0;privatevoidbutton1_Click(objectsender,EventArgse){a[i]=Convert.ToDouble(textBox1.Text);label2.Text+=a[i]+"";i++;}privatevoidbutton2_Click(objectsender,EventArgse){doublet;intj=0,k;label3.Text+="\n排序之后为:";for(j=0;j<i-1;j++)for(k=j+1;k<i;k++)if(a[j]>a[k]){t=a[j];a[j]=a[k];a[k]=t;}for(j=0;j<i;j++)label3.Text+=a[j]+"";}}}实验结果:输入5个数据:2.54.81.73.34.1,则输出:排序之前为:2.54.81.73.34.1排序之后为:1.72.53.34.14.82、namespace-32{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}structstudent{publicstringname;publicfloatscore;}student[]persons=newstudent[10];inti=0;privatevoidbutton1_Click(objectsender,EventArgse){persons[i].name=textBox1.Text;persons[i].score=Convert.ToSingle(textBox2.Text);lblShow.Text+=persons[i].name+""+persons[i].score+"";i++;}privatevoidbutton2_Click(objectsender,EventArgse){floatt=0;stringj=null;foreach(studentcinpersons){if(t<c.score){t=c.score;j=c.name;lblShow.Text="最高分:"+j+""+t;}}}}}实验结果:输入:张三65添加李四78添加王二84添加钱一80则显示学生成绩:张三65李四78王二84钱一80最高分:王二843、privatevoidbutton1_Click(objectsender,EventArgse){intn=0;string[]words=newstring[10];int[]times=newint[10];for(inti=0;i<textBox1.Text.Length-2;i++){boolisSame=false;stringsource=textBox1.Text.Substring(i,2);intj=i+2;while(j<textBox1.Text.Length-2){stringtarget=textBox1.Text.Substring(j,2);if(source==target){times[n]++;if(Array.IndexOf(words,target)==-1){isSame=true;words[n]=target;}}j++;}if(isSame)n++;}Label1.Text=string.Format("一共有{0}个反复的词汇!\n其中,",n);for(inti=0;i<10;i++){if(!string.IsNullOrEmpty(words[i]))label1.Text+=String.Format("“{0}”反复{1}次",words[i],times[i]+1);}实验结果:在Text中输入中国人永远爱中国,世世代代永远在中国大地上拼搏!,则显示:一共有2个反复的词汇!其中,“中国”反复3次,“永远”反复2次六、实验体会本次实验又加深了我对label、button和textBox等控件的应用,前两次自己设计窗体界面还不太纯熟,这次实验更加得到了锻炼,印象更加深刻了。也掌握了C#的if、switch分支语句和while、do/while、for、foreach等循环语句的使用方法。实验项目名称:面向对象程序设计入门实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:批改时间:一、实验目的1、理解面向对象的概念,掌握C#的定义类和创建对象的方法。2、区分类的不同数据成员,涉及常量、字段、和属性的定义方法,并学会控制其可访问性。3、掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。4、理解构造函数和析构函数的作用机制。二、实验规定1、熟悉VisualStudio.NET2023的基本操作方法。2、认真阅读本章相关内容,特别是案例。3、实验前进行程序设计,完毕源程序的编写任务。4、反复操作,直到不需要参考教材、能纯熟操作为止。三、实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023四、实验任务1、设计一个简朴的Windows应用程序,在文本框中输入两个点的坐标值,单击“拟定”按钮时显示两点之间的距离。规定定义一个Point类,涉及:(1)两个私有字段表达两个坐标值。(2)一个构造函数通过传入的参数对坐标值初始化。(3)两个只读属性对坐标值的读取。(4)一个方法包含一个Point类对象作为形参该对象和自己的距离。2、自定义一个时间类。该类包含小时、分、秒、字段与属性,具有将秒增长1秒的方法。规定定义一个Time类,涉及:(1)三个私有字段表达时、分、秒。(2)两个构造函数,一个通过传入的参数对时间初始化,另一个获取系统当前的时间。(3)三个只读属性实现对时、分、秒的读取。(4)一个方法用于对秒增长1秒(注意60进位的问题)。3、设计一个Windows应用程序,在该程序中定义一个类和班级类,以解决每个学生的学号、姓名,语文、数学和英语三门课程的期末考试成绩,规定:(1)能查询每个学生的总成绩。(2)能显示全班前三名的名单。(3)能显示单科成绩最高分和不及格的学生名单。(4)能记录全班学生的平均成绩。(5)能显示各科成绩在不同分数段的学生人数比例。设计提醒:(1)定义一个Student学生类,包含字段(学号、姓名、语文成绩、数学成绩、英语成绩)和属性(总成绩)等。(2)定义一个StudentList班级类,包含一个Student类型的数组(用来保存全班学生的信息)以及若干个实现上述规定的方法等。(3)设计用户操作界面,一方面让用户能输入一个学生的信息,当单击“添加”按键时把这些信息添加到班级对象的学生数组中。当单击“完毕”按钮时调用班级类的方法来显示所规定记录的记录结果。当用户在查询框中输入学生的名字后,并单击“查询”按钮时显示该学生的总成绩。五、核心代码1、privatevoidbutton1_Click(objectsender,EventArgse){intx1,y1,x2,y2;x1=Convert.ToInt16(textBox1.Text);y1=Convert.ToInt16(textBox2.Text);x2=Convert.ToInt16(textBox3.Text);y2=Convert.ToInt16(textBox4.Text);Pointp1=newPoint(x1,y1);Pointp2=newPoint(x2,y2);label7.Text=p1.Distance(p2).ToString();}classPoint{publicintX,Y;publicPoint(intI,intJ){X=I;Y=J;}publicdoubleDistance(Pointp){returnSystem.Math.Sqrt((this.X-p.X)*(this.X-p.X)+(this.Y-p.Y)*(this.Y-p.Y));}}实验结果:输入:x1:0y1:0,x2:3y2:4则显示:52、控件属性设立控件属性设立Text1“”Labe1:Text2“”Label2:Text3“”Button+privatevoidbutton1_Click(objectsender,EventArgse){Timet=newTime();texthour.Text=Convert.ToString(t.Gethour());textminute.Text=Convert.ToString(t.Getminute());textsecond.Text=Convert.ToString(t.Getsecond());}classTime{inthour,minute,second;publicintGethour(){returnhour;}publicintGetminute(){returnminute;}publiGetsecond(){returnsecond;}publicTime(){hour=System.DateTime.Now.Hour;minute=System.DateTime.Now.Minute;second=System.DateTime.Now.Second;}publicTime(inth,intm,ints){hour=h;minute=m;second=s;}publicvoidAddSecond(){second++;if(second>=60){second=second%60;minute++;}if(minute>=60){minute=minute%60;hour++;}}}实验结果:在text1、text2、text3中分别输入21、49、25,则获取当前时间21:49:25,按+即加1秒,隔几秒不点+后,再按+则加相应的值。3、namespace_43{classProgram{privatestaticStudentListstulist;staticvoidMain(string[]args);{stulist=newStudentList();Studentstu1=newStudent(“”,”zhangsan”,90,85,89);Studentstu2=newStudent(“”,”lisi”,75,85,94);Studentstu3=newStudent(“”,”wanger”,90,79,80);Studentstu4=newStudent(“”,”qianyi”.30,50,55);Studentstu5=newStudent(“”,”sunwu”,45,67,38);stulist.Add(stu1);stulist.Add(stu2);stulist.Add(stu3);stulist.Add(stu4);stulist.Add(stu5);stringstr;str=Console.ReadLine();if(str.Equal(“语文”)||str.Equals(“数学”)||str.Equals(“英语”))stulist.searchSubject(str);elsestulist.searchName(str);Console.ReadLine();}ClassStudent{privatestringnumber;privatestringname;privatefloatchinese;privatefloatmath;privatefloatenglish;publicStudent(stringnumber,stringname,floatchinese,floatmath,floatenglish){this.number=number;this.name=name;this.chinese=chinese;this.math=math;this.english=english;}publicstringNumber{get{returnthis.number;}}publicstringName{get{returnthis.name;}}publicfloatChinese{set{this.chiese=value;}get{returnthis.chinese;}}publicfloatMath{set{this.math=value;}get{returnthis.math;}}publicfloatEnglish{set{this.english=value;}get{returnthis.english;}}publicfloatTotal{get{returnchinese+math+english;}}}ClassStudentList{privateList<Student>list=newList<Student>();privatefloatchieseAver;privatefloatmathAver;privatefloatenglishAver;privatefloattotalAver;publicvoidAdd(Students){this.list.Add(s);}publicfloatChineseAver{get{floats=0;foreach(Studentstuinlist){s+=stu.Chinese;}chineseAver=s/list.Count;returnchineseAver;}}publicfloatMathAver{get{floats=0;foreach(Studentstuinlist){s+=stu.Math;}mathAver=s/list.Count;returnmathAver;}}publicfloatEnglishAver{get{floats=0;foreach(Studentstuinlist){s+=stu.English;}englishAver=s/list.Count;returnenglishAver;}}publicfloatTotalAver{get{floats=0;foreach(Studentstuinlist){s+=stu.Total;}totalAver=s/list.Count;returntotalAver;}}publicvoidsearchName(stringname){foreach(Studentstuinlist){if(stu.Name.Equals(name))Console.WriteLine(“学号:{0},姓名:{1},语文:{2},数学:{3},英语:{4}”,stu.Number,stu.Name,stu.Chinese,stu.Math,stu.English);}}publicvoicsearchSubject(stringsubject){switch(subject){case”语文”;floatmax=0;foreach(Studentstuinlist){if(max<stu.Chinese)max=stu.Chinese;if(stu.Chinese<60)Console.WriteLine(string.Format(“学号:”+stu.Number+”姓名:{0},语文:{1}”,stu.Name,stu.Chinese));}Console.WriteLine(“最高分{0}”,max);break;case”数学”;floatmax1=0;foreach(Studentstuinlist){if(max1<stu.Math)max1=stu.Math;if(stu.Math<60)Console.WriteLine(string.Format(“学号:”+stu.Number+”姓名:{0},语文:{1}”,stu.Name,stu.Math));}Console.WriteLine(“最高分{0}”,max1);break;case”英语”;floatmax2=0;foreach(Studentstuinlist){if(max2<stu.English)max2=stu.English;if(stu.English<60)Console.WriteLine(string.Format(“学号:”+stu.Number+”姓名:{0},语文:{1}”,stu.Name,stu.English);}Console.WriteLine(“最高分{0}”,max2);break;}}}}实验结果:输入:zhangsan之后,按查询,则显示:264六、实验体会本次实验重要是围绕对象、类、构造和析构函数的,而之前在其它语言中我们也学习过,这次实验让我更加理解面向对象的概念,掌握了C#的定义类和创建对象的方法,理解了构造函数和析构函数的作用机制。实验项目名称:面向对象的高级程序设计实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:批改时间:一、实验目的1、区别静态类与非静态类,掌握静态字段、静态方法和静态构造函数的定义方法。2、理解类的继承性与多态性,掌握其应用方法。3、理解抽象类、接口的概念,掌握抽象类与接口的定义及使用方法。4、理解分部类和命名空间的概念,掌握分部类和命名空间的使用方法。二、实验规定1、熟悉VisualStudio.NET2023的基本操作方法。2、认真阅读本章相关内容,特别是案例。3、实验前进行程序设计,完毕源程序的编写任务。4、反复操作,直到不需要参考教材、能纯熟操作为止。三、实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023四、实验任务1、设计一个Windows应用程序,在该程序中一方面构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(“小学生”、“中学生”、“大学生”)时将分别创建不同的学生对象,并输出当前的学生总人数,该学生的姓名、学生类型和平均成绩。规定如下:(1)每个学生都有的字段为姓名、年龄。(2)小学生的字段尚有语文、数学,用来表达这两科的成绩。(3)中学生在此基础上增长英语成绩。(4)大学生分为必修课和选修课两项成绩。(5)学生类提供方法来记录自己的总成绩并输出。(6)通过静态成员自动记录学生总人数。(7)成员初始化能通过构造函数完毕。2、设计一个Windows应用程序,在该程序中定义平面图形抽象类和其派生类圆、矩形和三角形。该程序实现的功能涉及:输入相应图形的参数,如矩形的长和宽,单击相应的按钮,根据输入参数创建图形类并输出该对象的面积。3、声明一个接口IPlayer,包含5个接口方法:播放、停止、暂停、上一首和下一首。设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口,最后创建相应类的实例测试程序,假如单击AVI按钮后,再单击“播放”按钮则应显示“正在播放AVI视频!”。五、核心代码1、控件属性设立控件属性设立Label1姓名:Button1小学生Label2年龄:Button2中学生Label3语文/必修课:Button3大学生Label4数学/选修课:Label6Label5英语:publicabstractclassStudent{protectedstringname;protectedintage;publicstaticintnumber;publicStudent(stringname,intage){this.name=name;this.age=age;number++;}publicstringName{get{returnname;}}publicabstractdoubleAverage();}publicclassPupil:Student{protecteddoublechinese;protecteddoublemath;publicPupil(stringname,intage,doublechinese,doublemath):base(name,age){this.chinese=chinese;this.math=math;}publicoverridedoubleAverage(){return(chinese+math)/2;}}publicclassMiddle:Pupil{protecteddoubleenglish;publicMiddle(stringname,intage,doublechinese,doublemath,doubleenglish):base(name,age,chinese,math){this.chinese=chinese;this.math=math;this.english=english;}publicoverridedoubleAverage(){return(chinese+math+english)/3;}}publicclassCollege:Student{protecteddoublexuanxiu;protecteddoublebixiu;publicCollege(stringname,intage,doublexuanxiu,doublebixiu):base(name,age){this.xuanxiu=xuanxiu;this.bixiu=bixiu;}publicoverridedoubleAverage(){return(xuanxiu+bixiu)/2;}}privatevoidbutton1_Click(objectsender,EventArgse){stringn=Convert.ToString(textBox1.Text);inta=Convert.ToInt16(textBox2.Text);doublech=Convert.ToDouble(textBox3.Text);doublema=Convert.ToDouble(textBox4.Text);PupilP1=newPupil(n,a,ch,ma);lable6.Text+="总人数:"+Student.number+",姓名:"+P1.Name+",小学生,平均成绩为:"+P1.Average();}privatevoidbutton2_Click(objectsender,EventArgse){stringn=Convert.ToString(textBox1.Text);inta=Convert.ToInt16(textBox2.Text);doublech=Convert.ToDouble(textBox3.Text);doublema=Convert.ToDouble(textBox4.Text);doubleen=Convert.ToDouble(textBox5.Text);MiddleM1=newMiddle(n,a,ch,ma,en);lable6.Text+="总人数:"+Student.number+",姓名"+M1.Name+",中学生,平均成绩为:"+M1.Average();}privatevoidbutton3_Click(objectsender,EventArgse){stringn=Convert.ToString(textBox1.Text);inta=Convert.ToInt16(textBox2.Text);doublebx=Convert.ToDouble(textBox3.Text);doublexx=Convert.ToDouble(textBox4.Text);CollegeC1=newCollege(n,a,bx,xx);lable6.Text+="总人数:"+Student.number+",姓名"+C1.Name+",大学生,平均成绩为:"+C1.Average();}实验结果:输入张伟109698;赵恒15858687;李静218183后,按小学生,中学生,大学生之后结果为:总人数:1,姓名:张伟,小学生,平均成绩为:97总人数:2,姓名:赵恒,中学生,平均成绩为:86总人数:3,姓名:李静,大学生,平均成绩为:822、publicabstractclassFigure{publicabstractdoubleArea();}publicclassCircle:Figure{doubleradius;publicCircle(doubler){radius=r;}publicoverridedoubleArea(){returnradius*radius*3.14;}}publicclassJX:Figure{doublech,k;publicJX(doublea,doubleb){ch=a;k=b;}publicoverridedoubleArea(){returnch*k;}}publicclassSJX:Figure{doubled,g;publicSJX(doublea,doubleb){d=a;g=b;}publicoverridedoubleArea(){returnd*g/2;}}privatevoidbutton1_Click(objectsender,EventArgse){CircleC1=newCircle(Convert.ToDouble(textBox1.Text));label6.Text+="圆的面积是"+C1.Area();}privatevoidbutton2_Click(objectsender,EventArgse){JXA1=newJX(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text));label6.Text+="\n矩形的面积是"+A1.Area();}privatevoidbutton3_Click(objectsender,EventArgse){SJXB1=newSJX(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text));label6.Text+="\n三角形的面积是"+B1.Area();}实验结果:输入5,6,分别按圆,矩形,三角形之后,结果分别显示:圆面积为:78.5矩形面积为:30三角形面积为:153、控件属性设立控件属性设立Button1MP3Button5播放Button2AVIButton6暂停Button3上一首Button7下一首Button4停止Label1IPlayeriplayer;MP3mp3;AVIavi;privatevoidbutton3_Click(objectsender,EventArgse){label1.Text=iplayer.Pre();}privatevoidbutton4_Click(objectsender,EventArgse){label1.Text=iplayer.Stop();}privatevoidbutton5_Click(objectsender,EventArgse){label1.Text=iplayer.Play();}privatevoidbutton6_Click(objectsender,EventArgse){label1.Text=iplayer.Pause();}privatevoidbutton7_Click(objectsender,EventArgse){label1.Text=iplayer.Next();}privatevoidbutton2_Click(objectsender,EventArgse){avi=newAVI();iplayer=(IPlayer)avi;}privatevoidbutton1_Click(objectsender,EventArgse){mp3=newMP3();iplayer=(IPlayer)mp3;}}interfaceIPlayer{stringPlay();stringStop();stringPause();stringPre();stringNext();}publicclassMP3:IPlayer{publicstringPlay(){return"正在播放MP3歌曲!";}publicstringStop(){return"停止播放MP3歌曲!";}publicstringPause(){return"暂停播放MP3歌曲!";}publicstringPre(){return"播放上一首MP3歌曲!";}publicstringNext(){return"播放下一首MP3歌曲!";}}publicclassAVI:IPlayer{publicstringPlay(){return"正在播放AVI视频!";}publicstringStop(){return"停止播放AVI视频!";}publicstringPause(){return"暂停播放AVI视频!";}publicstringPre(){return"播放上一部AVI视频!";}publicstringNext(){return"播放下一部AVI视频!";}}实验结果:当按MP3,再按播放,则显示正在播放MP3歌曲!当按AVI,再按播放,则显示正在播放AVI视频!六、实验体会本次实验程序,整体没有多大困难,重要都是关于类的内容,因此,实验加深了我们对类的了解。通过本次实验,我理解了类的继承性与多态性,并且掌握了其应用方法,以后的实验也会努力完毕的。实验项目名称:集合、索引器与泛型实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:蔡群批改时间:一、实验目的1、初步掌握常用集合的创建和操作方法。2、初步掌握索引器的定义与使用。3、初步掌握泛型接口、泛型类、泛型属性和泛型方法的使用。二、实验规定1、熟悉VisualStudio.NET2023的基本操作方法。2、认真阅读本章相关内容,特别是案例。3、实验前进行程序设计,完毕源程序的编写任务。4、反复操作,直到不需要参考教材、能纯熟操作为止。三、实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023四、实验任务1、设计一个Windows应用程序,定义一个Teacher类,包含姓名和职称两个字段和一个输出教师信息的方法,并用ArrayList实现与实例6-2相似的功能。2、设计一个Windows应用程序,定义一个Student类,包含学号和姓名两个字段,并定义一个班级类ClassList,该类涉及一个Student集合,使用索引器访问该集合,实现与实例6-3类似的功能。3、设计一个Windows应用程序,规定如下:(1)构造一个学生基本类。(2)分别构造小学生、中学生、大学生等派生类,规定具有不同的特性和行为。(3)定义一个泛型班级类,约束参数类型为学生类,该泛型的班级类涉及一个泛型集合,用于存放各种学生对象,并包含一个方法用于输出每个学生的相关信息。(4)仿照实例6-4,定义泛型的班级类对象,完毕对学生的添加和信息的输出。五、核心代码1、namespace_61{publicpartialclassForm1:Form{ArrayListALTeachers=newArrayList();privatevoidForeach(){foreach(objecttinALTeachers){TeachertForeach=(Teacher)t;lblShow.Text+="\n"+tForeach.showmsg();}}publicForm1(){InitializeComponent();privatevoidbtnAdd_Click(objectsender,EventArgse){stringn=Convert.ToString(textBox1.Text);stringc=Convert.ToString(textBox2.Text);Teachert1=newTeacher(n,c);ALTeachers.Add(t1);lblShow.Text="";Foreach();}privatevoidbtnDele_Click(objectsender,EventArgse){intkey=Convert.ToInt32(textBox3.Text);ALTeachers.RemoveAt(key);lblShow.Text="";Foreach();}privatevoidlblShow_Click(objectsender,EventArgse){}}}{classTeacher{stringname;stringzhicheng;publicTeacher(stringname,stringzhicheng){this.name=name;this.zhicheng=zhicheng;}publicstringshowmsg(){returnstring.Format("姓名:{0},职称:{1}",name,zhicheng);}}}publicabstractclassStudentBase{intage;strin;publicvirtualvoidwalk();{}publicclassPupil:StudentBase{publicoverridevoidwalk(){}}publicinterfaceIClass<T>{}publicclassClassA:IClass<ClassA>{publiclist<StudentBase>NumberList;}2、namespace_62{publicpartialclassForm1:FormclassStudent{stringname;stringnumber;publicStudent(stringname,stringnumber){this.name=name;this.number=number;}publicstringshowmsg(){returnstring.Format("姓名:{0},学号:{1}",name,number);}}classClassList{Student[]students;publicClassList(intn){students=newStudent[n];}}publicStudentthis[intindex]{get{if(index<0||index>=students.Length){returnnull;}returnstudents[index];}set{if(index<0||index>=students.Length){return;}students[index]=value;}}}3、namespace_63{publicpartialclassForm1:Form{Banji<Xuesheng>myBanji=newBanji<Xuesheng>();PrivatevoidbtnXiaoxuesheng_Click(objectsender,EventArgse){myBanji.Xueshengs.Add(newXiaoxuesheng(txtName.Text));lblShow.Text+=string.Format(“\n添加Xiaoxuesheng{0}成功”,txtName.Text);}PrivatevoidbtnZhongxuesheng_Click(objectsender,EventArgse){myBanji.Xueshengs.Add(newZhongxuesheng(txtName.Text));lblShow.Text+=string.Format(“\n添加Zhongxuesheng{0}成功”,txtName.Text);}PrivatevoidbtnDaxuesheng_Click(objectsender,EventArgse){myBanji.Xueshengs.Add(newDaxuesheng(txtName.Text));lblShow.Text+=string.Format(“\n添加Daxuesheng{0}成功”,txtName.Text);}publicabstractclassXuesheng{protectedstringname;publicXuesheng(stringname){this.name=name;}publicabstractstringAct();}publicclass(Xiaoxuesheng:Xuesheng{publicXiaoxuesheng(stringname):base(name){}publicoverridestringAct(){returnstring.Format(“{0}:我是Xiaoxuesheng,我要玩!”,name);}}publicclass(Zhongxuesheng:Xuesheng{publicZhongxuesheng(stringname):base(name){}publicoverridestringAct(){returnstring.Format(“{0}:我是Zhongxuesheng,我要看书!”,name);}}publicclass(Dagxuesheng:Xuesheng{publicDaxuesheng(stringname):base(name){}publicoverridestringAct(){returnstring.Format(“{0}:我是Daxuesheng,我要兼职!”,name);}}publicclassBanji<T>whereT:Xuesheng{privateList<T>xueshengs=newList<T>;publicList<T>Xueshengs{get{returnxueshengs;}}}}}六、实验体会通过这次实验,我掌握了常用集合的创建和操作方法,也知道了索引器的该如何定义与使用,知道了在应用程序中怎么构造各种不同的类,学会了泛型接口、泛型类、泛型属性和泛型方法的使用。实验项目名称:程序调试与异常解决实验学时:2同组学生姓名:实验地点:1416实验日期:实验成绩:批改教师:蔡群批改时间:一、实验目的1、理解程序错误和异常的概念2、掌握VisualStudio.NET2023的调试器的使用方法3、掌握C#的try-catch、finally和throw语句的使用方法二、实验规定1、熟悉VisualStudio.NET2023的基本操作方法。2、认真阅读本章相关内容,特别是案例。3、实验前进行程序设计,完毕源程序的编写任务。4、反复操作,直到不需要参考教材、能纯熟操作为止。三、实验设备及环境设备:奔腾4及奔腾4以上计算机环境:VisualStudio.NET2023四、实验任务1、设计一个Windows应用程序,在一个文本框中输入n个数字,中间用逗号作间隔,然后编程对数字排序并输出。2、按F11键启用逐语句方式跟踪每一条语句的执行情况,在调试过程中将数组a添加到监视窗口。注意,观测各数组元素的变化过程。3、设立“for(inti=0;i<sources.Length;i++)”语句为断点,然后按F5键启用调试器,当程序中断运营时,将数组sources添加到监视窗口,观测各数组元素的值。4、上述代码在用户不按规定输入数据时会发生异常。修改源代码,使用try-catch语句添加异常解决功能。5、然后输入以下数据:“5,3,7,8,1,4;6,9,2”,单击“排序”按钮,注意观测异常信息,分析错误的因素。五、核心代码namespace_71{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbtnSort_Click(objectsender,EventArgse){string[]sources=txtSource.Text.Split(',');int[]a=newint[sources.Length];for(inti=0;i<sources.Length;i++){a[i]=Convert.ToInt16(sources[i]);}for(inti=0;i<a.Length;i++){intk=i;for(intj=i+1;j<a.Length;j++)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论