C++实验报告同名16372.docx

上传人:b****5 文档编号:6925163 上传时间:2023-01-12 格式:DOCX 页数:25 大小:69.01KB
下载 相关 举报
C++实验报告同名16372.docx_第1页
第1页 / 共25页
C++实验报告同名16372.docx_第2页
第2页 / 共25页
C++实验报告同名16372.docx_第3页
第3页 / 共25页
C++实验报告同名16372.docx_第4页
第4页 / 共25页
C++实验报告同名16372.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

C++实验报告同名16372.docx

《C++实验报告同名16372.docx》由会员分享,可在线阅读,更多相关《C++实验报告同名16372.docx(25页珍藏版)》请在冰豆网上搜索。

C++实验报告同名16372.docx

C++实验报告同名16372

C++实验报告(同名16372)

Lt

D

实验1-1过程化编程

【实验目的】

理解、掌握过程化编程程序设计思想。

【实验内容】

1.程序填空,练习类、对象、继承的定义和实现方法。

2.根据程序运行结果,补充完整程序。

【实验要求】

我们在进行英语阅读的时候,会发现一个有趣的现象:

有些字串是左右对称的,如madam。

我们把这种字串称为symmetrytext即“对称文〞。

现在有假设干航字串,每一行可以由数字、标点符号、空格符以及英文字符〔包括大小写〕组成。

要你帮助编程判断是否是对称文,否那么,就不能最大限度地发现有趣现象了。

输入说明

每个字串为一行,每行结束以回车符为标志,可能有上百上千行业说不定。

当字串为“000000〞时,输入结束。

英文字符不区分大小写,即Madam亦为对称文。

不要忘了“{<([〞与“])>}〞也是互为对称的。

输出说明

如果是对称文,那么输出“Symmetry〞,否那么输出“Notsymmetry〞。

每个结论占一行。

图1symmetry.in

图2symmetry.out

【程序代码】

#include

#include

usingnamespacestd;

boolisMatch(strings);

intmain(){

strings;

while

(1){

cin>>s;

if(spare("000000")==0){

break;

}

if(isMatch(s)){

cout<<"Symmetry"<

}

else{

cout<<"Notsymmetry"<

}

}

return0;

}

boolisMatch(strings){

intlen=s.length();

for(inti=0;i

if((s[i]>='a'&&s[i]<='z')&&(s[len-i-1]>='A'&&s[len-i-1]<='Z')){

if(s[i]!

=(s[len-i-1]+('a'-'A'))){

returnfalse;

}

}

elseif((s[i]>='A'&&s[i]<='Z')&&(s[len-i-1]>='a'&&s[len-i-1]<='z')){

if(s[i]!

=(s[len-i-1]-('a'-'A'))){

returnfalse;

}

}

elseif(((s[i]=='{'&&s[len-i-1]=='}'))){

continue;

}

elseif(((s[i]=='<'&&s[len-i-1]=='>'))){

continue;

}

elseif(((s[i]=='['&&s[len-i-1]==']'))){

continue;

}

elseif(((s[i]=='('&&s[len-i-1]==')'))){

continue;

}

else{

if(s[i]!

=s[len-i-1]){

returnfalse;

}

}

}

returntrue;

}

【运行结果】

图3实验一运行结果

实验1-2面向对象编程技术〔1〕

【实验目的】

理解面向对象的的程序设计思想。

【实验内容】

定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,要求包括定义时间对象,设置时间,输出该对象提供的时间。

并请将类定义作为界面,用多文件结构实现之。

【程序代码】

//Time.h

#include

classTime

{

public:

inth;

intm;

ints;

voidinputT();

voidchangeT();

voidoutputT();

};

//Time.cpp

#include"Time.h"

#include

 

voidTime:

:

inputT()

{

begin:

inta,b,c;

std:

:

cout<<"Inputtime(H:

M:

S)\n";

std:

:

cin>>a>>b>>c;

if(c>60||c<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin;

}

else

{

if(b>60||b<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin;

}

else

{

if(a>24||a<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin;

}

elseif(a==24)

{

if(b!

=0||c!

=0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin;

}

else

{

h=a;

m=b;

s=c;

}

}

else

{

h=a;

m=b;

s=c;

}

}

}

}

voidTime:

:

changeT()

{

charp;

std:

:

cout<<"Doyouwannachangetime?

(Y/N):

\n";

std:

:

cin>>p;

if(p=='n'||p=='N')

{

std:

:

cout<<"Thankyouforyourusing!

\n";

}

else

{

begin1:

inta,b,c;

std:

:

cout<<"Inputtime(H:

M:

S)\n";

std:

:

cin>>a>>b>>c;

if(c>60||c<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin1;

}

else

{

if(b>60||b<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin1;

}

else

{

if(a>24||a<0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin1;

}

elseif(a==24)

{

if(b!

=0||c!

=0)

{

std:

:

cout<<"Wrongtime!

Pleasesetagain!

\n";

gotobegin1;

}

else{

h=a;

m=b;

s=c;

}

}

else

{

h=a;

m=b;

s=c;

}

}

}

}

}

voidTime:

:

outputT()

{

std:

:

cout<<"Outputtime(H:

M:

S)\n"<

"<

"<

}

//testmain.cpp

#include"Time.h"

#include

voidmain(void)

{

Timetime1;

time1.inputT();

time1.outputT();

time1.changeT();

time1.outputT();

}

【运行结果】

图4实验二运行结果

实验3面向对象程序设计

(2)

【实验要求】

改写程序f0815.cpp,使之含有构造函数,拷贝构造函数和析构函数。

并对主函数和矩阵向量的乘法也进行改写。

对于第91和92行,合并成“multiply(ve,ma).display();〞使之不会产生内存泄漏。

【实验程序】

//实验三

//改写f0815.cpp

#include

#include

#include

usingnamespacestd;

classVector

{

int*v;//指向一个数组,表示向量

intsz;

public:

intsize(){returnsz;}

Vector(int);

Vector(constVector&s);

int&operator[](int);

voiddisplay();

~Vector();

};

Vector:

:

Vector(ints)

{

sz=s;

if(s<=0)

{

cerr<<"badVectorsize.\n";

exit

(1);

}

v=newint[s];

}

Vector:

:

Vector(constVector&s)

