山东科技大学—C++实验
题目一: 时间之差
Description
定义一个类Time,包含小时、分钟、秒三个属性。定义其构造函数Time(int, int,
int)分别初始化其小时、分钟、秒。重载减法运算符,用于求两个时间之间相差的秒数(非负整数)。
Input
输入有2行。每行表示1个时间,包括小时、分钟、秒三个值。输入都是合法的24小时制的时间。
Output
见样例。
Sample Input
12 10 10
10 20 20
Sample Output
Deference is 6590 seconds.
题目给出部分(main函数):
int main()
{
int a, b, c;
cin>>a>>b>>c;
Time t1(a, b, c);
cin>>a>>b>>c;
Time t2(a, b, c);
cout<<"Deference is "<<(t2 - t1)<<" seconds."<<endl;
return 0;
}
code:
//山东科技大学---C++实验---时间之差
#include<stdio.h>
#include<iostream>
using namespace std;
class Time//时间类
{
private://私有成员
int h;
int m;
int s;
public://共有方法
Time(int H,int M,int S){
h=H;
m=M;
s=S;
}
int operator -(Time& T){
//重载-运算符
int ans=(3600*h+60*m+s-3600*T.h-60*T.m-T.s);
if(ans<0)return -ans;
else return ans;
}
};
int main()
{
int a, b, c;
cin>>a>>b>>c;
Time t1(a, b, c);
cin>>a>>b>>c;
Time t2(a, b, c);
cout<<"Deference is "<<(t2 - t1)<<" seconds."<<endl;
return 0;
}
2.今年多少岁
Description
定义类Date,用于表示日期,拥有:
3个int类型属性,分别表示年、月、日。
构造函数、析构函数。要输出一些样例所示的信息。
void show方法,用于输出日期值。格式见样例。
定义类Person,包括
一个Date类的对象用于表示生日,一个字符串表示其姓名(假定不含有空白符)。
构造函数和析构函数。要输出一些样例所示的信息。
int getAge(Date& now)方法,求在当前日期now时的周岁年龄。日期now一定大于birthday。
void show()方法,用于显示Person的信息,格式见样例。
getName()方法,用于返回其名字。
Input
输入3行。第1行包括1个合法的日期的年、月、日三个数据。 第2行是一个不含空白符的字符串,是一个人的名字。
第3行是当前日期,包括年、月、日三个数据。
Output
见样例。
Sample Input
1999 2 2
Tom
2014 5 8
Sample Output
Date 1999-2-2 is created.
Person Tom is created.
Tom’s birthday is 1999-2-2.
Date 2014-5-8 is created.
Now, Tom is 15.
Date 2014-5-8 is erased.
Person Tom is erased.
Date 1999-2-2 is erased.
题目给出部分(main函数):
int main()
{
int y, m, d;
string name;
cin >> y >> m >> d >> name;
Person person(y, m, d, name);
person.show();
cin >> y >> m >> d;
Date now(y, m, d);
cout << "Now, " << person.getName() << " is " << person.getAge(now) << "." << endl;
return 0;
}
code:
// 山东科技大学---C++实验---今年多少岁
#include<iostream>
using namespace std;
class Date{
//构造Date类
private://私有成员
int year,month,day;
public://共有方法
Date(int Y,int M,int D){
//构造函数
year=Y;
month=M;
day=D;
cout<<"Date "<<year<<"-"<<month<<"-"<<day<<" is created."<<endl;
}
int getYear(){
//获取年份
return year;
}
int getMonth(){
//获取月份
return month;
}
int getDay(){
//获取日份
return day;
}
~Date(){
//析构函数
cout<<"Date "<<year<<"-"<<month<<"-"<<day<<" is erased."<<endl;
}
};
class Person//构造Person类
{
private://私有成员
string name;
Date d;
public://共有方法
Person(int Y,int M,int D,string Name):d(Y,M,D){
//先初始化d
name=Name
;
cout<<"Person "<<name<<" is created."<<endl;
}
int getAge(Date &now){
//获取年龄
return (now.getYear()-d.getYear());
}
void show(){
//展示信息函数
cout<<name<<"'s birthday is"<<d.getYear()<<"-"<<d.getMonth()<<"-"<<d.getDay()<<"."<<endl;
}
~Person()//析构函数
{
cout<<"Person "<<name<<" is erased."<<endl;
}
string getName()//获取名字函数
{
return name;
}
};
int main()
{
int y, m, d;
string name;
cin >> y >> m >> d >> name;
Person person(y, m, d, name);
person.show();
cin >> y >> m >> d;
Date now(y, m, d);
cout << "Now, " << person.getName() << " is " << person.getAge(now) << "." << endl;
return 0;
}