c++习题程序编写Word下载.docx
- 文档编号:18644026
- 上传时间:2022-12-30
- 格式:DOCX
- 页数:14
- 大小:22.86KB
c++习题程序编写Word下载.docx
《c++习题程序编写Word下载.docx》由会员分享,可在线阅读,更多相关《c++习题程序编写Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
SetMonth(intnMonth/*=1*/)
m_nMonth=nMonth;
SetYear(intnYear/*=2000*/)
voidPrintDate(TDatecTDate)
printf("
Dateis:
%d-%d-%d"
cTDate.m_nYear,cTDate.m_nMonth,cTDate.m_nDay);
voidmain()
TDatecTdate;
cTdate.SetDate(12,19,2010);
//使用成员函数
cTdate.SetDay(10);
TDateCMyDate(12,19,2010);
//重载的构造函数生成对象实例
PrintDate(CMyDate);
//使用友员函数
设计一个日期类Date私有数据成员年year(int),月month(int),日day(int)intLeapYear(),判断是否闰年(能
设计一个日期类Date
私有数据成员年year(int),月month(int),日day(int)
intLeapYear(),判断是否闰年(能被4整除不能被100整除或能被400整除),是返回1否则返回0.
私有成员函数voidSetYear(intyy),年份必须是四位正整数,否则年份设置为2000。
void。
SetMonth(intmm),月份必须是1-12之间的数,否则设置为1。
voidSetDay(intdd),天数必须是1-31之间的数,并且必须是根据年份月份得到的范围内的天数,否则设置为1.
公有成员函数
voidSetDate(intyy=2000,intmm=1,intdd=1)调用SetYear(),SetMonth(),SetDay()设定正确的日期
voidShowDate()显示日期(日期显示格式如12/23/10,月份两位,天数两位,年份为year的最后两位)
要求:
类的声明和成员函数的定义分离
在头文件Date.h中声明类Date
在Date.cpp中定义成员函数
在main.cpp的主函数中定义Date对象d1,d2,指向对象的指针变量*ptd3,ptd4(new运算符),对象的引用d5.
5个测试数据:
(2000,2,30),(1998,9,31),(23452,4),(2012),(1998,2,29)
#ifndefHEADER_DATE
#defineHEADER_DATE
#include<
ctime>
iostream>
iomanip>
usingnamespacestd;
classdate{
intyear,month,day;
voidinit();
staticconstinttians[];
intage();
intgetyear()
intgetmonth()
intgetday()
date();
date(conststring&
s);
date(constdate&
d)
date(inty=2000,intm=1,intd=1);
boolisleapyear()const;
friendostream&
operator<
<
(ostream&
o,constdate&
d);
doublexg();
doublexiangge(dateb);
frienddateoperator+=(date&
a,intn);
friendbooloperator==(date&
a,date&
b);
voiddisplay();
stringhuanyuan();
#endif
#include"
date.h"
//日期的计算
constintdate:
tians[]=;
constlongintyear4=1461;
constlongintyear100=36524;
constlongintyear400=146097;
voiddate:
init(){
if(year>
5000||month>
12||day>
31||day<
1||month<
1||year<
1)
{cout<
"
年月日范围出错!
endl;
exit
(1);
date:
date(inty,intm,intd):
year(y),month(m),day(d){
init();
s):
da(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());
booldate:
isleapyear()const{
if(year%4==0&
&
year%100!
=0||year%400==0)
returntrue;
returnfalse;
ostream&
d){
o<
setfill('
0'
)<
setw(4)<
d.year<
-"
setw
(2)<
d.month<
d.day<
returno;
dateoperator+=(date&
a,intn){
switch(a.month){
case4:
case6:
case9:
case11:
elsea.day+=n;
if(a.month>
12)break;
case2:
if((a.year%4==0&
a.year%100!
=0)||a.year%400==0)
elsea.day+=n;
12)break;
else{
if(a.day+n>
28)elsea.day+=n;
12)}break;
case1:
case3:
case5:
case7:
case8:
case10:
case12:
if(a.month>
}
returna;
doubledate:
xg(){
intdays1[12]=;
longintsum1;
//4年一共1461天
sum1=year/400*year400+year%400/100*year100+year%400%100/4*year4+year%400%100%4*365;
//年份转换为天数所得
for(inti=0;
i<
12;
i++)
if(month-1==i)sum1+=days1[i]+day;
returnsum1;
xiangge(dateb){
return(xg()-b.xg());
booloperator==(date&
b){
if(a.year==b.year&
a.month==b.month&
a.day==b.day)return1;
elsereturn0;
display(){
cout<
day<
month<
year<
date(){//无参为系统时间
time_ttimer;
structtm*t_tm;
time(&
timer);
t_tm=localtime(&
year=t_tm->
tm_year+1900,
month=t_tm->
tm_mon+1,
day=t_tm->
tm_mday;
intdate:
age(){//计算当前时间差
inty=t_tm->
m=t_tm->
d=t_tm->
if(m<
month||m==month&
d<
day)return(year-y-1);
elsereturn(y-year);
stringdate:
huanyuan(){
stringm;
chara1[10],b1[10],c1[10];
itoa(year,a1,10);
itoa(month,b1,10);
itoa(day,c1,10);
m+=a1;
m+='
/'
;
m+=b1;
m+=c1;
returnm;
(参考)
4、定义一个date类,重载+号,实现指定日期加上指定天数后新日期的计算。
#incldue<
iostream.h>
Staticmon_day[]={31,28,31,30,31,31,31,31,30,31,30,31};
Classdate
Public:
Cdate(intm=0,intd=0,inty=0)
{month=m;
day=d;
year=y;
Voiddisplay()
{cont<
”/”<
Friend(dateoperator+(intd,cdatedt);
Private:
Intmonth,day,year;
Cdateoperator+(intd,cdatedt)//重载+运算符
{dt.day=mon_day[dt.month-1];
If(++dt.month==13)
{dt.month=1;
dt.year++;
}}
Returndt;
Voidmain()
{cdateolddate(2,20,99);
Cdatenewdate;
Newdate=21+olddate;
Newdate.display();
运行结果:
3/13/99
5、给字符串类定义下列重载运算符函数赋值运算符连接运算符+关系
1、#include"
iostream"
#include"
string.h"
classString
char*str;
String(char*s=0)
if(s)
{
str=newchar[strlen(s)+1];
strcpy(str,s);
elsestr=0;
~String()
if(str)delete[]str;
Stringoperator+(constString&
);
String&
operator=(String&
voidshow()
if(str)cout<
str<
StringString:
operator+(constString&
s)
Stringt;
t.str=newchar[strlen(str)+strlen(s.str)+1];
strcpy(t.str,str);
strcat(t.str,s.str);
returnt;
String&
String:
if(str)delete[]str;
str=newchar[strlen(s.str)+1];
strcpy(str,s.str);
return*this;
}
Strings1("
computer"
Strings2("
tests"
Strings3;
s1.show();
s2.show();
s3=s1+s2;
s3.show();
2、String.h文件:
String(){p=NULL;
String(char*str);
friendStringoperator+(String&
string1,String&
string2);
voiddisplay();
~String()
{delete[]p;
char*p;
intlen;
String.cpp文件:
string.h>
String.h"
String:
String(char*str)
{p=str;
voidString:
display()
p<
'
\n'
Stringoperator+(String&
string2)
Stringstring3;
string3.len=string1.len+string2.len;
string3.p=newchar[string3.len+1];
strcpy(string3.p,string1.p);
strcat(string3.p,string2.p);
returnstring3;
main.cpp文件:
intmain()
Strings1("
Hello"
),s2("
World"
),s3;
s1.display();
s2.display();
s3=s1+s2;
s3.display();
return0;
2010-10-2310:
05
最佳答案
构造函数有问题
p还没有开辟空间
任务
1、对下标运算符进行重载,使之具有检查下表是否越界的功能。
classCArray
intlen;
float*arp;
CArray(intn=0);
~CArray(){if(arp)delete[]arp;
intGetLen()const{returnlen;
voidSetLen(intl){
if(l>
0){
if(arp)delete[]arp;
arp=newfloat[l];
memset(arp,0,sizeof(float)*l);
//A
len=l;
float&
operator[](intindex);
//定义重载的下标运算符
CArray:
CArray(intn)
if(n>
arp=newfloat[n];
memset(arp,0,sizeof(float)*n);
len=n;
else{
len=0;
arp=0;
CArray:
operator[](intindex)//下表运算符的实现
{
if(index>
=len||index<
0){//如果参数index超出规定的范围,则输出越界信息
\nError:
下标"
index<
出界!
exit
(2);
returnarp[index];
//如果不越界,则返回相应的数据
voidmain13_8(void)
CArraym1(10),m2(3);
inti;
for(i=0;
10;
i++)m1[i]=i;
//重载数组下标的使用
for(i=1;
11;
i++)//B
m1[i]<
"
//C
m2[2]=26;
m2[2]="
m2[2]<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 习题 程序 编写