一个简单的万年历的实现

由于CPP中没有自带的date类,自己造了一个,待优化。

//
// Created by chaomaer on 2018/4/18.
//

#ifndef SIMPLE_OJ_DATE_H
#define SIMPLE_OJ_DATE_H

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


class date {
public:
    string str_date;
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int week;
    date();
    date(std::string& s);
    void setweek();
    void printdate();
    void getnextday();

private:
    int getdaynum();
};
// 计算从1970年1月1日到s有多少天
bool isLeapyear(int year);

#endif //SIMPLE_OJ_DATE_H
//
// Created by chaomaer on 2018/4/18.
//

#include "date.h"
//date::date(int year, int month, int day, int minute, int week) {}
int MONTH[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeapyear(int year){
    return year%400==0||(year%100&&year%4==0);
}
date::date() {}

date::date(std::string &s) {
    str_date = s;
    year = atoi(s.substr(0, 4).c_str());
    month = atoi(s.substr(4, 2).c_str());
    day = atoi(s.substr(6, 2).c_str());
    hour = atoi(s.substr(8, 2).c_str());
    minute = atoi(s.substr(10, 2).c_str());
}

void date::printdate() {
    cout<<"year "<<year<<" month "<<month<<" day "<<day<<" week "<<week<<endl;
}

// 根据隐含信息去计算
// 1970年1月1日是星期四
void date::setweek() {
    int d = getdaynum();
    week = (4+d)%7;
    if (week == 0)
        week = 1;
}

int date::getdaynum() {
    // 计算减法
    int sum = 0;
    // 首先计算从日上计算
    sum += day-1;
    // 计算月份
    for (int i = 1; i <month ; ++i) {
        if (i == 2&&isLeapyear(year)){
            sum += 29;
        } else{
            sum += MONTH[i];
        }
    }
    // 计算年份
    for (int i = 1970; i <year ; ++i) {
        if (isLeapyear(i)){
            sum += 366;
        } else{
            sum += 365;
        }
    }
    return sum;
}

void date::getnextday() {
    day++;
    week = week+1;
    if (week == 8)
        week = 1;
    if (month == 2 && isLeapyear(year)){
        if (day>29){
            month++;
            day = 1;
        }
        if (month==13){
            month = 1;
            year ++;
        }
    }
    else if (day>MONTH[month]){
        month++;
        day = 1;
        if (month==13){
            month = 1;
            year ++;
        }
    }
}

测试

#include <iostream>
#include "date.h"

using namespace std;
int main(){
    string s = "197001010000 ";
    date date1(s);
    date1.setweek();
    date1.printdate();
    while (true){
        date1.getnextday();
        date1.printdate();
    }
}
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int rows, cols;
const int MAXSIZE = 12;
int start;
char road[MAXSIZE][MAXSIZE];
int visit[MAXSIZE][MAXSIZE];
int main(){
//    freopen("../in.txt", "r", stdin);
    while (cin>>rows>>cols>>start&&rows){
        memset(road, -1, sizeof(road));
        memset(visit, 0, sizeof(visit));
        for (int i = 1; i <=rows ; ++i) {
            for (int j = 1; j <=cols ; ++j) {
                char temp;
                cin>>temp;
                road[i][j] = temp;
            }
        }
        //输入结束,开始模拟
        pair<int ,int > point = make_pair(1, start);
        int step1 = 0;
        bool flag = false;
        while (true) {
            if (visit[point.first][point.second]) {
                cout<<visit[point.first][point.second]
                    <<" step(s) before a loop of "<<step1-visit[point.first][point.second]<<" step(s)"<<endl;
                break;
            }
            int ins = road[point.first][point.second];
            visit[point.first][point.second] = step1;
            step1++;
            switch (ins) {
                case 'S':
                    point.first++;
                    break;
                case 'N':
                    point.first--;
                    break;
                case 'W':
                    point.second--;
                    break;
                case 'E':
                    point.second++;
                    break;
                default:
                    step1--;
                    flag = true;
                    break;
            }
            if (flag) {
                cout<<step1<<" step(s) to exit"<<endl;
                break;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值