C++练习题(自己整理的)(3)

2020-05-23 14:22

if (year==q.year)//如果年相同 {

if (month==q.month)//若月相同 {

differece = q.day-day; }

else//月不同 {

if (month>q.month)//让pSmall指针指向小日期,pBig指针指向大日期 {

pSmall = &q; pBig = this; } else {

pSmall = this; pBig = &q; }

temp.month=pSmall->month; temp.day=pSmall->day;

for (i=1;i<=366;i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数 {//由于年份相同,相差的天数必然不会大于366,实际循环次数不大于366 temp.add(1);

if (temp.month==pBig->month && temp.day==pBig->day) {

differece=i; break; } }

differece=(month>q.month?-differece:differece); } }

else//年份不同 {

if (year>q.year)//让pSmall指针指向小日期,pBig指针指向大日期 {

pSmall = &q; pBig = this; } else {

pSmall = this; pBig = &q; }

temp.year=pSmall->year; temp.month=pSmall->month; temp.day=pSmall->day;

m=pBig->year-pSmall->year;

for (i=(m-1)*365;i<=(m+1)*366;i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数

{//计算出年份差m后,两个日期的天数差必定在[(m-1)*365,(m+1)*366]范围内,实际循环的次数不大于2*366

temp.add(1);

if (temp.month==pBig->month && temp.day==pBig->day) {

differece=i-(m-1)*365+1; break; }

}

differece=(year>q.year?-differece:differece); }

return differece; }

void Date::display() {

printf(\}

//主函数演示 void main() {

Date d(2013,5,21); printf(\原日期t1:\ d.display(); d.add(73);

printf(\ d.display(); d.del(73);

printf(\ d.display();

printf(\与2014-5-21日相差%d天\\n\}

10、设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。并可以输出4个坐标值和面积。使用测试程序验证程序。

class CPoint {

public: CPoint(){ iX = 0; iY = 0; } CPoint(int a,int b){ iX = a; iY = b; } void setXY(int a, int b){ iX = a; iY = b; } int getX(){ return iX; } int getY(){ return iY; } ~CPoint(){} private: int iX, iY; };

Class CRect {

public: CRect(){} ~CRect(){} CRect::CRect(CPoint one, CPoint two) { Point1 = one; Point4 = two; init(); } CRect::CRect(int iX, int iY, int iX2, int iY2) { Point1.setXY(iX, iY); Point4.setXY(iX2, iY2); init(); } Int RectArea();

Void init(); void disPlay(); private: CPoint Point1, Point2, Point3, Point4; };

void CRect::init() { Point2.setXY(Point4.getX(), Point1.getY()); Point3.setXY(Point1.getX(), Point4.getY()); }

int CRect::RectArea() { In t width, height, area; width = Point2.getX() - Point1.getX(); height = Point1.getY() - Point3.getY(); area = width*height; if (area>0) return area; else return -area; }

void CRect::disPlay() { cout << \ << Point1.getX() << \ << Point1.getY() << endl; cout << \ << Point2.getX() << \ << Point2.getY() << endl; cout << \ << Point3.getX() << \ << Point3.getY() << endl; cout << \ << Point4.getX() << \ << Point4.getY() << endl; }

Int main() { CPoint p1(-8, 5), p2(4, -10); CRect c1(p1, p2); CRect c2(1, 5, 5, 1); cout << \四个点的坐标:\ << endl; c1.disPlay(); cout << \的面积:\ << c1.RectArea() << endl; cout << \的面积:\ << c2.RectArea() << endl; system(\); return 0; }


C++练习题(自己整理的)(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:财政学试卷B

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: