C--实验报告剖析_第1页
C--实验报告剖析_第2页
C--实验报告剖析_第3页
C--实验报告剖析_第4页
C--实验报告剖析_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验1-1过程化编程【实验目的】理解、掌握过程化编程程序设计思想。【实验内容】1. 程序填空,练习类、对象、继承的定义和实现方法。2. 根据程序运行结果,补充完整程序。【实验要求】我们在进行英语阅读的时候,会发现一个有趣的现象:有些字串是左右对称的,如madam我们把这种字串称为symmetry text即“对称文”。现在有若干航字串,每一行可以由数字、标点符号、空格符以及英文字符(包括大小写)组成。要你帮忙编程判断是否是对称文,否则,就不能最大限 度地发现有趣现象了。输入说明每个字串为一行,每行结束以回车符为标志,可能有上百上千行业说不定。 当字串为“ 000000时,输入结束。英文字符不区

2、分大小写,即 Madams为对称文。不要忘了 “(”与“ ) 也是互为对称的。输出说明如果是对称文,则输出“ Symmetry”,否则输出“ Not symmetry”。每个结 论占一行。Maiain*-1-madarit- kkghkkhgOOOOOM图 1 symmetry.i n: cht)6ezv_07 STnmttipSynfimetn-Not syninietn-,图 2 symmetry.out【程序代码】#inelude #inelude using namespaee std;bool isMatch( strings);int main() stri ng s;while

