C++實驗報告參考模板_第1頁
C++實驗報告參考模板_第2頁
C++實驗報告參考模板_第3頁
C++實驗報告參考模板_第4頁
C++實驗報告參考模板_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗1-1 過程化編程【實驗?zāi)康摹坷斫?、掌握過程化編程程序設(shè)計思想。【實驗內(nèi)容】1. 程序填空,練習(xí)類、對象、繼承的定義和實現(xiàn)方法。2. 根據(jù)程序運行結(jié)果,補充完整程序?!緦嶒炓蟆课覀冊谶M(jìn)行英語閱讀的時候,會發(fā)現(xiàn)一個有趣的現(xiàn)象:有些字串是左右對稱的,如madam。我們把這種字串稱為symmetry text 即“對稱文”。現(xiàn)在有若干航字串,每一行可以由數(shù)字、標(biāo)點符號、空格符以及英文字符(包括大小寫)組成。要你幫忙編程判斷是否是對稱文,否則,就不能最大限度地發(fā)現(xiàn)有趣現(xiàn)象了。輸入說明每個字串為一行,每行結(jié)束以回車符為標(biāo)志,可能有上百上千行業(yè)說不定。當(dāng)字串為“000000”時,輸入結(jié)束。英文字符不

2、區(qū)分大小寫,即Madam亦為對稱文。不要忘了“”也是互為對稱的。輸出說明如果是對稱文,則輸出“Symmetry”,否則輸出“Not symmetry”。每個結(jié)論占一行。圖 1 symmetry.in1 / 20圖 2 symmetry.out【程序代碼】#include#includeusing namespace std;bool isMatch(string s);int main() string s;while (1) cin s;if (pare(000000) = 0) break;if (isMatch(s) cout Symmetry endl;else cout Not sy

3、mmetry endl;return 0;bool isMatch(string s) int len = s.length();for (int i = 0; i= a&si = A&slen - i - 1 = A&si = a&slen - i - 1 = z) if (si != (slen - i - 1 - (a - A) return false;else if (si = &slen - i - 1 = ) continue;else if (si = ) continue;else if (si = &slen - i - 1 = ) continue;else if (si

4、 = (&slen - i - 1 = ) continue;else if (si != slen - i - 1) return false;return true;【運行結(jié)果】圖 3 實驗一運行結(jié)果實驗1-2 面向?qū)ο缶幊碳夹g(shù)(1)【實驗?zāi)康摹坷斫饷嫦驅(qū)ο蟮牡某绦蛟O(shè)計思想?!緦嶒瀮?nèi)容】定義一個時間類Time,能提供和設(shè)置由時、分、秒組成的時間,并編出應(yīng)用程序,要求包括定義時間對象,設(shè)置時間,輸出該對象提供的時間。并請將類定義作為界面,用多文件結(jié)構(gòu)實現(xiàn)之?!境绦虼a】/Time.h#includeclass Timepublic:int h;int m;int s;void inputT(

5、);void changeT();void outputT(); /Time.cpp#include Time.h#includevoid Time:inputT()begin:int a, b, c;std:cout a b c;if (c 60 | c 0)std:cout 60 | b 0)std:cout 24 | a 0)std:cout Wrong time!Please set again!n;goto begin;else if (a = 24)if (b!=0 | c!=0)std:cout Wrong time!Please set again!n;goto begin;e

6、lseh = a;m = b;s = c;elseh = a;m = b;s = c;void Time:changeT()char p;std:cout p;if (p = n | p = N)std:cout Thank you for your using!n ;elsebegin1:int a, b, c;std:cout a b c;if (c 60 | c 0)std:cout 60 | b 0)std:cout 24 | a 0)std:cout Wrong time!Please set again!n ;goto begin1;else if (a = 24)if (b !=

7、 0 | c != 0)std:cout Wrong time!Please set again!n;goto begin1;else h = a;m = b;s = c;elseh = a;m = b;s = c;void Time:outputT()std:cout Output time(H:M:S)n h : m : s;/testmain.cpp#include Time.h#includevoid main(void)Time time1;time1.inputT();time1.outputT();time1.changeT();time1.outputT();【運行結(jié)果】圖 4

8、 實驗二運行結(jié)果實驗3 面向?qū)ο蟪绦蛟O(shè)計(2)【實驗要求】改寫程序f0815.cpp,使之含有構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù)。并對主函數(shù)和矩陣向量的乘法也進(jìn)行改寫。對于第91和92行,合并成“multiply(ve,ma).display();”使之不會產(chǎn)生內(nèi)存泄漏?!緦嶒灣绦颉?實驗三/改寫f0815.cpp#include#include#includeusing namespace std;class Vectorint* v;/指向一個數(shù)組,表示向量int sz;public:int size() return sz; Vector(int);Vector(const Vector&

