博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象的程序第三次实验作业
阅读量:4325 次
发布时间:2019-06-06

本文共 4293 字,大约阅读时间需要 14 分钟。

第一题

#include 
using namespace std;class Point {public: Point(int xx = 0, int yy = 0) { setX(xx); setY(yy); } void setX(int xx) { x = xx; } int getX() { return x; } void setY(int yy) { y = yy; } int getY() { return y; } void print() { cout << " (" << x << "," << y << ")"; } void moveRight(int offset) { x += offset; } void moveDown(int offset) { y += offset; }private: int x; int y;};int main () { int x, y; cout << "Please input a point:"; cin >> x >> y; cout << x <

 第二题

#include 
using namespace std;bool isleap(int year) { bool flag = 0; if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) flag = 1; return flag;}int mon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};class Date{public: Date(int year = 1990, int month = 1, int day = 1) { setYear(year); setMonth(month); setDay(day); } void setDate(int year_, int month_, int day_) { year = year_; month = month_; day = day_; } void setYear(int year_) { year = year_; } int getYear() { return year; } void setMonth(int month_) { month = month_; } int getMonth() { return month; } void setDay(int day_) { day = day_; } int getDay() { return day; } void setSeparator(char separator_ = '-') { separator = separator_; } char getSeparator() { return separator; } void printFullYear() { if (getMonth() > 10) cout << getYear() << getSeparator() << getMonth() << getSeparator() << getDay() << endl; else cout << getYear() << getSeparator() << 0 << getMonth() << getSeparator() << getDay() << endl; } void printStandardYear() { if (getMonth() > 10) cout << getYear() % 100 << getSeparator() << getMonth() << getSeparator() << getDay() << endl; else cout << getYear() << getSeparator() << 0 << getMonth() << getSeparator() << getDay() << endl; } int fullYearsTo(int year_, int month_, int day_) { int ans = year_ - year; if(month_ < month) ans--; else if (day_ < day) ans--; // cout << ans << ',' << "满" << ans << "岁" << endl; return ans; } int daysTo(int year_, int month_, int day_) { if (year_ > year) { int ans = 0; for (int i = getYear() + 1; i < year_; ++i) { ans += 365; if(isleap(i)) ans++; } for (int i = 1; i < month_; ++i) { ans += mon[i - 1]; } for (int i = 12; i > month; --i) { ans += mon[i - 1]; } if(isleap(year_) && month_ > 2) ans++; if(isleap(year) && month < 3) ans++; ans += day_; ans += mon[month] - day + 1; return ans; } else { int ans = 0; for (int i = year_ + 1; i < getYear(); ++i) { ans += 365; if(isleap(i)) ans++; } for (int i = 1; i < month; ++i) { ans += mon[i - 1]; } for (int i = 12; i > month_; --i) { ans += mon[i - 1]; } if(isleap(year) && month > 2) ans++; if(isleap(year_) && month_ < 3) ans++; ans += day; ans += mon[month_] - day_ + 1; return -ans; } // cout << -ans << "," << "共和国比我早诞生了" << ans << "天" << endl; }private: int year; int month; int day; char separator = '-';};int main () { Date birthDate(1969, 8, 11); birthDate.printFullYear(); birthDate.printStandardYear(); birthDate.setSeparator('/'); birthDate.printFullYear(); cout << birthDate.fullYearsTo(2010, 4, 15); cout << "," << "满四十岁" << endl; cout << birthDate.daysTo(2010, 4, 15) << endl; cout << birthDate.daysTo(1949, 10, 1); cout << "," << "共和国比我早诞生了" << -birthDate.daysTo(1949, 10, 1) << "天" << endl; return 0;}

 

转载于:https://www.cnblogs.com/lightac/p/10604511.html

你可能感兴趣的文章
springboot(一)注解
查看>>
07 Mybatis的多表查询1----1对多和多对1
查看>>
debian和ubuntu的sh dash bash
查看>>
java9-8 局部内部类
查看>>
数据库分页
查看>>
Centos6.8源码编译安装PHP7
查看>>
012 debug调试工具的指令
查看>>
慕课网消息的接收与响应3
查看>>
第三十二讲:UML类图(下)
查看>>
linux下更改时区
查看>>
复杂链表的复制
查看>>
code vs 3376 符号三角形
查看>>
[CF193B] Xor(暴力,剪枝,异或)
查看>>
[CF825D] Suitable Replacement (贪心乱搞)
查看>>
大数据笔记(二十五)——Scala函数式编程
查看>>
win7 IIS7 运行vs2003 web 项目 无法识别的配置节“system.webServer” 解决
查看>>
jQuery源码分析_工具方法(学习笔记)
查看>>
有穷自动机的转换
查看>>
ncbi-blast 本地安装
查看>>
在android上使用 stand-alone toolchains移植 transmission
查看>>