2025年C 编程实践指南核心概念深度解析_第1页
2025年C 编程实践指南核心概念深度解析_第2页
2025年C 编程实践指南核心概念深度解析_第3页
2025年C 编程实践指南核心概念深度解析_第4页
2025年C 编程实践指南核心概念深度解析_第5页
已阅读5页,还剩14页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

试验17过程化编程

【试验目的】

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

【试验内容】

i.程序填空,练习类、对象、继承的定义和实现措施。

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

【试验规定】

我们在进行英语阅读的时候,会发现一种有趣的现象:有些字串是左右对称的,如

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

目前有若干航字串,每一行可以由数字、标点符号、空格符以及英文字符(包括大

小写)构成。要你帮忙编程判断与否是对称文,否则,就不能最大程度地发既有趣现象

了。

输入阐明

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

为“000000”时,输入结束c

英文字符不辨别大小写,即Madam亦为对称文。不要忘了“{<([”与"])>}”也是

互为对称的。

输出阐明

假如是对称文,则输出"Symmetry”,否则输出"Notsymmetry”。每个结论占一行。

Madam”

<madam>+

hnwg121wgnil*'

kkghkkhg^

00000M

图1symmetry.in

e:'ch06e>zy_07”

Symmetrvp

Symmetry。

Symmetrv”

Notsymmetry”

图2symmetry.out

【程序代码】

#inc1ude<iostream>

#include<string>

usingnamespacest<i:

boolisMatch(st:ings):

intmain(){

strings;

while(1){

cin»s:

if(a.compare("000000*)-0)(

break;

)

if(isMatch(s)){

cout«"Syranetry"«endl;

}

else(

cout«Totsymmetry"«endl:

}

)

return0;

}

boolisMatch(strings){

intlen=s.lengthO:

for(inti=0;i<len/2;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]+Ca'-'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]-Ca'-"A'))){

returnfalse;

)

elseif(((s[ij=='】on-i-1J=J')))I

continue;

)

elseif(((s[i]==><'&&s?ler>-i-1]==*>>)))(

continue;

}

elseif(((s[i]==*['?ler>-i-1]==1],))){

continue;

}

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

continue;

)

else(

if(s[i]!=s[len-i-1])(

returnfalse;

)

)

)

returntrue;

【运行成果】

3E:\VisualStdio\jL§\Project1\Debug\Project1.exe□X

frladan

ISynnetry

|<nadan>

ISynnetry

■ling121gnil

Motsymmetry

Kynnetry

Motsymmetry

Lkghkkhg

|Notsymmetry

搜狗拼音输入法全:

图3试验一运行成果

试验1-2面向对象编程技术(1)

【试验目的】

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

【试验内容】

定义一种时间类Time,能提供和设置由时、分、秒构成的时间,并编出应用程序,

规定包括定义时间对象,设置时间,输出该对•象提供的时间。并请将类定义作为界面,

用多文献构造实现之。

【程序代码】

//Time,h

#inc1ude<iosIream〉

classTime

{

public:

inth;

intm;

ints;

voidinputTO:

voidchangeT0;

voidoutputTO;

};

//Time,cpp

^include"Time,h”

#include<iostream〉

voidTime::inputT()

{

begin:

inta,b,c;

std::cout«"Inputtime(H:\l: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«"WrongtimeiPloasesetagain!\nw;

gotobegin;

|

else

{

if(a>24||a<0)

(

std::cout«"Wrongtime!Pleasesetagain!”」;

gotobegin;

)

elseiT(a=24)

(

if(b!=0||c!=0)

(

std::coat«"Wrongtime!Pleasesetagain!'.n

gotobegin:

)

else

(

h=a;

m=b:

s=c:

)

)

else

(

h=a:

m=b;

s=c;

)

}

)

}

voidTime::change!()

{

charp;

std::cout<<"Doyouwannachangetime?(Y/N):\n”;

std::cin»p:

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

std::cout<<"Thankyouforyourusing!\n”;

)

else

beginl:

inta,b,c;

std::cout«"Inputlime(H:M:S)\rT;

std::cin»a»b»c;

if(c>60||c<0)

{

std::cout«"WrongtimelPleasesetagain!\n";

gotobeginl;

)

else

{

if(b>60||b<0)

(

std::cout<<"Wrongtime!Pleasesetagain八n”:

gotobeginl:

)

else

(

if(a>24||a<0)

(

std::cojt«"WrongtimelPleasesetagain!\n";

gotobe;inl:

)

elseif(a==24)

(

if(b!=0||c!=0)

{

std::cout«”Wrongtimc!Pleasesetagain!\n":

gotobeginl;

}

else{

h=a;

m=b;

s=c;

)

)

else

(

h=a;

m=b:

s=c:

)

)

voidTime::outputT()

std::cout«”Outputtime(H:M:S)\n*«h««n««s;

1

//testmain.cpp

#include,Time,h”

#include<iostream>

voidmain(void)

{

TimeIime1;

timel.inputTO;

timel.outputT():

timel.change!();

timel.outputT();

|

【运行成果】

图4试验二运行成果

试验3面向对象程序设计(2)

【试验规定】

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

和矩阵向量的乘法也进行改写。对于第91和92行,合并成

multiply(ve,ma),display();"使之不会产生内存泄漏。

【试验程序】

〃试验三

//改写f0815.cpp

#include<iostream>

#include<fstream〉

#include<string.h>

usingnamespacest<i:

classVector

{

ini*v;〃指向一种数组,表达向昆

intsz;

public:

intsize。(returnGZ;}

Vector(int);

Vector(constVectorfcs);

int&operator[](int);

voiddisplay():

'Vector0:

};

Vector::Vector(ints)

{

sz=s;

if(s<=0)

(

cerr<<"badVectorsize.\n*;

exit(l):

)

v=newint[s];

}

Vector::Vector(constVectoAs)

(

inti;

sz=s.sz:

v=newint[sz];

for(i=0;i<sz;i++)

(

v[i]=s.v[i]:

)

}

Voctor::Vector()

dcleteLjv;

}

int&Vector::operator[](inti)〃引用返回的目的是返回值可以做左值

|

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

(

cerr«”Vectorindexoutofrang.\n";

exit(l);

)

returnv[i];

}

voidVector::display()

{

inti;

for(i=0;i<sz;++i)

cout«v[i]«*

cout<<*\n*;

}

classMatrix

{

int*m;

intszl,szr:

public:

Matrix(int,int);

Matrix(constMatrix&m):

'Matrix();

intsizeLO{returnszl;}

intsizeR(){returnszr;}

int&elem(int,int);

};

Matrix::Matrix(inti,int,i)

{

szl=i;szr=j;

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

(

cerr«”badMatrixsize.\n”;

exit⑴;

)

m=newint[i*.i];

}

Manix::Matrix(constMairix&s)

|

inti,j;

szl=s.sz);

szr=s.szr:

m=newint[szl*szr];

for(i=0;i<szl;i++)

(

for(j=0;j<szr;j++)

{

m[i*szr+j]=s.n[i*szr+j]:

}

)

}

Manix::'Matrix()

(

(ieletefm:

}

int&Matrix::elem(inti,intj)〃引用返回值的目的是⑴以做左值

{

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

(

cerr«Matrixindexoutofrange.\n*:

cxit(l);

)

returnm[i*szr+j];

}

Vectormultiply(Matrix&m,Vectorftv)

{〃矩阵乘向盘

inti,j;

Matrixme(in):

Vectorva(v):

if(in.sizeRO!=v.sizeO)

(

cerr«”badmultiplyMatrixwithvector.\n*;

exit(l);

)

Vectorr(m.sizeLO);〃创立一种寄存成果的空向砥

for(i=0;i<me.sizeLO;i++)

(

r[i]=0;

for(j=0;j<me.sizeRO;j++)

|

r[i]+=ine.elem(i,j)*vaj];

)

}

returnr;

}