9、 s);int& operator(int);void display();Vector();Vector:Vector(int s)sz = s;if (s = 0)cerr bad Vector size.n;exit(1);v = new ints;Vector:Vector(const Vector& s)int i;sz = s.sz;v = new intsz;for (i = 0; isz; i+)vi = s.vi;Vector:Vector()delete v;int& Vector:operator(int i)/引用返回的目的是返回值可以做左值if (i= sz)cerr

10、 Vector index out of rang.n;exit(1);return vi;void Vector:display()int i;for (i = 0; isz; +i)cout vi ;cout n;class Matrixint* m;int szl, szr;public:Matrix(int, int);Matrix(const Matrix& m);Matrix();int sizeL() return szl; int sizeR() return szr; int& elem(int, int);Matrix:Matrix(int i, int j)szl = i

11、; szr = j;if (i = 0 | j = 0)cerr bad Matrix size.n;exit(1);m = new inti*j;Matrix:Matrix(const Matrix& s)int i, j;szl = s.szl;szr = s.szr;m = new intszl*szr;for (i = 0; iszl; i+)for (j = 0; jszr; j+)mi*szr + j = s.mi*szr + j;Matrix:Matrix()delete m;int& Matrix:elem(int i, int j)/引用返回值的目的是可以做左值if (i0

12、| szl = i | j0 | szr = j)cerr Matrix index out of range.n;exit(1);return mi*szr + j;Vector multiply(Matrix& m, Vector& v)/矩陣乘向量int i, j;Matrix me(m);Vector va(v);if (m.sizeR() != v.size()cerr bad multiply Matrix with vector.n;exit(1);Vector r(m.sizeL();/創(chuàng)建一個存放結(jié)果的空向量for (i = 0; ime.sizeL(); i+)ri = 0

13、;for (j = 0; j x y;Matrix ma(x, y);for (i = 0; ix; +i)for (j = 0; j ma.elem(i, j);in x;Vector ve(x);for (i = 0; i vei;Matrix me(ma);Vector va(ve);multiply(ma, ve).display();【實驗結(jié)果】圖 5 實驗三運行結(jié)果實驗4 面向?qū)ο蟪绦蛟O(shè)計(3)【實驗要求】請在程序f0904.cpp中的日期類的基礎(chǔ)上,實現(xiàn)一個可以進(jìn)行加天數(shù)操作獲得另一個日期,以及進(jìn)行日期減日期操作獲得相隔天數(shù)的日期類,并進(jìn)行應(yīng)用程序設(shè)計:創(chuàng)建2015.8.21和2

14、008.8.8兩個日期,并計算中間相隔的天數(shù),前者加上300天會是什么日子呢?【實驗程序】一、頭文件部分#pragma once/class Date with year-month-day version#include#includeusing namespace std;class Date int year, month, day;Date(int n = 1) i2ymd(n);int ymd2i()const;void i2ymd(int n);static const int tians;public:Date(const string& s);Date(int y,int m,

15、int d):year(y),month(m),day(d) Date operator+ (int n)const return Date(ymd2i() + n);Date& operator+=(int n) i2ymd(ymd2i() + n);return *this;Date& operator+() return *this += 1;int operator-(Date& d)const return ymd2i() - d.ymd2i();bool isLeapYear()const return!(year % 4) & (year % 100) | !(year % 40

16、0);friend ostream& operator(ostream& o, const Date& d);二、函數(shù)定義/Date.cpp/Class Date with year-month-day Version#includeDate.h#include#include#includeusing namespace std;const int Date:tians = 0,31,59,90,120,151,181,212,243,273,304,334 ;const int Y400 = 146097;/number of days of 400 yearsconst int Y100

17、 = 36524;/number of days of 100 yearsconst int Y4 = 1461;/number of days of 4 yearsDate:Date(const string& s) year = atoi(s.substr(0, 4).c_str();month = atoi(s.substr(5, 2).c_str();day = atoi(s.substr(8, 2).c_str();void Date:i2ymd(int absDay) year = absDay / Y400 * 400;int y = absDay%Y400;/被400年除得的天

18、數(shù)if (y = Y400 - 1) month = 12, day = 30;return;year += y / Y100 * 100;y %= Y100;year += y / Y4 * 4;y%=Y4;if (y = Y4 - 1) month = 12, day = 30;return;year += y / 365;y %= 365;if (y = 0) month = 12, day = 31;return;year+;bool leap = isLeapYear();for (month = 1; monthtiansmonth + (month = 2 & leap); mo

19、nth+);day = y - tiansmonth - 1;int Date:ymd2i()const int yearDay = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;return yearDay + tiansmonth - 1 + (isLeapYear() & month 2) + day;ostream& operator(ostream& o, const Date& d) return o setfill(0) setw(4) d.year - setw(2) d.mont

20、h - setw(2) d.day setfill( );三、testmain/testmain.cpp/using Date class#includeDate.h#includeusing namespace std;int main() Date d1(2005, 8, 21);Date d2(2008, 8, 8);cout 2005.8.21與2008.8,8中間相隔的天數(shù)是: d2 - d1 n;cout 2005.8.21加上300天是: d1 + 300 n;【程序結(jié)果】實驗5 面向?qū)ο蟪绦蛟O(shè)計(4)【實驗要求】在上題Date類的基礎(chǔ)上,繼承一個WDate類,它包含了星期幾信息

21、,因而,顯示日期的成員要做修改,應(yīng)同時顯示星期幾。另外,還要增加獲得星期幾的成員。想一想,類中數(shù)據(jù)成員置年、月、日好呢,還是絕對天數(shù)好呢?進(jìn)而進(jìn)行應(yīng)用程序設(shè)計:創(chuàng)建2005.8.21和2008.8.8兩個日期,分別顯示這兩個日期。【實驗程序】一、頭文件Date.h#pragma once/Date.h/class Date with year-month-day version#include#includeusing namespace std;class Date int year, month, day;static const int tians;protected:Date(int

22、n = 1) i2ymd(n);int ymd2i( )const;void i2ymd(int n);public:Date(const string& s );Date(int y, int m, int d) :year(y), month(m), day(d) Date operator+(int n)const return Date(ymd2i() + n);Date& operator+=(int n) i2ymd(ymd2i() + n);return *this;Date& operator+() return *this += 1;int operator-(Date& d

23、)const return ymd2i() - d.ymd2i();bool isLeapYear()const return !(year % 4) & (year % 100) | !(year % 400);friend ostream& operator(ostream& o, const Date& d);二、頭文件WDate.h#pragma once/WDate.h#includeDate.h#include#includeusing namespace std;class WDate :public Date protected:WDate(int n = 1) :Date(n

24、) WDate(const Date& d) :Date(d) public:WDate(const string& s) :Date(s) WDate(int y, int m, int d) :Date(y, m, d) WDate operator+(int n) const return Date:operator+(n); WDate& operator+=(int n) Date:operator+=(n); return *this; WDate& operator+() return *this += 1;int getWeekDay() return ymd2i() % 7;

25、 /0:Sunday,1:Monday, operator-(WDate& wd)const return ymd2i() - wd.ymd2i(); friend ostream& operator(ostream& o, const WDate& wd);三、Date.cpp/Date.cpp/Class Date with year-month-day Version#includeDate.h#include#include#includeusing namespace std;const int Date:tians = 0,31,59,90,120,151,181,2

26、12,243,273,304,334 ;const int Y400 = 146097;/number of days of 400 yearsconst int Y100 = 36524;/number of days of 100 yearsconst int Y4 = 1461;/number of days of 4 yearsDate:Date(const string& s) year = atoi(s.substr(0, 4).c_str();month = atoi(s.substr(5, 2).c_str();day = atoi(s.substr(8, 2).c_str()

27、;void Date:i2ymd(int absDay) year = absDay / Y400 * 400;int y = absDay%Y400;/被400年除得的天數(shù)if (y = Y400 - 1) month = 12, day = 30;return;year += y / Y100 * 100;y %= Y100;year += y / Y4 * 4;y %= Y4;if (y = Y4 - 1) month = 12, day = 30;return;year += y / 365;y %= 365;if (y = 0) month = 12, day = 31;return

28、;year+;bool leap = isLeapYear();for (month = 1; monthtiansmonth + (month = 2 & leap); month+);day = y - tiansmonth - 1;int Date:ymd2i()const int yearDay = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;return yearDay + tiansmonth - 1 + (isLeapYear() & month 2) + day;ostream& operator(ostream& o, const Date& d) return o setfill(0) setw(4) d.year - setw(2) d.month - setw(2) d.day setfill(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論