C#实验二实验报告3116010036陈志灵

上传人:xt****7 文档编号:101243105 上传时间:2022-06-04 格式:DOC 页数:18 大小:153.50KB
收藏 版权申诉 举报 下载
C#实验二实验报告3116010036陈志灵_第1页
第1页 / 共18页
C#实验二实验报告3116010036陈志灵_第2页
第2页 / 共18页
C#实验二实验报告3116010036陈志灵_第3页
第3页 / 共18页
资源描述:

《C#实验二实验报告3116010036陈志灵》由会员分享,可在线阅读,更多相关《C#实验二实验报告3116010036陈志灵(18页珍藏版)》请在装配图网上搜索。

1、福建农林大学计算机与信息学院实验报告系: 计算机与信息学院 专业: 计算机科学与技术 年级: 2011 姓名: 陈志灵 学号: 3116010036 实验室号 田C507 计算机号 108 实验时间: 指导教师签字: 成绩: 实验名称:一、实验目的和要求1、理解继承在软件工程中的作用。2、掌握多态的“通用编程”的思想。3、掌握运算符重载。二、实验内容和原理1、多态在工资系统中的应用。请给出一个根据员工类型完成每周工资单计算的程序,具体要求如下:(1) 定义一个类Employee作为父类,该类有三个子类:Boss类、PieceWorker类、HourlyWorker。(2) Boss类的工资计算

2、的原则:不计工作时间,每周获得固定工资。(3) PieceWorker类的工资计算原则:根据生产的产品数量,每周发放工资。(4) HourlyWorker类的工资计算原则:根据工作时间,每周发放工资。每周工作的时间不超过40小时。(5) Employee类中定义了一个方法Earnings(),该方法没有实质性的工作;而在每个子类中都提供了恰当的Earnings()方法的重写。(6) 定义TestEmployee类,在其Main方法中,创建三种类型的员工对象,并计算相应的工资。2、请定义一个矩阵类,实现矩阵的转置、矩阵元素值的取反、矩阵的相减、矩阵的相加、矩阵的相乘、以及将矩阵元素的值乘以n等运

3、算。参考实验步骤及类图:(1)在Matrix类中,定义私有变量rows、cols和mat。(2)定义两个构造方法:Matrix(int iRow, int Cols),Matrix(int iRow, int Cols, int dispersion )。其中,参数dispersion指定随机生成的矩阵元素的范围。如参数值dispersion为5,则使得矩阵元素的值在-55的范围内。(3)定义属性Rows和Cols。在Set访问器中,提供参数合法性检查的代码。(4)定义索引器thisint iRow, int iCol,实现根据行列号访问矩阵中的元素。(5)重写ToString()方法,以“矩

4、阵”的格式输出矩阵中的所有元素。(6)定义私有方法Matrix StrassenMultiply(Matrix A, Matrix B) ,实现两个矩阵的相乘。(7)定义私有方法Matrix Multiply(double n, Matrix m),实现将矩阵元素的值乘以n。(8)定义私有方法 Matrix Add(Matrix m1, Matrix m2),实现两个矩阵的相加。(9)调用上述方法,重载运算符“-”、“+”、“*”,依次实现将矩阵元素的取反、矩阵的相减、矩阵的相加、矩阵的相乘、以及将矩阵元素的值乘以n五类运算。如:public static Matrix operator -(

5、Matrix m) return Matrix.Multiply(-1, m); public static Matrix operator -(Matrix m1, Matrix m2) return Matrix.Add(m1, -m2); (10)定义public static Matrix Transpose(Matrix m)方法,生成转置矩阵。(11)定义Test类,在其Main方法中,完成相关测试。三、实验环境Windows 7四、算法描述及实验步骤这个实验主要做类的继承以及运算符的重载,在充分理解继承和运算符重载,在编写代码是没什么困难的。五、调试过程在测试结果的时候,发现总是

6、要输入矩阵的值,经过检查发现原来是每个重载里面都用到构造方法Matrix(int iRows,int iCols),而这个方法里面每次都要输入矩阵,所以重载的方法中的矩阵的初始化都被我改成无参构造,通过赋值初始化其他成员变量。六、实验结果1.2.七、总结这次实验熟悉了类的继承和运算符的重载,更加了解C#编程的一些细节,也学了些快捷键。写代码的时候要注意编译器给出的错误提示,找出错误,然后更改,记住错误发生的原因,争取以后不犯同样的错误。附录:1.Employee.csusing System;using System.Collections.Generic;using System.Linq;

