Shandong University of Science and Technology---C++ experiment

Shandong University of Science and Technology—C++ Experiment
Topic 1: Time Difference

Description

Define a class Time, which contains three attributes: hour, minute, and second. Define its constructor Time(int, int,
int) to initialize the hour, minute, and second respectively. Overloaded subtraction operator, used to find the number of seconds (non-negative integer) between two times.

Input

There are 2 lines of input. Each line represents 1 time, including three values ​​of hour, minute, and second. The input is a legal 24-hour time.

Output

See example.

Sample Input

12 10 10
10 20 20

Sample Output

Deference is 6590 seconds.

Part of the title (main function):

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. How old this year
Description

Define the class Date, used to represent the date, with:

The three int type attributes represent year, month, and day respectively.
Constructor, destructor. To output some information shown in the sample.
The void show method is used to output the date value. See the example for the format.
Define the class Person, including
an object of Date class used to represent the birthday, and a string to represent its name (assuming it does not contain blanks).
Constructor and destructor. To output some information shown in the sample.
The int getAge(Date& now) method finds the age at the current date now. The date now must be greater than birthday.
The void show() method is used to display the information of Person. See the example for the format.
The getName() method is used to return its name.
Input

Enter 3 lines. The first line contains three data of year, month, and day of a legal date. The second line is a string without whitespace, which is a person's name.
The third line is the current date, including three data of year, month, and day.

Output

See example.

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.

Part of the title (main function):

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;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/114705948