问题一
题目内容:
设计一个日期类(Date),用来实现日期的操作。包括一个空构造函数,三个成员函数,其余所需自行决定。
用成员函数setDate()用来给Date类设置日期。
用成员函数isLeapYear()用来判断是否是闰年。
用成员函数getSkip(Date o)用来计算两个日期之间相差的天数。
输入格式:
输入两个日期,输入格式参考样例。
输出格式:
判断两个日期是否是闰年,以及两个日期的间隔天数,输出格式参考样例。
输入样例:
2012 10 20
2017 11 07
输出样例:
2012 is leap year.
2017 is not leap year.
The skip of two date is 1844.
#include<iostream> #include<cmath> using namespace std; class date { int year=0, month=0, day=0; public: date() {} void set(int y, int m, int d) { year = y; month = m; day = d; } bool isleapyear(int y) { if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) { return true; } else { return false; } } long getskip(date p2) { long count = 0; count = abs(this->sumyear() - p2.sumyear() +(this->sum() - p2.sum())); return count; } long sumyear() { int i,p=0,q=0; for (i = 0; i < year; i++) { if (isleapyear(i)) p++; else q++; } return (p * 366) + (q * 365); } long sum() { long sum = 0; if(isleapyear(year)) switch (month) { case 1:sum = day; break; case 2:sum = 31 + day; break; case 3:sum = 31 + 29 + day; break; case 4:sum = 31 + 29 + 31 + day; break; case 5:sum = 31 + 29 + 31 + 30 + day; break; case 6:sum = 31 + 29 + 31 + 30 + 31 + day; break; case 7:sum = 31 + 29 + 31 + 30 + 31 + 30 + day; break; case 8:sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; break; case 9:sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; break; case 10:sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break; case 11:sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break; case 12:sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break; } else switch(month) { case 1:sum = day; break; case 2:sum = 31 + day; break; case 3:sum = 31 + 28 + day; break; case 4:sum = 31 + 28 + 31 + day; break; case 5:sum = 31 + 28 + 31 + 30 + day; break; case 6:sum = 31 + 28 + 31 + 30 + 31 + day; break; case 7:sum = 31 + 28 + 31 + 30 + 31 + 30 + day; break; case 8:sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; break; case 9:sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; break; case 10:sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break; case 11:sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break; case 12:sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break; } return sum; } }; int main() { int y1, m1, d1, y2, m2, d2; cin >> y1 >> m1 >> d1; cin >> y2 >> m2 >> d2; date p1; date p2; p1.set(y1, m1, d1); p2.set(y2, m2, d2); if (p1.isleapyear(y1)) cout << y1 << " is leap year." << endl; else cout << y1 << " is not leap year." << endl; if(p2.isleapyear(y2)) cout << y2<< " is leap year." << endl; else cout << y2 << " is not leap year." << endl; cout<<"The skip of two date is "<<p1.getskip(p2)<<'.'; return 0; }
先定义对象再赋值的时候直接date p1;就可以了,不是date p1();这样会默认是一个函数
计算天数的时候要注意闰年和平年2月的天数不一样
计算日期差的时候先相减再取绝对值,不然天数会变多
题目要求是空构造函数,所以在定义数据成员的时候先初始化,不然可以在构造函数处初始化数据成员
问题二
题目内容:
先定义一个能描述平面上一条线段的类Beeline,包含私有数据成员为线段两个端点的坐标(X1,Y1,X2,Y2),在类中定义形参默认值为0的构造函数,计算线段长度的公有成员函数Length(),显示线段两个端点坐标的公有成员函数show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1,line2,line3。在类中定义的构造函数要能对对象成员进行初始化。再定义计算三角形面积的函数Area()及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用show()函数显示三条边两端点坐标。
输入格式:
输入三角形三个顶点的坐标(x1,y1)、(x2,y2)、(x3,y3)。
其中 -100 <= x1,x2,x3,y1,y2,y3 <= 100,且为整数。
在主函数中创建类对象tri(x1,y1,x2,y2,x3,y3),对应line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1)。
输出格式:
调用Print()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。
输入样例:
0 0
0 4
3 0
输出样例:
Three edges' points are listed as follows:
(0, 0),(0, 4)
(0, 4),(3, 0)
(3, 0),(0, 0)
The area of this triangle is: 6.00.
#include<iostream> #include<cmath> #include<iomanip> using namespace std; class beeline { public: int x1, y1, x2, y2; beeline() { x1 = 0; y1 = 0; x2 = 0; y2 = 0; } double length() { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } void show() { cout << '(' << x1 << ", " << y1 << "),(" << x2 << ", " << y2 << ')' << endl; } }; class triangle :public beeline { beeline line1; beeline line2; beeline line3; public: triangle(int x, int y, int xx, int yy, int xxx, int yyy) { line1.x1 = x; line1.y1 = y; line1.x2 = xx; line1.y2 = yy; line2.x1 = xx; line2.y1 = yy; line2.x2 = xxx; line2.y2 = yyy; line3.x1 = xxx; line3.y1 = yyy; line3.x2 = x; line3.y2 = y; } void print() { line1.show(); line2.show(); line3.show(); } double area() { double p = (line1.length() + line2.length() + line3.length()) / 2; return sqrt(p * (p - line1.length()) * (p - line2.length()) * (p - line3.length())); } }; int main() { int x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; triangle tr1(x1, y1, x2, y2, x3, y3); cout << "Three edges' points are listed as follows:" << endl; tr1.print(); cout << "The area of this triangle is: "; cout << setiosflags(ios::fixed) << setprecision(2) << tr1.area() << '.'; return 0; }
求面积用到了海伦公式
保留小数:cout<<setiosflags(ios::fixed)<<setprecision(保留位数)<<