第一篇:C#基礎(chǔ)編程設(shè)計(jì)實(shí)驗(yàn)報(bào)告
C# 基礎(chǔ)編程 設(shè)計(jì)實(shí)驗(yàn)報(bào)告
一、實(shí)驗(yàn)?zāi)康?/span>
1、熟悉 Visual Studio.NET 開(kāi)發(fā)環(huán)境。
2、掌握 C#應(yīng)用程序的基本操作過(guò)程。
3、掌握 C#的數(shù)據(jù)類(lèi)型,運(yùn)算符以及表達(dá)式的使用。
4、掌握分支和循環(huán)語(yǔ)句的使用方法。
5、掌握一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實(shí)驗(yàn)步驟
1、分析題意。
2、根據(jù)題目要求,新建項(xiàng)目。
3、編寫(xiě)并輸入相關(guān)的程序代碼。
5、運(yùn)行與調(diào)試項(xiàng)目。
6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、編寫(xiě)一個(gè)簡(jiǎn)單的控制臺(tái)應(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、編寫(xiě)一個(gè)簡(jiǎn)單的 Windows 應(yīng)用程序,在窗體 Load 事件中書(shū)寫(xiě)代碼,標(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、編寫(xiě)一個(gè)一個(gè)程序,用來(lái)判斷輸入的是大寫(xiě)字母,小寫(xiě)字母,數(shù)字還是其他的字符。
using System;using System.Collections.Generic;using System.Text;
namespace one.third {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“請(qǐng)輸入一個(gè)字符:”);
char c = Convert.ToChar(Console.ReadLine());
if((c>=“a”&&c<=“z”)||(c>=“A”&&c<=“Z”))
Console.WriteLine(“這是一個(gè)字母”);
if(char.IsDigit(c))
Console.WriteLine(“這是一個(gè)數(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、定義一個(gè)一維數(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、實(shí)現(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、實(shí)現(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]);
}
}
}
} } 六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
剛開(kāi)始編程的時(shí)候覺(jué)得無(wú)從下手,盡管我們已經(jīng)學(xué)了好幾種高級(jí)編程語(yǔ)言,但每個(gè)都有其獨(dú)特的地方,稍不留神就會(huì)混淆。
通過(guò)這次實(shí)驗(yàn),我體會(huì)到課后復(fù)習(xí)鞏固的重要性。在編程的時(shí)候,很多內(nèi)容都不記得,需要去翻書(shū)。不得不說(shuō),實(shí)驗(yàn)是鞏固課程的好方法!本次實(shí)驗(yàn),我熟悉 Visual Studio.NET 開(kāi)發(fā)環(huán)境;掌握了 C#應(yīng)用程序的基本操作過(guò)程;掌握了 C#的數(shù)據(jù)類(lèi)型,運(yùn)算符以及表達(dá)式的使用;掌握了分支和循環(huán)語(yǔ)句的使用方法以及一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
實(shí)驗(yàn)項(xiàng)目名稱(chēng):
類(lèi)與對(duì)象
實(shí)驗(yàn)學(xué)時(shí):
同組學(xué)生姓名:
實(shí)驗(yàn)地點(diǎn):
1318
實(shí)驗(yàn)日期:
月 26 日-11 月 9 日 實(shí)驗(yàn)成績(jī):
批改教師:
批改時(shí)間:
實(shí)驗(yàn) 2
類(lèi)與對(duì)象
一、實(shí)驗(yàn)?zāi)康?、要?/p>
(1)掌握類(lèi)的定義和使用;(2)掌握類(lèi)的數(shù)據(jù)成員,屬性的定義和使用;(3)掌握方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞;(4)掌握構(gòu)造函數(shù)的定義和使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實(shí)驗(yàn)步驟
1、分析題意; 2、根據(jù)題目要求,新建項(xiàng)目; 3、編寫(xiě)并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項(xiàng)目; 6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、定義一個(gè)方法,實(shí)現(xiàn)兩個(gè)數(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、定義一個(gè)方法,實(shí)現(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ù)組順序?yàn)椋骸?;
show(a);
int i, j, m;
for(i = 0;i < 10;i++)
{
m = a[i];
j = i-1;//a[j]為數(shù)組前一個(gè)值
while(j >= 0 && m > a[j])//判斷 i 下標(biāo)的數(shù)是否大于 j 下標(biāo)的數(shù)
{
a[j + 1] = a[j];//如果 i 下標(biāo)大于j 把 j 往后移一個(gè)位
j--;
}
a[j+1] = m;//當(dāng)不大于 j 的時(shí)候就把 M的值放到 i 下標(biāo)下面 j+1 是為了下標(biāo)減到最前時(shí)考慮-1 + 1 還是下標(biāo)的最前面
}
Console.WriteLine(“排序后,數(shù)組順序?yàn)椋骸?;
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、定義一個(gè)學(xué)生類(lèi),把學(xué)生類(lèi)當(dāng)作對(duì)象來(lái)傳遞。
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、定義一個(gè)方法,求兩個(gè)數(shù)的和和差,通過(guò)參數(shù)把這兩個(gè)值帶回。
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ù)重載,實(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(“求矩形面積,長(zhǎng)為 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è)計(jì)一個(gè) windows 應(yīng)用程序,在該程序中定義一個(gè)學(xué)生類(lèi)和班級(jí)類(lèi),以處理每個(gè)學(xué)生的學(xué)號(hào),姓名,語(yǔ)文,數(shù)學(xué)和英語(yǔ)成績(jī),要求:
1)能查詢(xún)每個(gè)學(xué)生的總成績(jī)。
2)能顯示全班前三名的名單。
3)能顯示單科成績(jī)最高分和不及格的學(xué)生名單。
4)能統(tǒng)計(jì)全班學(xué)生的平均成績(jī)。
5)能顯示各科成績(jī)不同分?jǐn)?shù)段的學(xué)生人數(shù)的百分比。
Student 類(lèi):
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 類(lèi):
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;
}
//給所有成績(jī)排序,用后面實(shí)現(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;
}
}
}
//顯示單科成績(jī)的最高分
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 += “ 單 科 語(yǔ) 文 最 高 :
” +
stu[HighScore(1)].name + “n”;
Maxer += “ 單 科 英 語(yǔ) 最 高 :
” + stu[HighScore(2)].name + “n”;
Loser += “單科數(shù)學(xué)掛科名單:” +BuhgName(0)+ “n”;
Loser += “單科語(yǔ)文掛科名單:” + BuhgName(1)+ “n”;
Loser += “單科英語(yǔ)掛科名單:” + BuhgName(2)+ “n”;
return Maxer + “n” + Loser;
}
//全班的平均成績(jī)
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 “班級(jí)總分平均分:”+avg;
}
//各科成績(jī)不同分?jǐn)?shù)段的學(xué)生百分比
//英語(yǔ)成績(jī)各分?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 “ 語(yǔ) 文 成 績(jī) 百 分 比 :”+“n”+“90~100:”+per1+“
80~90:”+per2+“
80~70:”+per3+“
70~60:”+per4+“
以下的:”+per5;
}
//數(shù)學(xué)成績(jī)各分?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é)成績(jī)百分比:” + “n” + “90~100:” + per1 + “
80~90:” + per2 + “
80~70:” + per3 + “
70~60:” + per4 + “
以下的:” + per5);
}
//英語(yǔ)成績(jī)各分?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é)成績(jī)百分比:” + “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 + “的總成績(jī):” + sl.stu[pos].sumScore;
}
else { MessageBox.Show(“不存在這個(gè)人!”);}
}
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í)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
通過(guò)本次實(shí)驗(yàn),我掌握了類(lèi)的定義與使用;掌握了類(lèi)的數(shù)據(jù)成員,屬性的定義和使用;掌握了方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞以及構(gòu)造函數(shù)的定義和使用。值得注意的是:本次實(shí)驗(yàn)中 return的使用以及所在的位置,類(lèi)型轉(zhuǎn)換時(shí)也經(jīng)常用到
實(shí)驗(yàn)項(xiàng)目名稱(chēng):
繼承與多態(tài)
實(shí)驗(yàn)學(xué)時(shí):
同組學(xué)生姓名:
實(shí)驗(yàn)地點(diǎn):
1318
實(shí)驗(yàn)日期:月 16 日-11 月 30 日 實(shí)驗(yàn)成績(jī):
批改教師:
批改時(shí)間:
實(shí)驗(yàn) 3
繼承與多態(tài)
一、實(shí)驗(yàn)?zāi)康?、要?/span>
(1)掌握類(lèi)的繼承性與多態(tài)性;(2)掌握虛方法的定義以及如何使用虛方法實(shí)現(xiàn)多態(tài);(3)掌握抽象類(lèi)的定義以及如何使用抽象方法實(shí)現(xiàn)多態(tài); 二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實(shí)驗(yàn)步驟
1、分析題意; 2、根據(jù)題目要求,新建項(xiàng)目; 3、編寫(xiě)并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項(xiàng)目; 6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、設(shè)計(jì)一個(gè) Windows 應(yīng)用程序,在該程序中首先構(gòu)造一個(gè)學(xué)生基本類(lèi),再分別構(gòu)造小學(xué)生、中學(xué)生、大學(xué)生派生類(lèi),當(dāng)輸入相關(guān)數(shù)據(jù),單擊不用的按鈕時(shí),將分別創(chuàng)建不同的學(xué)生類(lèi)對(duì)象,并輸出當(dāng)前學(xué)生的總?cè)藬?shù),該學(xué)生的姓名,學(xué)生類(lèi)型,平均成績(jī)。
Student 類(lèi):
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é)生” + “,” + “平均成績(jī)?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é)生” + “,” + “平均成績(jī)?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é)生” + “,” + “平均成績(jī)?yōu)椋骸?+ c.Average()+ “n”;
}
} } 2、設(shè)計(jì)一個(gè) Windows 應(yīng)用程序,在該程序中定義平面圖形抽象類(lèi)和派生類(lèi)圓,矩形和三角形。
Figure 類(lèi)代碼:
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、定義一個(gè) Person 類(lèi),包含姓名字段和一個(gè)方法,早上 8:30學(xué)生開(kāi)始上課,教師開(kāi)始講課。分別用 new 關(guān)鍵字,虛方法,抽象類(lèi)實(shí)現(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 開(kāi)始上課”;}
}
public class Teacher:Person
{
public Teacher(string name)
{ this.name = name;}
public string work()
{ return “開(kāi)始講課”;}
} } 虛方法:
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 開(kāi)始”,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 開(kāi)始上課”, 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}:開(kāi)始講課”, name);
}
} }
抽象類(lèi):
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 開(kāi)始上課”, 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}:開(kāi)始講課”, name);
}
}
}
六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
通過(guò)本次實(shí)驗(yàn),我理解了類(lèi)的繼承性與多態(tài)性;掌握了虛方法的定義以及如何用虛方法來(lái)實(shí)現(xiàn)多態(tài);掌握了抽象類(lèi)的定義以及如何用抽象方法來(lái)實(shí)現(xiàn)多態(tài)。
這次實(shí)驗(yàn)與前兩次不同,采用 Windows 應(yīng)用程序,既涉及到代碼段也涉及到界面的設(shè)計(jì)。所以,勉強(qiáng)通過(guò)實(shí)驗(yàn)。
實(shí)驗(yàn)項(xiàng)目名稱(chēng):
接口、文件和流
實(shí)驗(yàn)學(xué)時(shí):
同組學(xué)生姓名:
實(shí)驗(yàn)地點(diǎn):
A205
實(shí)驗(yàn)日期:月 7 日-12 月 21 日 實(shí)驗(yàn)成績(jī):
批改教師:
批改時(shí)間:
實(shí)驗(yàn) 4
接口、文件和流
一、實(shí)驗(yàn)?zāi)康?/span>
(1)掌握接口的定義及使用方法;(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的讀寫(xiě)操作類(lèi)及其使用方法;(4)掌握 OpenFileDialog,SaveFileDialog 等控件的使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有 Visual Studio.NET 軟件。
四、實(shí)驗(yàn)步驟
1、分析題意; 2、根據(jù)題目要求,新建項(xiàng)目; 3、編寫(xiě)并輸入相關(guān)的程序代碼; 5、運(yùn)行與調(diào)試項(xiàng)目; 6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、定義一個(gè) Person 類(lèi),包含姓名字段和一個(gè)方法,早上 8:30學(xué)生開(kāi)始上課,教師開(kāi)始講課。用接口來(lái)實(shí)現(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 開(kāi)始上課”, name);
}
}
...
第二篇:C#程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)報(bào)告書(shū)寫(xiě)要求
實(shí)驗(yàn)報(bào)告原則上要求學(xué)生手寫(xiě),要求書(shū)寫(xiě)工整。若因課程特點(diǎn)需打印的,標(biāo)題采用四號(hào)黑體,正文采用小四號(hào)宋體,單倍行距。紙張一律采用A4的紙張。
實(shí)驗(yàn)報(bào)告書(shū)寫(xiě)說(shuō)明
實(shí)驗(yàn)報(bào)告中實(shí)驗(yàn)?zāi)康暮鸵?、?shí)驗(yàn)儀器和設(shè)備、實(shí)驗(yàn)內(nèi)容與過(guò)程、實(shí)驗(yàn)結(jié)果與分析這四項(xiàng)內(nèi)容為必需項(xiàng)。教師可根據(jù)學(xué)科特點(diǎn)和實(shí)驗(yàn)具體要求增加項(xiàng)目。
填寫(xiě)注意事項(xiàng)
(1)細(xì)致觀察,及時(shí)、準(zhǔn)確、如實(shí)記錄。(2)準(zhǔn)確說(shuō)明,層次清晰。
(3)盡量采用專(zhuān)用術(shù)語(yǔ)來(lái)說(shuō)明事物。
(4)外文、符號(hào)、公式要準(zhǔn)確,應(yīng)使用統(tǒng)一規(guī)定的名詞和符號(hào)。(5)應(yīng)獨(dú)立完成實(shí)驗(yàn)報(bào)告的書(shū)寫(xiě),嚴(yán)禁抄襲、復(fù)印,一經(jīng)發(fā)現(xiàn),以零分論處。
實(shí)驗(yàn)報(bào)告批改說(shuō)明
實(shí)驗(yàn)報(bào)告的批改要及時(shí)、認(rèn)真、仔細(xì),一律用紅色筆批改。實(shí)驗(yàn)報(bào)告的批改成績(jī)采用五級(jí)記分制或百分制,按《金陵科技學(xué)院課堂教學(xué)實(shí)施細(xì)則》中作業(yè)批閱成績(jī)?cè)u(píng)定要求執(zhí)行。
實(shí)驗(yàn)報(bào)告裝訂要求
實(shí)驗(yàn)批改完畢后,任課老師將每門(mén)課程的每個(gè)實(shí)驗(yàn)項(xiàng)目的實(shí)驗(yàn)報(bào)告以自然班為單位、按學(xué)號(hào)升序排列,裝訂成冊(cè),并附上一份該門(mén)課程的實(shí)驗(yàn)大綱。
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)項(xiàng)目名稱(chēng): C#基礎(chǔ)編程 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: 實(shí)驗(yàn)地點(diǎn): 1318 實(shí)驗(yàn)日期: 10月5日-10月19日 實(shí)驗(yàn)成績(jī): 批改教師: 批改時(shí)間:
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)1 C#基礎(chǔ)編程
一、實(shí)驗(yàn)?zāi)康?/p>
1、熟悉Visual Studio.NET開(kāi)發(fā)環(huán)境。
2、掌握C#應(yīng)用程序的基本操作過(guò)程。
3、掌握C#的數(shù)據(jù)類(lèi)型,運(yùn)算符以及表達(dá)式的使用。
4、掌握分支和循環(huán)語(yǔ)句的使用方法。
5、掌握一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實(shí)驗(yàn)步驟
1、分析題意。
2、根據(jù)題目要求,新建項(xiàng)目。
3、編寫(xiě)并輸入相關(guān)的程序代碼。
5、運(yùn)行與調(diào)試項(xiàng)目。
6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、編寫(xiě)一個(gè)簡(jiǎn)單的控制臺(tái)應(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、編寫(xiě)一個(gè)簡(jiǎn)單的Windows應(yīng)用程序,在窗體Load事件中書(shū)寫(xiě)代碼,標(biāo)簽中顯示你的姓名。
using System;using System.Collections.Generic;using System.ComponentModel;
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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、編寫(xiě)一個(gè)一個(gè)程序,用來(lái)判斷輸入的是大寫(xiě)字母,小寫(xiě)字母,數(shù)字還是其他的字符。
using System;using System.Collections.Generic;using System.Text;
namespace one.third {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“請(qǐng)輸入一個(gè)字符:”);
char c = Convert.ToChar(Console.ReadLine());
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
Console.WriteLine(“這是一個(gè)字母”);
if(char.IsDigit(c))
Console.WriteLine(“這是一個(gè)數(shù)字”);
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
}
} }
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é)院實(shí)驗(yàn)報(bào)告
} 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、定義一個(gè)一維數(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、實(shí)現(xiàn)二維數(shù)組的輸入和輸出。
using System;using System.Collections.Generic;using System.Linq;
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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、實(shí)現(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]);
}
}
}
} }
六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
剛開(kāi)始編程的時(shí)候覺(jué)得無(wú)從下手,盡管我們已經(jīng)學(xué)了好幾種高級(jí)編程語(yǔ)言,但每個(gè)都
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
有其獨(dú)特的地方,稍不留神就會(huì)混淆。
通過(guò)這次實(shí)驗(yàn),我體會(huì)到課后復(fù)習(xí)鞏固的重要性。在編程的時(shí)候,很多內(nèi)容都不記得,需要去翻書(shū)。不得不說(shuō),實(shí)驗(yàn)是鞏固課程的好方法!本次實(shí)驗(yàn),我熟悉Visual Studio.NET開(kāi)發(fā)環(huán)境;掌握了C#應(yīng)用程序的基本操作過(guò)程;掌握了C#的數(shù)據(jù)類(lèi)型,運(yùn)算符以及表達(dá)式的使用;掌握了分支和循環(huán)語(yǔ)句的使用方法以及一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)項(xiàng)目名稱(chēng): 類(lèi)與對(duì)象 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: 實(shí)驗(yàn)地點(diǎn): 1318 實(shí)驗(yàn)日期: 10月26日-11月9日 實(shí)驗(yàn)成績(jī): 批改教師: 批改時(shí)間:
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)2 類(lèi)與對(duì)象
一、實(shí)驗(yàn)?zāi)康?、要?/p>
(1)掌握類(lèi)的定義和使用;
(2)掌握類(lèi)的數(shù)據(jù)成員,屬性的定義和使用;
(3)掌握方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞;(4)掌握構(gòu)造函數(shù)的定義和使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實(shí)驗(yàn)步驟
1、分析題意;
2、根據(jù)題目要求,新建項(xiàng)目;
3、編寫(xiě)并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項(xiàng)目;
6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、定義一個(gè)方法,實(shí)現(xiàn)兩個(gè)數(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é)院實(shí)驗(yàn)報(bào)告
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、定義一個(gè)方法,實(shí)現(xiàn)數(shù)組的排序。using System;using System.Collections.Generic;using System.Text;
namespace second.two {
class Program
{
string.Format(”
按
值
傳
參
交
換
之
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
public class sort
{
public void change(int[] a)
{
Console.WriteLine(“排序前,數(shù)組順序?yàn)椋骸?;
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é)院實(shí)驗(yàn)報(bào)告
}
} }
5、用構(gòu)造函數(shù)重載,實(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é)院實(shí)驗(yàn)報(bào)告
square s = new square(a,b);
Console.WriteLine(“求矩形面積,長(zhǎng)為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è)計(jì)一個(gè)windows應(yīng)用程序,在該程序中定義一個(gè)學(xué)生類(lèi)和班級(jí)類(lèi),以處理每個(gè)學(xué)生的學(xué)號(hào),姓名,語(yǔ)文,數(shù)學(xué)和英語(yǔ)成績(jī),要求:
1)能查詢(xún)每個(gè)學(xué)生的總成績(jī)。2)能顯示全班前三名的名單。
3)能顯示單科成績(jī)最高分和不及格的學(xué)生名單。4)能統(tǒng)計(jì)全班學(xué)生的平均成績(jī)。
5)能顯示各科成績(jī)不同分?jǐn)?shù)段的學(xué)生人數(shù)的百分比。
Student類(lèi): 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é)院實(shí)驗(yàn)報(bào)告
get { return chinese + math + english;}
}
} } StudentList類(lèi): 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;
}
//給所有成績(jī)排序,用后面實(shí)現(xiàn)前三名的排名
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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;
}
}
}
//顯示單科成績(jī)的最高分
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é)院實(shí)驗(yàn)報(bào)告
}
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 += “ 單科語(yǔ)文最高:” + stu[HighScore(1)].name + “n”;
Maxer += “ 單科英語(yǔ)最高:” + stu[HighScore(2)].name + “n”;
Loser += “單科數(shù)學(xué)掛科名單:” +BuhgName(0)+ “n”;
Loser += “單科語(yǔ)文掛科名單:” + BuhgName(1)+ “n”;
Loser += “單科英語(yǔ)掛科名單:” + BuhgName(2)+ “n”;
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
return Maxer + “n” + Loser;
}
//全班的平均成績(jī)
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 “班級(jí)總分平均分:”+avg;
}
//各科成績(jī)不同分?jǐn)?shù)段的學(xué)生百分比
//英語(yǔ)成績(jī)各分?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é)院實(shí)驗(yàn)報(bào)告
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 “語(yǔ)文成績(jī)百分比:”+“n”+“90~100:”+per1+“ 80~90:”+per2+“ 80~70:”+per3+“ 70~60:”+per4+“ 60以下的:”+per5;
}
//數(shù)學(xué)成績(jī)各分?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é)院實(shí)驗(yàn)報(bào)告
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é)成績(jī)百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);
}
//英語(yǔ)成績(jī)各分?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é)院實(shí)驗(yàn)報(bào)告
}
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é)成績(jī)百分比:” + “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é)院實(shí)驗(yàn)報(bào)告
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 + “的總成績(jī):sl.stu[pos].sumScore;
}
else { MessageBox.Show(”不存在這個(gè)人!“);}
}
private void btnFinish_Click(object sender, EventArgs e)
{
label7.Text = ”前3名:“+”n“;
” + 金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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í)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
通過(guò)本次實(shí)驗(yàn),我掌握了類(lèi)的定義與使用;掌握了類(lèi)的數(shù)據(jù)成員,屬性的定義和使用;掌握了方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞以及構(gòu)造函數(shù)的定義和使用。值得注意的是:本次實(shí)驗(yàn)中return的使用以及所在的位置,類(lèi)型轉(zhuǎn)換時(shí)也經(jīng)常用到
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)項(xiàng)目名稱(chēng): 繼承與多態(tài) 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: 實(shí)驗(yàn)地點(diǎn): 1318 實(shí)驗(yàn)日期: 11月16日-11月30日 實(shí)驗(yàn)成績(jī): 批改教師: 批改時(shí)間:
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)3 繼承與多態(tài)
一、實(shí)驗(yàn)?zāi)康?、要?/p>
(1)掌握類(lèi)的繼承性與多態(tài)性;
(2)掌握虛方法的定義以及如何使用虛方法實(shí)現(xiàn)多態(tài);(3)掌握抽象類(lèi)的定義以及如何使用抽象方法實(shí)現(xiàn)多態(tài);
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實(shí)驗(yàn)步驟
1、分析題意;
2、根據(jù)題目要求,新建項(xiàng)目;
3、編寫(xiě)并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項(xiàng)目;
6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、設(shè)計(jì)一個(gè)Windows應(yīng)用程序,在該程序中首先構(gòu)造一個(gè)學(xué)生基本類(lèi),再分別構(gòu)造小學(xué)生、中學(xué)生、大學(xué)生派生類(lèi),當(dāng)輸入相關(guān)數(shù)據(jù),單擊不用的按鈕時(shí),將分別創(chuàng)建不同的學(xué)生類(lèi)對(duì)象,并輸出當(dāng)前學(xué)生的總?cè)藬?shù),該學(xué)生的姓名,學(xué)生類(lèi)型,平均成績(jī)。
Student類(lèi): 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é)院實(shí)驗(yàn)報(bào)告
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é)院實(shí)驗(yàn)報(bào)告
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é)院實(shí)驗(yàn)報(bào)告
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é)生” + “,” + “平均成績(jī)?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é)生” + “,” + “平均成績(jī)?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é)生” + “,” + “平均成績(jī)?yōu)椋骸?+ c.Average()+ “n”;
}
}
c
=
new
College(txtName.Text,Convert.ToDouble(txtChinese.Text),金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
}
2、設(shè)計(jì)一個(gè)Windows應(yīng)用程序,在該程序中定義平面圖形抽象類(lèi)和派生類(lèi)圓,矩形和三角形。
Figure類(lèi)代碼: 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é)院實(shí)驗(yàn)報(bào)告
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é)院實(shí)驗(yàn)報(bào)告
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、定義一個(gè)Person類(lèi),包含姓名字段和一個(gè)方法,早上8:30學(xué)生開(kāi)始上課,教師開(kāi)始講課。分別用new關(guān)鍵字,虛方法,抽象類(lèi)實(shí)現(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é)院實(shí)驗(yàn)報(bào)告
{
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開(kāi)始上課”;}
}
public class Teacher:Person
{
public Teacher(string name)
{ this.name = name;}
public string work()
{ return “開(kāi)始講課”;}
} } 虛方法: using System;using System.Collections.Generic;using System.Text;
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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開(kāi)始”,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開(kāi)始上課”, name);
}
}
public class Teacher : Person
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
{
private string type;
public Teacher(string name, string type)
: base(name)
{ this.type = type;}
public override string Work()
{
return string.Format(“Person{0}:開(kāi)始講課”, name);
}
} }
抽象類(lèi): 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é)院實(shí)驗(yàn)報(bào)告
{
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開(kāi)始上課”, 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}:開(kāi)始講課”, name);
}
} }
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))
通過(guò)本次實(shí)驗(yàn),我理解了類(lèi)的繼承性與多態(tài)性;掌握了虛方法的定義以及如何用虛方法來(lái)實(shí)現(xiàn)多態(tài);掌握了抽象類(lèi)的定義以及如何用抽象方法來(lái)實(shí)現(xiàn)多態(tài)。這次實(shí)驗(yàn)與前兩次不同,采用Windows應(yīng)用程序,既涉及到代碼段也涉及到界面的設(shè)計(jì)。所以,勉強(qiáng)通過(guò)實(shí)驗(yàn)。
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)項(xiàng)目名稱(chēng): 接口、文件和流 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: 實(shí)驗(yàn)地點(diǎn): A205 實(shí)驗(yàn)日期: 12月7日-12月21日 實(shí)驗(yàn)成績(jī): 批改教師: 批改時(shí)間:
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)4 接口、文件和流
一、實(shí)驗(yàn)?zāi)康?/p>
(1)掌握接口的定義及使用方法;
(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的讀寫(xiě)操作類(lèi)及其使用方法;
(4)掌握OpenFileDialog,SaveFileDialog等控件的使用。
二、實(shí)驗(yàn)要求
(1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。
三、實(shí)驗(yàn)設(shè)備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實(shí)驗(yàn)步驟
1、分析題意;
2、根據(jù)題目要求,新建項(xiàng)目;
3、編寫(xiě)并輸入相關(guān)的程序代碼;
5、運(yùn)行與調(diào)試項(xiàng)目;
6、保存項(xiàng)目。
五、實(shí)驗(yàn)內(nèi)容
1、定義一個(gè)Person類(lèi),包含姓名字段和一個(gè)方法,早上8:30學(xué)生開(kāi)始上課,教師開(kāi)始講課。用接口來(lái)實(shí)現(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é)院實(shí)驗(yàn)報(bào)告
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開(kāi)始上課”, name);
}
}
public class Teacher :Person, IPerson
{
public string type
{
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
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開(kāi)始講課”, name);
}
}
} }
2、聲明一個(gè)接口Iplayer,包含5個(gè)接口方法:播放,停止,暫停,上一首和下一首。在該程序中定義一個(gè)MP3播放器類(lèi)和一個(gè)AVI播放器類(lèi),以實(shí)現(xiàn)該接口。
MP3類(lèi)(包含Iplayer接口,AVI類(lèi)): 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é)院實(shí)驗(yàn)報(bào)告
{
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é)院實(shí)驗(yàn)報(bào)告
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é)院實(shí)驗(yàn)報(bào)告
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、實(shí)現(xiàn)對(duì)文本文件的讀寫(xiě)操作,用文件操作控件打開(kāi)和保存文件。
using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace Test4_3
金陵科技學(xué)院實(shí)驗(yàn)報(bào)告
{
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、實(shí)現(xiàn)對(duì)二進(jìn)制文件的讀寫(xiě)操作。
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é)院實(shí)驗(yàn)報(bào)告
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é)號(hào)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é)院實(shí)驗(yàn)報(bào)告
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、實(shí)現(xiàn)對(duì)象序列化和反序化。
Student類(lèi): 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é)院實(shí)驗(yàn)報(bào)告
}
} } StudentList類(lèi): 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é)院實(shí)驗(yàn)報(bào)告
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è)計(jì)語(yǔ)言的三大特點(diǎn)是什么?答:【23】Main函數(shù)特點(diǎn)?答:1)Main方法是C#程序的限定,默認(rèn)的為private【2】三種命名法則的特點(diǎn)?答:匈牙利命名法:在變限定符,返回類(lèi)型為void或int類(lèi)型,Main方法必須是靜態(tài)方法;3)一個(gè)類(lèi)或結(jié)構(gòu)只能有一個(gè)有效的入駝表示法:一個(gè)標(biāo)示符用若干個(gè)有意義的英語(yǔ)單詞或口點(diǎn)函數(shù);4)main方法必須定義在某一個(gè)類(lèi)中??s寫(xiě)組成,第一個(gè)單詞首字母小寫(xiě),后邊的首字母大【24】多態(tài)(重寫(xiě)、隱藏)
寫(xiě);Pascal表示法:與駱駝表示法類(lèi)似,但是第一個(gè) 單詞的首字母也需要大寫(xiě)。【3】C#注釋的三種形式特點(diǎn)?答1)單行注釋?zhuān)?/ 2)class A /// {public virtual void F()【4】引用類(lèi)型和值類(lèi)型的數(shù)據(jù)類(lèi)型? {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的實(shí)例時(shí),產(chǎn)生什么輸出?(2)public class EnumTest答:x=1,y=0 { enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};分析:執(zhí)行new B()時(shí),由于B繼承自A,所以會(huì)調(diào)用static void Main()A的構(gòu)造函數(shù),并執(zhí)行其中的PrintFields()方法,由{int x=(int)Days.Sun;inty=(int)Days.Fri;于該方法為虛方法,在繼承類(lèi)中被重寫(xiě),所以,將執(zhí)Console.WriteLine(“Sun = {0}”, x);行B中的PrintFields()方法。此時(shí),將在DOS界面Console.WriteLine(“Fri = {0}”, y);}} 上輸出“x=1,y=0”,然后,在運(yùn)行B的構(gòu)造函數(shù)中的答:輸出結(jié)果:Sun = 2Fri = 7 y=-1。(建議同學(xué)們將此程序放入到代碼中,設(shè)置斷點(diǎn)【5】枚舉類(lèi)型的字段和關(guān)聯(lián)值?枚舉類(lèi)型有三個(gè)要看執(zhí)行過(guò)程。)
【25】什么是多態(tài)對(duì)象?答:用基類(lèi)類(lèi)名聲明,但是特性 修飾符 enum 枚舉名 : 基礎(chǔ)類(lèi)型 {枚舉成員聲明,枚舉成員聲明,??,枚舉成員聲明}默認(rèn)的基礎(chǔ)函數(shù)來(lái)實(shí)例化的對(duì)象。這類(lèi)對(duì)象的主要用途是引發(fā)多類(lèi)型為int;關(guān)聯(lián)值:如果沒(méi)有被聲明,默認(rèn)為0。態(tài),為了將它們和一般的對(duì)象(聲明和創(chuàng)建都使用同【6】強(qiáng)制類(lèi)型轉(zhuǎn)換(例:若有double f=2.7;int 一個(gè)類(lèi)型名的對(duì)象)加以區(qū)別、揭示它們的特點(diǎn)和用2)將源類(lèi)型的對(duì)象途,這種形態(tài)的對(duì)象稱(chēng)為多態(tài)對(duì)象。轉(zhuǎn)換成為目的類(lèi)型的對(duì)象 【26】接口的特點(diǎn)。答:接口只能包含抽象方法,不【7】運(yùn)算符&和&&的區(qū)別?答:條件“與”運(yùn)算符(&&)沒(méi)有訪(fǎng)問(wèn)修飾符,接口成員必須是方法屬性事件或者時(shí)才計(jì)算第二個(gè)操作數(shù)。而&需要計(jì)算所有操作數(shù),索引器不能包含常數(shù)字段運(yùn)算符也不能有靜態(tài)成員。并且優(yōu)先級(jí)高于&& 【27】委托和事件,【8】裝箱和拆箱的概念?答:裝箱就是把一個(gè)值類(lèi)型委托的定義修飾符 delegate 返回類(lèi)型 委托類(lèi)型名(參數(shù)列表); 【9】for循環(huán)和if語(yǔ)句聯(lián)合使用的程序分析,for(;;)eg: public delegate int DelegateClass(stringinfo);
和continue的區(qū)別?答:break跳出循委托的創(chuàng)建(實(shí)例化)委托對(duì)象 = new 委托名(關(guān)聯(lián)方法); 【11】命名空間的特點(diǎn)答:它提供一種命名機(jī)制,是eg: DelegateClass obj=new DelegateClass(MethodA);
合方式無(wú)關(guān),不能表明源文件的存取方式,命名空間DelegateClass obj=MethodA;//隱式創(chuàng)建和初是按層次組織的。始化(不用new)【12】數(shù)組元素的的復(fù)制和讀值 例:分析下列語(yǔ)句: 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;該語(yǔ)句的作用是:在TestCS 命名空間中定義了了一C)6;D)越界 個(gè)名為Hello 的委托類(lèi)型;
【13】類(lèi)和對(duì)象的關(guān)系?答:類(lèi)是對(duì)象的抽象,對(duì)象【28】Windows窗體中Button按鈕觸發(fā)的事件是什【14】關(guān)鍵字this和base的區(qū)別?答:base指代基【29】Windows窗體中控件的標(biāo)識(shí)符如何修改?答:【15】關(guān)鍵字new、using的多種用法?答:new修飾【30】如何修改Windows窗體的啟動(dòng)窗體?答:修改被重寫(xiě),但new修飾符可終止這一特性;向下傳播; 實(shí)例化一個(gè)對(duì)象。Using:導(dǎo)入命名空間;自動(dòng)釋放【31】要使用SQL Server需要使用哪兩個(gè)命名空間? Using代碼框里的資源。【16】索引器的特點(diǎn)?答:索引器允許重載;字符串Using System.Date.SqlClient: 【32】什么是DataSet、DataAdapter??jī)烧呗?lián)系?答:過(guò)簽名標(biāo)識(shí);通過(guò)元素訪(fǎng)問(wèn)來(lái)訪(fǎng)問(wèn);必須為實(shí)例成員;索引器的get訪(fǎng)問(wèn)器具有與索引器相同的形參表;除DataAdapter:數(shù)據(jù)適配器,數(shù)據(jù)庫(kù)與DataSet間的橋value參數(shù)外,索引器的set訪(fǎng)問(wèn)器還具有與索引器梁,把數(shù)據(jù)庫(kù)中數(shù)據(jù)下載到DataSet或回傳回去。相同的形參表?!?3】用戶(hù)登錄和密碼修改(帶數(shù)據(jù)庫(kù))【17】靜態(tài)數(shù)據(jù)成員特點(diǎn)?答:為所有類(lèi)所共享,區(qū)用戶(hù)登錄 【18】構(gòu)造函數(shù)的特點(diǎn)?答:(1)構(gòu)造函數(shù)名與類(lèi)名UserName='“ + txtUsername.Text.Trim().ToLower()+ ”' and UserPwd='“ + txtPassword.Text.Trim()+ 【19】析構(gòu)函數(shù)的特點(diǎn)?答:(1)析構(gòu)函數(shù)名是在類(lèi)”'“;if(OperateDB.ExecuteReader(sql))型(默認(rèn)為空)和修飾符;(3)析構(gòu)函數(shù)不能被重載。{username = txtUsername.Text.Trim().ToLower();【20】什么是方法的重載?重載的特點(diǎn)是什么?答: frmMain frm = new frmMain();frm.Show();this.Hide();} 定義一組方法。重載的特點(diǎn):1)位于同一類(lèi)中;2)else
方法名相同;3)方法參考列表不同,包括參數(shù)個(gè)數(shù)不{MessageBox.Show(”用戶(hù)名或密碼錯(cuò)誤“, ”出錯(cuò)了“, 同和參數(shù)類(lèi)型不同;4)與方法返回值和修飾符沒(méi)關(guān)系。MessageBoxButtons.OK, MessageBoxIcon.Error);} 【21】虛函數(shù)的特點(diǎn)?答:1)虛方法前不允許有修改密碼: 修飾符;2)虛方法不能是私有的,因此不能使用private修飾符; where UserName='” + frmLogin.username + “' and 【22】抽象類(lèi)和抽象方法的主要特點(diǎn)?答:抽象類(lèi):UserPwd='” + txtOldPwd.Text.Trim()+ “'”;(或者if(OperateDB.ExecuteReader(sqlCheckPwd))說(shuō),不能產(chǎn)生對(duì)象。但是,它可以有構(gòu)造函數(shù)。(2){string sql = “update UserInfo set UserPwd='” 設(shè)計(jì)abstract類(lèi)的目的是為了被繼承。抽象方法:是+ txtNewPwd.Text.Trim()+ “' where UserName='” + 不完整的,不能執(zhí)行的。frmLogin.username + “'”;
if(OperateDB.ExecuteNonQuery(sql)== 1)
{MessageBox.Show(“密碼修改成功!”);}else
{ MessageBox.Show(“密碼修改失??!”);}}
else{MessageBox.Show(“舊密碼不正確!”);}
【34】抽象類(lèi)定義和繼承使用
特點(diǎn):1.沒(méi)有被完整定義,因而它不能用來(lái)實(shí)例化,或者說(shuō),不能產(chǎn)生對(duì)象。(但是,它可以有構(gòu)造函數(shù)。)2.設(shè)計(jì)abstract類(lèi)的目的是為了被繼承。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】接口及繼承類(lèi)的使用
特定功能的抽象成員的集合。一個(gè)類(lèi)可以繼承多個(gè)接口,從而獲得多個(gè)行為的描述,將它們組合成新的功能并在類(lèi)中實(shí)現(xiàn)。繼承類(lèi)中必須實(shí)現(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類(lèi),包括屬性成員——網(wǎng)絡(luò)類(lèi)型(NetworkType),字段成員——屏幕尺寸(screenSize)、手機(jī)品牌(brand),手機(jī)型號(hào)
(brandModel),公共方法成員——Open、Close。其中screenSize為單位是英寸的雙精度數(shù),brand為字符串,NetworkType只能是“GSM”或“CDMA”字符串。要求:(1)在此類(lèi)中包含構(gòu)造函數(shù),構(gòu)造函數(shù)用于對(duì)數(shù)據(jù)(屏幕尺寸、手機(jī)品牌和手機(jī)型號(hào))進(jìn)行初始化。(2)公共成員方法中輸出相應(yīng)提示信息(參見(jiàn)(3)中的輸出結(jié)果格式)。(3)寫(xiě)一測(cè)試類(lèi),在類(lèi)中實(shí)例化一MobilePhone對(duì)象,最后能在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();} }
【例】寫(xiě)一名為Desk的類(lèi),包含兩個(gè)字段Length(雙精度類(lèi)型)、Height(雙精度類(lèi)型)。再寫(xiě)一繼承類(lèi)ComputerDesk類(lèi)。ComputerDesk類(lèi)除了有Length和Height外,還有KeyboardTray(字符串類(lèi)型)。Public class desk {double length;double height;}
Public class computerdesk:desk {string keyboardtray}
第四篇:計(jì)算器編程設(shè)計(jì)心得體會(huì)
計(jì)算器編程設(shè)計(jì)心得體會(huì)
——
本次有關(guān)計(jì)算器程序的編寫(xiě),個(gè)人感覺(jué)還是有一定難度的。在考察運(yùn)算符的重載時(shí),其他的運(yùn)算符還好,但是在定義“()”運(yùn)算符時(shí)在邏輯上考慮的比較復(fù)雜,因?yàn)槔ㄌ?hào)運(yùn)算符內(nèi)的計(jì)算優(yōu)先進(jìn)行,所以要考慮的有括號(hào)內(nèi)的各種“+”、“-”、“*”、“/”的組合使用,還有括號(hào)里含括號(hào)的情況。括號(hào)都是成對(duì)存在的,首先要在運(yùn)算式中找到最里面的一對(duì)括號(hào)(即:此括號(hào)內(nèi)不再含有其他的括號(hào))。之前的想法是用指針按次找到第一個(gè)右括號(hào),然后再找出右括號(hào)左邊的第一個(gè)左括號(hào),計(jì)算出這兩個(gè)半括號(hào)之間的公式,用t表示并替代。同理,再尋找出替換后的最里面的一對(duì)括號(hào),計(jì)算出這兩個(gè)半括號(hào)之間的公式,用t表示并替代。以此類(lèi)推,使用for循環(huán)語(yǔ)句,直到找不到括號(hào)為止,return t;其他的方面,遇到的難點(diǎn)有:不知道怎么判斷輸入的數(shù)學(xué)公式不符合規(guī)定,除了分母不能為零比較好考慮,其他的形式總覺(jué)得會(huì)有疏漏。例如在判斷“/”的右操作數(shù)不為零時(shí)則繼續(xù)進(jìn)行,反之則跳出,并給get賦值為1。只有當(dāng)get為0時(shí),才能正常輸出。當(dāng) set為1時(shí)輸出 “n您輸入的不匹配,有錯(cuò)誤發(fā)生。Result lost!” ;如果set為2,則輸出 “n您輸入了非法字符 , 請(qǐng)重新輸入,謝謝合作!”;如果set值為3則輸出“nStack is full, Lost result!”若是set 等于4則輸出“nERROR!Divide by 0!”。但是在判斷2、3、4情況時(shí)感覺(jué)不是很好描述編寫(xiě)。
第五篇:軟件技術(shù)基礎(chǔ)實(shí)驗(yàn)報(bào)告
《軟件開(kāi)發(fā)技術(shù)基礎(chǔ)》
實(shí)驗(yàn)報(bào)告
學(xué)院:XX學(xué)院
班級(jí):XX
姓名: XX
學(xué)號(hào):XX
《軟件開(kāi)發(fā)技術(shù)基礎(chǔ)》實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)一 順序表的操作
班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī)
一、實(shí)驗(yàn)?zāi)康模?/p>
1、掌握順序表結(jié)構(gòu)的實(shí)現(xiàn)方式;
2、掌握順序表常用算法的實(shí)現(xiàn);
3、熟悉利用順序表解決問(wèn)題的一般思路;
4、參照給定的順序表的程序樣例,驗(yàn)證給出的順序表的常見(jiàn)算法,領(lǐng)會(huì)順序表結(jié)構(gòu)的優(yōu)點(diǎn)和不足。
二、實(shí)驗(yàn)要求:
1、掌握順序表的特點(diǎn)及常見(jiàn)算法。
2、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。
三、實(shí)驗(yàn)內(nèi)容:
1、設(shè)計(jì)一個(gè)靜態(tài)數(shù)組存儲(chǔ)結(jié)構(gòu)的順序表,要求編程實(shí)現(xiàn)如下任務(wù):
(1)建立一個(gè)順序表,首先依次輸人整數(shù)數(shù)據(jù)元素(個(gè)數(shù)根據(jù)需要鍵盤(pán)給定)。(2)刪除指定位置的數(shù)據(jù)元素(指定元素位置通過(guò)鍵盤(pán)輸入),再依次顯示刪除后的順序表中的數(shù)據(jù)元素。
(3)查找指定數(shù)據(jù)的數(shù)據(jù)元素(指定數(shù)據(jù)由鍵盤(pán)輸入),若找到則顯示位置,若沒(méi)有找到則顯示0。
2、使用順序表實(shí)現(xiàn)一個(gè)電話(huà)本的管理程序,電話(huà)本中的每條記錄包括學(xué)號(hào)、姓名、手機(jī)號(hào)碼和固定電話(huà)四項(xiàng)。要求實(shí)現(xiàn)菜單、初始化、添加、刪除和顯示等功能。
四、程序要求:
1、采用順序表實(shí)現(xiàn),假設(shè)該順序表的數(shù)據(jù)元素個(gè)數(shù)在最壞情況下不會(huì)超過(guò)50個(gè)。
2、寫(xiě)出完整的程序并能調(diào)試運(yùn)行。
五、實(shí)驗(yàn)結(jié)果:
1、順序表的結(jié)果:
2、電話(huà)簿的結(jié)果:
六、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法:
1.在刪除數(shù)據(jù)的時(shí)候如果有兩個(gè)一樣的數(shù)時(shí)要怎樣解決? 解決方法:用while進(jìn)行判斷。
2.在刪除操作中,刪除函數(shù)中的l是指針,所以用->指向,而在主函數(shù)中l(wèi)是結(jié)構(gòu)體,用“.”。3.在查找的時(shí)候有一個(gè)返回值,而這個(gè)返回值是指針,所以在寫(xiě)查找函數(shù)的時(shí)候要把返回值類(lèi)型寫(xiě)上。
七、實(shí)驗(yàn)心得體會(huì):
一開(kāi)始不知所措,首先應(yīng)該有一個(gè)大的方向,把主程序編號(hào),再逐步求精,落實(shí)到每一個(gè)函數(shù)的編寫(xiě)。對(duì)于c語(yǔ)言要熟練掌握,要熟悉循環(huán)等得操作,要熟練掌握順序表中的插入,刪除,查找,的基本函數(shù)。在此的基礎(chǔ)上靈活使用。附:
1、順序表的程序: #include
struct Seqlist {
};Seqlist *init(){
} void insert(Seqlist *p){
int num=0;printf(“請(qǐng)輸入要鍵入的個(gè)數(shù):”);scanf(“%d”,&num);if(num<1)printf(“鍵入的數(shù)據(jù)個(gè)數(shù)錯(cuò)誤!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ù)組已滿(mǎn)n”);break;getchar();void deletee(Seqlist *p){
} int find(Seqlist *p){ int i=0;printf(“請(qǐng)輸入要?jiǎng)h除元素的位置:”);scanf(“%d”,&i);if(i<1||i>p->length)printf(“表中沒(méi)有第%d個(gè)元素!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(“請(qǐng)輸入要查找的數(shù)據(jù):”);scanf(“%d”,&x);for(int i=0;i
length;i++){
} if(i>=p->length)printf(“數(shù)組中沒(méi)有此數(shù)據(jù)!n”);if(p->data[i]==x){
} printf(“此數(shù)據(jù)位于第%d個(gè)位置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(“請(qǐng)輸入操作:”);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、電話(huà)簿的程序: #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<<“|----姓名----|--電話(huà)號(hào)碼--|-------------地址------------|”< } void Phonebook::Display(){ CNT *p;cout<<“|----姓名----|--電話(huà)號(hào)碼--|-------------地址------------|”< } 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<<“請(qǐng)輸入姓名:”;cin>>temp->name;cout<<“請(qǐng)輸入手機(jī)號(hào)碼:”;cin>>temp->phonenumber;cout<<“請(qǐng)輸入固定電話(huà):”;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<<“請(qǐng)輸入要?jiǎng)h除聯(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<<“請(qǐng)輸入要查找或要修改的聯(lián)系人的信息:”;cin>>data;for(p=p->next;p!=NULL&&p->name!=data&&p->phonenumber!=data&&p->phone!=data;cout<<“ 電話(huà)簿”< if(p!=NULL)Show_item(p);else } cout<<“不存在該信息”< } int main(){ int select;Phonebook phbook;CNT *p;p=Find();if(p!=NULL){ } cout<<“n”;cout<<“請(qǐng)輸入姓名:”;cin>>p->name;cout<<“請(qǐng)輸入手機(jī)號(hào)碼:”;cin>>p->phonenumber;cout<<“請(qǐng)輸入固定電話(huà):”;cin>>p->phone; phbook.Showmenu();while(1){ cout<<“n請(qǐng)輸入要選擇的操作(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; 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)二 鏈表的操作 (一)班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 1、掌握單鏈表結(jié)構(gòu)的實(shí)現(xiàn)方式; 2、掌握單鏈表常用算法的實(shí)現(xiàn)。 二、實(shí)驗(yàn)要求: 1、掌握鏈表的特點(diǎn)及常見(jiàn)算法。 2、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1、設(shè)計(jì)一個(gè)鏈表,要求編程實(shí)現(xiàn)如下任務(wù): (1)建立一個(gè)鏈表,首先依次輸人整數(shù)數(shù)據(jù)元素(個(gè)數(shù)根據(jù)需要鍵盤(pán)給定)。(2)刪除指定值的結(jié)點(diǎn)(指定值通過(guò)鍵盤(pán)輸入),再依次顯示刪除后的鏈表中的數(shù)據(jù)元素。 (3)查找指定值的結(jié)點(diǎn)(指定數(shù)據(jù)由鍵盤(pán)輸入),若找到則顯示查找成功,若沒(méi)有找到則顯示查找失敗。 (4)在第i個(gè)節(jié)點(diǎn)(i由鍵盤(pán)輸入,i=0表示插入的結(jié)點(diǎn)作為第1個(gè)結(jié)點(diǎn))之后插入一個(gè)元素為x的節(jié)點(diǎn)。 四、程序要求: 1、采用鏈表實(shí)現(xiàn),假設(shè)該鏈表的結(jié)點(diǎn)數(shù)在最壞情況下不會(huì)超過(guò)40個(gè)。 2、寫(xiě)出完整的程序并能調(diào)試運(yùn)行。 五、實(shí)驗(yàn)結(jié)果: 六、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 問(wèn)題:在查找數(shù)據(jù)時(shí)顯示的是一樣的? 解決方法:在查找函數(shù)前要標(biāo)明返回值的類(lèi)型lnode *find。 七、實(shí)驗(yàn)心得體會(huì): 首先要把書(shū)上的關(guān)于單鏈表的定義理解,在這個(gè)的基礎(chǔ)上使用它。單鏈表通過(guò)結(jié)點(diǎn)在數(shù)據(jù)后附加后繼元素的指針信息,可以更靈活的添加,刪除,查找,插入,元素。要清楚指針的指向。首先要定義一個(gè)頭結(jié)點(diǎn),并且指針域要為空。將各個(gè)操作分別編寫(xiě)函數(shù),有利于分塊使用而且可以多次使用。附: #include { cout<<“請(qǐng)輸入數(shù)組的個(gè)數(shù)(小于50):”;cin>>leng;cout<<“請(qǐng)輸入數(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ù)組中沒(méi)有你要查找的數(shù)”;} 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)三 鏈表的操作 (二)班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 1、熟悉利用線(xiàn)性鏈表解決問(wèn)題的一般思路; 2、參照給定的鏈表的程序樣例,驗(yàn)證給出的鏈表的常見(jiàn)算法,了解單鏈表結(jié)構(gòu)的優(yōu)點(diǎn)和不足。 二、實(shí)驗(yàn)要求: 1、熟練掌握鏈表的使用,并能運(yùn)用其解決些常規(guī)問(wèn)題。 2、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1、使用鏈表實(shí)現(xiàn)一個(gè)電話(huà)本的管理程序,電話(huà)本中的每條記錄包括姓名和電話(huà)兩項(xiàng)。要求實(shí)現(xiàn)菜單、初始化、添加、刪除和顯示等功能。 四、程序要求: 1、采用鏈表實(shí)現(xiàn),假設(shè)該鏈表的結(jié)點(diǎn)數(shù)在最壞情況下不會(huì)超過(guò)40個(gè)。 2、寫(xiě)出完整的程序并能調(diào)試運(yùn)行。 五、實(shí)驗(yàn)結(jié)果: 六、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 1.實(shí)驗(yàn)結(jié)果版面不好看 解決方法:反復(fù)調(diào)試執(zhí)行,每次都對(duì)自己不滿(mǎn)意的地方進(jìn)行一些改正。2.怎樣比較name與s->name? 解決方法:用string.h中的strcmp函數(shù)。3.在傳遞數(shù)據(jù)時(shí)有問(wèn)題? 解決方法:返回的值要注意數(shù)據(jù)類(lèi)型,是指針還是整數(shù)等。 七、實(shí)驗(yàn)心得體會(huì): 在應(yīng)用中可以利用while實(shí)現(xiàn)選擇,并根據(jù)選擇用switch來(lái)完成選擇。為了簡(jiǎn)化編程,結(jié)點(diǎn)插入操作可僅僅在頭部進(jìn)行。單鏈表的方法使添加操作更加簡(jiǎn)便。不需要事先分配結(jié)點(diǎn)空間,而是在需要時(shí)動(dòng)態(tài)的申請(qǐng),節(jié)省了空間。、附: #include void show_menu(){ puts(“1添加 2刪除 3查找 4插入 5顯示 0退出”);} void show_item(STU *p){ printf(“|編號(hào)| 學(xué)號(hào) | 姓名 | 電話(huà)號(hào)碼 |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(“|編號(hào)| 學(xué)號(hào) | 姓名 | 電話(huà)號(hào)碼 |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(“請(qǐng)輸入姓名:”);gets(temp->stu_name);printf(“請(qǐng)輸入學(xué)號(hào):”);gets(temp->stu_number);printf(“請(qǐng)輸入電話(huà)號(hào)碼:”);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(“輸入錯(cuò)誤”);else { for(p=head;p->next!=NULL&&i>0;i--,p=p->next); if(i>0) { puts(“不存在該結(jié)點(diǎn)”); 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(“輸入錯(cuò)誤”);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(“請(qǐng)選擇操作:”); scanf(“%d”,&selct); getchar(); switch(selct) { case 1: add(head); putchar('n'); break; case 2: display(head); printf(“請(qǐng)輸入要?jiǎng)h除學(xué)生的學(xué)號(hào)、姓名或電話(huà)號(hào)碼:”); } scanf(“%s”,data); getchar(); delet(head,data); display(head); putchar('n'); break;case 3: puts(“請(qǐng)輸入要查找的數(shù)據(jù)”); scanf(“%s”,data); getchar(); if(temp=find_stu(head,data)) { show_item(temp->next); } else puts(“ 不存在該信息”); putchar('n'); break;case 4: puts(“請(qǐng)輸入要插入結(jié)點(diǎn)的位置”); scanf(“%d”,&node); getchar(); insert(head,node); putchar('n'); break;case 5: display(head); putchar('n'); break;case 0: exit(0); break;} } return 0; 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)四 棧的操作 班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 掌握棧的的定義和運(yùn)算,了解棧的應(yīng)用。 二、實(shí)驗(yàn)要求: 1、掌握棧的特點(diǎn)及常見(jiàn)算法。 2、參照給定的棧的程序樣例,驗(yàn)證給出的棧的常見(jiàn)算法。 3、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1、堆棧的測(cè)試和應(yīng)用。要求: 設(shè)計(jì)一個(gè)主函數(shù)實(shí)現(xiàn)對(duì)順序堆棧代碼進(jìn)行測(cè)試。測(cè)試方法為:依次把數(shù)據(jù)元素1,3,5,7,9入棧,然后出棧堆棧中的數(shù)據(jù)元素并在屏幕上顯示。 四、程序要求: 1、棧的長(zhǎng)度自行確定。 2、重點(diǎn)理解棧的算法思想,能夠根據(jù)實(shí)際情況選擇合適的存儲(chǔ)結(jié)構(gòu)。 3、寫(xiě)出完整的程序并能調(diào)試通過(guò)。 五、實(shí)驗(yàn)結(jié)果: 六、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 七、實(shí)驗(yàn)心得體會(huì): 棧是后進(jìn)先出表,只允許在棧頂進(jìn)行插入和刪除。要掌握順序棧初始化,出棧,入棧的基本程序,在理解得基礎(chǔ)上加以運(yùn)用。要清楚棧和單鏈表的區(qū)別。附: #include void Sqstack::push(){ int temp;if(top>=maxsize-1){ cout<<“堆棧已滿(mǎn)”< cout<<“請(qǐng)輸入要進(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<<“請(qǐng)輸入棧的長(zhǎng)度”< maxsize=temp;else cout<<“輸入錯(cuò)誤棧的長(zhǎng)度必須>0”< int main(){ Sqstack test;int temp,select;cout<<“1.進(jìn)棧 2.出棧 3.顯示 4修改棧長(zhǎng)度 0.退出”< cout<<“請(qǐng)選擇操作:”; 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<<“輸入錯(cuò)誤按任意鍵重新輸入”< getch(); break; } } return 0;} 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)五 隊(duì)列的操作 班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 掌握隊(duì)列的定義及其運(yùn)算,了解隊(duì)列的應(yīng)用。 二、實(shí)驗(yàn)要求: 1、掌握隊(duì)列的特點(diǎn)及常見(jiàn)算法。 2、參照給定的隊(duì)列的程序樣例,驗(yàn)證給出的隊(duì)列的常見(jiàn)算法。 3、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1、隊(duì)列測(cè)試和應(yīng)用。要求: 設(shè)計(jì)一個(gè)主函數(shù)對(duì)循環(huán)隊(duì)列代碼進(jìn)行測(cè)試。測(cè)試方法為:依次把數(shù)據(jù)元素2,4,6,8,10入隊(duì),然后出隊(duì)中的數(shù)據(jù)元素并在屏幕上顯示。 四、程序要求: 1、隊(duì)列的長(zhǎng)度由自行確定。 2、重點(diǎn)理解隊(duì)列的算法思想,能夠根據(jù)實(shí)際情況選擇合適的存儲(chǔ)結(jié)構(gòu)。 3、寫(xiě)出完整的程序并能調(diào)試通過(guò)。 五、實(shí)驗(yàn)結(jié)果 六、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 七、實(shí)驗(yàn)心得體會(huì): 隊(duì)列是只允許在一端進(jìn)行刪除操作的線(xiàn)性表。在操作時(shí)會(huì)有假溢出的情況出現(xiàn),可以將存放隊(duì)列元素的數(shù)組守衛(wèi)相接,形成循環(huán)隊(duì)列。即: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<<“請(qǐng)輸入入隊(duì)元素的值:”< void Linksqueue::Dequeue(T *s){ T *temp;if(front->next!=NULL){ temp=front; front=front->next; *s=*front; cout<<“出隊(duì)元素的值:n”< delete temp;} else cout<<“隊(duì)列已空”< void Linksqueue::display(){ T *temp;if(front==rear) cout<<“空隊(duì)”< cout<<“隊(duì)列所有元素為 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入隊(duì) 2出隊(duì) 3顯示 0退出”< cout<<“請(qǐng)選擇操作:”; 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<<“輸入錯(cuò)誤按任意鍵重新輸入”< getch(); break;} } return 0; 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)六 二叉樹(shù)的生成與遍歷 班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 1、熟悉二叉樹(shù)節(jié)點(diǎn)的定義和生成方式; 2、熟悉二叉樹(shù)鏈?zhǔn)浇Y(jié)構(gòu)的生成方式; 3、掌握二叉樹(shù)遍歷算法的實(shí)現(xiàn)。 二、實(shí)驗(yàn)要求: 1、掌握二叉樹(shù)的常見(jiàn)算法。 2、參照給定的二叉樹(shù)的程序樣例,驗(yàn)證給出的有關(guān)二叉樹(shù)的常見(jiàn)算法,并實(shí)現(xiàn)有關(guān)的操作。 3、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1.設(shè)計(jì)實(shí)現(xiàn)二叉樹(shù)的建立及遍歷算法,要求: (1)編寫(xiě)創(chuàng)建二叉鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的二叉樹(shù)程序并輸出。(2)編寫(xiě)遞歸實(shí)現(xiàn)二叉樹(shù)的先序、中序、后序遍歷算法。 (3)編寫(xiě)主函數(shù)測(cè)試以上二叉樹(shù)的創(chuàng)建和遍歷函數(shù)。2.假設(shè)二叉樹(shù)采用鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)進(jìn)行存儲(chǔ),編寫(xiě)程序?qū)崿F(xiàn)二叉樹(shù)的所有葉子結(jié)點(diǎn)的統(tǒng)計(jì)并輸出統(tǒng)計(jì)個(gè)數(shù)。 四、實(shí)驗(yàn)結(jié)果: 五、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 調(diào)試的時(shí)候顯示沒(méi)有錯(cuò)誤但是程序運(yùn)行不出來(lái)? 在if語(yǔ)句中的相等要用“==”。要注意!! 六、實(shí)驗(yàn)心得體會(huì): 二叉樹(shù)的遍歷有先序,中序,后序遍歷。這三種遍歷都是用遞歸的形式實(shí)現(xiàn)的。而二叉樹(shù)的插入結(jié)點(diǎn)的算法與許多因素有關(guān),我們要在理解二叉樹(shù)結(jié)構(gòu)的基礎(chǔ)上根據(jù)需要來(lái)編寫(xiě)程序。我在實(shí)驗(yàn)中使用的二叉排序樹(shù)可以作為靜態(tài)查找表使用也可以作為動(dòng)態(tài)查找表。附: #include front=1;rear=0;printf(“請(qǐng)輸入二叉樹(shù)的各節(jié)點(diǎn),@表示虛節(jié)點(diǎn),#表示節(jié)點(diǎn):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é)點(diǎn)的個(gè)數(shù)為:%dn”,yzjd(root)); printf(“n”);} 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)七 查找算法實(shí)現(xiàn) 班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 掌握各種查找算法的特點(diǎn),測(cè)試并驗(yàn)證查找常見(jiàn)算法。 二、實(shí)驗(yàn)要求: 1、參照各種查找算法程序樣例,驗(yàn)證給出的查找常見(jiàn)算法。 2、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 1.建立有序表,采用折半查找實(shí)現(xiàn)某一已知的關(guān)鍵字的查找。 2.利用折半查找算法在一個(gè)有序表中插入一個(gè)元素,并保持表的有序性。 四、實(shí)驗(yàn)結(jié)果: 五、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 六、實(shí)驗(yàn)心得體會(huì): 本實(shí)驗(yàn)使用的是折半法查找,優(yōu)點(diǎn)是查找效率高,在編程的時(shí)候要設(shè)置low,high,mid,分別表示區(qū)間和中間位置的值。 附: #include }; Seqlist * init()int data[MAXSIZE];int length; { } void insert(Seqlist *p){ int num=0;printf(“請(qǐng)輸入要鍵入的個(gè)數(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(“請(qǐng)輸入數(shù)據(jù):n”);for(;num>0;num--){ } p->length++;scanf(“%d”,p->data+p->length-1);if(p->length==MAXSIZE){ } printf(“數(shù)組已滿(mǎn)n”);break;printf(“輸入個(gè)數(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(“請(qǐng)輸入要查找的數(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(“請(qǐng)輸入操作:”);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;} 實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)八 排序綜合實(shí)驗(yàn) 班 級(jí) 學(xué) 號(hào) 姓 名 第 周 星 期 節(jié) 成 績(jī) 一、實(shí)驗(yàn)?zāi)康模?/p> 參照各種排序算法程序樣例,驗(yàn)證給出的排序常見(jiàn)算法。 二、實(shí)驗(yàn)要求: 1、掌握各種排序算法的特點(diǎn),測(cè)試并驗(yàn)證排序的常見(jiàn)算法。 2、提交實(shí)驗(yàn)報(bào)告,報(bào)告內(nèi)容包括:目的、要求、算法描述、程序結(jié)構(gòu)、主要變量說(shuō)明、程序清單、調(diào)試情況、設(shè)計(jì)技巧、心得體會(huì)。 三、實(shí)驗(yàn)內(nèi)容: 輸入一組關(guān)鍵字序列分別實(shí)現(xiàn)下列排序,并將上述幾種排序的算法編寫(xiě)成菜單,根據(jù)輸入的數(shù)字不同執(zhí)行對(duì)應(yīng)的排序算法(任選兩種排序方法實(shí)現(xiàn))。 1、直接插入排序。 2、希爾排序。 3、冒泡排序。 4、直接選擇排序。 5、快速排序。 6、堆排序。 7、歸并排序。 8、基數(shù)排序。 四、實(shí)驗(yàn)結(jié)果: 五、實(shí)驗(yàn)中遇到的問(wèn)題及解決方法: 怎樣實(shí)現(xiàn)待排序數(shù)據(jù)長(zhǎng)度從鍵盤(pán)輸入? 解決方法:設(shè)置一個(gè)n,從鍵盤(pán)輸入n的值。 六、實(shí)驗(yàn)心得體會(huì): 排序的常用方法有直接插入排序,簡(jiǎn)單選擇排序,冒泡排序,快速排序。我在實(shí)驗(yàn)中用了冒泡排序和快速排序,冒泡排序的程序比較簡(jiǎn)單容易理解而快速排序則比較復(fù)雜??焖倥判蛞葎澐中蛄腥缓蠼⒃趧澐只A(chǔ)上進(jìn)行排序,這個(gè)排序是由遞歸實(shí)現(xiàn)的。但是快速排序的優(yōu)點(diǎn)是排序比較快。附: #include Seqlist * init(){ Seqlist *p;p=(Seqlist *)malloc(sizeof(Seqlist));p->length=0;return p;} void insert(Seqlist *p){ int num=0;printf(“請(qǐng)輸入要鍵入的個(gè)數(shù):n”);scanf(“%d”,&num);if(num<1) printf(“輸入個(gè)數(shù)有誤n”);else{ printf(“請(qǐng)輸入數(shù)據(jù):n”); for(;num>0;num--) { p->length++; scanf(“%d”,p->data+p->length-1); if(p->length==MAXSIZE) printf(“數(shù)組已滿(mǎn)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(“請(qǐng)輸入操作:”); 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;}