C#基础编程设计实验报告.docx

上传人:b****4 文档编号:3593611 上传时间:2022-11-24 格式:DOCX 页数:20 大小:19.37KB
下载 相关 举报
C#基础编程设计实验报告.docx_第1页
第1页 / 共20页
C#基础编程设计实验报告.docx_第2页
第2页 / 共20页
C#基础编程设计实验报告.docx_第3页
第3页 / 共20页
C#基础编程设计实验报告.docx_第4页
第4页 / 共20页
C#基础编程设计实验报告.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

C#基础编程设计实验报告.docx

《C#基础编程设计实验报告.docx》由会员分享,可在线阅读,更多相关《C#基础编程设计实验报告.docx(20页珍藏版)》请在冰豆网上搜索。

C#基础编程设计实验报告.docx

C#基础编程设计实验报告

C#基础编程设计实验报告

一、实验目的

1、熟悉VisualStudio.NET开发环境。

2、掌握C#应用程序的基本操作过程。

3、掌握C#的数据类型,运算符以及表达式的使用。

4、掌握分支和循环语句的使用方法。

5、掌握一维数组,二维数组及数组型数组的使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录

(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有VisualStudio.NET软件。

四、实验步骤

1、分析题意。

2、根据题目要求,新建项目。

3、编写并输入相关的程序代码。

5、运行与调试项目。

6、保存项目。

五、实验内容

1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。

usingSystem;usingSystem.Collections.Generic;

usingSystem.Linq;usingSystem.Text;

namespaceone.first{

classProgram

{

staticvoidMain(string[]args)

{

System.Console.WriteLine(“我叫王蕾!

”);

}

}}2、编写一个简单的Windows应用程序,在窗体Load事件中书写代码,标签中显示你的姓名。

usingSystem;usingSystem.Collections.Generic;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=newLabel();

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(string[]args)

{

Console.WriteLine(“请输入一个字符:

”);

charc=Convert.ToChar(Console.ReadLine());

if((c>=“a”&&c=“A”&&c=0&&m>a[j])//判断i下标的数是否大于j下标的数

{

a[j+1]=a[j];//如果i下标大于j把j往后移一个位

j--;

}

a[j+1]=m;//当不大于j的时候就把M的值放到i下标下面j+1是为了下标减到最前时考虑-1+1还是下标的最前面

}

Console.WriteLine(“排序后,数组顺序为:

”);

show(a);

}

voidshow(int[]a)

{

inti;

for(i=0;istu[k].sumScore)k=j;

if(k!

=i)

{

Studenttemp;

temp=stu[k];

stu[k]=stu[i];

stu[i]=temp;

}

}

}

//显示单科成绩的最高分

publicintHighScore(intk)

{

intp=0;

if(k==0)

{

for(inti=1;istu[p].math)p=i;

}

elseif(k==1)

{

for(inti=1;istu[p].chinese)p=i;

}

else

{

for(inti=1;istu[p].chinese)p=i;

}

returnp;

}

//显示不及格名单

publicstring

BuhgName(intk)

{

stringname=““;

if(k==0)

{

for(inti=0;i90)&&(stu[i].chinese90)&&(stu[i].math90)&&(stu[i].english<=100))

{

sumC1++;

}

elseif((80<=stu[i].english)&&(stu[i].english<90))

{

sumC2++;

}

elseif((70<=stu[i].english)&&(stu[i].english<80))

{

sumC3++;

}

elseif((60<=stu[i].english)&&(stu[i].english<70))

{

sumC4++;

}

else

{sumC5++;}

}

per1=sumC1/snums;

per2=sumC2/snums;

per3=sumC3/snums;

per4=sumC4/snums;

per5=sumC5/snums;

returnstring.Format(“数学成绩百分比:

”+”\n”+”90~100:

”+per1+”

80~90:

”+per2+”

80~70:

”+per3+”

70~60:

”+per4+”

60以下的:

”+per5);

}

}}From窗体代码:

usingSystem;usingSystem.Collections.Generic;

usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceTest2_6{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

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.addstu(s);

MessageBox.Show(“添加成功”);

}

privatevoidbtnSearch_Click(objectsender,EventArgse)

{

intpos=sl.searchstu(this.textBox1.Text);

if(pos!

=-1)

{

label7.Text=this.textBox1.Text+”的总成绩:

”+sl.stu[pos].sumScore;

}

else{MessageBox.Show(“不存在这个人!

”);}

}

privatevoidbtnFinish_Click(objectsender,EventArgse)

{

label7.Text=“前3名:

”+”\n”;

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

{

sl.ProThree();

label7.Text+=sl.stu[i].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”;

}

}}

六、实验体会(遇到问题及解决办法,编程后的心得体会)

通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。

值得注意的是:

本次实验中return的使用以及所在的位置,类型转换时也经常用到

实验项目名称:

继承与多态

实验学时:

6

同组学生姓名:

实验地点:

1318

实验日期:

11月16日-11月30日实验成绩:

批改教师:

批改时间:

实验3

继承与多态

一、实验目的、要求

(1)掌握类的继承性与多态性;

(2)掌握虚方法的定义以及如何使用虚方法实现多态;(3)掌握抽象类的定义以及如何使用抽象方法实现多态;二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;

(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有VisualStudio.NET软件。

四、实验步骤

1、分析题意;2、根据题目要求,新建项目;3、编写并输入相关的程序代码;5、运行与调试项目;6、保存项目。

五、实验内容

1、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。

Student类:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceTest3_1{

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:

Student

{

protecteddoublechinese;

protecteddoublemath;

protecteddoubleenglish;

publicMiddle(stringname,intage,double

chinese,doublemath,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)

:

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{

publicpartialclassForm1:

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.Name+”,”+”小学生”+”,”+”平均成绩为:

”+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(Student.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)+”,”+”姓名:

”+c.Name+”,”+”大学生”+”,”+”平均成绩为:

”+c.Average()+”\n”;

}

}}2、设计一个Windows应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。

Figure类代码:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceTest3_2

{

publicabstractclassFigure

{

publicabstractdoubleArea();

}

publicclassCircle:

Figure

{

doubleradius;

publicCircle(doubler)

{

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.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()

{

InitializeComponent();

}

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=“矩形的面积为:

”+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;usingSystem.Text;

namespacethird.three{

classProgram

{

staticvoidMain(string[]args)

{

Students=newStudent(“学生”);

Teachert=newTeacher(“教师”);

Console.WriteLine(s.name+s.work());

Console.WriteLine(t.name+t.work());

Console.ReadLine();

}

}

publicclassPerson

{

publicstringname;

publicinterfacemethod

{stringwork();}

}

publicclassStudent:

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

{

staticvoidMain(string[]args)

{

Students=newStudent(“张三”,”学生”);

PersonWork(s);

Teachert=newTeacher(“李斯”,”教师”);

PersonWork(t);

}

privatestaticvoidPersonWork(PersonPerson)

{Console.WriteLine(Person.Work());}

}

publicclassPerson

{

publicstringname;

publicPerson(stringname)

{this.name=name;}

publicvirtualstringWork()

{returnstring.Format(“Person{0}:

早上8:

30开始”,name);}

}

publicclassStudent:

Person

{

privatestringtype;

publicStudent(stringname,stringtype)

:

base(name)

{this.type=type;}

publicoverridestringWork()

{

returnstring.Format(“Person{0}:

早上8:

30开始上课”,name);

}

}

publicclassTeacher:

Person

{

privatestringtype;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1