《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(); } } }
運算結果:
四、實驗總結
通過本次實驗加深我對面向對象編程的概念的理解,掌握了類的用法及方法的聲明,學會了對於如何使用方法的參數傳遞,讓我對課本上學的知識有了更深的理解