voidmain()

inti,j;

Hireixt");〃文献名

intx,y;in»x»y;

Matrixnia(x,y);

for(i=0;i<x;++i)

for(j=0;j<y;++j)

in»ma.elem(i,j);

in»x;

Vectorve(x);

for(i=0:i<x:++i)

(

in»veLiJ:

)

Matrixme(ma);

Vectorva(ve):

multiply(ma,ve).display():

【试验成果】

图S试限三运行成果

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

【试验规定】

请在程序fD904.cpp中的日期类的基础上,实现•种可以进行加天数操作获得另一

种日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:

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

呢?

【试验程序】

一、头文献部分

^pragmaonce

//classDalewithyear-month-dayversion

#include<iostream>

#include<string>

usingnamespacestd;

classDate{

intyear,month,day:

Date(intn=1){

i2ymd(n):

)

intymd2i()conut;

voidi2ymd(intn);

staticconstinttians[]:

public:

Date(conststring&s);

Date(inty,intm,intd):year(y),month(in),day(d)(}

Daleoperator+(intn)const(

returnDate(ymd2i()+n):

)

Dale&operator+=(intn)(

i2ymd(ymd2i()+n);

return*this;

)

Datc&operator++(){

return*this+_1;

)

intoperatoi-(Datefcd)const{

returnymd2i()-:l.ymd2i();

)

boolisLeapYear()const{

return!(year%4)&&(year%100))!(year%400):

)

friendostrearnftoperator«(ostreamfco,constDat'ftd):

);

二、函数定义

//Date,epp

//ClassDatewithyear-month-dayVersion

#include"Da(e.h”

#include<iostream>

#include<iomanip>

