1 实验目的
(1)学习如何声明和编写类的代码。
(2)学习如何编写改变类的属性的成员函数。
(3)学习如何声明和创建对象,如何通过对象调用类的成员函数。
2 实验内容
2.1 设计Point类(40分)
(1)问题描述
计算机的显示屏的坐标系是这样的,左上角的坐标为(0,0),如下图所示。
定义计算机显示屏上的点Point类。该类具有两个私有数据成员x、y,分别表示该点的横坐标、纵坐标。类的声明如下:
class Point {
public:
// 默认构造函数,默认值为左上角坐标(0, 0)
Point(int xx = 0, int yy = 0);
void setX(int xx);
int getX();
void setY(int yy);
int getY();
void print();
void moveRight(int offset);
void moveDown(int offset);
private:
int x;
int y;
};
(2)问题要求
请实现以下函数声明,要求能得到如下图所示的运行结果。
(1)接受用户的输入,生成两个对象;
(2)打印这两个点;
(3)向右平移其中一个点后,打印该点;向下平移另一个点后,打印该点。
(3)主函数代码框架
void main() {
int xx, yy;
cout << "Please input a point: ";
cin >> xx >> yy;
Point p1(xx, yy); // 生成点对象1
cout << "Point p1: ";
p1.print();
cout << endl;
Point p2(xx * 2, yy * 2); //生成点对象2
cout << "Point p2: ";
p2.print();
cout << endl;
p1.moveRight(10);
cout << "After moving right, p1: ";
p1.print();
cout << endl;
p2.moveDown(-10); // 位移量为负数,表示向上移动
cout << "After moving down, p2: ";
p2.print();
cout << endl;
}
(4)运行结果示例
Please input a point: 12 8
Point p1: (12, 8)
Point p2: (24, 16)
After moving right, p1: (22, 8)
After moving down, p2: (24, 6)
#include<iostream>
using namespace std;
class Point {
public:
Point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
void setX(int xx);
int getX();
void setY(int yy);
int getY();
void print();
void moveRight(int offset);
void moveDown(int offset);
private:
int x;
int y;
};
void Point::setX(int xx) {
x = xx;
}
int Point::getX() {
return x;
}
void Point::setY(int yy) {
y = yy;
}
int Point::getY() {
return y;
}
void Point::print() {
cout << "(" << x << "," << y << ")" ;
}
void Point::moveRight(int offset) {
x += offset;
}
void Point::moveDown(int offset) {
y += offset;
}
int main() {
int xx, yy;
cout << "Please input a point: ";
cin >> xx >> yy;
Point p1(xx, yy); // 生成点对象1
cout << "Point p1: ";
p1.print();
cout << endl;
Point p2(xx * 2, yy * 2); //生成点对象2
cout << "Point p2: ";
p2.print();
cout << endl;
p1.moveRight(10);
cout << "After moving right, p1: ";
p1.print();
cout << endl;
p2.moveDown(-10); // 位移量为负数,表示向上移动
cout << "After moving down, p2: ";
p2.print();
cout << endl;
}
2.2 设计日期类Date(60分)
(1)问题描述
设计一个日期类Date,类的声明如下:
class Date {
public:
/* 默认构造函数,以fullyear的形式给出年月日,默认值为1990年1月1日,同时设置日期分隔符为“-” */
Date(int ayear = 1990, int amonth = 1, int aday = 1);
/* get、set方法 */
// 设置日期,如果有非法的月或日,将其置为1
void setDate(int ayear, int amonth, int aday);
void setYear(int ayear);
int getYear();
void setMonth(int amonth);
int getMonth();
void setDay(int aday);
int getDay();
void setSeparator(char separator);
/* 输出函数,请使用setfill(‘0’)和setw(2),需要包含头文件 /
void printFullYear(); // 以YYYY-MM-DD的形式打印,2011-01-08
void printStandardYear(); // 以YY-MM-DD的形式打印,比如11-01-08
/ 计算函数 /
// 计算当前日期与参数日期之间相差几个整年,仅考虑参数日期比当前日期晚的情况
int fullYearsTo(int ayear, int amonth, int aday);
/ 计算当前日期与参数日期之间相差多少天(考虑闰年),如果参数日期在当前日期之前,返回负数。 */
int daysTo(int ayear, int amonth, int aday);
private:
int year;
int month;
int day;
char separator; // 日期分隔符
};
(2)问题要求
请实现日期类,使得主函数及其输出如下:
void main() {
Date birthDate(1969, 8, 11);
birthDate.printFullYear(); // 打印:1969-08-11
birthDate.printStandardYear(); // 打印:69-08-11
birthDate.setSeparator(’/’);
birthDate.printFullYear(); // 打印:1969/08/11
cout << birthDate.fullYearsTo(2010, 4, 15); // 打印:40,满四十岁
cout << birthDate.daysTo(2010, 4, 15); // 打印14857
// 打印-7254,共和国比我早诞生了7254天
cout << birthDate.daysTo(1949, 10, 1);
}
#include<iostream>
#include<iomanip>
using namespace std;
class Date {
public:
Date(int ayear = 1990, int amonth = 1, int aday = 1);
void setDate(int ayear, int amonth, int aday);
void setYear(int ayear);
int getYear();
void setMonth(int amonth);
int getMonth();
void setDay(int aday);
int getDay();
void setSeparator(char sepa);
void printFullYear();
void printStandardYear();
int fullYearsTo(int ayear, int amonth, int aday);
int daysTo(int year, int month, int day);
int getLeftDaysYear();
int getDayOfYear();
int getYearDays(int year);
private:
int year;
int month;
int day;
char separator='-';
int checkDay(int testDay);
bool isLeapyear(int year);
const static int DAYS_PER_MONTH[12];
};
const int Date::DAYS_PER_MONTH[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
Date::Date(int ayear , int amonth , int aday) {
year = ayear;
month = amonth;
day = aday;
}
void Date::setYear(int ayear) {
year = ayear;
}
int Date::getYear() {
return year;
}
void Date::setMonth(int amonth) {
month = amonth;
}
int Date::getMonth() {
return month;
}
void Date::setDay(int aday) {
day = aday;
}
int Date::getDay() {
return day;
}
void Date::setSeparator(char sepa) {
separator = sepa;
}
void Date::printFullYear() {
cout << year << separator << setfill('0') << setw(2) << month << separator << day<<endl;
}
void Date::printStandardYear() {
cout<<setfill('0')<<setw(2)<<year%100<<separator<< setfill('0') << setw(2) << month << separator << day<<endl;
}
int Date::fullYearsTo(int ayear, int amonth, int aday) {
int temp;
if (amonth > month) {
return temp = ayear - year;
}
else if (amonth == month) {
if (aday >= day)
return temp = ayear - year;
else
return temp = ayear - year - 1;
}
else
return temp = ayear - year - 1;
}
int Date::getYearDays(int year) {
if (isLeapyear(year))
return 366;
else return 365;
}
bool Date::isLeapyear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
int Date::getLeftDaysYear() {
return getYearDays(year) - getDayOfYear();
}
int Date::checkDay(int testday) {
if (testday > 0 && testday <= DAYS_PER_MONTH[month - 1])
return testday;
if (isLeapyear(year) && month == 2 && testday == 29)
return testday;
cout << "Invalid day(" << testday << ")set to 1.\n";
return 1;
}
int Date::getDayOfYear() {
int days = 0;
for (int i = 1; i < month; i++) {
days += DAYS_PER_MONTH[i - 1];
}
days += day;
if (isLeapyear(year) && month > 2)
days++;
return days;
}
int Date::daysTo(int year, int month, int day) {
Date date(year, month, day);
Date* startDate; // 开始日期
Date* endDate; // 结束日期
bool ascent = true; // 当前日期大于参数日期,则为真。默认为真。
// 如果在同一年中
if (this->year == year) {
return getDayOfYear() - date.getDayOfYear();
}
// 不在同一年中,且当前日期大于参数日期
if (this->year > year) {
startDate = &date;
endDate = this;
// 如果当前日期早
}
else {
startDate = this;
endDate = &date;
ascent = false;
}
// 先加上开始日期到该年结束的天数
int offsetDays = startDate->getLeftDaysYear();
// 再加上开始日期和结束日期之间的所有年的天数 (不包括起止年)
for (int i = startDate->year + 1; i < endDate->year; i++)
offsetDays += getYearDays(i);
// 再加上结束日期时,该年已经过的天数
offsetDays += endDate->getDayOfYear();
// 如果是升序,返回负数
if (ascent)
return 0 - offsetDays;
// 如果是降序
else
return offsetDays;
}
int main() {
Date birthDate(1969, 8, 11);
birthDate.printFullYear(); // 打印:1969-08-11
birthDate.printStandardYear(); // 打印:69-08-11
birthDate.setSeparator('/');
birthDate.printFullYear(); // 打印:1969/08/11
cout << birthDate.fullYearsTo(2010, 4, 15)<<endl; // 打印:40,满四十岁
cout << birthDate.daysTo(2010, 4, 15)<<endl; // 打印14857
// 打印-7254,共和国比我早诞生了7254天
cout << birthDate.daysTo(1949, 10, 1)<<endl;
}
之前写的有点错误,已进行修改,还有一部分需要改正,之后有时间了再放上来