第一篇:C#基礎(chǔ)編程設(shè)計實驗報告
C# 基礎(chǔ)編程 設(shè)計實驗報告
一、實驗?zāi)康?/span>
1、熟悉 Visual Studio.NET 開發(fā)環(huán)境。
2、掌握 C#應(yīng)用程序的基本操作過程。
3、掌握 C#的數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用。
4、掌握分支和循環(huán)語句的使用方法。
5、掌握一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實驗步驟
1、分析題意。
2、根據(jù)題目要求,新建項目。
3、編寫并輸入相關(guān)的程序代碼。
5、運(yùn)行與調(diào)試項目。
6、保存項目。
五、實驗內(nèi)容
1、編寫一個簡單的控制臺應(yīng)用程序,打印一行文字(如你的姓名)。
using System;using System.Collections.Generic;
using System.Linq;using System.Text;
namespace one.first {
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(“我叫王蕾!”);
}
} } 2、編寫一個簡單的 Windows 應(yīng)用程序,在窗體 Load 事件中書寫代碼,標(biāo)簽中顯示你的姓名。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;
using System.Windows.Forms;
namespace one.second {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = “Windows 程序”;
Label lblShow = new Label();
lblShow.Location = new Point(20, 30);
lblShow.AutoSize = true;
lblShow.Text = “王蕾!”;
this.Controls.Add(lblShow);
}
}
} 3、編寫一個一個程序,用來判斷輸入的是大寫字母,小寫字母,數(shù)字還是其他的字符。
using System;using System.Collections.Generic;using System.Text;
namespace one.third {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“請輸入一個字符:”);
char c = Convert.ToChar(Console.ReadLine());
if((c>=“a”&&c<=“z”)||(c>=“A”&&c<=“Z”))
Console.WriteLine(“這是一個字母”);
if(char.IsDigit(c))
Console.WriteLine(“這是一個數(shù)字”);
}
}
} 4、分別用 while,do-while,for 循環(huán)求 1 到 100 的和。
using System;using System.Collections.Generic;using System.Text;
namespace one.forth.one {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
while(i <= 100)
{
sum = sum + i;
i++;
}
Console.WriteLine(“1 到 100 的自然數(shù)之和為:” + sum);
}
}
} using System;using System.Collections.Generic;using System.Text;
namespace one.forth.two {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
do
{
sum = sum + i;
i++;
}
while(i <= 100);
Console.WriteLine(“1 到 100 的自然數(shù)的和為:” + sum);
}
}
} using System;using System.Collections.Generic;using System.Text;
namespace one.forth.three {
class Program
{
static void Main(string[] args)
{
int i , sum = 0;
for(i = 1;i <= 100;i++)
{
sum = sum + i;
}
Console.WriteLine(“1 到 100 的自然數(shù)的和為:” + sum);
}
} } 5、定義一個一維數(shù)組,用隨機(jī)數(shù)為此賦值,用 foreach 循環(huán)輸
出其中的內(nèi)容。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace first.five {
class Program
{
static void Main(string[] args)
{
int[] a = {0,1,2,3,4};
foreach(int i in a)
{
Console.WriteLine(a[i]);
}
}
} } 6、實現(xiàn)二維數(shù)組的輸入和輸出。
using System;
using System.Collections.Generic;using System.Linq;using System.Text;
namespace first.six {
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
{
for(int i = 0;i < 2;i++)
{
for(int j = 0;j < 3;j++)
{ Console.WriteLine(a[i, j]);}
}
}
}
} }
7、實現(xiàn)數(shù)組型數(shù)組的輸入和輸出。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace first.seven {
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
for(int i = 0;i < a.Length;i++)
{
for(int j = 0;j < a[i].Length;j++)
{
Console.WriteLine(a[i][j]);
}
}
}
} } 六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
剛開始編程的時候覺得無從下手,盡管我們已經(jīng)學(xué)了好幾種高級編程語言,但每個都有其獨(dú)特的地方,稍不留神就會混淆。
通過這次實驗,我體會到課后復(fù)習(xí)鞏固的重要性。在編程的時候,很多內(nèi)容都不記得,需要去翻書。不得不說,實驗是鞏固課程的好方法!本次實驗,我熟悉 Visual Studio.NET 開發(fā)環(huán)境;掌握了 C#應(yīng)用程序的基本操作過程;掌握了 C#的數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用;掌握了分支和循環(huán)語句的使用方法以及一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
實驗項目名稱:
類與對象
實驗學(xué)時:
同組學(xué)生姓名:
實驗地點:
1318
實驗日期:
月 26 日-11 月 9 日 實驗成績:
批改教師:
批改時間:
實驗 2
類與對象
一、實驗?zāi)康摹⒁?/p>
(1)掌握類的定義和使用;(2)掌握類的數(shù)據(jù)成員,屬性的定義和使用;(3)掌握方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞;(4)掌握構(gòu)造函數(shù)的定義和使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實驗步驟
1、分析題意; 2、根據(jù)題目要求,新建項目; 3、編寫并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項目; 6、保存項目。
五、實驗內(nèi)容
1、定義一個方法,實現(xiàn)兩個數(shù)的交換(分別把參數(shù)按值傳遞和按引用傳遞)。
using System;
using System.Collections.Generic;using System.Text;
namespace second.one {
class Program
{
static void Main(string[] args)
{
Swaper s = new Swaper();
Console.WriteLine(“輸入 x 的值:”);
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“輸入 y 的值:”);
int b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(s.Swap(a, b));
Console.WriteLine(s.Swap(ref a,ref b));
}
class Swaper
{
public string Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format(“按值傳參交換之后:x={0},y={1}”,x,y);
}
public string Swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format(“按引用傳參交換之后:x={0},y={1}”, x, y);
}
}
} }2、定義一個方法,實現(xiàn)數(shù)組的排序。
using System;using System.Collections.Generic;using System.Text;
namespace second.two {
class Program
{
public class sort
{
public void change(int[] a)
{
Console.WriteLine(“排序前,數(shù)組順序為:”);
show(a);
int i, j, m;
for(i = 0;i < 10;i++)
{
m = a[i];
j = i-1;//a[j]為數(shù)組前一個值
while(j >= 0 && m > a[j])//判斷 i 下標(biāo)的數(shù)是否大于 j 下標(biāo)的數(shù)
{
a[j + 1] = a[j];//如果 i 下標(biāo)大于j 把 j 往后移一個位
j--;
}
a[j+1] = m;//當(dāng)不大于 j 的時候就把 M的值放到 i 下標(biāo)下面 j+1 是為了下標(biāo)減到最前時考慮-1 + 1 還是下標(biāo)的最前面
}
Console.WriteLine(“排序后,數(shù)組順序為:”);
show(a);
}
void show(int[] a)
{
int i;
for(i = 0;i < 10;i++)
{
Console.Write(“{0} ”, a[i]);
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int[] a ={ 4, 7, 1, 2, 5, 8, 9, 10, 3, 6 };
sort s=new sort();
s.change(a);
}
} } 3、定義一個學(xué)生類,把學(xué)生類當(dāng)作對象來傳遞。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace second.three {
class Program
{
public class student
{
public void st()
{
int a = 999;
}
}
public class st
{
public void aa(student s)
{
Console.WriteLine(s);
}
}
static void Main(string[] args)
{
student s=new student();
st s1 = new st();
s1.aa(s);
}
} } 4、定義一個方法,求兩個數(shù)的和和差,通過參數(shù)把這兩個值帶回。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace
second.four
{
class Program
{
public class sum
{
public void ab(out int m, out
int n,int a, int b)
{
m = a + b;
n = a-b;
}
}
static void Main(string[] args)
{
sum s = new sum();
int a = 10;
int b = 3;
int m, n;
s.ab(out m, out n, a, b);
Console.WriteLine(“{0}+{1}={2};{0}-
{1}={3}”,a,b,m,n);
}
} } 5、用構(gòu)造函數(shù)重載,實現(xiàn)矩形的面積,圓的面積,梯形的面積; using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace secong.five {
class Program
{
public class square
{
public double area;
public square(){ }
public square(double a)
{
area = a * a * 3.14;
}
public square(double a, double b)
{
area = a * b;
}
public square(double a, double b, double h)
{
area =(a + b)/ 2 * h;
}
}
static void Main(string[] args)
{
double a, b, h,area;
a = 2;b = 5;h = 3;
square s = new square(a,b);
Console.WriteLine(“求矩形面積,長為 a={0},寬為 b={1},面積 area={2}”,a,b,s.area);
square i = new square(a);
Console.WriteLine(“求圓形面積,半徑 a={0},面積 area={1}”, a, i.area);
square j = new square(a, b, h);
Console.WriteLine(“求梯形面積,上底為a={0},下底為 b={1},高為 h={2}面積 area={3}”, a, b,h, j.area);
}
} } 6、設(shè)計一個 windows 應(yīng)用程序,在該程序中定義一個學(xué)生類和班級類,以處理每個學(xué)生的學(xué)號,姓名,語文,數(shù)學(xué)和英語成績,要求:
1)能查詢每個學(xué)生的總成績。
2)能顯示全班前三名的名單。
3)能顯示單科成績最高分和不及格的學(xué)生名單。
4)能統(tǒng)計全班學(xué)生的平均成績。
5)能顯示各科成績不同分?jǐn)?shù)段的學(xué)生人數(shù)的百分比。
Student 類:
using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {
public class Student
{
public string stuNo;
public string name;
public double chinese;
public double math;
public double english;
public double sumScore
{
get { return chinese + math + english;}
}
} } StudentList 類:
using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {
public class StudentList:Student
{
int snums;
public Student[] stu=new Student[50];
public StudentList()
{
snums = 0;
}
public void addstu(Student s)
{
stu[snums] = s;
snums++;
}
public int searchstu(string name)
{
int i;
for(i = 0;i < snums;i++)
{
if(stu[i].name == name)break;
}
if(i == snums)return-1;
else return i;
}
//給所有成績排序,用后面實現(xiàn)前三名的排名
public void ProThree()
{
for(int i = 0;i < snums;i++)
{
int k = i;
for(int j = i + 1;j < snums;j++)
if(stu[j].sumScore > stu[k].sumScore)k = j;
if(k!= i)
{
Student temp;
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
}
//顯示單科成績的最高分
public int HighScore(int k)
{
int p = 0;
if(k == 0)
{
for(int i = 1;i < snums;i++)
if(stu[i].math > stu[p].math)p = i;
}
else if(k == 1)
{
for(int i = 1;i < snums;i++)
if(stu[i].chinese > stu[p].chinese)p = i;
}
else
{
for(int i = 1;i < snums;i++)
if(stu[i].chinese > stu[p].chinese)p = i;
}
return p;
}
//顯示不及格名單
public string
BuhgName(int k)
{
string name=“ ”;
if(k == 0)
{
for(int i = 0;i < snums;i++)
if(stu[i].math < 60)name +=stu[i].name+“n”;
}
else if(k == 1)
{
for(int i = 0;i < snums;i++)
if(stu[i].chinese < 60)name += stu[i].name + “n”;
}
else
{
for(int i = 0;i < snums;i++)
if(stu[i].english < 60)name += stu[i].name + “n”;
}
return name;
}
public string getHL()
{
string Maxer = “ ”, Loser = “ ”;
Maxer += “ 單 科 數(shù) 學(xué) 最 高 :
” + stu[HighScore(0)].name + “n”;
Maxer += “ 單 科 語 文 最 高 :
” +
stu[HighScore(1)].name + “n”;
Maxer += “ 單 科 英 語 最 高 :
” + stu[HighScore(2)].name + “n”;
Loser += “單科數(shù)學(xué)掛科名單:” +BuhgName(0)+ “n”;
Loser += “單科語文掛科名單:” + BuhgName(1)+ “n”;
Loser += “單科英語掛科名單:” + BuhgName(2)+ “n”;
return Maxer + “n” + Loser;
}
//全班的平均成績
public string SumScore()
{
double sum = 0;
double avg=0;
for(int i = 0;i < snums;i++)
{
sum = sum + stu[i].sumScore;
}
avg = sum / snums;
return “班級總分平均分:”+avg;
}
//各科成績不同分?jǐn)?shù)段的學(xué)生百分比
//英語成績各分?jǐn)?shù)段百分比
public string PerC()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].chinese > 90)&&(stu[i].chinese <= 100))
{
sumC1++;
}
else if((80 <= stu[i].chinese)&&(stu[i].chinese < 90))
{
sumC2++;
}
else if((70<=stu[i].chinese)&&(stu[i].chinese < 80))
{
sumC3++;
}
else if((60<=stu[i].chinese)&&(stu[i].chinese < 70))
{
sumC4++;
}
else
{sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return “ 語 文 成 績 百 分 比 :”+“n”+“90~100:”+per1+“
80~90:”+per2+“
80~70:”+per3+“
70~60:”+per4+“
以下的:”+per5;
}
//數(shù)學(xué)成績各分?jǐn)?shù)段百分比
public string PerM()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].math> 90)&&(stu[i].math <= 100))
{
sumC1++;
}
else if((80 <= stu[i].math)&&(stu[i].math < 90))
{
sumC2++;
}
else if((70 <= stu[i].math)&&(stu[i].math < 80))
{
sumC3++;
}
else if((60 <= stu[i].math)&&(stu[i].math < 70))
{
sumC4++;
}
else
{ sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format(“數(shù)學(xué)成績百分比:” + “n” + “90~100:” + per1 + “
80~90:” + per2 + “
80~70:” + per3 + “
70~60:” + per4 + “
以下的:” + per5);
}
//英語成績各分?jǐn)?shù)段百分比
public string PerE()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].english > 90)&&(stu[i].english <= 100))
{
sumC1++;
}
else if((80 <= stu[i].english)&&(stu[i].english < 90))
{
sumC2++;
}
else if((70 <= stu[i].english)&&(stu[i].english < 80))
{
sumC3++;
}
else if((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;
return string.Format(“數(shù)學(xué)成績百分比:” + “n” + “90~100:” + per1 + “
80~90:” + per2 + “
80~70:” + per3 + “
70~60:” + per4 + “
以下的:” + per5);
}
} } From 窗體代碼:
using System;using System.Collections.Generic;
using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test2_6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public StudentList sl = new StudentList();
private void btnAdd_Click(object sender, EventArgs e)
{
Student s = new Student();
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(“添加成功”);
}
private void btnSearch_Click(object sender, EventArgs e)
{
int pos = sl.searchstu(this.textBox1.Text);
if(pos!=-1)
{
label7.Text = this.textBox1.Text + “的總成績:” + sl.stu[pos].sumScore;
}
else { MessageBox.Show(“不存在這個人!”);}
}
private void btnFinish_Click(object sender, EventArgs e)
{
label7.Text = “前 3 名:”+“n”;
for(int i = 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”;
}
} }
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
通過本次實驗,我掌握了類的定義與使用;掌握了類的數(shù)據(jù)成員,屬性的定義和使用;掌握了方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞以及構(gòu)造函數(shù)的定義和使用。值得注意的是:本次實驗中 return的使用以及所在的位置,類型轉(zhuǎn)換時也經(jīng)常用到
實驗項目名稱:
繼承與多態(tài)
實驗學(xué)時:
同組學(xué)生姓名:
實驗地點:
1318
實驗日期:月 16 日-11 月 30 日 實驗成績:
批改教師:
批改時間:
實驗 3
繼承與多態(tài)
一、實驗?zāi)康?、要?/span>
(1)掌握類的繼承性與多態(tài)性;(2)掌握虛方法的定義以及如何使用虛方法實現(xiàn)多態(tài);(3)掌握抽象類的定義以及如何使用抽象方法實現(xiàn)多態(tài); 二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實驗步驟
1、分析題意; 2、根據(jù)題目要求,新建項目; 3、編寫并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項目; 6、保存項目。
五、實驗內(nèi)容
1、設(shè)計一個 Windows 應(yīng)用程序,在該程序中首先構(gòu)造一個學(xué)生基本類,再分別構(gòu)造小學(xué)生、中學(xué)生、大學(xué)生派生類,當(dāng)輸入相關(guān)數(shù)據(jù),單擊不用的按鈕時,將分別創(chuàng)建不同的學(xué)生類對象,并輸出當(dāng)前學(xué)生的總?cè)藬?shù),該學(xué)生的姓名,學(xué)生類型,平均成績。
Student 類:
using System;using System.Collections.Generic;using System.Text;namespace Test3_1 {
public abstract class Student
{
protected string name;
protected int age;
public static int number;
public Student(string name, int age)
{
this.name = name;
this.age = age;
number++;
}
public string Name
{
get { return name;}
}
public abstract double Average();
}
public class Pupil : Student
{
protected double chinese;
protected double math;
public Pupil(string name, int age, double chinese, double math)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override double Average()
{
return(chinese + math)/ 2;
}
}
public class Middle : Student
{
protected double chinese;
protected double math;
protected double english;
public Middle(string name, int age, double
chinese, double math, double english)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
this.english = english;
}
public override double Average()
{
return(chinese + math + english)/ 3;
}
}
public class College : Student
{
protected double required;
protected double elective;
public College(string name, int age, double required, double elective)
: base(name, age)
{
this.required = required;
this.elective = elective;
}
public override double Average()
{
return(required + elective)/ 2;
}
} } Form 窗體內(nèi)的代碼:
using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_1 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSmall_Click(object sender, EventArgs e)
{
Pupil p = new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text));
lblShow.Text += “ 總 人 數(shù) :” +Convert.ToString(Student.number)+ “,” + “姓名:” + p.Name + “,” + “小學(xué)生” + “,” + “平均成績?yōu)椋骸?+ p.Average()+“n”;
}
private void btnMiddle_Click(object sender, EventArgs e)
{
Middle m = new Middle(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));
lblShow.Text += “ 總 人 數(shù) :” + Convert.ToString(Student.number)+ “,” + “姓名:” + m.Name +
“,” + “中學(xué)生” + “,” + “平均成績?yōu)椋骸?+ m.Average()+ “n”;
}
private void btnBig_Click(object sender, EventArgs e)
{
College c = new College(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text));
lblShow.Text += “ 總 人 數(shù) :” + Convert.ToString(Student.number)+ “,” + “姓名:” + c.Name + “,” + “大學(xué)生” + “,” + “平均成績?yōu)椋骸?+ c.Average()+ “n”;
}
} } 2、設(shè)計一個 Windows 應(yīng)用程序,在該程序中定義平面圖形抽象類和派生類圓,矩形和三角形。
Figure 類代碼:
using System;using System.Collections.Generic;using System.Text;namespace Test3_2
{
public abstract class Figure
{
public abstract double Area();
}
public class Circle:Figure
{
double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return radius * radius * 3.14;
}
}
public class JUxing:Figure
{
double chang;
double kuan;
public JUxing(double c, double k)
{
this.chang = c;
this.kuan = k;
}
public override double Area()
{
return chang * kuan;
}
}
public class San:Figure
{
double bian;
double heigth;
public San(double b, double h)
{
this.bian = b;
this.heigth = h;
}
public override double Area()
{
return bian * heigth / 2;
}
} } Form 窗體代碼:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_2 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCircle_Click(object sender, EventArgs e)
{
Circle c=new
Circle(Convert.ToInt32(TxtChang.Text));
lblShow.Text = “圓的面積為:” + c.Area();
}
private void btnJu_Click(object sender, EventArgs e)
{
JUxing j = new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));
lblShow.Text = “矩形的面積為:” + j.Area();
}
private void btnSan_Click(object sender, EventArgs e)
{
San s = new San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));
lblShow.Text = “三角形的面積為:” + s.Area();
}
} }
3、定義一個 Person 類,包含姓名字段和一個方法,早上 8:30學(xué)生開始上課,教師開始講課。分別用 new 關(guān)鍵字,虛方法,抽象類實現(xiàn)多態(tài)性。
New 關(guān)鍵字:
using System;using System.Collections.Generic;using System.Text;
namespace third.three {
class Program
{
static void Main(string[] args)
{
Student s=new Student(“學(xué)生”);
Teacher t=new Teacher(“教師”);
Console.WriteLine(s.name+s.work());
Console.WriteLine(t.name+t.work());
Console.ReadLine();
}
}
public class Person
{
public string name;
public interface method
{ string work();}
}
public class Student:Person
{
public Student(string name)
{ this.name = name;}
public string work()
{ return “早上 8:30 開始上課”;}
}
public class Teacher:Person
{
public Teacher(string name)
{ this.name = name;}
public string work()
{ return “開始講課”;}
} } 虛方法:
using System;
using System.Collections.Generic;using System.Text;
namespace third.three.two {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”,“學(xué)生”);
PersonWork(s);
Teacher t=new Teacher(“李斯”,“教師”);
PersonWork(t);
}
private static void PersonWork(Person Person)
{ Console.WriteLine(Person.Work());}
}
public class Person
{
public string name;
public Person(string name)
{ this.name = name;}
public virtual string Work()
{ return string.Format(“Person{0}:早上 8:30 開始”,name);}
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{ this.type = type;}
public override string Work()
{
return string.Format(“Person{0}:早上 8:30 開始上課”, name);
}
}
public class Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{ this.type = type;}
public override string Work()
{
return string.Format(“Person{0}:開始講課”, name);
}
} }
抽象類:
using System;using System.Collections.Generic;using System.Text;
namespace third.three.three {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”, “學(xué)生”);
PersonWork(s);
Teacher t = new Teacher(“李斯”, “教師”);
PersonWork(t);
}
private static void PersonWork(Person person)
{
Console.WriteLine(person.Work());
}
}
public abstract class Person
{
public string name;
public Person(string name)
{ this.name = name;}
public abstract string Work();
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(“Person{0}:早上 8:30 開始上課”, name);
}
}
public class Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(“Person{0}:開始講課”, name);
}
}
}
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
通過本次實驗,我理解了類的繼承性與多態(tài)性;掌握了虛方法的定義以及如何用虛方法來實現(xiàn)多態(tài);掌握了抽象類的定義以及如何用抽象方法來實現(xiàn)多態(tài)。
這次實驗與前兩次不同,采用 Windows 應(yīng)用程序,既涉及到代碼段也涉及到界面的設(shè)計。所以,勉強(qiáng)通過實驗。
實驗項目名稱:
接口、文件和流
實驗學(xué)時:
同組學(xué)生姓名:
實驗地點:
A205
實驗日期:月 7 日-12 月 21 日 實驗成績:
批改教師:
批改時間:
實驗 4
接口、文件和流
一、實驗?zāi)康?/span>
(1)掌握接口的定義及使用方法;(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的讀寫操作類及其使用方法;(4)掌握 OpenFileDialog,SaveFileDialog 等控件的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實驗步驟
1、分析題意; 2、根據(jù)題目要求,新建項目; 3、編寫并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項目; 6、保存項目。
五、實驗內(nèi)容
1、定義一個 Person 類,包含姓名字段和一個方法,早上 8:30學(xué)生開始上課,教師開始講課。用接口來實現(xiàn)。
using System;using System.Collections.Generic;
using System.Text;namespace Test4_1 {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”,“學(xué)生”);
Console.WriteLine(s.Work());
Teacher t = new Teacher(“李四”,“老師”);
Console.WriteLine(t.Work());
}
public abstract class Person
{
public string name;
public Person(string name)
{
this.name = name;
}
}
interface IPerson
{
string type { get;}
string Work();
}
public class Student :Person, IPerson
{
public string type
{
get { return string.Format(“老師”);}
}
public Student(string name, string type)
: base(name)
{
this.name=name;
}
public string Work()
{
return string.Format(“Person{0}:早上 8:30 開始上課”, name);
}
}
...
第二篇:C#程序設(shè)計實驗報告
實驗報告書寫要求
實驗報告原則上要求學(xué)生手寫,要求書寫工整。若因課程特點需打印的,標(biāo)題采用四號黑體,正文采用小四號宋體,單倍行距。紙張一律采用A4的紙張。
實驗報告書寫說明
實驗報告中實驗?zāi)康暮鸵?、實驗儀器和設(shè)備、實驗內(nèi)容與過程、實驗結(jié)果與分析這四項內(nèi)容為必需項。教師可根據(jù)學(xué)科特點和實驗具體要求增加項目。
填寫注意事項
(1)細(xì)致觀察,及時、準(zhǔn)確、如實記錄。(2)準(zhǔn)確說明,層次清晰。
(3)盡量采用專用術(shù)語來說明事物。
(4)外文、符號、公式要準(zhǔn)確,應(yīng)使用統(tǒng)一規(guī)定的名詞和符號。(5)應(yīng)獨(dú)立完成實驗報告的書寫,嚴(yán)禁抄襲、復(fù)印,一經(jīng)發(fā)現(xiàn),以零分論處。
實驗報告批改說明
實驗報告的批改要及時、認(rèn)真、仔細(xì),一律用紅色筆批改。實驗報告的批改成績采用五級記分制或百分制,按《金陵科技學(xué)院課堂教學(xué)實施細(xì)則》中作業(yè)批閱成績評定要求執(zhí)行。
實驗報告裝訂要求
實驗批改完畢后,任課老師將每門課程的每個實驗項目的實驗報告以自然班為單位、按學(xué)號升序排列,裝訂成冊,并附上一份該門課程的實驗大綱。
金陵科技學(xué)院實驗報告
實驗項目名稱: C#基礎(chǔ)編程 實驗學(xué)時: 6 同組學(xué)生姓名: 實驗地點: 1318 實驗日期: 10月5日-10月19日 實驗成績: 批改教師: 批改時間:
金陵科技學(xué)院實驗報告
實驗1 C#基礎(chǔ)編程
一、實驗?zāi)康?/p>
1、熟悉Visual Studio.NET開發(fā)環(huán)境。
2、掌握C#應(yīng)用程序的基本操作過程。
3、掌握C#的數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用。
4、掌握分支和循環(huán)語句的使用方法。
5、掌握一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意。
2、根據(jù)題目要求,新建項目。
3、編寫并輸入相關(guān)的程序代碼。
5、運(yùn)行與調(diào)試項目。
6、保存項目。
五、實驗內(nèi)容
1、編寫一個簡單的控制臺應(yīng)用程序,打印一行文字(如你的姓名)。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace one.first {
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(“我叫王蕾!”);
}
} }
2、編寫一個簡單的Windows應(yīng)用程序,在窗體Load事件中書寫代碼,標(biāo)簽中顯示你的姓名。
using System;using System.Collections.Generic;using System.ComponentModel;
金陵科技學(xué)院實驗報告
using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;
namespace one.second {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = “Windows 程序”;
Label lblShow = new Label();
lblShow.Location = new Point(20, 30);
lblShow.AutoSize = true;
lblShow.Text = “王蕾!”;
this.Controls.Add(lblShow);
}
} }
3、編寫一個一個程序,用來判斷輸入的是大寫字母,小寫字母,數(shù)字還是其他的字符。
using System;using System.Collections.Generic;using System.Text;
namespace one.third {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“請輸入一個字符:”);
char c = Convert.ToChar(Console.ReadLine());
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
Console.WriteLine(“這是一個字母”);
if(char.IsDigit(c))
Console.WriteLine(“這是一個數(shù)字”);
金陵科技學(xué)院實驗報告
}
} }
4、分別用while,do-while,for循環(huán)求1到100的和。
using System;using System.Collections.Generic;using System.Text;
namespace one.forth.one {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
while(i <= 100)
{
sum = sum + i;
i++;
}
Console.WriteLine(“1到100的自然數(shù)之和為:” + sum);
}
} } using System;using System.Collections.Generic;using System.Text;
namespace one.forth.two {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
do
{
sum = sum + i;
i++;
}
while(i <= 100);
Console.WriteLine(“1到100的自然數(shù)的和為:” + sum);
}
}
金陵科技學(xué)院實驗報告
} using System;using System.Collections.Generic;using System.Text;
namespace one.forth.three {
class Program
{
static void Main(string[] args)
{
int i , sum = 0;
for(i = 1;i <= 100;i++)
{
sum = sum + i;
}
Console.WriteLine(“1到100的自然數(shù)的和為:” + sum);
}
} }
5、定義一個一維數(shù)組,用隨機(jī)數(shù)為此賦值,用foreach循環(huán)輸出其中的內(nèi)容。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace first.five {
class Program
{
static void Main(string[] args)
{
int[] a = {0,1,2,3,4};
foreach(int i in a)
{
Console.WriteLine(a[i]);
}
}
} }
6、實現(xiàn)二維數(shù)組的輸入和輸出。
using System;using System.Collections.Generic;using System.Linq;
金陵科技學(xué)院實驗報告
using System.Text;
namespace first.six {
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
{
for(int i = 0;i < 2;i++)
{
for(int j = 0;j < 3;j++)
{ Console.WriteLine(a[i, j]);}
}
}
}
} }
7、實現(xiàn)數(shù)組型數(shù)組的輸入和輸出。
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace first.seven {
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
for(int i = 0;i < a.Length;i++)
{
for(int j = 0;j < a[i].Length;j++)
{
Console.WriteLine(a[i][j]);
}
}
}
} }
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
剛開始編程的時候覺得無從下手,盡管我們已經(jīng)學(xué)了好幾種高級編程語言,但每個都
金陵科技學(xué)院實驗報告
有其獨(dú)特的地方,稍不留神就會混淆。
通過這次實驗,我體會到課后復(fù)習(xí)鞏固的重要性。在編程的時候,很多內(nèi)容都不記得,需要去翻書。不得不說,實驗是鞏固課程的好方法!本次實驗,我熟悉Visual Studio.NET開發(fā)環(huán)境;掌握了C#應(yīng)用程序的基本操作過程;掌握了C#的數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用;掌握了分支和循環(huán)語句的使用方法以及一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
金陵科技學(xué)院實驗報告
實驗項目名稱: 類與對象 實驗學(xué)時: 6 同組學(xué)生姓名: 實驗地點: 1318 實驗日期: 10月26日-11月9日 實驗成績: 批改教師: 批改時間:
金陵科技學(xué)院實驗報告
實驗2 類與對象
一、實驗?zāi)康?、要?/p>
(1)掌握類的定義和使用;
(2)掌握類的數(shù)據(jù)成員,屬性的定義和使用;
(3)掌握方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞;(4)掌握構(gòu)造函數(shù)的定義和使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據(jù)題目要求,新建項目;
3、編寫并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項目;
6、保存項目。
五、實驗內(nèi)容
1、定義一個方法,實現(xiàn)兩個數(shù)的交換(分別把參數(shù)按值傳遞和按引用傳遞)。
using System;using System.Collections.Generic;using System.Text;
namespace second.one {
class Program
{
static void Main(string[] args)
{
Swaper s = new Swaper();
Console.WriteLine(“輸入x的值:”);
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“輸入y的值:”);
int b=Convert.ToInt32(Console.ReadLine());
金陵科技學(xué)院實驗報告
Console.WriteLine(s.Swap(a, b));
Console.WriteLine(s.Swap(ref a,ref b));
}
class Swaper
{
public string Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return 后:x={0},y={1}“,x,y);
}
public string Swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format(”按引用傳參交換之后:x={0},y={1}“, x, y);
}
}
} }
2、定義一個方法,實現(xiàn)數(shù)組的排序。using System;using System.Collections.Generic;using System.Text;
namespace second.two {
class Program
{
string.Format(”
按
值
傳
參
交
換
之
金陵科技學(xué)院實驗報告
public class sort
{
public void change(int[] a)
{
Console.WriteLine(“排序前,數(shù)組順序為:”);
show(a);
int i, j, m;
for(i = 0;i < 10;i++)
{
m = a[i];
j = ib;
}
}
static void Main(string[] args)
{
sum s = new sum();
int a = 10;
int b = 3;
int m, n;
s.ab(out m, out n, a, b);
Console.WriteLine(“{0}+{1}={2};{0}-{1}={3}”,a,b,m,n);
金陵科技學(xué)院實驗報告
}
} }
5、用構(gòu)造函數(shù)重載,實現(xiàn)矩形的面積,圓的面積,梯形的面積;
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace secong.five {
class Program
{
public class square
{
public double area;
public square(){ }
public square(double a)
{
area = a * a * 3.14;
}
public square(double a, double b)
{
area = a * b;
}
public square(double a, double b, double h)
{
area =(a + b)/ 2 * h;
}
}
static void Main(string[] args)
{
double a, b, h,area;
a = 2;b = 5;h = 3;
金陵科技學(xué)院實驗報告
square s = new square(a,b);
Console.WriteLine(“求矩形面積,長為a={0},寬為b={1},面積area={2}”,a,b,s.area);
square i = new square(a);
Console.WriteLine(“求圓形面積,半徑a={0},面積area={1}”, a, i.area);
square j = new square(a, b, h);
Console.WriteLine(“求梯形面積,上底為a={0},下底為b={1},高為h={2}面積area={3}”, a, b,h, j.area);
}
} }
6、設(shè)計一個windows應(yīng)用程序,在該程序中定義一個學(xué)生類和班級類,以處理每個學(xué)生的學(xué)號,姓名,語文,數(shù)學(xué)和英語成績,要求:
1)能查詢每個學(xué)生的總成績。2)能顯示全班前三名的名單。
3)能顯示單科成績最高分和不及格的學(xué)生名單。4)能統(tǒng)計全班學(xué)生的平均成績。
5)能顯示各科成績不同分?jǐn)?shù)段的學(xué)生人數(shù)的百分比。
Student類: using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {
public class Student
{
public string stuNo;
public string name;
public double chinese;
public double math;
public double english;
public double sumScore
{
金陵科技學(xué)院實驗報告
get { return chinese + math + english;}
}
} } StudentList類: using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {
public class StudentList:Student
{
int snums;
public Student[] stu=new Student[50];
public StudentList()
{
snums = 0;
}
public void addstu(Student s)
{
stu[snums] = s;
snums++;
}
public int searchstu(string name)
{
int i;
for(i = 0;i < snums;i++)
{
if(stu[i].name == name)break;
}
if(i == snums)return-1;
else return i;
}
//給所有成績排序,用后面實現(xiàn)前三名的排名
金陵科技學(xué)院實驗報告
public void ProThree()
{
for(int i = 0;i < snums;i++)
{
int k = i;
for(int j = i + 1;j < snums;j++)
if(stu[j].sumScore > stu[k].sumScore)k = j;
if(k!= i)
{
Student temp;
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
}
//顯示單科成績的最高分
public int HighScore(int k)
{
int p = 0;
if(k == 0)
{
for(int i = 1;i < snums;i++)
if(stu[i].math > stu[p].math)p = i;
}
else if(k == 1)
{
for(int i = 1;i < snums;i++)
if(stu[i].chinese > stu[p].chinese)p = i;
}
else
{
for(int i = 1;i < snums;i++)
if(stu[i].chinese > stu[p].chinese)p = i;
金陵科技學(xué)院實驗報告
}
return p;
}
//顯示不及格名單
public string BuhgName(int k)
{
string name=“ ”;
if(k == 0)
{
for(int i = 0;i < snums;i++)
if(stu[i].math < 60)name +=stu[i].name+“n”;
}
else if(k == 1)
{
for(int i = 0;i < snums;i++)
if(stu[i].chinese < 60)name += stu[i].name + “n”;
}
else
{
for(int i = 0;i < snums;i++)
if(stu[i].english < 60)name += stu[i].name + “n”;
}
return name;
}
public string getHL()
{
string Maxer = “ ”, Loser = “ ”;
Maxer += “單科數(shù)學(xué)最高:” + stu[HighScore(0)].name + “n”;
Maxer += “ 單科語文最高:” + stu[HighScore(1)].name + “n”;
Maxer += “ 單科英語最高:” + stu[HighScore(2)].name + “n”;
Loser += “單科數(shù)學(xué)掛科名單:” +BuhgName(0)+ “n”;
Loser += “單科語文掛科名單:” + BuhgName(1)+ “n”;
Loser += “單科英語掛科名單:” + BuhgName(2)+ “n”;
金陵科技學(xué)院實驗報告
return Maxer + “n” + Loser;
}
//全班的平均成績
public string SumScore()
{
double sum = 0;
double avg=0;
for(int i = 0;i < snums;i++)
{
sum = sum + stu[i].sumScore;
}
avg = sum / snums;
return “班級總分平均分:”+avg;
}
//各科成績不同分?jǐn)?shù)段的學(xué)生百分比
//英語成績各分?jǐn)?shù)段百分比
public string PerC()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].chinese > 90)&&(stu[i].chinese <= 100))
{
sumC1++;
}
else if((80 <= stu[i].chinese)&&(stu[i].chinese < 90))
{
sumC2++;
}
金陵科技學(xué)院實驗報告
else if((70<=stu[i].chinese)&&(stu[i].chinese < 80))
{
sumC3++;
}
else if((60<=stu[i].chinese)&&(stu[i].chinese < 70))
{
sumC4++;
}
else
{sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return “語文成績百分比:”+“n”+“90~100:”+per1+“ 80~90:”+per2+“ 80~70:”+per3+“ 70~60:”+per4+“ 60以下的:”+per5;
}
//數(shù)學(xué)成績各分?jǐn)?shù)段百分比
public string PerM()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].math> 90)&&(stu[i].math <= 100))
{
sumC1++;
}
else if((80 <= stu[i].math)&&(stu[i].math < 90))
{
金陵科技學(xué)院實驗報告
sumC2++;
}
else if((70 <= stu[i].math)&&(stu[i].math < 80))
{
sumC3++;
}
else if((60 <= stu[i].math)&&(stu[i].math < 70))
{
sumC4++;
}
else
{ sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format(“數(shù)學(xué)成績百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);
}
//英語成績各分?jǐn)?shù)段百分比
public string PerE()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for(int i = 0;i < snums;i++)
{
if((stu[i].english > 90)&&(stu[i].english <= 100))
{
sumC1++;
金陵科技學(xué)院實驗報告
}
else if((80 <= stu[i].english)&&(stu[i].english < 90))
{
sumC2++;
}
else if((70 <= stu[i].english)&&(stu[i].english < 80))
{
sumC3++;
}
else if((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;
return string.Format(“數(shù)學(xué)成績百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);
}
} } From窗體代碼: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;
金陵科技學(xué)院實驗報告
using System.Windows.Forms;namespace Test2_6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public StudentList sl = new StudentList();
private void btnAdd_Click(object sender, EventArgs e)
{
Student s = new Student();
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(“添加成功”);
}
private void btnSearch_Click(object sender, EventArgs e)
{
int pos = sl.searchstu(this.textBox1.Text);
if(pos!=-1)
{
label7.Text = this.textBox1.Text + “的總成績:sl.stu[pos].sumScore;
}
else { MessageBox.Show(”不存在這個人!“);}
}
private void btnFinish_Click(object sender, EventArgs e)
{
label7.Text = ”前3名:“+”n“;
” + 金陵科技學(xué)院實驗報告
for(int i = 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”;
}
} }
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
通過本次實驗,我掌握了類的定義與使用;掌握了類的數(shù)據(jù)成員,屬性的定義和使用;掌握了方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞以及構(gòu)造函數(shù)的定義和使用。值得注意的是:本次實驗中return的使用以及所在的位置,類型轉(zhuǎn)換時也經(jīng)常用到
金陵科技學(xué)院實驗報告
實驗項目名稱: 繼承與多態(tài) 實驗學(xué)時: 6 同組學(xué)生姓名: 實驗地點: 1318 實驗日期: 11月16日-11月30日 實驗成績: 批改教師: 批改時間:
金陵科技學(xué)院實驗報告
實驗3 繼承與多態(tài)
一、實驗?zāi)康?、要?/p>
(1)掌握類的繼承性與多態(tài)性;
(2)掌握虛方法的定義以及如何使用虛方法實現(xiàn)多態(tài);(3)掌握抽象類的定義以及如何使用抽象方法實現(xiàn)多態(tài);
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據(jù)題目要求,新建項目;
3、編寫并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項目;
6、保存項目。
五、實驗內(nèi)容
1、設(shè)計一個Windows應(yīng)用程序,在該程序中首先構(gòu)造一個學(xué)生基本類,再分別構(gòu)造小學(xué)生、中學(xué)生、大學(xué)生派生類,當(dāng)輸入相關(guān)數(shù)據(jù),單擊不用的按鈕時,將分別創(chuàng)建不同的學(xué)生類對象,并輸出當(dāng)前學(xué)生的總?cè)藬?shù),該學(xué)生的姓名,學(xué)生類型,平均成績。
Student類: using System;using System.Collections.Generic;using System.Text;namespace Test3_1 {
public abstract class Student
{
protected string name;
protected int age;
public static int number;
public Student(string name, int age)
{
this.name = name;
this.age = age;
金陵科技學(xué)院實驗報告
number++;
}
public string Name
{
get { return name;}
}
public abstract double Average();
}
public class Pupil : Student
{
protected double chinese;
protected double math;
public Pupil(string name, int age, double chinese, double math)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override double Average()
{
return(chinese + math)/ 2;
}
}
public class Middle : Student
{
protected double chinese;
protected double math;
protected double english;
public Middle(string name, int age, double chinese, double math, double english)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
金陵科技學(xué)院實驗報告
this.english = english;
}
public override double Average()
{
return(chinese + math + english)/ 3;
}
}
public class College : Student
{
protected double required;
protected double elective;
public College(string name, int age, double required, double elective)
: base(name, age)
{
this.required = required;
this.elective = elective;
}
public override double Average()
{
return(required + elective)/ 2;
}
} } Form窗體內(nèi)的代碼:
using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_1 {
public partial class Form1 : Form
{
金陵科技學(xué)院實驗報告
public Form1()
{
InitializeComponent();
}
private void btnSmall_Click(object sender, EventArgs e)
{
Pupil),Convert.ToDouble(txtMath.Text));
lblShow.Text += “總?cè)藬?shù):” +Convert.ToString(Student.number)+ “,” + “姓名:” + p.Name + “,” + “小學(xué)生” + “,” + “平均成績?yōu)椋骸?+ p.Average()+“n”;
}
private void btnMiddle_Click(object sender, EventArgs e)
{
Middle Convert.ToInt32(txtAge.Text),m
=
new
Middle(txtName.Text,Convert.ToDouble(txtChinese.Text), p
=
new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.TextConvert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));
lblShow.Text += “總?cè)藬?shù):” + Convert.ToString(Student.number)+ “,” + “姓名:” + m.Name + “,” + “中學(xué)生” + “,” + “平均成績?yōu)椋骸?+ m.Average()+ “n”;
}
private void btnBig_Click(object sender, EventArgs e)
{
College Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtMath.Text));
lblShow.Text += “總?cè)藬?shù):” + Convert.ToString(Student.number)+ “,” + “姓名:” + c.Name + “,” + “大學(xué)生” + “,” + “平均成績?yōu)椋骸?+ c.Average()+ “n”;
}
}
c
=
new
College(txtName.Text,Convert.ToDouble(txtChinese.Text),金陵科技學(xué)院實驗報告
}
2、設(shè)計一個Windows應(yīng)用程序,在該程序中定義平面圖形抽象類和派生類圓,矩形和三角形。
Figure類代碼: using System;using System.Collections.Generic;using System.Text;namespace Test3_2 {
public abstract class Figure
{
public abstract double Area();
}
public class Circle:Figure
{
double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return radius * radius * 3.14;
}
}
public class JUxing:Figure
{
double chang;
double kuan;
public JUxing(double c, double k)
{
this.chang = c;
this.kuan = k;
}
金陵科技學(xué)院實驗報告
public override double Area()
{
return chang * kuan;
}
}
public class San:Figure
{
double bian;
double heigth;
public San(double b, double h)
{
this.bian = b;
this.heigth = h;
}
public override double Area()
{
return bian * heigth / 2;
}
} } Form窗體代碼: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_2 {
public partial class Form1 : Form
{
public Form1()
{
金陵科技學(xué)院實驗報告
InitializeComponent();
}
private void btnCircle_Click(object sender, EventArgs e)
{
Circle c=new Circle(Convert.ToInt32(TxtChang.Text));
lblShow.Text = “圓的面積為:” + c.Area();
}
private void btnJu_Click(object sender, EventArgs e)
{
JUxing
j
=
new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));
lblShow.Text = “矩形的面積為:” + j.Area();
}
private void btnSan_Click(object sender, EventArgs e)
{
San
s
=
new
San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));
lblShow.Text = “三角形的面積為:” + s.Area();
}
} }
3、定義一個Person類,包含姓名字段和一個方法,早上8:30學(xué)生開始上課,教師開始講課。分別用new關(guān)鍵字,虛方法,抽象類實現(xiàn)多態(tài)性。
New關(guān)鍵字: using System;using System.Collections.Generic;using System.Text;
namespace third.three {
class Program
{
static void Main(string[] args)
金陵科技學(xué)院實驗報告
{
Student s=new Student(“學(xué)生”);
Teacher t=new Teacher(“教師”);
Console.WriteLine(s.name+s.work());
Console.WriteLine(t.name+t.work());
Console.ReadLine();
}
}
public class Person
{
public string name;
public interface method
{ string work();}
}
public class Student:Person
{
public Student(string name)
{ this.name = name;}
public string work()
{ return “早上8:30開始上課”;}
}
public class Teacher:Person
{
public Teacher(string name)
{ this.name = name;}
public string work()
{ return “開始講課”;}
} } 虛方法: using System;using System.Collections.Generic;using System.Text;
金陵科技學(xué)院實驗報告
namespace third.three.two {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”,“學(xué)生”);
PersonWork(s);
Teacher t=new Teacher(“李斯”,“教師”);
PersonWork(t);
}
private static void PersonWork(Person Person)
{ Console.WriteLine(Person.Work());}
}
public class Person
{
public string name;
public Person(string name)
{ this.name = name;}
public virtual string Work()
{ return string.Format(“Person{0}:早上8:30開始”,name);}
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{ this.type = type;}
public override string Work()
{
return string.Format(“Person{0}:早上8:30開始上課”, name);
}
}
public class Teacher : Person
金陵科技學(xué)院實驗報告
{
private string type;
public Teacher(string name, string type)
: base(name)
{ this.type = type;}
public override string Work()
{
return string.Format(“Person{0}:開始講課”, name);
}
} }
抽象類: using System;using System.Collections.Generic;using System.Text;
namespace third.three.three {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”, “學(xué)生”);
PersonWork(s);
Teacher t = new Teacher(“李斯”, “教師”);
PersonWork(t);
}
private static void PersonWork(Person person)
{
Console.WriteLine(person.Work());
}
}
public abstract class Person
金陵科技學(xué)院實驗報告
{
public string name;
public Person(string name)
{ this.name = name;}
public abstract string Work();
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(“Person{0}:早上8:30開始上課”, name);
}
}
public class Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(“Person{0}:開始講課”, name);
}
} }
金陵科技學(xué)院實驗報告
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
通過本次實驗,我理解了類的繼承性與多態(tài)性;掌握了虛方法的定義以及如何用虛方法來實現(xiàn)多態(tài);掌握了抽象類的定義以及如何用抽象方法來實現(xiàn)多態(tài)。這次實驗與前兩次不同,采用Windows應(yīng)用程序,既涉及到代碼段也涉及到界面的設(shè)計。所以,勉強(qiáng)通過實驗。
金陵科技學(xué)院實驗報告
實驗項目名稱: 接口、文件和流 實驗學(xué)時: 6 同組學(xué)生姓名: 實驗地點: A205 實驗日期: 12月7日-12月21日 實驗成績: 批改教師: 批改時間:
金陵科技學(xué)院實驗報告
實驗4 接口、文件和流
一、實驗?zāi)康?/p>
(1)掌握接口的定義及使用方法;
(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的讀寫操作類及其使用方法;
(4)掌握OpenFileDialog,SaveFileDialog等控件的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機(jī)調(diào)試過程和結(jié)果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據(jù)題目要求,新建項目;
3、編寫并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項目;
6、保存項目。
五、實驗內(nèi)容
1、定義一個Person類,包含姓名字段和一個方法,早上8:30學(xué)生開始上課,教師開始講課。用接口來實現(xiàn)。
using System;using System.Collections.Generic;using System.Text;namespace Test4_1 {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”,“學(xué)生”);
Console.WriteLine(s.Work());
Teacher t = new Teacher(“李四”,“老師”);
Console.WriteLine(t.Work());
}
金陵科技學(xué)院實驗報告
public abstract class Person
{
public string name;
public Person(string name)
{
this.name = name;
}
}
interface IPerson
{
string type { get;}
string Work();
}
public class Student :Person, IPerson
{
public string type
{
get { return string.Format(“老師”);}
}
public Student(string name, string type)
: base(name)
{
this.name=name;
}
public string Work()
{
return string.Format(“Person{0}:早上8:30開始上課”, name);
}
}
public class Teacher :Person, IPerson
{
public string type
{
金陵科技學(xué)院實驗報告
get { return string.Format(“學(xué)生”);}
}
public Teacher(string name, string type)
: base(name)
{
this.name = name;
}
public string Work()
{
return string.Format(“Person{0}:早上8:30開始講課”, name);
}
}
} }
2、聲明一個接口Iplayer,包含5個接口方法:播放,停止,暫停,上一首和下一首。在該程序中定義一個MP3播放器類和一個AVI播放器類,以實現(xiàn)該接口。
MP3類(包含Iplayer接口,AVI類): using System;using System.Collections.Generic;using System.Text;namespace Test4_2 {
interface IPlayer
{
string Play();
string Stop();
string Pause();
string Pre();
string Next();
}
public class MP3:IPlayer
{
public string Play()
金陵科技學(xué)院實驗報告
{
return “正在播放MP3歌曲!”;
}
public string Stop()
{
return “停止播放MP3歌曲!”;
}
public string Pause()
{
return “暫停播放MP3歌曲!”;
}
public string Pre()
{
return “播放上一首MP3歌曲!”;
}
public string Next()
{
return “播放下一首MP3歌曲!”;
}
}
public class AVI : IPlayer
{
public string Play()
{
return “正在播放AVI歌曲!”;
}
public string Stop()
{
return “停止播放AVI歌曲!”;
}
public string Pause()
{
return “暫停播放AVI歌曲!”;
}
金陵科技學(xué)院實驗報告
public string Pre()
{
return “播放上一首AVI歌曲!”;
}
public string Next()
{
return “播放下一首AVI歌曲!”;
}
} } Form窗體里代碼: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test4_2 {
public partial class Form1 : Form
{
IPlayer iplayer;
MP3 mp3;
AVI avi;
public Form1()
{
InitializeComponent();
}
private void btnMp3_Click(object sender, EventArgs e)
{
mp3 = new MP3();
iplayer =(IPlayer)mp3;
}
金陵科技學(xué)院實驗報告
private void btnPlay_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Play();
}
private void btnUp_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Pre();
}
private void btnStop_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Stop();
}
private void btnPause_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Pause();
}
private void btnNext_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Next();
}
private void btnAvi_Click(object sender, EventArgs e)
{
avi = new AVI();
iplayer =(IPlayer)avi;
}
} }
3、實現(xiàn)對文本文件的讀寫操作,用文件操作控件打開和保存文件。
using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace Test4_3
金陵科技學(xué)院實驗報告
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
StreamWriter
sw = StreamWriter(saveFileDialog1.FileName,true);
sw.WriteLine(DateTime.Now.ToString());
sw.WriteLine(txtSource.Text);
sw.Close();
}
private void btnShow_Click(object sender, EventArgs e)
{
StreamReader sr = StreamReader(saveFileDialog1.FileName);
txtShow.Text = sr.ReadToEnd();
sr.Close();
}
} }
4、實現(xiàn)對二進(jìn)制文件的讀寫操作。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;
new
new
金陵科技學(xué)院實驗報告
namespace Test4_4 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
FileStream
fs
= FileStream(@“d:Datastudent.dat”,FileMode.Append,FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Int32.Parse(txtStuNo.Text));
bw.Write(TxtName.Text);
bool isMale;
if(rdoMan.Checked)
isMale = true;
else
isMale = false;
bw.Write(isMale);
fs.Close();
bw.Close();
}
private void btnShow_Click(object sender, EventArgs e)
{
lstShow.Items.Clear();
lstShow.Items.Add(“學(xué)號t姓名t性別”);
FileStream
fs
= FileStream(@“d:Datastudent.dat”,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
fs.Position = 0;
while(fs.Position!= fs.Length)
{
new
new
金陵科技學(xué)院實驗報告
int s_no = br.ReadInt32();
string name = br.ReadString();
string sex = “";
if(br.ReadBoolean())
sex = ”男“;
else
sex = ”女“;
string
result String.Format(”{0}t{1}t{2}“,s_no,name,sex);
lstShow.Items.Add(result);
}
br.Close();
fs.Close();
}
} }
5、實現(xiàn)對象序列化和反序化。
Student類: using System;using System.Collections.Generic;using System.Text;namespace Test4_5 {
[Serializable]
public class Student
{
public int sno;
public string name;
public bool sex;
public Student(int s_no, string name, bool isMale)
{
this.sno = s_no;
this.name = name;
this.sex = isMale;
=
金陵科技學(xué)院實驗報告
}
} } StudentList類: using System;using System.Collections.Generic;using System.Text;namespace Test4_5 {
[Serializable]
public class StudentList
{
private Student[] list = new Student[100];
public Student this[int index]
{
get
{
if(index < 0 || index >= 100)
return list[0];
else
return list[index];
}
set
{
if(!(index < 0 || index >=100))
list[index] = value;
}
}
} } Form下的代碼: using System;using System.Collections.Generic;using System.ComponentModel;
金陵科技學(xué)院實驗報告
using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace Test4_5 {
public partial class Test9_32 : Form
{
private StudentList list = new StudentList();
private int i = 0;
public Test9_32()
{
InitializeComponent();
}
private void Test9_32_Load(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
string file = @”d:datastudent.dat";
Stream
stream
= FileStream(file,FileMode.OpenOrCreate,FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream,list);
stream.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
int sno = Int32.Parse(txtStuNo.Text);
bool isMale;
if(rdoMan.Checked)
isMale = true;
new
第三篇:c#基礎(chǔ)總結(jié)
【1】面向?qū)ο蟪绦蛟O(shè)計語言的三大特點是什么?答:【23】Main函數(shù)特點?答:1)Main方法是C#程序的限定,默認(rèn)的為private【2】三種命名法則的特點?答:匈牙利命名法:在變限定符,返回類型為void或int類型,Main方法必須是靜態(tài)方法;3)一個類或結(jié)構(gòu)只能有一個有效的入駝表示法:一個標(biāo)示符用若干個有意義的英語單詞或口點函數(shù);4)main方法必須定義在某一個類中。縮寫組成,第一個單詞首字母小寫,后邊的首字母大【24】多態(tài)(重寫、隱藏)
寫;Pascal表示法:與駱駝表示法類似,但是第一個 單詞的首字母也需要大寫?!?】C#注釋的三種形式特點?答1)單行注釋:// 2)class A /// {public virtual void F()【4】引用類型和值類型的數(shù)據(jù)類型? {Console.WriteLine(“A.F”);}} abstract class B:A(1)int valOne = 0;{public abstract override void F();}int valTwo = valOne;答:abstract override 是不可以一起修飾 int valThree = 333;例:在下面的例子里 valTwo = 333;TestValueRefRef1 = new TestValueRef();class A TestValueRefRef2 = Ref1;{public A(){PrintFields();} Ref2.value = 444;public virtual void PrintFields(){} } Console.WriteLine(“values:{0},{1}”, Ref1.value, class B:A Ref2.value);{int x=1;int y;public B(){y=-1;} Console.WriteLine(“values:{0}, {1},{2}”,valOne, public override void valTwo,valThree);PrintFields(){Console.WriteLine(“x={0},y={1}”,答:輸出結(jié)果:values:444,444 x,y);} 當(dāng)使用new B()創(chuàng)建B的實例時,產(chǎn)生什么輸出?(2)public class EnumTest答:x=1,y=0 { enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};分析:執(zhí)行new B()時,由于B繼承自A,所以會調(diào)用static void Main()A的構(gòu)造函數(shù),并執(zhí)行其中的PrintFields()方法,由{int x=(int)Days.Sun;inty=(int)Days.Fri;于該方法為虛方法,在繼承類中被重寫,所以,將執(zhí)Console.WriteLine(“Sun = {0}”, x);行B中的PrintFields()方法。此時,將在DOS界面Console.WriteLine(“Fri = {0}”, y);}} 上輸出“x=1,y=0”,然后,在運(yùn)行B的構(gòu)造函數(shù)中的答:輸出結(jié)果:Sun = 2Fri = 7 y=-1。(建議同學(xué)們將此程序放入到代碼中,設(shè)置斷點【5】枚舉類型的字段和關(guān)聯(lián)值?枚舉類型有三個要看執(zhí)行過程。)
【25】什么是多態(tài)對象?答:用基類類名聲明,但是特性 修飾符 enum 枚舉名 : 基礎(chǔ)類型 {枚舉成員聲明,枚舉成員聲明,??,枚舉成員聲明}默認(rèn)的基礎(chǔ)函數(shù)來實例化的對象。這類對象的主要用途是引發(fā)多類型為int;關(guān)聯(lián)值:如果沒有被聲明,默認(rèn)為0。態(tài),為了將它們和一般的對象(聲明和創(chuàng)建都使用同【6】強(qiáng)制類型轉(zhuǎn)換(例:若有double f=2.7;int 一個類型名的對象)加以區(qū)別、揭示它們的特點和用2)將源類型的對象途,這種形態(tài)的對象稱為多態(tài)對象。轉(zhuǎn)換成為目的類型的對象 【26】接口的特點。答:接口只能包含抽象方法,不【7】運(yùn)算符&和&&的區(qū)別?答:條件“與”運(yùn)算符(&&)沒有訪問修飾符,接口成員必須是方法屬性事件或者時才計算第二個操作數(shù)。而&需要計算所有操作數(shù),索引器不能包含常數(shù)字段運(yùn)算符也不能有靜態(tài)成員。并且優(yōu)先級高于&& 【27】委托和事件,【8】裝箱和拆箱的概念?答:裝箱就是把一個值類型委托的定義修飾符 delegate 返回類型 委托類型名(參數(shù)列表); 【9】for循環(huán)和if語句聯(lián)合使用的程序分析,for(;;)eg: public delegate int DelegateClass(stringinfo);
和continue的區(qū)別?答:break跳出循委托的創(chuàng)建(實例化)委托對象 = new 委托名(關(guān)聯(lián)方法); 【11】命名空間的特點答:它提供一種命名機(jī)制,是eg: DelegateClass obj=new DelegateClass(MethodA);
合方式無關(guān),不能表明源文件的存取方式,命名空間DelegateClass obj=MethodA;//隱式創(chuàng)建和初是按層次組織的。始化(不用new)【12】數(shù)組元素的的復(fù)制和讀值 例:分析下列語句: int[3]{5,6,2},new int[5]{6,9,7,8,3},new Hello(string target);} int[2]{3,2}};myArray3[2][2]的值是(D)A)9;B)2;該語句的作用是:在TestCS 命名空間中定義了了一C)6;D)越界 個名為Hello 的委托類型;
【13】類和對象的關(guān)系?答:類是對象的抽象,對象【28】Windows窗體中Button按鈕觸發(fā)的事件是什【14】關(guān)鍵字this和base的區(qū)別?答:base指代基【29】Windows窗體中控件的標(biāo)識符如何修改?答:【15】關(guān)鍵字new、using的多種用法?答:new修飾【30】如何修改Windows窗體的啟動窗體?答:修改被重寫,但new修飾符可終止這一特性;向下傳播; 實例化一個對象。Using:導(dǎo)入命名空間;自動釋放【31】要使用SQL Server需要使用哪兩個命名空間? Using代碼框里的資源?!?6】索引器的特點?答:索引器允許重載;字符串Using System.Date.SqlClient: 【32】什么是DataSet、DataAdapter?兩者聯(lián)系?答:過簽名標(biāo)識;通過元素訪問來訪問;必須為實例成員;索引器的get訪問器具有與索引器相同的形參表;除DataAdapter:數(shù)據(jù)適配器,數(shù)據(jù)庫與DataSet間的橋value參數(shù)外,索引器的set訪問器還具有與索引器梁,把數(shù)據(jù)庫中數(shù)據(jù)下載到DataSet或回傳回去。相同的形參表。【33】用戶登錄和密碼修改(帶數(shù)據(jù)庫)【17】靜態(tài)數(shù)據(jù)成員特點?答:為所有類所共享,區(qū)用戶登錄 【18】構(gòu)造函數(shù)的特點?答:(1)構(gòu)造函數(shù)名與類名UserName='“ + txtUsername.Text.Trim().ToLower()+ ”' and UserPwd='“ + txtPassword.Text.Trim()+ 【19】析構(gòu)函數(shù)的特點?答:(1)析構(gòu)函數(shù)名是在類”'“;if(OperateDB.ExecuteReader(sql))型(默認(rèn)為空)和修飾符;(3)析構(gòu)函數(shù)不能被重載。{username = txtUsername.Text.Trim().ToLower();【20】什么是方法的重載?重載的特點是什么?答: frmMain frm = new frmMain();frm.Show();this.Hide();} 定義一組方法。重載的特點:1)位于同一類中;2)else
方法名相同;3)方法參考列表不同,包括參數(shù)個數(shù)不{MessageBox.Show(”用戶名或密碼錯誤“, ”出錯了“, 同和參數(shù)類型不同;4)與方法返回值和修飾符沒關(guān)系。MessageBoxButtons.OK, MessageBoxIcon.Error);} 【21】虛函數(shù)的特點?答:1)虛方法前不允許有修改密碼: 修飾符;2)虛方法不能是私有的,因此不能使用private修飾符; where UserName='” + frmLogin.username + “' and 【22】抽象類和抽象方法的主要特點?答:抽象類:UserPwd='” + txtOldPwd.Text.Trim()+ “'”;(或者if(OperateDB.ExecuteReader(sqlCheckPwd))說,不能產(chǎn)生對象。但是,它可以有構(gòu)造函數(shù)。(2){string sql = “update UserInfo set UserPwd='” 設(shè)計abstract類的目的是為了被繼承。抽象方法:是+ txtNewPwd.Text.Trim()+ “' where UserName='” + 不完整的,不能執(zhí)行的。frmLogin.username + “'”;
if(OperateDB.ExecuteNonQuery(sql)== 1)
{MessageBox.Show(“密碼修改成功!”);}else
{ MessageBox.Show(“密碼修改失敗!”);}}
else{MessageBox.Show(“舊密碼不正確!”);}
【34】抽象類定義和繼承使用
特點:1.沒有被完整定義,因而它不能用來實例化,或者說,不能產(chǎn)生對象。(但是,它可以有構(gòu)造函數(shù)。)2.設(shè)計abstract類的目的是為了被繼承。public abstract class Employee{public virtual void Pay(){ }
public abstract void CalculatePay();} public class HourlyEmployee: Employee
{public override void Pay(){CalculatePay();}public override void CalculatePay(){ }} 【35】接口及繼承類的使用
特定功能的抽象成員的集合。一個類可以繼承多個接口,從而獲得多個行為的描述,將它們組合成新的功能并在類中實現(xiàn)。繼承類中必須實現(xiàn)接口中的所有抽象成員。
定義接口的格式:修飾符 interface 接口名:基接口列表 {接口體} 其中,接口體的聲明可以包括:接口方法聲明;接口屬性聲明;接口事件聲明;接口索引器聲明
public delegate void
StringListEvent(IStringList sender);public interface IStringList{ void Add(string s);//方法int Count{get;}//屬性event StringListEvent Changed;//事件string this[int index]{get;set;}//索引器} 【編程題例題】
定義一MobilePhone類,包括屬性成員——網(wǎng)絡(luò)類型(NetworkType),字段成員——屏幕尺寸(screenSize)、手機(jī)品牌(brand),手機(jī)型號
(brandModel),公共方法成員——Open、Close。其中screenSize為單位是英寸的雙精度數(shù),brand為字符串,NetworkType只能是“GSM”或“CDMA”字符串。要求:(1)在此類中包含構(gòu)造函數(shù),構(gòu)造函數(shù)用于對數(shù)據(jù)(屏幕尺寸、手機(jī)品牌和手機(jī)型號)進(jìn)行初始化。(2)公共成員方法中輸出相應(yīng)提示信息(參見(3)中的輸出結(jié)果格式)。(3)寫一測試類,在類中實例化一MobilePhone對象,最后能在DOS界面下顯示如下結(jié)果:諾基亞N81(屏幕尺寸2.0英寸),是一款GSM手機(jī)。手機(jī)關(guān)機(jī)了。using System;
public enum NetworkType {GSM,CDMA,}
public class MobilePhone {public double screenSize;public string brand;
public string brandModel;
public NetworkType networkType;public NetworkType NetworkType{get { return networkType;}}
public MobilePhone(double ss, string bra, string bm, NetworkType nt){screenSize = ss;brand = bra;brandModel = bm;networkType = nt;}public void Open()
{Console.WriteLine(“{0}{1}(屏幕尺寸{2}英寸),是一款{3}手機(jī).”,brand,brandModel,screenSize.ToString(“.0”), networkType);}
public void Close()
{Console.WriteLine(“手機(jī)關(guān)機(jī)了。”);} }
public class Test
{public static void Main()
{MobilePhone mp = new MobilePhone(2.0, “諾基亞”, “N81”, NetworkType.GSM);mp.Open();mp.Close();
System.Console.ReadLine();} }
【例】寫一名為Desk的類,包含兩個字段Length(雙精度類型)、Height(雙精度類型)。再寫一繼承類ComputerDesk類。ComputerDesk類除了有Length和Height外,還有KeyboardTray(字符串類型)。Public class desk {double length;double height;}
Public class computerdesk:desk {string keyboardtray}
第四篇:計算器編程設(shè)計心得體會
計算器編程設(shè)計心得體會
——
本次有關(guān)計算器程序的編寫,個人感覺還是有一定難度的。在考察運(yùn)算符的重載時,其他的運(yùn)算符還好,但是在定義“()”運(yùn)算符時在邏輯上考慮的比較復(fù)雜,因為括號運(yùn)算符內(nèi)的計算優(yōu)先進(jìn)行,所以要考慮的有括號內(nèi)的各種“+”、“-”、“*”、“/”的組合使用,還有括號里含括號的情況。括號都是成對存在的,首先要在運(yùn)算式中找到最里面的一對括號(即:此括號內(nèi)不再含有其他的括號)。之前的想法是用指針按次找到第一個右括號,然后再找出右括號左邊的第一個左括號,計算出這兩個半括號之間的公式,用t表示并替代。同理,再尋找出替換后的最里面的一對括號,計算出這兩個半括號之間的公式,用t表示并替代。以此類推,使用for循環(huán)語句,直到找不到括號為止,return t;其他的方面,遇到的難點有:不知道怎么判斷輸入的數(shù)學(xué)公式不符合規(guī)定,除了分母不能為零比較好考慮,其他的形式總覺得會有疏漏。例如在判斷“/”的右操作數(shù)不為零時則繼續(xù)進(jìn)行,反之則跳出,并給get賦值為1。只有當(dāng)get為0時,才能正常輸出。當(dāng) set為1時輸出 “n您輸入的不匹配,有錯誤發(fā)生。Result lost!” ;如果set為2,則輸出 “n您輸入了非法字符 , 請重新輸入,謝謝合作!”;如果set值為3則輸出“nStack is full, Lost result!”若是set 等于4則輸出“nERROR!Divide by 0!”。但是在判斷2、3、4情況時感覺不是很好描述編寫。
第五篇:軟件技術(shù)基礎(chǔ)實驗報告
《軟件開發(fā)技術(shù)基礎(chǔ)》
實驗報告
學(xué)院:XX學(xué)院
班級:XX
姓名: XX
學(xué)號:XX
《軟件開發(fā)技術(shù)基礎(chǔ)》實驗報告
實驗名稱:實驗一 順序表的操作
班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績
一、實驗?zāi)康模?/p>
1、掌握順序表結(jié)構(gòu)的實現(xiàn)方式;
2、掌握順序表常用算法的實現(xiàn);
3、熟悉利用順序表解決問題的一般思路;
4、參照給定的順序表的程序樣例,驗證給出的順序表的常見算法,領(lǐng)會順序表結(jié)構(gòu)的優(yōu)點和不足。
二、實驗要求:
1、掌握順序表的特點及常見算法。
2、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。
三、實驗內(nèi)容:
1、設(shè)計一個靜態(tài)數(shù)組存儲結(jié)構(gòu)的順序表,要求編程實現(xiàn)如下任務(wù):
(1)建立一個順序表,首先依次輸人整數(shù)數(shù)據(jù)元素(個數(shù)根據(jù)需要鍵盤給定)。(2)刪除指定位置的數(shù)據(jù)元素(指定元素位置通過鍵盤輸入),再依次顯示刪除后的順序表中的數(shù)據(jù)元素。
(3)查找指定數(shù)據(jù)的數(shù)據(jù)元素(指定數(shù)據(jù)由鍵盤輸入),若找到則顯示位置,若沒有找到則顯示0。
2、使用順序表實現(xiàn)一個電話本的管理程序,電話本中的每條記錄包括學(xué)號、姓名、手機(jī)號碼和固定電話四項。要求實現(xiàn)菜單、初始化、添加、刪除和顯示等功能。
四、程序要求:
1、采用順序表實現(xiàn),假設(shè)該順序表的數(shù)據(jù)元素個數(shù)在最壞情況下不會超過50個。
2、寫出完整的程序并能調(diào)試運(yùn)行。
五、實驗結(jié)果:
1、順序表的結(jié)果:
2、電話簿的結(jié)果:
六、實驗中遇到的問題及解決方法:
1.在刪除數(shù)據(jù)的時候如果有兩個一樣的數(shù)時要怎樣解決? 解決方法:用while進(jìn)行判斷。
2.在刪除操作中,刪除函數(shù)中的l是指針,所以用->指向,而在主函數(shù)中l(wèi)是結(jié)構(gòu)體,用“.”。3.在查找的時候有一個返回值,而這個返回值是指針,所以在寫查找函數(shù)的時候要把返回值類型寫上。
七、實驗心得體會:
一開始不知所措,首先應(yīng)該有一個大的方向,把主程序編號,再逐步求精,落實到每一個函數(shù)的編寫。對于c語言要熟練掌握,要熟悉循環(huán)等得操作,要熟練掌握順序表中的插入,刪除,查找,的基本函數(shù)。在此的基礎(chǔ)上靈活使用。附:
1、順序表的程序: #include
struct Seqlist {
};Seqlist *init(){
} void insert(Seqlist *p){
int num=0;printf(“請輸入要鍵入的個數(shù):”);scanf(“%d”,&num);if(num<1)printf(“鍵入的數(shù)據(jù)個數(shù)錯誤!n”);Seqlist *p;p=(Seqlist *)malloc(sizeof(Seqlist));p->length=0;return p;int data[MAXSIZE];int length;else {
printf(“鍵入數(shù)據(jù)為:n”);for(;num>0;num--){ p->length++;scanf(“%d”,p->data+p->length-1);
}
}
} if(p->length==MAXSIZE){
} printf(“數(shù)組已滿n”);break;getchar();void deletee(Seqlist *p){
} int find(Seqlist *p){ int i=0;printf(“請輸入要刪除元素的位置:”);scanf(“%d”,&i);if(i<1||i>p->length)printf(“表中沒有第%d個元素!n”,i);else {
} getchar();for(int j=i;j<=p->length-1;j++)p->data[j-1]=p->data[j];p->length--;
} int x;printf(“請輸入要查找的數(shù)據(jù):”);scanf(“%d”,&x);for(int i=0;i
length;i++){
} if(i>=p->length)printf(“數(shù)組中沒有此數(shù)據(jù)!n”);if(p->data[i]==x){
} printf(“此數(shù)據(jù)位于第%d個位置n”,i+1);return i+1;getchar();return 0;void display(Seqlist *p){
} int main(void){ Seqlist *p;for(int i=0;i
length;i++)printf(“%-5d”,p->data[i]);putchar('n');getchar();
p=init();char flag;printf(“1-插入 2-刪除 3-查找 4-顯示 0-退出n”);while(1){
printf(“請輸入操作:”);switch(flag=getchar()){ case '1':insert(p);putchar('n');break;case '2':deletee(p);printf(“刪除后數(shù)據(jù)為:”);display(p);putchar('n');getchar();break;case '3':find(p);putchar('n');getchar();break;case '4':printf(“顯示數(shù)據(jù)為:”);display(p);putchar('n');break;case '0':free(p);return 0;}
}
}
2、電話簿的程序: #include
string name;string phonenumber;string phone;contact *next;}CNT;class Phonebook { public:
Phonebook();CNT *Input();CNT *Turn_to_end();void Add();void Delete();void Showmenu();CNT *Find();void Show_item(CNT *p);void Display();void Modification();private: };
Phonebook::Phonebook(){ head=new CNT;CNT *head;
} head->next=NULL;void Phonebook::Show_item(CNT *p){ cout<<“|----姓名----|--電話號碼--|-------------地址------------|”< } void Phonebook::Display(){ CNT *p;cout<<“|----姓名----|--電話號碼--|-------------地址------------|”< } cout<<“|------------------------|”< CNT *Phonebook::Input(){ } CNT *Phonebook::Turn_to_end(){ } CNT *temp=head;while(temp->next!=NULL){ } cout<<“n”;return temp;temp=temp->next;CNT *temp;temp=new CNT;cout<<“請輸入姓名:”;cin>>temp->name;cout<<“請輸入手機(jī)號碼:”;cin>>temp->phonenumber;cout<<“請輸入固定電話:”;cin>>temp->phone;temp->next=NULL;cout<<“n”;return temp; void Phonebook::Add(){ } void Phonebook::Delete(){ CNT *p,*q;q=p=head;string data;cout<<“請輸入要刪除聯(lián)系人的信息:”;cin>>data;for(p=p->next;p!=NULL&&p->name!=data&&p->phonenumber!=data&&p->phone!=data;CNT *temp,*p;temp=Input();p=Turn_to_end();p->next=temp;cout<<“n”;p=p->next) q=p;if(p!=NULL){ q->next=p->next;delete p; } } else cout<<“該信息不存在”< } CNT *Phonebook::Find(){ CNT *p=head;string data;cout<<“請輸入要查找或要修改的聯(lián)系人的信息:”;cin>>data;for(p=p->next;p!=NULL&&p->name!=data&&p->phonenumber!=data&&p->phone!=data;cout<<“ 電話簿”< if(p!=NULL)Show_item(p);else } cout<<“不存在該信息”< } int main(){ int select;Phonebook phbook;CNT *p;p=Find();if(p!=NULL){ } cout<<“n”;cout<<“請輸入姓名:”;cin>>p->name;cout<<“請輸入手機(jī)號碼:”;cin>>p->phonenumber;cout<<“請輸入固定電話:”;cin>>p->phone; phbook.Showmenu();while(1){ cout<<“n請輸入要選擇的操作(0~6):”;cin>>select;getchar();switch(select){ case 0: exit(0);break;case 1: phbook.Add();break;case 2: phbook.Display();break;case 3: phbook.Delete();break;case 4: phbook.Find();break;case 5: phbook.Modification();break; } } } return 0; 實驗名稱:實驗二 鏈表的操作 (一)班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 1、掌握單鏈表結(jié)構(gòu)的實現(xiàn)方式; 2、掌握單鏈表常用算法的實現(xiàn)。 二、實驗要求: 1、掌握鏈表的特點及常見算法。 2、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1、設(shè)計一個鏈表,要求編程實現(xiàn)如下任務(wù): (1)建立一個鏈表,首先依次輸人整數(shù)數(shù)據(jù)元素(個數(shù)根據(jù)需要鍵盤給定)。(2)刪除指定值的結(jié)點(指定值通過鍵盤輸入),再依次顯示刪除后的鏈表中的數(shù)據(jù)元素。 (3)查找指定值的結(jié)點(指定數(shù)據(jù)由鍵盤輸入),若找到則顯示查找成功,若沒有找到則顯示查找失敗。 (4)在第i個節(jié)點(i由鍵盤輸入,i=0表示插入的結(jié)點作為第1個結(jié)點)之后插入一個元素為x的節(jié)點。 四、程序要求: 1、采用鏈表實現(xiàn),假設(shè)該鏈表的結(jié)點數(shù)在最壞情況下不會超過40個。 2、寫出完整的程序并能調(diào)試運(yùn)行。 五、實驗結(jié)果: 六、實驗中遇到的問題及解決方法: 問題:在查找數(shù)據(jù)時顯示的是一樣的? 解決方法:在查找函數(shù)前要標(biāo)明返回值的類型lnode *find。 七、實驗心得體會: 首先要把書上的關(guān)于單鏈表的定義理解,在這個的基礎(chǔ)上使用它。單鏈表通過結(jié)點在數(shù)據(jù)后附加后繼元素的指針信息,可以更靈活的添加,刪除,查找,插入,元素。要清楚指針的指向。首先要定義一個頭結(jié)點,并且指針域要為空。將各個操作分別編寫函數(shù),有利于分塊使用而且可以多次使用。附: #include { cout<<“請輸入數(shù)組的個數(shù)(小于50):”;cin>>leng;cout<<“請輸入數(shù)據(jù):”;for(int i=0;i } for(int j=i;j { if(a[i]==temp){ x=i+1;cout<<“你要查找的數(shù)是數(shù)組的第”< } } if(x==0)cout<<“數(shù)組中沒有你要查找的數(shù)”;} 實驗名稱:實驗三 鏈表的操作 (二)班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 1、熟悉利用線性鏈表解決問題的一般思路; 2、參照給定的鏈表的程序樣例,驗證給出的鏈表的常見算法,了解單鏈表結(jié)構(gòu)的優(yōu)點和不足。 二、實驗要求: 1、熟練掌握鏈表的使用,并能運(yùn)用其解決些常規(guī)問題。 2、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1、使用鏈表實現(xiàn)一個電話本的管理程序,電話本中的每條記錄包括姓名和電話兩項。要求實現(xiàn)菜單、初始化、添加、刪除和顯示等功能。 四、程序要求: 1、采用鏈表實現(xiàn),假設(shè)該鏈表的結(jié)點數(shù)在最壞情況下不會超過40個。 2、寫出完整的程序并能調(diào)試運(yùn)行。 五、實驗結(jié)果: 六、實驗中遇到的問題及解決方法: 1.實驗結(jié)果版面不好看 解決方法:反復(fù)調(diào)試執(zhí)行,每次都對自己不滿意的地方進(jìn)行一些改正。2.怎樣比較name與s->name? 解決方法:用string.h中的strcmp函數(shù)。3.在傳遞數(shù)據(jù)時有問題? 解決方法:返回的值要注意數(shù)據(jù)類型,是指針還是整數(shù)等。 七、實驗心得體會: 在應(yīng)用中可以利用while實現(xiàn)選擇,并根據(jù)選擇用switch來完成選擇。為了簡化編程,結(jié)點插入操作可僅僅在頭部進(jìn)行。單鏈表的方法使添加操作更加簡便。不需要事先分配結(jié)點空間,而是在需要時動態(tài)的申請,節(jié)省了空間。、附: #include void show_menu(){ puts(“1添加 2刪除 3查找 4插入 5顯示 0退出”);} void show_item(STU *p){ printf(“|編號| 學(xué)號 | 姓名 | 電話號碼 |n”);printf(“|1 |%-12s|%-12s|%-12s|n”,p->stu_number,p->stu_name,p->stu_phone); } STU *init(){ STU *p;p=(STU *)malloc(sizeof(STU));p->next=NULL;return p;} void display(STU *head){ int i;STU *p;p=head->next;printf(“|編號| 學(xué)號 | 姓名 | 電話號碼 |n”);for(i=1;p!=NULL;i++,p=p->next){ printf(“|%-4d|%-12s|%-12s|%-12s|n”,i,p->stu_number,p->stu_name,p->stu_phone);} } STU *turn_to_end(STU *head){ STU *p;for(p=head;p->next!=NULL;p=p->next);return p;} STU *putin(){ STU *temp;temp=(STU *)malloc(sizeof(STU));printf(“請輸入姓名:”);gets(temp->stu_name);printf(“請輸入學(xué)號:”);gets(temp->stu_number);printf(“請輸入電話號碼:”);gets(temp->stu_phone);temp->next=NULL;return temp;} void add(STU *head){ STU *temp,*p;p=head;temp=putin();p=turn_to_end(head);p->next=temp;} STU *find(STU *head,int i){ STU *p;if(i<1) puts(“輸入錯誤”);else { for(p=head;p->next!=NULL&&i>0;i--,p=p->next); if(i>0) { puts(“不存在該結(jié)點”); return NULL; } } return p;} STU *find_stu(STU *head,char *data){ STU *p,*q;q=p=head;for(p=head->next;p!=NULL&&strcmp(p->stu_name,data)!=0&&strcmp(p->stu_number,data)!=0&&strcmp(p->stu_phone,data)!=0;p=p->next) q=p;if(p!=NULL) return q;else { puts(“不存在該信息”); return p;} } void delet(STU *head,char *data){ STU *p,*q;p=q=head;q=find_stu(head,data);if(q!=NULL){ p=q->next; q->next=p->next; free(p);} } void insert(STU *head,int i) { STU *p,*q,*temp;q=p=head;if(i<0) puts(“輸入錯誤”);else if(i==0){ temp=putin(); temp->next=p->next; q->next=temp; } else if(find(head,i)!=NULL){ q=find(head,i); p=q->next; temp=putin(); q->next=temp; temp->next=p;} else return;} int main(void){ STU *head,*temp;int selct,node;char data[12];head=init();show_menu();while(1){ printf(“請選擇操作:”); scanf(“%d”,&selct); getchar(); switch(selct) { case 1: add(head); putchar('n'); break; case 2: display(head); printf(“請輸入要刪除學(xué)生的學(xué)號、姓名或電話號碼:”); } scanf(“%s”,data); getchar(); delet(head,data); display(head); putchar('n'); break;case 3: puts(“請輸入要查找的數(shù)據(jù)”); scanf(“%s”,data); getchar(); if(temp=find_stu(head,data)) { show_item(temp->next); } else puts(“ 不存在該信息”); putchar('n'); break;case 4: puts(“請輸入要插入結(jié)點的位置”); scanf(“%d”,&node); getchar(); insert(head,node); putchar('n'); break;case 5: display(head); putchar('n'); break;case 0: exit(0); break;} } return 0; 實驗名稱:實驗四 棧的操作 班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 掌握棧的的定義和運(yùn)算,了解棧的應(yīng)用。 二、實驗要求: 1、掌握棧的特點及常見算法。 2、參照給定的棧的程序樣例,驗證給出的棧的常見算法。 3、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1、堆棧的測試和應(yīng)用。要求: 設(shè)計一個主函數(shù)實現(xiàn)對順序堆棧代碼進(jìn)行測試。測試方法為:依次把數(shù)據(jù)元素1,3,5,7,9入棧,然后出棧堆棧中的數(shù)據(jù)元素并在屏幕上顯示。 四、程序要求: 1、棧的長度自行確定。 2、重點理解棧的算法思想,能夠根據(jù)實際情況選擇合適的存儲結(jié)構(gòu)。 3、寫出完整的程序并能調(diào)試通過。 五、實驗結(jié)果: 六、實驗中遇到的問題及解決方法: 七、實驗心得體會: 棧是后進(jìn)先出表,只允許在棧頂進(jìn)行插入和刪除。要掌握順序棧初始化,出棧,入棧的基本程序,在理解得基礎(chǔ)上加以運(yùn)用。要清楚棧和單鏈表的區(qū)別。附: #include void Sqstack::push(){ int temp;if(top>=maxsize-1){ cout<<“堆棧已滿”< cout<<“請輸入要進(jìn)棧數(shù)的值”< cin>>temp; top++; *(data+top)=temp;} } void Sqstack::pop(int &e){ if(top>-1){ e=*(data+top); top--; cout< cout<<“堆棧已空”;} cout< i=top; for(;i>=0;i--) cout<<*(data+i)<<“ ”;} else cout<<“空?!?cout< void Sqstack::modify(){ int temp;cout<<“請輸入棧的長度”< maxsize=temp;else cout<<“輸入錯誤棧的長度必須>0”< int main(){ Sqstack test;int temp,select;cout<<“1.進(jìn)棧 2.出棧 3.顯示 4修改棧長度 0.退出”< cout<<“請選擇操作:”; cin>>select; switch(select) { case 1: test.push(); break; case 2: test.pop(temp); break; case 3: test.display(); break; case 4: test.modify(); break; case 0: exit(0); break; default: cout<<“輸入錯誤按任意鍵重新輸入”< getch(); break; } } return 0;} 實驗名稱:實驗五 隊列的操作 班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 掌握隊列的定義及其運(yùn)算,了解隊列的應(yīng)用。 二、實驗要求: 1、掌握隊列的特點及常見算法。 2、參照給定的隊列的程序樣例,驗證給出的隊列的常見算法。 3、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1、隊列測試和應(yīng)用。要求: 設(shè)計一個主函數(shù)對循環(huán)隊列代碼進(jìn)行測試。測試方法為:依次把數(shù)據(jù)元素2,4,6,8,10入隊,然后出隊中的數(shù)據(jù)元素并在屏幕上顯示。 四、程序要求: 1、隊列的長度由自行確定。 2、重點理解隊列的算法思想,能夠根據(jù)實際情況選擇合適的存儲結(jié)構(gòu)。 3、寫出完整的程序并能調(diào)試通過。 五、實驗結(jié)果 六、實驗中遇到的問題及解決方法: 七、實驗心得體會: 隊列是只允許在一端進(jìn)行刪除操作的線性表。在操作時會有假溢出的情況出現(xiàn),可以將存放隊列元素的數(shù)組守衛(wèi)相接,形成循環(huán)隊列。即:rear=(rear+1)%m;Front=(front+1)%m 附: #include typedef struct Temp { int data; struct Temp *next;}T;class Linksqueue { private: T *front,*rear;public: Linksqueue();void Enqueue();void Dequeue(T *);void display();}; Linksqueue::Linksqueue(){ front=rear=new(T);front->next=NULL;} void Linksqueue::Enqueue(){ T *temp;temp=new(T);cout<<“請輸入入隊元素的值:”< void Linksqueue::Dequeue(T *s){ T *temp;if(front->next!=NULL){ temp=front; front=front->next; *s=*front; cout<<“出隊元素的值:n”< delete temp;} else cout<<“隊列已空”< void Linksqueue::display(){ T *temp;if(front==rear) cout<<“空隊”< cout<<“隊列所有元素為 front-> ”; for(temp=front->next;temp->next!=NULL;temp=temp->next) { cout< } cout< cout<<“->rear”< int main(){ Linksqueue temp;T * a;a=new(T);int select;cout<<“1入隊 2出隊 3顯示 0退出”< cout<<“請選擇操作:”; cin>>select; switch(select) { case 1: temp.Enqueue(); break; case 2: temp.Dequeue(a); break; case 3: temp.display(); break; case 0: } exit(0); break;default: cout<<“輸入錯誤按任意鍵重新輸入”< getch(); break;} } return 0; 實驗名稱:實驗六 二叉樹的生成與遍歷 班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 1、熟悉二叉樹節(jié)點的定義和生成方式; 2、熟悉二叉樹鏈?zhǔn)浇Y(jié)構(gòu)的生成方式; 3、掌握二叉樹遍歷算法的實現(xiàn)。 二、實驗要求: 1、掌握二叉樹的常見算法。 2、參照給定的二叉樹的程序樣例,驗證給出的有關(guān)二叉樹的常見算法,并實現(xiàn)有關(guān)的操作。 3、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1.設(shè)計實現(xiàn)二叉樹的建立及遍歷算法,要求: (1)編寫創(chuàng)建二叉鏈?zhǔn)酱鎯Y(jié)構(gòu)的二叉樹程序并輸出。(2)編寫遞歸實現(xiàn)二叉樹的先序、中序、后序遍歷算法。 (3)編寫主函數(shù)測試以上二叉樹的創(chuàng)建和遍歷函數(shù)。2.假設(shè)二叉樹采用鏈?zhǔn)酱鎯Y(jié)構(gòu)進(jìn)行存儲,編寫程序?qū)崿F(xiàn)二叉樹的所有葉子結(jié)點的統(tǒng)計并輸出統(tǒng)計個數(shù)。 四、實驗結(jié)果: 五、實驗中遇到的問題及解決方法: 調(diào)試的時候顯示沒有錯誤但是程序運(yùn)行不出來? 在if語句中的相等要用“==”。要注意?。?/p> 六、實驗心得體會: 二叉樹的遍歷有先序,中序,后序遍歷。這三種遍歷都是用遞歸的形式實現(xiàn)的。而二叉樹的插入結(jié)點的算法與許多因素有關(guān),我們要在理解二叉樹結(jié)構(gòu)的基礎(chǔ)上根據(jù)需要來編寫程序。我在實驗中使用的二叉排序樹可以作為靜態(tài)查找表使用也可以作為動態(tài)查找表。附: #include front=1;rear=0;printf(“請輸入二叉樹的各節(jié)點,@表示虛節(jié)點,#表示節(jié)點:n”);scanf(“%c”,&ch);while(ch!='#'){ putchar(ch); s=NULL; if(ch!='@') { s=(bitree *)malloc(sizeof(bitree));s->data=ch; s->lchild=NULL; s->rchild=NULL; } rear++; Q[rear]=s; if(rear==1)root=s; else {if(s&&Q[front]) if(rear%2==0) Q[front]->lchild=s; else Q[front]->rchild=s; if(rear%2==1) front++; }scanf(“%c”,&ch);} return root;} void preorder(bitree *p){ if(p!=NULL){ printf(“%c”,p->data); preorder(p->lchild); preorder(p->rchild);} return;} void inorder(bitree *p){ if(p!=NULL){ inorder(p->lchild); printf(“%c”,p->data); inorder(p->rchild);} return;} void postorder(bitree *p){ if(p!=NULL){ postorder(p->lchild); postorder(p->rchild); printf(“%c”,p->data);} return;} int yzjd(bitree *t){ if(t==NULL)return(0);if(t->lchild==NULL&&t->rchild==NULL)return(1); return(yzjd(t->lchild)+yzjd(t->rchild));} void main(){ bitree *root;root=CREATREE(); printf(“n先序遍歷結(jié)果如下:n”);preorder(root);printf(“n中序遍歷結(jié)果如下:n”);inorder(root);printf(“n后序遍歷結(jié)果如下:n”);postorder(root);printf(“n葉子節(jié)點的個數(shù)為:%dn”,yzjd(root)); printf(“n”);} 實驗名稱:實驗七 查找算法實現(xiàn) 班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 掌握各種查找算法的特點,測試并驗證查找常見算法。 二、實驗要求: 1、參照各種查找算法程序樣例,驗證給出的查找常見算法。 2、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 1.建立有序表,采用折半查找實現(xiàn)某一已知的關(guān)鍵字的查找。 2.利用折半查找算法在一個有序表中插入一個元素,并保持表的有序性。 四、實驗結(jié)果: 五、實驗中遇到的問題及解決方法: 六、實驗心得體會: 本實驗使用的是折半法查找,優(yōu)點是查找效率高,在編程的時候要設(shè)置low,high,mid,分別表示區(qū)間和中間位置的值。 附: #include }; Seqlist * init()int data[MAXSIZE];int length; { } void insert(Seqlist *p){ int num=0;printf(“請輸入要鍵入的個數(shù):n”);if(num<1){ } int temp;for(int i=1;i length;i++) for(int j=0;j length-i;j++) if(p->data[j]>p->data[j+1]){ } temp=p->data[j];p->data[j]=p->data[j+1];p->data[j+1]=temp;printf(“請輸入數(shù)據(jù):n”);for(;num>0;num--){ } p->length++;scanf(“%d”,p->data+p->length-1);if(p->length==MAXSIZE){ } printf(“數(shù)組已滿n”);break;printf(“輸入個數(shù)有誤n”);else Seqlist *p;p=(Seqlist *)malloc(sizeof(Seqlist));p->length=0;return p;scanf(“%d”,&num); } getchar();int binsearch(Seqlist *p){ } void display(Seqlist *p){ } int main(void){ Seqlist *p;char flag;p=init();int i;for(i=0;i length;i++)printf(“%-5d”,p->data[i]);putchar('n');getchar();int low,high,mid,key;low=0;high=p->length-1;printf(“請輸入要查找的數(shù)據(jù):n”);scanf(“%d”,&key);getchar();while(low<=high){ } return 0;mid=(low+high)/2;if(key==p->data[mid]){ } else if(key data[mid])high=mid-1;else low=mid+1;printf(“此數(shù)據(jù)位于第%d位置n”,mid+1);return mid+1; } printf(“1-插入 2-折半法查找 3-顯示 0-退出n”);while(1){ } return 0;printf(“請輸入操作:”);switch(flag=getchar()){ case '1' : insert(p);break;case '2' : binsearch(p);break;case '3' : printf(“所有數(shù)據(jù)為:”);display(p);break;case '0' : free(p);return 0;} 實驗名稱:實驗八 排序綜合實驗 班 級 學(xué) 號 姓 名 第 周 星 期 節(jié) 成 績 一、實驗?zāi)康模?/p> 參照各種排序算法程序樣例,驗證給出的排序常見算法。 二、實驗要求: 1、掌握各種排序算法的特點,測試并驗證排序的常見算法。 2、提交實驗報告,報告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說明、程序清單、調(diào)試情況、設(shè)計技巧、心得體會。 三、實驗內(nèi)容: 輸入一組關(guān)鍵字序列分別實現(xiàn)下列排序,并將上述幾種排序的算法編寫成菜單,根據(jù)輸入的數(shù)字不同執(zhí)行對應(yīng)的排序算法(任選兩種排序方法實現(xiàn))。 1、直接插入排序。 2、希爾排序。 3、冒泡排序。 4、直接選擇排序。 5、快速排序。 6、堆排序。 7、歸并排序。 8、基數(shù)排序。 四、實驗結(jié)果: 五、實驗中遇到的問題及解決方法: 怎樣實現(xiàn)待排序數(shù)據(jù)長度從鍵盤輸入? 解決方法:設(shè)置一個n,從鍵盤輸入n的值。 六、實驗心得體會: 排序的常用方法有直接插入排序,簡單選擇排序,冒泡排序,快速排序。我在實驗中用了冒泡排序和快速排序,冒泡排序的程序比較簡單容易理解而快速排序則比較復(fù)雜??焖倥判蛞葎澐中蛄腥缓蠼⒃趧澐只A(chǔ)上進(jìn)行排序,這個排序是由遞歸實現(xiàn)的。但是快速排序的優(yōu)點是排序比較快。附: #include Seqlist * init(){ Seqlist *p;p=(Seqlist *)malloc(sizeof(Seqlist));p->length=0;return p;} void insert(Seqlist *p){ int num=0;printf(“請輸入要鍵入的個數(shù):n”);scanf(“%d”,&num);if(num<1) printf(“輸入個數(shù)有誤n”);else{ printf(“請輸入數(shù)據(jù):n”); for(;num>0;num--) { p->length++; scanf(“%d”,p->data+p->length-1); if(p->length==MAXSIZE) printf(“數(shù)組已滿n”); } } getchar();} void bublesort(Seqlist *p){ int temp;for(int i=1;i length;i++) for(int j=0;j length-i;j++) if(p->data[j]>p->data[j+1]) { temp=p->data[j]; p->data[j]=p->data[j+1]; p->data[j+1]=temp; } } void insertsort(Seqlist *p){ int i,j,temp;for(i=1;i length;i++){ temp=p->data[i]; j=i; while(j>0&&temp data[j-1]) { p->data[j]=p->data[j-1]; j--; } p->data[j]=temp;} } void display(Seqlist *p){ int i;for(i=0;i length;i++) printf(“%-5d”,p->data[i]);putchar('n');getchar();} int main(void){ Seqlist *p,*q;char flag;p=init();q=(Seqlist *)malloc(sizeof(Seqlist));q->length=0;printf(“1-輸入數(shù)據(jù) 2-直接插入排序 3-冒泡排序 4-顯示 0-退出n”);while(1){ printf(“請輸入操作:”); switch(flag=getchar()) { case '1' : insert(p);break; case '2' : *q=*p;insertsort(q);printf(“直接插入排序后的數(shù)據(jù)為:”);display(q);break; case '3' : *q=*p;bublesort(q);printf(“冒泡排序后的數(shù)據(jù)為:”);display(q);break; case '4' : printf(“原數(shù)據(jù)為:”);display(p);break; case '0' : free(p);return 0; } } return 0;}