#include<string>

usingnamespacestd;

constintDate::tians[]={0,31,59,90,120,151,181,212,243,273,304,334):

constintY100=146097;//numberofdaysof100years

constintY100=36524;//numberofdaysof100years

constintY4=1461://numberofdaysof4years

Date::Date(conststring){

year=atoi(s.suhstr(0,4).cstr());

month=atoi(s.substr(5,2).c_slr());

day=atoi(s.substr(8,2).c_str());

)

voidDate::i2ymd(intabsDay)(

year=absDay/Y400*400;

inty=absDay%Y400;//被4除得的天数

if(y—Y100-I)(

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

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

day=y-tians[month-1]:

}

intDate::ym<12i0const{

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

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

}

ostri'airfioporator<<(ostrcaiofco,constDarcftd)(

returno«setfillCO')«setw(4)«d.year«*'«setw(2)«d.month«'«setw(2)«

d.day«setfill;

)

三、testmain

//testinain.cpp

//usingDaleclass

#include"Date.h"

#include<iostream>

usingnamespacestd;

intmainO{

Date<11(,8,21);

Daled2(,8,8);

cout«”.8.21与.8,8中间相隔的天数是:-«d2-dl«”\n";

cout<<”.8.21加上300天是:"«dl+300«”\n";

)

【程序成果】

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

【试验规定】

在上题Date类的基础上,继承一种WDate类,它包括了星期几信息,因而,显示

日期的组员要做修改,应同步显示星期儿。此外,还要增长获得星期儿的组员。想一想,

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

进而进行应用程序设计:

创立.8.21和.8.8两个日期,分别显示这两个日期。

【试验程序】

一、头文献Date.h

^pragmaonce

//Date,h

//classDatewithyear-month-dayversion

#inc1ude<iostream>

#include<string>

usingnamespacestd:

classDale{

intyear,month,day:

staticconstinttians[]:

protected:

Date(intn=1){

i2ymd(n);

)

intymd2i()const:

voidi2ynd(intn);

public:

Date(conststringfis);

Date(inty,intin,int(:):year(y),month(m),day(<l){}

Dateoperator+(intn)const(

returnDate(ymd2i()+n):

)

Datt'Aoperator+=(intn){

i2ymd(ymd2i0+n):

return*this;

)

Dateftoperator++(){

return*this+=1;

)

intoperator-(Datd)const{

returnymd2i0-d.ymd2i():

)

boolislA?ap¥car()const{

return!(year%4)&&(year%100)!(year%400):

)

friendostrcairjfiopcrator«(ostreainfto,constDatefcd):

};

二、头文献WDate.h

#pragmaonce

//WDate.h

#include*Date.h"

#inc1ude<iostream>

#include<string>

usingnamespacestd;

classWDate:publicDate{

protected:

WDate(intn=1):Dato(n){)

WDate(constDate&d):Dale(d){)

public:

WDate(conststring&s):Date(s){}

WDate(iniy,intni,intd):l)ate(y,m,d)(}

I'D.II-■opei;it<>i-(intn)const{returnDate::oporator+(n):)

.i;:,i.it,or»=(intn)(Date::operatoi*=(n);return*this;

WDate&operator++(){return*this+=1;}

intgetWeekDayO{returnymd2i()%7:)//0:Sunday,1:Monday,etc..

intopei-ui.or(I'II.H.■&„I)conut(returnymd2i()-1­.I,ymd2i();)

friendostreair&operator«(ostream&o,constWDate&wd);

):

三、Date,epp

//Date,epp

//ClassDalewithyear-monlh-dayVersion

#include"Date.h"

#include<iostream>

Sinclude<iomanip>

#include<string>

usingnamespacestd:

constintDate::tians[]=(0,31,59,90,120,151,181,212,243,273,304,334J;

constintY100=146097://numberofdaysof400years

constintY100=36524;//numberofdaysof100years

constintY4=1461;//numberofdaysof4years

(kite::Date(conststringfc){

year=atoi(s.substr(0,4).c_str());

month=atoi(s.substr(5,2).c_str());

day=atoi(s.substr(8,2).cstrO);

}

voidDate::i2ym<i(intabsDay){

year=absDay/Y400*400;

inty=absDay%Y400;〃被4除得的天数

if(y==Y400-1){

month=12,day=30:

return;

)

year+=y/Y100*100:

y%=Y100;

year+=y/Y4♦4;

yVY4;

if(y--Y4-1){

month=12,day=30;

return;

)

year+=y/365;

y%=365;

if(y==0){

month=12,day=31;

return:

)

year++:

boolleap=isLeapYearf);

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

duy-y-tiunu[monlh_1];

}

intDate::ymd2i()const{

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

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

)

oslreainftoperator<<(ostreanAo,constDaleft

温馨提示

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

最新文档

评论

0/150

提交评论