zoukankan      html  css  js  c++  java
  • 面向对象的程序第三次实验作业

    第一题

    #include <bits/stdc++.h>
    
    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 <<endl;
        Point p1(x, y);
        cout << "Point p1:";
        p1.print();
        cout << endl;
        Point p2(x * 2, y * 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;
        return 0;
    }
    

     第二题

    #include <bits/stdc++.h>
    
    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;
    }
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    如何判断PHP 是ts还是nts版的
    让IE支持placeholder属性
    解决点击浏览器后退按钮页面过期的问题
    git记住用户名密码
    php保存base64数据
    azure注册码
    SQL Server 2008 R2密钥序列号
    SQL允许远程访问
    PHP生成表格
    PHP发起get post put delete请求
  • 原文地址:https://www.cnblogs.com/lightac/p/10604511.html
Copyright © 2011-2022 走看看