第一篇:程序設計實驗報告范文
軟件學院實驗卡和實驗報告
學 號:
姓 名:
專業(yè)班級:
實驗地點:
軟件學院實驗室 實驗日期:
2013 年 4-6 月
課程名稱 C++程序設計 B 實驗課時 16 實驗名稱 和序號 圖書管理系統(tǒng) CS0504401 同組者 姓
名
實驗目的 1、掌握面向對象程序設計的基本方法 2、運用 C++語法知識,完成圖書管理系統(tǒng) 實驗環(huán)境 PC 機、Visual Studio 2010 實驗內容 和原理 模擬現實圖書管理過程,設計實現圖書信息的管理,包含增加、刪除、修改、查詢和借還書等基本功能:
1、實現外部文件存取 2、合理設計類的層次 3、有相應的菜單界面 實驗步驟
方
法
關鍵代碼
((接上頁)
實驗步驟
方
法
關鍵代碼
測試記錄 分
析 結
論
小
結
以下由實驗教師填寫 成績評定
平時成績_______
實驗報告成績________
綜合成績 _________
指導教師簽名:
第二篇:C程序設計實驗報告
浙江理工大學信息學院
C程序設計實驗報告
實驗名稱: 學時安排:
實驗類別:
實驗要求:1人1組
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、實驗目的
二、實驗設備介紹
軟件需求: Visual C++ 6.0或CFree3.5以上版本
硬件需求: 對于硬件方面的要求,建議配置是Pentium III 450以上的CPU處理器,64MB以上的內存,200MB的自由硬盤空間、CD-ROM驅動器、能支持24位真彩色的顯示卡、彩色顯示器、打印機。
三、實驗內容
四、程序清單
五、運行結果
六、實驗心得
第三篇:C#程序設計實驗報告
實驗報告書寫要求
實驗報告原則上要求學生手寫,要求書寫工整。若因課程特點需打印的,標題采用四號黑體,正文采用小四號宋體,單倍行距。紙張一律采用A4的紙張。
實驗報告書寫說明
實驗報告中實驗目的和要求、實驗儀器和設備、實驗內容與過程、實驗結果與分析這四項內容為必需項。教師可根據學科特點和實驗具體要求增加項目。
填寫注意事項
(1)細致觀察,及時、準確、如實記錄。(2)準確說明,層次清晰。
(3)盡量采用專用術語來說明事物。
(4)外文、符號、公式要準確,應使用統(tǒng)一規(guī)定的名詞和符號。(5)應獨立完成實驗報告的書寫,嚴禁抄襲、復印,一經發(fā)現,以零分論處。
實驗報告批改說明
實驗報告的批改要及時、認真、仔細,一律用紅色筆批改。實驗報告的批改成績采用五級記分制或百分制,按《金陵科技學院課堂教學實施細則》中作業(yè)批閱成績評定要求執(zhí)行。
實驗報告裝訂要求
實驗批改完畢后,任課老師將每門課程的每個實驗項目的實驗報告以自然班為單位、按學號升序排列,裝訂成冊,并附上一份該門課程的實驗大綱。
金陵科技學院實驗報告
實驗項目名稱: C#基礎編程 實驗學時: 6 同組學生姓名: 實驗地點: 1318 實驗日期: 10月5日-10月19日 實驗成績: 批改教師: 批改時間:
金陵科技學院實驗報告
實驗1 C#基礎編程
一、實驗目的
1、熟悉Visual Studio.NET開發(fā)環(huán)境。
2、掌握C#應用程序的基本操作過程。
3、掌握C#的數據類型,運算符以及表達式的使用。
4、掌握分支和循環(huán)語句的使用方法。
5、掌握一維數組,二維數組及數組型數組的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機調試過程和結果要有記錄(2)做完實驗后給出本實驗的實驗報告。
三、實驗設備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意。
2、根據題目要求,新建項目。
3、編寫并輸入相關的程序代碼。
5、運行與調試項目。
6、保存項目。
五、實驗內容
1、編寫一個簡單的控制臺應用程序,打印一行文字(如你的姓名)。
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應用程序,在窗體Load事件中書寫代碼,標簽中顯示你的姓名。
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、編寫一個一個程序,用來判斷輸入的是大寫字母,小寫字母,數字還是其他的字符。
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(“這是一個數字”);
金陵科技學院實驗報告
}
} }
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的自然數之和為:” + 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的自然數的和為:” + 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的自然數的和為:” + sum);
}
} }
5、定義一個一維數組,用隨機數為此賦值,用foreach循環(huán)輸出其中的內容。
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、實現二維數組的輸入和輸出。
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、實現數組型數組的輸入和輸出。
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]);
}
}
}
} }
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
剛開始編程的時候覺得無從下手,盡管我們已經學了好幾種高級編程語言,但每個都
金陵科技學院實驗報告
有其獨特的地方,稍不留神就會混淆。
通過這次實驗,我體會到課后復習鞏固的重要性。在編程的時候,很多內容都不記得,需要去翻書。不得不說,實驗是鞏固課程的好方法!本次實驗,我熟悉Visual Studio.NET開發(fā)環(huán)境;掌握了C#應用程序的基本操作過程;掌握了C#的數據類型,運算符以及表達式的使用;掌握了分支和循環(huán)語句的使用方法以及一維數組,二維數組及數組型數組的使用。
金陵科技學院實驗報告
實驗項目名稱: 類與對象 實驗學時: 6 同組學生姓名: 實驗地點: 1318 實驗日期: 10月26日-11月9日 實驗成績: 批改教師: 批改時間:
金陵科技學院實驗報告
實驗2 類與對象
一、實驗目的、要求
(1)掌握類的定義和使用;
(2)掌握類的數據成員,屬性的定義和使用;
(3)掌握方法的定義,調用和重載以及方法參數的傳遞;(4)掌握構造函數的定義和使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機調試過程和結果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據題目要求,新建項目;
3、編寫并輸入相關的程序代碼;
5、運行與調試項目;
6、保存項目。
五、實驗內容
1、定義一個方法,實現兩個數的交換(分別把參數按值傳遞和按引用傳遞)。
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 后: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、定義一個方法,實現數組的排序。using System;using System.Collections.Generic;using System.Text;
namespace second.two {
class Program
{
string.Format(”
按
值
傳
參
交
換
之
金陵科技學院實驗報告
public class sort
{
public void change(int[] a)
{
Console.WriteLine(“排序前,數組順序為:”);
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);
金陵科技學院實驗報告
}
} }
5、用構造函數重載,實現矩形的面積,圓的面積,梯形的面積;
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、設計一個windows應用程序,在該程序中定義一個學生類和班級類,以處理每個學生的學號,姓名,語文,數學和英語成績,要求:
1)能查詢每個學生的總成績。2)能顯示全班前三名的名單。
3)能顯示單科成績最高分和不及格的學生名單。4)能統(tǒng)計全班學生的平均成績。
5)能顯示各科成績不同分數段的學生人數的百分比。
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;
}
//給所有成績排序,用后面實現前三名的排名
金陵科技學院實驗報告
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 += “單科數學最高:” + stu[HighScore(0)].name + “n”;
Maxer += “ 單科語文最高:” + stu[HighScore(1)].name + “n”;
Maxer += “ 單科英語最高:” + stu[HighScore(2)].name + “n”;
Loser += “單科數學掛科名單:” +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;
}
//各科成績不同分數段的學生百分比
//英語成績各分數段百分比
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+“ 60以下的:”+per5;
}
//數學成績各分數段百分比
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(“數學成績百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);
}
//英語成績各分數段百分比
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(“數學成績百分比:” + “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;
金陵科技學院實驗報告
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”;
}
} }
六、實驗體會(遇到問題及解決辦法,編程后的心得體會)
通過本次實驗,我掌握了類的定義與使用;掌握了類的數據成員,屬性的定義和使用;掌握了方法的定義,調用和重載以及方法參數的傳遞以及構造函數的定義和使用。值得注意的是:本次實驗中return的使用以及所在的位置,類型轉換時也經常用到
金陵科技學院實驗報告
實驗項目名稱: 繼承與多態(tài) 實驗學時: 6 同組學生姓名: 實驗地點: 1318 實驗日期: 11月16日-11月30日 實驗成績: 批改教師: 批改時間:
金陵科技學院實驗報告
實驗3 繼承與多態(tài)
一、實驗目的、要求
(1)掌握類的繼承性與多態(tài)性;
(2)掌握虛方法的定義以及如何使用虛方法實現多態(tài);(3)掌握抽象類的定義以及如何使用抽象方法實現多態(tài);
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機調試過程和結果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據題目要求,新建項目;
3、編寫并輸入相關的程序代碼;
5、運行與調試項目;
6、保存項目。
五、實驗內容
1、設計一個Windows應用程序,在該程序中首先構造一個學生基本類,再分別構造小學生、中學生、大學生派生類,當輸入相關數據,單擊不用的按鈕時,將分別創(chuàng)建不同的學生類對象,并輸出當前學生的總人數,該學生的姓名,學生類型,平均成績。
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窗體內的代碼:
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),Convert.ToDouble(txtMath.Text));
lblShow.Text += “總人數:” +Convert.ToString(Student.number)+ “,” + “姓名:” + p.Name + “,” + “小學生” + “,” + “平均成績?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 += “總人數:” + Convert.ToString(Student.number)+ “,” + “姓名:” + m.Name + “,” + “中學生” + “,” + “平均成績?yōu)椋骸?+ m.Average()+ “n”;
}
private void btnBig_Click(object sender, EventArgs e)
{
College Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtMath.Text));
lblShow.Text += “總人數:” + Convert.ToString(Student.number)+ “,” + “姓名:” + c.Name + “,” + “大學生” + “,” + “平均成績?yōu)椋骸?+ c.Average()+ “n”;
}
}
c
=
new
College(txtName.Text,Convert.ToDouble(txtChinese.Text),金陵科技學院實驗報告
}
2、設計一個Windows應用程序,在該程序中定義平面圖形抽象類和派生類圓,矩形和三角形。
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學生開始上課,教師開始講課。分別用new關鍵字,虛方法,抽象類實現多態(tài)性。
New關鍵字: using System;using System.Collections.Generic;using System.Text;
namespace third.three {
class Program
{
static void Main(string[] args)
金陵科技學院實驗報告
{
Student s=new Student(“學生”);
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(“張三”,“學生”);
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(“張三”, “學生”);
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)性;掌握了虛方法的定義以及如何用虛方法來實現多態(tài);掌握了抽象類的定義以及如何用抽象方法來實現多態(tài)。這次實驗與前兩次不同,采用Windows應用程序,既涉及到代碼段也涉及到界面的設計。所以,勉強通過實驗。
金陵科技學院實驗報告
實驗項目名稱: 接口、文件和流 實驗學時: 6 同組學生姓名: 實驗地點: A205 實驗日期: 12月7日-12月21日 實驗成績: 批改教師: 批改時間:
金陵科技學院實驗報告
實驗4 接口、文件和流
一、實驗目的
(1)掌握接口的定義及使用方法;
(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的讀寫操作類及其使用方法;
(4)掌握OpenFileDialog,SaveFileDialog等控件的使用。
二、實驗要求
(1)編寫程序要規(guī)范、正確,上機調試過程和結果要有記錄;(2)做完實驗后給出本實驗的實驗報告。
三、實驗設備、環(huán)境
安裝有Visual Studio.NET軟件。
四、實驗步驟
1、分析題意;
2、根據題目要求,新建項目;
3、編寫并輸入相關的程序代碼;
5、運行與調試項目;
6、保存項目。
五、實驗內容
1、定義一個Person類,包含姓名字段和一個方法,早上8:30學生開始上課,教師開始講課。用接口來實現。
using System;using System.Collections.Generic;using System.Text;namespace Test4_1 {
class Program
{
static void Main(string[] args)
{
Student s = new Student(“張三”,“學生”);
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);
}
}
public class Teacher :Person, IPerson
{
public string type
{
金陵科技學院實驗報告
get { return string.Format(“學生”);}
}
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播放器類,以實現該接口。
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()
金陵科技學院實驗報告
{
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歌曲!”;
}
金陵科技學院實驗報告
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;
}
金陵科技學院實驗報告
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、實現對文本文件的讀寫操作,用文件操作控件打開和保存文件。
using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace Test4_3
金陵科技學院實驗報告
{
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、實現對二進制文件的讀寫操作。
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
金陵科技學院實驗報告
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(“學號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
金陵科技學院實驗報告
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、實現對象序列化和反序化。
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;
=
金陵科技學院實驗報告
}
} } 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;
金陵科技學院實驗報告
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程序設計》實驗報告格式
中國計量學院 實 驗 報 告
實驗課程: C語言程序設計(2)實驗名稱: 綜合實驗 班 級: XXXXX 學 號: XXXXXX 姓 名: XX 實驗日期: 2011/10/13
一、實驗目的
1.熟練掌握用戶自定義函數的定義及調用 2.掌握函數間的參數傳遞 3.掌握函數的嵌套調用及遞歸調用 4.掌握文件的基本操作
二、實驗內容
1.編寫程序:定義函數sum(n)計算整數n的各位數之和,函數類型是int。主函數輸入整數n,將n的各個位數之和寫入到design1.dat文件中,要求調用自定義函數sum。
2.定義函數fact(n)計算n的階乘,函數返回值類型是double;定義函數cal(x,n)計算xn/n!,函數返回值類型是double,要求調用fact函數計算n!;定義函數main(),輸入正整數n和實數x,計算并將下列算式的值寫入到design2.dat文件中。要求
xx2x3xn?????調用函數cal(x,n)計算每一項。y?1??
1!2!3!n!
實驗成績: 指導教師簽名:
三、算法分析及源程序
1.(1)算法分析:定義變量、數組等……
從鍵盤輸入……
……(具體的算法描述)
輸出……
(2)源代碼:#include
(2)源代碼:#include
四、實驗數據分析及結論 1.上機調試,輸入……
運行結果: ……
調試結果分析:運行結果符合要求?;蛘哌\行結果有錯,分析錯誤原因。2.上機調試,輸入……
運行結果: ……
調試結果分析:運行結果符合要求?;蛘哌\行結果有錯,分析錯誤原因。
第五篇:《C++程序設計》實驗報告范例
注意:以下藍色文字部分根據情況增刪
實驗1熟悉VC++6.0集成開發(fā)環(huán)境
一、實驗時間:
2011年9月20日
二、實驗地點:
第7機房
三、指導教師:
鄧譜
四、實驗目的:
1.了解VC++6.0的主要特點;
2.熟悉VC++6.0的集成開發(fā)環(huán)境;
3.學習用VC++6.0編寫簡單的C++應用程序,掌握C++程序的上機調試步驟。
五、實驗內容:
1.P19 第1題:在屏幕上輸出字符串“歡迎大家學習VC++6.0!”
? 【編程思想】
? 【流程圖】
? 【源程序】見上機作業(yè)“實驗11_11_1.cpp”
問題1:不知道如何在VC++6.0里輸入源程序
解決方法:
(1)首先建立“Win32 Console Application”工程;
(2)再向工程中添加源文件;
(3)在添加的源文件中輸入代碼。
以上的詳細操作流程見“Visual C++ 6.0集成開發(fā)環(huán)境.doc”文件。
問題2:error LNK2001: unresolved external symbol _WinMain@16:連接錯誤2001:未解決的外部符號_WinMain@16
問題原因:創(chuàng)建工程時選的創(chuàng)建“Win32 Application”,這是創(chuàng)建圖形用戶界面的C++工程,而程序是字符界面,兩者不一致導致該錯誤。
解決方法:創(chuàng)建工程時應選“Win32 Console Application”
問題3:error C1010: unexpected end of file while looking for precompiled header directive:錯誤C1010:在查找預編譯頭時遇到意外的文件結尾。
問題原因:在選擇創(chuàng)建工程種類時選擇了“a simple application”,該工程還包含stdafx..h和stdafx..cpp兩個文件,這兩個文件是為編譯系統(tǒng)進行預編譯服務的,需要在源程序開始部分使用#include “stdafx.h”進行引用,如果未進行引用,會導致該錯誤。
解決方法1:在選擇創(chuàng)建工程種類時選擇第一項“an empty project”
解決方法2:如果工程文件列表中(窗口左邊)有stdafx..h和stdafx..cpp兩個文件,則源程序開始要使用#include “stdafx.h”進行引用。
問題4:error C2143: syntax error : missing ';' before '}':錯誤C2143:語法錯誤:在'}'之前丟失了';'
問題5:……
2.P19 第2題:求兩個整數之和
……
六、實驗總結:
(將實驗過程中或學習本章節(jié)的上機過程中值得記錄的內容按1.2.3.分條寫下來)