




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)1-1 過(guò)程化編程【實(shí)驗(yàn)?zāi)康摹坷斫?、掌握過(guò)程化編程程序設(shè)計(jì)思想?!緦?shí)驗(yàn)內(nèi)容】1. 程序填空,練習(xí)類(lèi)、對(duì)象、繼承的定義和實(shí)現(xiàn)方法。2. 根據(jù)程序運(yùn)行結(jié)果,補(bǔ)充完整程序?!緦?shí)驗(yàn)要求】我們?cè)谶M(jìn)行英語(yǔ)閱讀的時(shí)候,會(huì)發(fā)現(xiàn)一個(gè)有趣的現(xiàn)象:有些字串是左右對(duì)稱的,如madam。我們把這種字串稱為symmetry text 即“對(duì)稱文”。現(xiàn)在有若干航字串,每一行可以由數(shù)字、標(biāo)點(diǎn)符號(hào)、空格符以及英文字符(包括大小寫(xiě))組成。要你幫忙編程判斷是否是對(duì)稱文,否則,就不能最大限度地發(fā)現(xiàn)有趣現(xiàn)象了。輸入說(shuō)明每個(gè)字串為一行,每行結(jié)束以回車(chē)符為標(biāo)志,可能有上百上千行業(yè)說(shuō)不定。當(dāng)字串為“000000”時(shí),輸入結(jié)束。英文字符不
2、區(qū)分大小寫(xiě),即Madam亦為對(duì)稱文。不要忘了“”也是互為對(duì)稱的。輸出說(shuō)明如果是對(duì)稱文,則輸出“Symmetry”,否則輸出“Not symmetry”。每個(gè)結(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;【運(yùn)行結(jié)果】圖 3 實(shí)驗(yàn)一運(yùn)行結(jié)果實(shí)驗(yàn)1-2 面向?qū)ο缶幊碳夹g(shù)(1)【實(shí)驗(yàn)?zāi)康摹坷斫饷嫦驅(qū)ο蟮牡某绦蛟O(shè)計(jì)思想?!緦?shí)驗(yàn)內(nèi)容】定義一個(gè)時(shí)間類(lèi)Time,能提供和設(shè)置由時(shí)、分、秒組成的時(shí)間,并編出應(yīng)用程序,要求包括定義時(shí)間對(duì)象,設(shè)置時(shí)間,輸出該對(duì)象提供的時(shí)間。并請(qǐng)將類(lèi)定義作為界面,用多文件結(jié)構(gòu)實(shí)現(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();【運(yùn)行結(jié)果】圖 4
8、 實(shí)驗(yàn)二運(yùn)行結(jié)果實(shí)驗(yàn)3 面向?qū)ο蟪绦蛟O(shè)計(jì)(2)【實(shí)驗(yàn)要求】改寫(xiě)程序f0815.cpp,使之含有構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù)。并對(duì)主函數(shù)和矩陣向量的乘法也進(jìn)行改寫(xiě)。對(duì)于第91和92行,合并成“multiply(ve,ma).display();”使之不會(huì)產(chǎn)生內(nèi)存泄漏?!緦?shí)驗(yàn)程序】/實(shí)驗(yàn)三/改寫(xiě)f0815.cpp#include#include#includeusing namespace std;class Vectorint* v;/指向一個(gè)數(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)建一個(gè)存放結(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();【實(shí)驗(yàn)結(jié)果】圖 5 實(shí)驗(yàn)三運(yùn)行結(jié)果實(shí)驗(yàn)4 面向?qū)ο蟪绦蛟O(shè)計(jì)(3)【實(shí)驗(yàn)要求】請(qǐng)?jiān)诔绦騠0904.cpp中的日期類(lèi)的基礎(chǔ)上,實(shí)現(xiàn)一個(gè)可以進(jìn)行加天數(shù)操作獲得另一個(gè)日期,以及進(jìn)行日期減日期操作獲得相隔天數(shù)的日期類(lèi),并進(jìn)行應(yīng)用程序設(shè)計(jì):創(chuàng)建2015.8.21和2
14、008.8.8兩個(gè)日期,并計(jì)算中間相隔的天數(shù),前者加上300天會(huì)是什么日子呢?【實(shí)驗(yàn)程序】一、頭文件部分#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é)果】實(shí)驗(yàn)5 面向?qū)ο蟪绦蛟O(shè)計(jì)(4)【實(shí)驗(yàn)要求】在上題Date類(lèi)的基礎(chǔ)上,繼承一個(gè)WDate類(lèi),它包含了星期幾信息
21、,因而,顯示日期的成員要做修改,應(yīng)同時(shí)顯示星期幾。另外,還要增加獲得星期幾的成員。想一想,類(lèi)中數(shù)據(jù)成員置年、月、日好呢,還是絕對(duì)天數(shù)好呢?進(jìn)而進(jìn)行應(yīng)用程序設(shè)計(jì):創(chuàng)建2005.8.21和2008.8.8兩個(gè)日期,分別顯示這兩個(gè)日期?!緦?shí)驗(yàn)程序】一、頭文件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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 餐飲廚師營(yíng)養(yǎng)與健康指導(dǎo)合同
- 跨國(guó)公司員工派遣勞動(dòng)合同書(shū)
- 安全生產(chǎn)責(zé)任制基本原則
- 安全生產(chǎn)責(zé)任制(化工企業(yè))
- 高空作業(yè)安全方案及應(yīng)急預(yù)案
- 2025至2030設(shè)施管理服務(wù)行業(yè)發(fā)展趨勢(shì)分析與未來(lái)投資戰(zhàn)略咨詢研究報(bào)告
- 新質(zhì)生產(chǎn)力對(duì)糧食生產(chǎn)安全的影響機(jī)制
- 2025至2030中國(guó)汽車(chē)租賃行業(yè)深度發(fā)展研究與企業(yè)投資戰(zhàn)略規(guī)劃報(bào)告
- 熟食創(chuàng)意活動(dòng)方案
- 燎原超市活動(dòng)策劃方案
- 建筑裝飾設(shè)計(jì)收費(fèi)標(biāo)準(zhǔn)(完整版)資料
- 柔性防護(hù)網(wǎng)施工方案
- 網(wǎng)絡(luò)安全論文參考文獻(xiàn),參考文獻(xiàn)
- GB/T 9867-2008硫化橡膠或熱塑性橡膠耐磨性能的測(cè)定(旋轉(zhuǎn)輥筒式磨耗機(jī)法)
- 2023年初高中數(shù)學(xué)銜接知識(shí)點(diǎn)及習(xí)題
- ??低?視頻監(jiān)控原理培訓(xùn)
- 體育原理課件
- 教科版科學(xué)五年級(jí)下冊(cè)期末試卷測(cè)試卷(含答案解析)
- 不良事件報(bào)告與防范
- 【吉爾吉斯和國(guó)經(jīng)商指南-法律篇】
- 百家麗-中國(guó)-照明電器有限公司的精益生產(chǎn)應(yīng)用
評(píng)論
0/150
提交評(píng)論