一、问题及代码
/*
* 文件名称:Ex1-1.cpp
* 作 者:零梅
* 完成日期:2016 年 4月 20日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分: 设计含有静态数据成员和成员函数的Time类。静态数据成员是类中所有的对象共有的数据,在下面的设计中,时钟要采用12小时制,还是要使用24小时制,显示时,不足两位的数字前是否前导0,都是“影响全局”的设置,适合作为类中的静态数据成员。
* 输入描述:无
* 问题描述:略
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include<iostream>
using namespace std;
class Time
{
public:
Time(int=0,int=0,int=0);
void show_time( );
void add_seconds(int);
void add_minutes(int);
void add_hours(int);
static void change24(int n);
static void changefrom0(int n);
private:
static bool is_24;
static bool from0;
int hour;
int minute;
int sec;
};
bool Time::is_24=true;
bool Time::from0=false;
Time::Time(int h,int m,int s)
{
hour=h;minute=m;sec=s;
}
void Time::show_time()
{
if(is_24==1&&from0==0)
{
cout<<"24时制,不前导:"<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
else if(is_24==0&&from0==0)
{
cout<<"12时制,不前导:"<<endl;
if(hour<12)
cout<<hour<<":"<<minute<<":"<<sec<<"am"<<endl;
else
cout<<hour<<":"<<minute<<":"<<sec<<"pm"<<endl;
}
else if(is_24==0&&from0==1)
{
cout<<"12小时制,前导:"<<endl;
if(hour>12)
{
hour=hour-12;
if(hour<10)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(sec<10)
cout<<"0"<<sec;
else
cout<<sec;
cout<<"pm"<<endl;
}
else
{
using namespace std;
class Time
{
public:
Time(int=0,int=0,int=0);
void show_time( );
void add_seconds(int);
void add_minutes(int);
void add_hours(int);
static void change24(int n);
static void changefrom0(int n);
private:
static bool is_24;
static bool from0;
int hour;
int minute;
int sec;
};
bool Time::is_24=true;
bool Time::from0=false;
Time::Time(int h,int m,int s)
{
hour=h;minute=m;sec=s;
}
void Time::show_time()
{
if(is_24==1&&from0==0)
{
cout<<"24时制,不前导:"<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
else if(is_24==0&&from0==0)
{
cout<<"12时制,不前导:"<<endl;
if(hour<12)
cout<<hour<<":"<<minute<<":"<<sec<<"am"<<endl;
else
cout<<hour<<":"<<minute<<":"<<sec<<"pm"<<endl;
}
else if(is_24==0&&from0==1)
{
cout<<"12小时制,前导:"<<endl;
if(hour>12)
{
hour=hour-12;
if(hour<10)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(sec<10)
cout<<"0"<<sec;
else
cout<<sec;
cout<<"pm"<<endl;
}
else
{
if(hour<10)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(sec<10)
cout<<"0"<<sec;
else
cout<<sec;
cout<<"am"<<endl;
}
}
else if(is_24==1&&from0==1)
cout<<"24时制,前导:"<<endl;
if(hour<10)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(sec<10)
cout<<"0"<<sec;
else
cout<<sec;
}
void Time::add_seconds(int n)
{
sec=sec++;
if(sec>60)
sec=sec-60;
}
void Time::add_minutes(int n)
{
minute=minute++;
if(minute>60)
minute=minute-60;
}
void Time::add_hours(int n)
{
hour=hour++;
if(hour>24)
hour=hour-24;
}
void Time::change24(int n)
{
is_24=n;
}
void Time::changefrom0(int n)
{
from0=n;
}
int main( )
{
Time t1(23,14,25);
Time t2(8,45,6);
cout<<"t1是:";
t1.show_time();
cout<<"t2是:";
t2.show_time();
cout<<"10小时后,切换是否前导:"<<endl;
t1.add_hours(10);
t2.add_hours(10);
t1.changefrom0(1);
t2.changefrom0(1);
cout<<"t1是:";
t1.show_time();
cout<<"t2是:";
t2.show_time();
t1.change24(0);
t2.change24(0);
cout<<"t1是:";
t1.show_time();
cout<<"t2是:";
t2.show_time();
}
二、运行结果
三、心得体会
还有待进步
四、知识点总结
用静态函数调用静态数据成员。
/*
* 文件名称:Ex1-1.cpp
* 作 者:零梅
* 完成日期:2015 年 4月 20日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分: 模仿上面的示例,完成求点类中距离的任务。你需要实现求距离函数的三种版本:分别利用成员函数、友元函数和一般函数求两点间距离的函数,并设计main()函数完成测试
* 输入描述:无
* 问题描述:略
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include<iostream>
#include<cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy){}
double display1(CPoint &p2);
friend double display2(CPoint &p1,CPoint &p2);
int getX()
{
return x;
}
int getY()
{
return y;
}
};
double CPoint::display1(CPoint &p2)
{
double dx=x-p2.x;
double dy=y-p2.y;
return sqrt(dx*dx+dy*dy);
}
double display2(CPoint &p1,CPoint &p2)
{
double dx=p1.x-p2.x;
double dy=p1.y-p2.y;
return sqrt(dx*dx+dy*dy);
}
double display3(CPoint &p1,CPoint &p2)
{
double dx=p1.getX()-p2.getX();
double dy=p1.getY()-p2.getY();
return sqrt(dx*dx+dy*dy);
}
int main()
{
CPoint p1(3,4);
CPoint p2(4,5);
cout<<p1.display1(p2)<<endl;
cout<<display2(p1,p2)<<endl;
cout<<display3(p1,p2)<<endl;
return 0;
}
#include<cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy){}
double display1(CPoint &p2);
friend double display2(CPoint &p1,CPoint &p2);
int getX()
{
return x;
}
int getY()
{
return y;
}
};
double CPoint::display1(CPoint &p2)
{
double dx=x-p2.x;
double dy=y-p2.y;
return sqrt(dx*dx+dy*dy);
}
double display2(CPoint &p1,CPoint &p2)
{
double dx=p1.x-p2.x;
double dy=p1.y-p2.y;
return sqrt(dx*dx+dy*dy);
}
double display3(CPoint &p1,CPoint &p2)
{
double dx=p1.getX()-p2.getX();
double dy=p1.getY()-p2.getY();
return sqrt(dx*dx+dy*dy);
}
int main()
{
CPoint p1(3,4);
CPoint p2(4,5);
cout<<p1.display1(p2)<<endl;
cout<<display2(p1,p2)<<endl;
cout<<display3(p1,p2)<<endl;
return 0;
}
/*
* 文件名称:Ex1-1.cpp
* 作 者:零梅
* 完成日期:2015 年 4月 20日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分: 定义下面两个类的成员函数(为体验友元类,实际上本例并不一定是一个好的设计,将两个类的合并为一个DateTime,日期、时间都处理更好)
* 输入描述:无
* 问题描述:略
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include<iostream>
using namespace std;
class Date;
class Time
{
public:
Time(int,int,int);
void add_a_second(Date &p1);
void display(Date &p2);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
using namespace std;
class Date;
class Time
{
public:
Time(int,int,int);
void add_a_second(Date &p1);
void display(Date &p2);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
class Date
{
public:
Date(int,int,int);
friend class Time;
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
void Time::add_a_second(Date &p1)
{
sec+=1;
minute+=sec/60;
sec=sec%60;
hour+=minute/60;
minute=minute%60;
p1.day+=hour/24;
hour=hour%24;
if(p1.day>31)
{
p1.day=1;
p1.month+=1;
if(p1.month>12)
{
p1.month=1;
p1.year+=1;
}
else p1.year=p1.year;
}
else
{
p1.day=p1.day;
p1.month=p1.month;
p1.year=p1.year;
}
}
void Time::display(Date &p2)
{
cout<<p2.year<<"年"<<p2.month<<"月" <<p2.day<<"日"<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::display(Date &p2)
{
cout<<p2.year<<"年"<<p2.month<<"月" <<p2.day<<"日"<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
Time t1(23,59,32);
Date d1(12,31,2013);
for(int i=0; i<=100; i++)
{
t1.add_a_second(d1);
t1.display(d1);
}
return 0;
}