C++实验报告参考模板_第1页
C++实验报告参考模板_第2页
C++实验报告参考模板_第3页
C++实验报告参考模板_第4页
C++实验报告参考模板_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

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

2、区分大小写,即Madam亦为对称文。不要忘了“”也是互为对称的。输出说明如果是对称文,则输出“Symmetry”,否则输出“Not symmetry”。每个结论占一行。图 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;【运行结果】图 3 实验一运行结果实验1-2 面向对象编程技术(1)【实验目的】理解面向对象的的程序设计思想。【实验内容】定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,要求包括定义时间对象,设置时间,输出该对象提供的时间。并请将类定义作为界面,用多文件结构实现之。【程序代码】/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();【运行结果】图 4

8、 实验二运行结果实验3 面向对象程序设计(2)【实验要求】改写程序f0815.cpp,使之含有构造函数,拷贝构造函数和析构函数。并对主函数和矩阵向量的乘法也进行改写。对于第91和92行,合并成“multiply(ve,ma).display();”使之不会产生内存泄漏。【实验程序】/实验三/改写f0815.cpp#include#include#includeusing namespace std;class Vectorint* v;/指向一个数组,表示向量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();/创建一个存放结果的空向量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();【实验结果】图 5 实验三运行结果实验4 面向对象程序设计(3)【实验要求】请在程序f0904.cpp中的日期类的基础上,实现一个可以进行加天数操作获得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:创建2015.8.21和2

14、008.8.8两个日期,并计算中间相隔的天数,前者加上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);二、函数定义/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、数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中间相隔的天数是: d2 - d1 n;cout 2005.8.21加上300天是: d1 + 300 n;【程序结果】实验5 面向对象程序设计(4)【实验要求】在上题Date类的基础上,继承一个WDate类,它包含了星期几信息

21、,因而,显示日期的成员要做修改,应同时显示星期几。另外,还要增加获得星期几的成员。想一想,类中数据成员置年、月、日好呢,还是绝对天数好呢?进而进行应用程序设计:创建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年除得的天数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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论