C# 入门的几个小程序?

《C#程序设计》实验报告

实验名称: 实验日期: 班 级: 学 号: 姓 名: 一、实验目的

1.熟练掌握C#开发环境的安装与配置。

2.加深理解面向对象编程的概念,如类、对象、实例化等;

3.熟练掌握类的声明格式,特别是累的成员定义、构造函数、初始化对象等; 4.熟练掌握方法的声明,理解并学会使用方法的参数传递,方法的重载等;

二、实验内容

【实验3-1】

定义描述复数的类,并实现复数的输入输出。设计三个方法分别完成复数的加法、减法、乘法运算; 【实验3-2】

定义全班学生的类,包括:姓名、学号、c++成绩、英语成绩、数学成绩、平均成绩。 设计下列四个方法: 1) 全班成绩的输入;

2) 求出全班每一个同学平均成绩; 3) 按平均成绩升序排序; 4) 输出全班成绩。 【实验3-3】

定义一个描述学生基本情况的类数据成员包括姓名、学号、c++、英语、数学成绩。成员函数包括输出数据、置姓名、学号、三门课成绩,求总成绩和平均成绩。 【实验3-4】

设有一个描述坐标点的CPoint类,其私有成员点x,y坐标值,编写程序实现以下功能:利用构造函数传递参数,并设置默认值为60,75,利用成员函数display()输出这一默认值,利用公有成员函数setpoint()将坐标修改为80、150,并利用成员函数使之输出。 三、实验过程

【实验3-1】 主要代码如下:

using System;

using System.Collections.Generic; using System.Text; namespace 实验 {

class Program {

static void Main(string[] args) {

complex a = new complex(2, 5); complex b = new complex(4, 6); complex c = a + b; c.print(); complex d = a - b;

d.print();

complex m = a * b; m.print(); Console.Read(); } }

class complex {

double r, v;

public complex(double r, double v) {

this.r = r; this.v = v; }

public complex() { }

public static complex operator +(complex a, complex b) {

return new complex(a.r + b.r, a.v + b.v); }

public static complex operator -(complex a, complex b) {

return new complex(a.r - b.r, a.v - b.v); }

public static complex operator *(complex a, complex b) {

double j, k;

j = a.r * a.v - a.v * b.v; k = a.r * b.v + a.r * b.r; return new complex(j, k); }

public void print()

{ Console.Write(r + "+" + v + "i\n"); } } }

【实验3-2】

using System;

using System.Collections.Generic; using System.Text; namespace hanxu {

class Program {

static void Main(string[] args) {

int t = 0; int k = 1;

Console.Write("请输入全班同学的人数:"); t = Convert.ToInt32(Console.ReadLine()); score[] stu = new score[t];

Console.WriteLine("请输入全班同学的信息:"); for (int i = 0; i < t; i++) {

Console.WriteLine("请输入第{0}个学生信息:", k++); stu[i] = new score(); stu[i].init(); stu[i].AVG(); }

Console.WriteLine("按学号排序学生信息:");

Console.WriteLine(" 姓名 学号 C++ 英语 数学 平均成绩"); for (int j = 0; j < t; j++) {

stu[j].display(); }

Console.WriteLine("排序后的学生信息:"); paixu(t, stu); Console.Read(); }

public static void paixu(int t, score[] stu) {

score stud = new score(); if (stu.Length <= 0) return;

for (int i = 0; i < t; i++) {

for (int j = 0; j < t - i - 1; j++) {

if (stu[j].Avg > stu[j + 1].Avg)

{ stud = stu[j + 1]; stu[j + 1] = stu[j]; stu[j] = stud; } }

}

Console.WriteLine(" 姓名 学号 C++ 英语 数学 平均成绩"); foreach (score x in stu) x.display(); } } class score {

private string name; private int ID; private double c; private double english; private double math; double avg; public score() { this.avg = 0; } public string Name

{ get { return name; } set { name = value; } } public int id

{ get { return ID; } set { ID = value; } } public double C

{ get { return c; } set { c = value; } } public double English

{ get { return english; } set { english = value; } } public double Math

{ get { return math; } set { math = value; } } public double Avg

{ get { return avg; } set { avg = value; } } public void init() {

Console.Write("学号:");

this.ID = Convert.ToInt32(Console.ReadLine()); Console.Write("姓名:");

this.name = Convert.ToString(Console.ReadLine()); Console.Out.Write("c++:");

this.c = Convert.ToDouble(Console.ReadLine()); Console.Write("英语:");

this.english = Convert.ToDouble(Console.ReadLine()); Console.Write("数学:");

this.math = Convert.ToDouble(Console.ReadLine()); }

public void AVG() {

this.avg = (this.math + this.english + this.c) / 3;

}

public void display() {

Console.WriteLine("{0,5}{1,5}{2,6}{3,6}{4,6}{5,6}", name, ID, c, english, math, avg); } } }

运算结果:

【实验3-3】

主要代码如下:

using System;

using System.Collections.Generic; using System.Text; namespace hanxu {

class Program {

static void Main(string[] args) {

score stu = new score();

Console.WriteLine("请输入全班同学信息:"); stu.init();

Console.WriteLine(" 姓名 学号 C++ 英语 数学"); stu.display(); stu.AVG(); stu.Sum(); Console.Read(); } } class score {

private string name; private int ID; private double c; private double english; private double math; public string Name

{ get { return name; } set { name = value; } } public int id

{ get { return ID; } set { ID = value; } } public double C

{ get { return c; } set { c = value; } } public double English

{ get { return english; } set { english = value; } } public double Math

{ get { return math; } set { math = value; } } public void init() {

Console.Write("学号:");

this.ID = Convert.ToInt32(Console.ReadLine()); Console.Write("姓名:");

this.name = Convert.ToString(Console.ReadLine()); Console.Out.Write("c++:");

this.c = Convert.ToDouble(Console.ReadLine()); Console.Write("英语:");

this.english = Convert.ToDouble(Console.ReadLine()); Console.Write("数学:");

this.math = Convert.ToDouble(Console.ReadLine()); }

public void AVG() {

double avg;

avg = (this.math + this.english + this.c) / 3; Console.WriteLine("学生的平均成绩:{0}", avg); }

public void Sum() {

double sum;

sum = this.math + this.c + this.english; Console.WriteLine("学生的总成绩:{0}", sum); }

public void display() {

Console.WriteLine("{0,5}{1,5}{2,6}{3,6}{4,6}", name, ID, c, english, math); } } }

运算结果:

【实验3-4】

using System;

using System.Collections.Generic; using System.Text; namespace hanxu {

class Program {

static void Main(string[] args) {

CPoint p = new CPoint(); p.display();

Console.WriteLine("修改后的坐标:");

p.setpoint(80, 150); Console.Read(); } }

class CPoint {

private double x; private double y; public CPoint() {

x = 60; y = 75; }

public CPoint(double m, double n) {

this.x = m; this.y = n; }

public void display() {

Console.WriteLine("坐标({0},{1})", x, y); }

public void setpoint(double m, double n) {

this.x = m; this.y = n; display(); } } }

运算结果:

四、实验总结

通过本次实验加深我对面向对象编程的概念的理解,掌握了类的用法及方法的声明,学会了对于如何使用方法的参数传递,让我对课本上学的知识有了更深的理解