7、using System.Text;using System.Threading.Tasks;namespace shiyan2 abstract class Employee private string name; public string Name set this.name = value; get return this.name; public Employee() public abstract decimal Earnings(); public abstract string Tostring(); HourlyWorker.csusing System;using Sys

8、tem.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan2 class HourlyWorker : Employee private decimal hours; private decimal wageOfHour; public decimal Hours set this.hours = value; get return this.hours; public decimal WageOfHour set this.wageOfHou

9、r = value; get return this.wageOfHour; public HourlyWorker() : base() this.hours = 0; this.wageOfHour = 0; public override decimal Earnings() if (this.hours 40) Console.WriteLine(工作时间超过40小时); return 0; else return this.hours * this.wageOfHour; public override string Tostring() string str = ; str +=

10、this.Earnings() + ; return str; Boss.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan2 class Boss : Employee private decimal weeklySalary; public decimal WeeklySalary set this.weeklySalary = value; get return this.weekl

11、ySalary; public Boss():base() this.weeklySalary = 0; public override decimal Earnings() return this.weeklySalary; public override string Tostring() string str = ; str += this.Earnings() + ; return str; PieceWorker.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;us

12、ing System.Threading.Tasks;namespace shiyan2 class PieceWorker : Employee private int quantity; private decimal wageOfPiece; public int Quantity set this.quantity = value; get return this.quantity; public decimal WageOfPiece set this.wageOfPiece = value; get return this.wageOfPiece; public PieceWork

13、er():base() this.quantity = 0; this.wageOfPiece = 0; public override decimal Earnings() return this.quantity * this.wageOfPiece; public override string Tostring() string str = ; str += this.Earnings() + ; return str; 2.using System;using System.Collections.Generic;using System.Linq;using System.Text

14、;using System.Threading.Tasks;namespace shiyan2 class Matrix private int rows; private int cols; private int, mat; public int Rows set this.rows = value; get return this.rows; public int Cols set this.cols = value; get return this.cols; public int thisint iRow, int iCol set matiRow,iCol = value; get

15、 return matiRow,iCol; public Matrix() public Matrix(int iRows, int iCols) this.rows = iRows; this.cols = iCols; this.mat = new intthis.rows,this.cols; Console.WriteLine(请依次输入矩阵的各个元素:); for (int i = 0; i iRows; i+) for (int j = 0; j iCols; j+) this.mati, j = System.Int32.Parse(Console.ReadLine(); pub

16、lic Matrix(int iRow, int iCol, int dispersion) this.rows = iRow; this.cols = iCol; this.mat = new intthis.rows,this.cols; for (int i = 0; i this.rows; i+) for (int j = 0; j this.cols; j+) Random ran = new Random(); this.mati,j = ran.Next(-1*dispersion,dispersion); System.Threading.Thread.Sleep(50);

17、private static Matrix Add(Matrix m1, Matrix m2) Matrix sum = null; if (m1.rows != m2.rows | m1.cols != m2.cols) Console.WriteLine(矩阵行列数不同,不能进行加减运算!); else sum = new Matrix(); sum.rows = m1.rows; sum.cols = m1.cols; int, mat = new intm1.rows,m1.cols; for (int i = 0; i m1.rows; i+) for (int j = 0; j m

18、1.cols; j+) mati, j = m1.mati, j + m2.mati, j; sum.mat = mat; return sum; private static Matrix Multiply(int n, Matrix m) Matrix a = new Matrix(); a.rows = m.rows; a.cols = m.cols; int, mat = new inta.rows, a.cols; for (int i = 0; i m.rows; i+) for (int j = 0; j m.cols; j+) mati, j = n * m.mati, j;

19、a.mat = mat; return a; private static Matrix StrassenMultiply(Matrix A, Matrix B) Matrix a = null; if (A.rows != B.cols) Console.WriteLine(矩阵维数不匹配); else a = new Matrix(A.rows, B.cols); a.rows = A.rows; a.cols = A.cols; int, mat = new intA.rows, A.cols; for (int i = 0; i A.rows; i+) for (int j = 0;

20、j B.cols; j+) mati, j = 0; for (int k = 0; k A.cols; k+) mati, j += A.mati, k * B.matk, j; a.mat = mat; return a; public static Matrix operator -(Matrix m)/添加负号的情况 return Matrix.Multiply(-1, m); public static Matrix operator -(Matrix m1, Matrix m2) return Matrix.Add(m1, -m2); public static Matrix op

21、erator *(int n,Matrix m) return Matrix.Multiply(n, m); public static Matrix operator *(Matrix m1,Matrix m2) return Matrix.StrassenMultiply(m1, m2); public static Matrix operator +(Matrix m1, Matrix m2) return Matrix.Add(m1, m2); public Matrix Transpose(Matrix m) Matrix res = new Matrix(); res.rows =

22、 m.cols; res.cols = m.rows; int, mat = new intres.rows, res.cols; for (int i = 0; i res.rows; i+) for (int j = 0; j res.cols; j+) mati,j = m.matj,i; res.mat = mat; return res; public override string ToString() string result = ; for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) result += mati,

23、j + ; if (j = cols - 1) result += n; return result; class Test static void Main(string args) while (true) Console.WriteLine(请输入矩阵的个数(1或2,0退出); int cnt = Int32.Parse(Console.ReadLine(); if (cnt = 1) Matrix m = null; int x, y, z; Console.WriteLine(请依次输入矩阵行和列的大小:); x = Int32.Parse(Console.ReadLine(); y

24、 = Int32.Parse(Console.ReadLine(); Console.WriteLine(请输入矩阵元素随机值范围(不设置随机请输入0):); z = Int32.Parse(Console.ReadLine(); if (z = 0) m = new Matrix(x, y); else m = new Matrix(x, y, z); Console.WriteLine(矩阵为:n + m); while (true) Console.WriteLine(1.矩阵乘n 2.矩阵转置 3.矩阵取反 4.返回 请输入:); string count = Console.Read

25、Line(); if (count = 1) Console.WriteLine(请输入一个整数n:); int n = Int32.Parse(Console.ReadLine(); Console.WriteLine(矩阵乘n结果为:n + n*m); else if (count = 2) Console.WriteLine(矩阵转置结果为:n + m.Transpose(m); else if (count = 3) Console.WriteLine(矩阵取反结果为:n + (-m); else if (count = 4) Console.WriteLine(您选择了返回!); C

26、onsole.ReadKey(); break; else if (cnt = 2) Matrix m1 = null; Matrix m2 = null; int x1, y1, z1, x2, y2, z2; Console.WriteLine(请依次输入第一个矩阵行和列的大小:); x1 = Int32.Parse(Console.ReadLine(); y1 = Int32.Parse(Console.ReadLine(); Console.WriteLine(请输入第一个矩阵元素随机值范围(不设置随机请输入0):); z1 = Int32.Parse(Console.ReadLine

27、(); if (z1 = 0) m1 = new Matrix(x1, y1); else m1 = new Matrix(x1, y1, z1); Console.WriteLine(请依次输入第二个矩阵行和列的大小:); x2 = Int32.Parse(Console.ReadLine(); y2 = Int32.Parse(Console.ReadLine(); Console.WriteLine(请输入第二个矩阵元素随机值范围(不设置随机请输入0):); z2 = Int32.Parse(Console.ReadLine(); if (z2 = 0) m2 = new Matrix(

28、x2, y2); else m2 = new Matrix(x2, y2, z2); Console.WriteLine(第一个矩阵:n + m1 + n第二个矩阵:n + m2); while (true) Console.WriteLine(1.矩阵相加 2.矩阵相减 3.矩阵相乘 4.返回 请输入:); string count = Console.ReadLine(); if (count = 1) Console.WriteLine(矩阵相加结果为:n + (m1+m2); else if (count = 2) Console.WriteLine(矩阵相减结果为:n + (m1 - m2); else if (count = 3) Console.WriteLine(矩阵相乘结果为:n + (m1*m2); else if (count = 4) Console.WriteLine(您选择了返回!); Console.ReadKey(); break; else if (cnt = 0) break; else Console.WriteLine(请重新输入); Console.ReadKey();

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!