1、C#基础编程设计实验报告C#基础编程设计实验报告 一、实验目的 1、熟悉VisualStudio.NET开发环境。 2、掌握C#应用程序的基本操作过程。 3、掌握C#的数据类型,运算符以及表达式的使用。 4、掌握分支和循环语句的使用方法。 5、掌握一维数组,二维数组及数组型数组的使用。 二、实验要求 (1)编写程序要规范、正确,上机调试过程和结果要有记录(2)做完实验后给出本实验的实验报告。 三、实验设备、环境 安装有VisualStudio.NET软件。 四、实验步骤 1、分析题意。 2、根据题目要求,新建项目。 3、编写并输入相关的程序代码。 5、运行与调试项目。 6、保存项目。 五、实验
2、内容 1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。 usingSystem;usingSystem.Collections.Generic; usingSystem.Linq;usingSystem.Text; namespaceone.first classProgram staticvoidMain(stringargs) System.Console.WriteLine(“我叫王蕾!”); 2、编写一个简单的Windows应用程序,在窗体Load事件中书写代码,标签中显示你的姓名。 usingSystem;usingSystem.Collections.Generic;
3、usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text; usingSystem.Windows.Forms; namespaceone.second publicpartialclassForm1:Form publicForm1() InitializeComponent(); privatevoidForm1_Load(objectsender,EventArgse) this.Text=“Windows程序”; LabellblShow=newLa
4、bel(); lblShow.Location=newPoint(20,30); lblShow.AutoSize=true; lblShow.Text=“王蕾!”; this.Controls.Add(lblShow); 3、编写一个一个程序,用来判断输入的是大写字母,小写字母,数字还是其他的字符。 usingSystem;usingSystem.Collections.Generic;usingSystem.Text; namespaceone.third classProgram staticvoidMain(stringargs) Console.WriteLine(“请输入一个字符:
5、”); charc=Convert.ToChar(Console.ReadLine(); if(c=“a”&c=“A”&c=0&maj)/判断i下标的数是否大于j下标的数 aj+1=aj;/如果i下标大于j把j往后移一个位 j-; aj+1=m;/当不大于j的时候就把M的值放到i下标下面j+1是为了下标减到最前时考虑-1+1还是下标的最前面 Console.WriteLine(“排序后,数组顺序为:”); show(a); voidshow(inta) inti; for(i=0;istuk.sumScore)k=j; if(k!=i) Studenttemp; temp=stuk; stuk
6、=stui; stui=temp; /显示单科成绩的最高分 publicintHighScore(intk) intp=0; if(k=0) for(inti=1;istup.math)p=i; elseif(k=1) for(inti=1;istup.chinese)p=i; else for(inti=1;istup.chinese)p=i; returnp; /显示不及格名单 publicstring BuhgName(intk) stringname=“; if(k=0) for(inti=0;i90)&(stui.chinese90)&(stui.math90)&(stui.engl
7、ish=100) sumC1+; elseif(80=stui.english)&(stui.english90) sumC2+; elseif(70=stui.english)&(stui.english80) sumC3+; elseif(60=stui.english)&(stui.english70) sumC4+; else sumC5+; per1=sumC1/snums; per2=sumC2/snums; per3=sumC3/snums; per4=sumC4/snums; per5=sumC5/snums; returnstring.Format(“数学成绩百分比:”+”n
8、”+”90100:”+per1+” 8090:”+per2+” 8070:”+per3+” 7060:”+per4+” 60以下的:”+per5); From窗体代码: usingSystem;usingSystem.Collections.Generic; usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceTest2_6 publicpartialclassForm1:Form publicForm1() Ini
9、tializeComponent(); publicStudentListsl=newStudentList(); privatevoidbtnAdd_Click(objectsender,EventArgse) Students=newStudent(); s.stuNo=txtStuNo.Text; s.name=txtName.Text; s.chinese=Convert.ToDouble(txtChina.Text); s.math=Convert.ToDouble(txtMath.Text); s.english=Convert.ToDouble(txtEng.Text); sl.
10、addstu(s); MessageBox.Show(“添加成功”); privatevoidbtnSearch_Click(objectsender,EventArgse) intpos=sl.searchstu(this.textBox1.Text); if(pos!=-1) label7.Text=this.textBox1.Text+”的总成绩:”+sl.stupos.sumScore; elseMessageBox.Show(“不存在这个人!”); privatevoidbtnFinish_Click(objectsender,EventArgse) label7.Text=“前3名
11、:”+”n”; for(inti=0;i3;i+) sl.ProThree(); label7.Text+=sl.stui.name+”n”; label7.Text+=sl.getHL()+”n”; label7.Text+=Convert.ToString(sl.SumScore()+”n”; label7.Text+=sl.PerC()+”n”; label7.Text+=sl.PerM()+”n”; label7.Text+=sl.PerE()+”n”; 六、实验体会(遇到问题及解决办法,编程后的心得体会) 通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌
12、握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。值得注意的是:本次实验中return的使用以及所在的位置,类型转换时也经常用到 实验项目名称: 继承与多态 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 11月16日-11月30日实验成绩: 批改教师: 批改时间: 实验3 继承与多态 一、实验目的、要求 (1)掌握类的继承性与多态性;(2)掌握虚方法的定义以及如何使用虚方法实现多态;(3)掌握抽象类的定义以及如何使用抽象方法实现多态;二、实验要求 (1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。 三、实验设备
13、、环境 安装有VisualStudio.NET软件。 四、实验步骤 1、分析题意;2、根据题目要求,新建项目;3、编写并输入相关的程序代码;5、运行与调试项目;6、保存项目。 五、实验内容 1、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。 Student类: usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceTest3_1 p
14、ublicabstractclassStudent protectedstringname; protectedintage; publicstaticintnumber; publicStudent(stringname,intage) this.name=name; this.age=age; number+; publicstringName getreturnname; publicabstractdoubleAverage(); publicclassPupil:Student protecteddoublechinese; protecteddoublemath; publicPu
15、pil(stringname,intage,doublechinese,doublemath) :base(name,age) this.chinese=chinese; this.math=math; publicoverridedoubleAverage() return(chinese+math)/2; publicclassMiddle:Student protecteddoublechinese; protecteddoublemath; protecteddoubleenglish; publicMiddle(stringname,intage,double chinese,dou
16、blemath,doubleenglish) :base(name,age) this.chinese=chinese; this.math=math; this.english=english; publicoverridedoubleAverage() return(chinese+math+english)/3; publicclassCollege:Student protecteddoublerequired; protecteddoubleelective; publicCollege(stringname,intage,doublerequired,doubleelective)
17、 :base(name,age) this.required=required; this.elective=elective; publicoverridedoubleAverage() return(required+elective)/2; Form窗体内的代码: usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceTest3_1 publicpa
18、rtialclassForm1:Form publicForm1() InitializeComponent(); privatevoidbtnSmall_Click(objectsender,EventArgse) Pupilp=newPupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text); lblShow.Text+=“总人数:”+Convert.ToString(Student.number)+”,”+”姓名:”+p.N
19、ame+”,”+”小学生”+”,”+”平均成绩为:”+p.Average()+”n”; privatevoidbtnMiddle_Click(objectsender,EventArgse) Middlem=newMiddle(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text); lblShow.Text+=“总人数:”+Convert.ToString(Studen
20、t.number)+”,”+”姓名:”+m.Name+ “,”+”中学生”+”,”+”平均成绩为:”+m.Average()+”n”; privatevoidbtnBig_Click(objectsender,EventArgse) Collegec=newCollege(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text); lblShow.Text+=“总人数:”+Convert.ToString(Student.number)+”
21、,”+”姓名:”+c.Name+”,”+”大学生”+”,”+”平均成绩为:”+c.Average()+”n”; 2、设计一个Windows应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。 Figure类代码: usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceTest3_2 publicabstractclassFigure publicabstractdoubleArea(); publicclassCircle:Figure doubleradius; publicCircle(dou
22、bler) radius=r; publicoverridedoubleArea() returnradius*radius*3.14; publicclassJUxing:Figure doublechang; doublekuan; publicJUxing(doublec,doublek) this.chang=c; this.kuan=k; publicoverridedoubleArea() returnchang*kuan; publicclassSan:Figure doublebian; doubleheigth; publicSan(doubleb,doubleh) this
23、.bian=b; this.heigth=h; publicoverridedoubleArea() returnbian*heigth/2; Form窗体代码: usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceTest3_2 publicpartialclassForm1:Form publicForm1() Initial
24、izeComponent(); privatevoidbtnCircle_Click(objectsender,EventArgse) Circlec=new Circle(Convert.ToInt32(TxtChang.Text); lblShow.Text=“圆的面积为:”+c.Area(); privatevoidbtnJu_Click(objectsender,EventArgse) JUxingj=newJUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text); lblShow.Text=“矩形的面积为:
25、”+j.Area(); privatevoidbtnSan_Click(objectsender,EventArgse) Sans=newSan(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text); lblShow.Text=“三角形的面积为:”+s.Area(); 3、定义一个Person类,包含姓名字段和一个方法,早上8:30学生开始上课,教师开始讲课。分别用new关键字,虚方法,抽象类实现多态性。 New关键字: usingSystem;usingSystem.Collections.Generic;usingSyst
26、em.Text; namespacethird.three classProgram staticvoidMain(stringargs) Students=newStudent(“学生”); Teachert=newTeacher(“教师”); Console.WriteLine(s.name+s.work(); Console.WriteLine(t.name+t.work(); Console.ReadLine(); publicclassPerson publicstringname; publicinterfacemethod stringwork(); publicclassStu
27、dent:Person publicStudent(stringname) this.name=name; publicstringwork() return”早上8:30开始上课”; publicclassTeacher:Person publicTeacher(stringname) this.name=name; publicstringwork() return”开始讲课”; 虚方法: usingSystem; usingSystem.Collections.Generic;usingSystem.Text; namespacethird.three.two classProgram
28、staticvoidMain(stringargs) Students=newStudent(“张三”,”学生”); PersonWork(s); Teachert=newTeacher(“李斯”,”教师”); PersonWork(t); privatestaticvoidPersonWork(PersonPerson) Console.WriteLine(Person.Work(); publicclassPerson publicstringname; publicPerson(stringname) this.name=name; publicvirtualstringWork() returnstring.Format(“Person0:早上8:30开始”,name); publicclassStudent:Person privatestringtype; publicStudent(stringname,stringtype) :base(name) this.type=type; publicoverridestringWork() returnstring.Format(“Person0:早上8:30开始上课”,name); publicclassTeacher:Person privatestringtype;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1