3、(1) cin s;if (pare( 000000) = 0) break;if (isMatch(s) cout Symmetry endl;else cout Not symmetry endl;return 0;bool isMatch( strings) int len = s.len gth();for (int i = 0; i= a & i = A &s len - i - 1 = A & i = a & len - i - 1=z ) if (si != (s len - i - 1 - ( a- A )return false ;elseif(si=&slen -

4、i -1=)continue ;elseif(si=)continue ;elseif(si=&j len -i -1=T)continue ;elseif(si=(&j len -i -1=)continue ;elseif(si !=s len-i- 1 ) return falsereturn true【运行结果】E;vjsual stdioI趕XProjectiDebugPfojecti exe 口 1Ulifinc try vn4Hi(flanling 121 yn11 Not; s yntne c rIkicyliikkhyHot 5 yrnfietrV貝财拼音输人迭全2w图3实验

5、一运行结果【实验目的】理解面向对象的的程序设计思想。【实验内容】定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应 用程序,要求包括定义时间对象,设置时间,输出该对象提供的时间。并请 类定义作为界面,用多文件结构实现之。【程序代码】/Time.h#include class Timepublic :int h;int m;int s;void inputT();void changeT();void outputT();/Time.cpp#include Time.h#include void Time:inputT()begin:int a, b, c;std:cout a

6、 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;elseh = 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 T

7、hank you for your using!nelsebegin1: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 != 0 | c != 0)std:cout Wrong time!Please set again!n goto begin1; else h = a; m = b; s = c;else h = a; m = b; s =

8、 c;void Time:outputT()std:cout Output time(H:M:S)n h : m : s; /testmai n.cpp#in clude Time.h#include void main(void )Time time1;time1.i nputT();time1.outputT();time1.cha ngeT();time1.outputT();【运行结果】uus亡t$Projcc15Dpb gInput iIK 1H 1Output bind CH101M :lDo if AUthAD 时 tVJnfiit t jikeIQ 1 i粘Urcng昭匕 se

9、t again*Input clrneM :N:&图4实验二运行结果实验 3 面向对象 程序设计( 2)【实验要求】改写程序 f0815.cpp ,使之含有构造函数,拷贝构造函数和析构函数。并 对主函数和矩阵向量的乘法也进行改写。对于第91和 92行,合并成“multiply(ve,ma).display();”使之不会产生内存泄漏。【实验程序】/ 实验三/ 改写 f0815.cpp#include #include #include using namespace std;class Vectorint * v; / 指向一个数组,表示向量int sz;public :int size()

10、return sz; Vector( int );Vector( const Vector & 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 i nt s;Vector :Vector( const Vector & s)int i;sz = s.sz;v = new i nt sz;for (i = 0; isz; i+)vi = s.vi;Vector :Vector()delete

11、 v;int & Vector : operator (int i )/ 引用返回的目的是返回值可以做左值if (i = sz)cerr Vector index out of rang.n ; exit(1);return v i ;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( constMatrix & m);Matrix();int sizeL() returnszl;

12、int sizeR() returnszr;int & elem( int, int );Matrix :Matrix( int i , int j )szl = i ; szr = j ;if (i = 0 | j = 0)cerr bad Matrix size.n exit(1);m = new i nt i *j ;Matrix :Matrix( const Matrix & s)int i, j;szl = s.szl;szr = s.szr; m = new i nt szl*szr;for (i = 0; iszl; i+)for (j = 0; jszr; j+)mi*szr

13、+ j = s.mi*szr + j;Matrix :Matrix()delete m;int & Matrix :elem( int i, int j )/ 引用返回值的目的是可以做左值if (i0 | szl = i | j 0 | 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()ce

14、rr bad multiply Matrix with vector.n ; exit(1);Vector r( m.sizeL(); / 创建一个存放结果的空向量for (i = 0; ime.sizeL(); i+)r i = 0;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 ve i ;Matrix me(ma);Vector va(ve);multiply(ma, ve).display();【实验结果】s

15、iC: Wi n dotene继续图5实验三运行结果实验 4 面向对象 程序设计( 3)【实验要求】请在程序 f0904.cpp 中 的日期类的基础上,实现一个可以进 行加天数操作获 得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应 用 程序设计:创建 2015.8.21和 2008.8.8两个日期,并计算中间相隔的天数,前者加上 300 天会是什么日子呢?【实验程序】一、头文件部分#pragma once/class Date with year-month-day version#include #include using namespace std; class Da

16、te 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,int d):year( y),month( m),day( d) Date operator+ (int n) const return Date (ymd2i() + n);Date& operator+= ( int n) i2ymd(ymd2i() + n); ret

17、urn *this ;Date& operator+ () return *this += 1;int operator- ( Date & d) const return ymd2i() - d.ymd2i();bool isLeapYear() const return !(year %4) & (year % 100) | !(year % 400);friend ostream & operator ( ostream & o, const Date& d);、函数定义/Date.cpp/Class Date with year-month-day Version#include Da

18、te.h#include #include #include using namespace std;const int Date :tians= 0,31,59,90,120,151,181,212,243,273,304,334const 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) yea

19、r = 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年除得的天数if (y = Y400 - 1) month = 12, day = 30; return ;year += y / Y100 * 100; y %= Y100;year += y / Y4 * 4;

20、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); day = y - tiansmonth - 1;int Date:ymd2i() const ;month+);- 1) / 100 + (year - 1) /int yearDay =

21、(year - 1) * 365 + (year - 1) / 4 - (yearretur n yearDay + tia nsm onth - 1 + (isLeapYear() & month 2) + day;ostream & operator* (ostream & o, const Date& d) return o setfill( O ) setw(4) d.year - setw(2) d.m on th - setw(2) d.day setfill();三、testmain/testmai n.cpp/us ing Dateclass#include Date.h#in

22、clude using namespace std;int main() Date d1(2005, 8, 21);Date d2(2008, 8, 8); d2 - d1 ncout 2005.8.21与2008.8 , 8中间相隔的天数是:cout 2005.8.21加上300天是: d1 + 300 n【程序结果】实验 5 面向对象 程序设计( 4)【实验要求】在上题 Date 类 的基础上,继承一个 WDate 类,它包含了星期几信 息,因 而,显示日期的成员要做修改,应同时显示星期几。另外,还要增加获得星 期 几的成员。想一想,类中数据成员置年、月、日好呢,还是绝对天数好呢?进而进行

23、应用程序设计:创建 2005.8.21和 2008.8.8两个日期,分别显示这两个日期。【实验程序】一、头文件 Date.h#pragma once/Date.h/class Date with year-month-day version#include #include using namespace std;class Date int year, month, day;static const int tians;protected :Date( int n = 1) i2ymd( n);int ymd2i( ) const ;void i2ymd( int n);public :Da

24、te( 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) const return ymd2i() - d.ymd2i();bool isLeapY

25、ear() const return !(year %4) & (year % 100) | !(year % 400);friend ostream & operator ( ostream & o, const Date& d);二、头文件 WDate.h#pragma once/WDate.h#include Date.h#include #include using namespace std;class WDate : public Date protected :WDate(int n = 1) : Date(n) WDate(const Date& d) : Date( d) p

26、ublic :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; /0:Sunday,1:Monday

27、,etc. int 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#include Date.h#include #include #include using namespace std;const int Date :tians= 0,31,59,90,120,151,181,212,243,

28、273,304,334;const int Y400 = 146097; /number of days of 400 yearsconst int Y100 = 36524; /number of days of 100 years const 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_st

29、r();void Date:i2ymd( int absDay) year = absDay / Y400 * 400;int y = absDay%Y400/;/ 被 400年除得的天数 if (y = Y400 - 1) month = 12, day = 30;returnyear += 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 = 3

30、1;return ;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( );四、W

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论