{

inti;

sz=s.sz;

v=newint[sz];

for(i=0;i

{

v[i]=s.v[i];

}

}

Vector:

:

~Vector()

{

delete[]v;

}

int&Vector:

:

operator[](inti)//引用返回的目的是返回值可以做左值

{

if(i<0||i>=sz)

{

cerr<<"Vectorindexoutofrang.\n";

exit

(1);

}

returnv[i];

}

voidVector:

:

display()

{

inti;

for(i=0;i

cout<

cout<<"\n";

}

classMatrix

{

int*m;

intszl,szr;

public:

Matrix(int,int);

Matrix(constMatrix&m);

~Matrix();

intsizeL(){returnszl;}

intsizeR(){returnszr;}

int&elem(int,int);

};

Matrix:

:

Matrix(inti,intj)

{

szl=i;szr=j;

if(i<=0||j<=0)

{

cerr<<"badMatrixsize.\n";

exit

(1);

}

m=newint[i*j];

}

Matrix:

:

Matrix(constMatrix&s)

{

inti,j;

szl=s.szl;

szr=s.szr;

m=newint[szl*szr];

for(i=0;i

{

for(j=0;j

{

m[i*szr+j]=s.m[i*szr+j];

}

}

}

Matrix:

:

~Matrix()

{

delete[]m;

}

int&Matrix:

:

elem(inti,intj)//引用返回值的目的是可以做左值

{

if(i<0||szl<=i||j<0||szr<=j)

{

cerr<<"Matrixindexoutofrange.\n";

exit

(1);

}

returnm[i*szr+j];

}

Vectormultiply(Matrix&m,Vector&v)

{//矩阵乘向量

inti,j;

Matrixme(m);

Vectorva(v);

if(m.sizeR()!

=v.size())

{

cerr<<"badmultiplyMatrixwithvector.\n";

exit

(1);

}

Vectorr(m.sizeL());//创立一个存放结果的空向量

for(i=0;i

{

r[i]=0;

for(j=0;j

{

r[i]+=me.elem(i,j)*va[j];

}

}

returnr;

}

voidmain()

{

inti,j;

ifstreamin("in.txt");//文件名

intx,y;in>>x>>y;

Matrixma(x,y);

for(i=0;i

for(j=0;j

in>>ma.elem(i,j);

in>>x;

Vectorve(x);

for(i=0;i

{

in>>ve[i];

}

Matrixme(ma);

Vectorva(ve);

multiply(ma,ve).display();

 

}

【实验结果】

图5实验三运行结果

实验4面向对象程序设计(3)

【实验要求】

请在程序f0904.cpp中的日期类的根底上,实现一个可以进行加天数操作获得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:

创立2021.8.21和2021.8.8两个日期,并计算中间相隔的天数,前者加上300天会是什么日子呢?

【实验程序】

一、头文件局部

#pragmaonce

//classDatewithyear-month-dayversion

#include

#include

usingnamespacestd;

classDate{

intyear,month,day;

Date(intn=1){

i2ymd(n);

}

intymd2i()const;

voidi2ymd(intn);

staticconstinttians[];

public:

Date(conststring&s);

Date(inty,intm,intd):

year(y),month(m),day(d){}

Dateoperator+(intn)const{

returnDate(ymd2i()+n);

}

Date&operator+=(intn){

i2ymd(ymd2i()+n);

return*this;

}

Date&operator++(){

return*this+=1;

}

intoperator-(Date&d)const{

returnymd2i()-d.ymd2i();

}

boolisLeapYear()const{

return!

(year%4)&&(year%100)||!

(year%400);

}

friendostream&operator<<(ostream&o,constDate&d);

};

二、函数定义

//Date.cpp

//ClassDatewithyear-month-dayVersion

#include"Date.h"

#include

#include

#include

usingnamespacestd;

constintDate:

:

tians[]={0,31,59,90,120,151,181,212,243,273,304,334};

constintY400=146097;//numberofdaysof400years

constintY100=36524;//numberofdaysof100years

constintY4=1461;//numberofdaysof4years

Date:

:

Date(conststring&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());

}

voidDate:

:

i2ymd(intabsDay){

year=absDay/Y400*400;

inty=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;

}

year++;

boolleap=isLeapYear();

for(month=1;month<12&&y>tians[month]+(month>=2&&leap);month++);

day=y-tians[month-1];

}

intDate:

:

ymd2i()const{

intyearDay=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400;

returnyearDay+tians[month-1]+(isLeapYear()&&month>2)+day;

}

ostream&operator<<(ostream&o,constDate&d){

returno<

(2)<

(2)<

}

三、testmain

//testmain.cpp

//usingDateclass

#include"Date.h"

#include

usingnamespacestd;

intmain(){

Dated1(2005,8,21);

Dated2(2021,8,8);

cout<<与2021.8,8中间相隔的天数是:

"<

cout<<加上300天是:

"<

}

【程序结果】

实验5面向对象程序设计(4)

【实验要求】

在上题Date类的根底上,继承一个WDate类,它包含了星期几信息,因而,显示日期的成员要做修改,应同时显示星期几。

另外,还要增加获得星期几的成员。

想一想,类中数据成员置年、月、日好呢,还是绝对天数好呢?

进而进行应用程序设计:

创立2005.8.21和2021.8.8两个日期,分别显示这两个日期。

【实验程序】

一、头文件Date.h

#pragmaonce

//Date.h

//classDatewithyear-month-dayversion

#include

#include

usingnamespacestd;

classDate{

intyear,month,day;

staticconstinttians[];

protected:

Date(intn=1){

i2ymd(n);

}

intymd2i()const;

voidi2ymd(intn);

public:

Date(conststring&s);

Date(inty,intm,intd):

year(y),month(m),day(d){}

Dateoperator+(intn)const{

returnDate(ymd2i()+n);

}

Date&operator+=(intn){

i2ymd(ymd2i()+n);

return*this;

}

Date&operator++(){

return*this+=1;

}

intoperator-(Date&d)const{

returnymd2i()-d.ymd2i();

}

boolisLeapYear()const{

return!

(year%4)&&(year%100)||!

(year%400);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1