猫史档案馆


C++控制台版万年历

用户:开源盛世开源盛世查看:2 回复:2 评论:2 创建时间:2020-03-11T13:24:57


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

int MONTHDAY[]={
    31,28,31,30,
    31,30,31,31,
    30,31,30,31
    };
int month__=1;
long year__=2010;

void MonthYearOperat(int j){
	month__ += j;
	if(month__<1){
	    year__--,month__=12;
        if(year__<2006) 
		    year__=2006,month__=1;
	}	
    if(month__>12) year__++,month__=1;
}
bool CnkLeapYear(int year) {
    if(year%100==0?year%400==0:year%4==0)
        return true;
    return false;
}
int GetDay(int year,int month,int day) {
    int ans=0;
    for(int i=2006;i<year;i++) {
        if(CnkLeapYear(i)==1) ans+=366;
        else ans+=365;
    }
    if(CnkLeapYear(year)) MONTHDAY[1]=29;
    for(int m=0;m<month-1;m++)
        ans+=MONTHDAY[m];
    ans+=day-1;
    return ans%7;
}
bool CnkLastDay(int year,int month,int day) {
	switch(month) {
		case 1 :return day==31;
		case 3 :return day==31;
		case 5 :return day==31;
		case 7 :return day==31;
		case 8 :return day==31;
		case 10:return day==31;
		case 12:return day==31;
		case 4 :return day==30;
		case 6 :return day==30;
		case 9 :return day==30;
		case 11:return day==30;
		case 2 :
			if(CnkLeapYear(year))
			    return day==29;
			else
			    return day==28;
	}
}

void GoToMonth() {
	cout << "Date:      ";
	cout <<             year__;
	cout <<                "-";
	cout <<                  setw(2)<<month__<<endl;
    cout << "====================" << endl;
    cout << "Su Mo Tu We Th Fr Sa" << endl;
    cout << "--------------------" << endl;
    int first=GetDay(year__,month__,1);
    for(int i=0;i<first;i++)
        cout << "   " ;
    for(int i=1;i<=MONTHDAY[month__-1];i++) {
        first++,first%=7;
        cout << setw(2) << i << " ";
        if(first==0&&!CnkLastDay(year__,month__,i))
            cout << endl;
    }
    cout << endl << "====================" << endl;
    MONTHDAY[1]=28;
}

int main() {
	string mode;
	do {
		GoToMonth();
		restart :
		cout << ">>> ";
		cin >> mode;
		if(mode=="LT") { 
		    system("cls");
		    MonthYearOperat(-1);
    	}
		else if(mode=="NX") {
		    system("cls");
			MonthYearOperat(1 );
		}
		else if(mode=="GT") {
			do {
				cout << "-->";
				cin>>year__>>month__;
			}while(year__<2006||(month__<1||month__>12));
			system("cls");
		}
		else if(mode=="EX") 
		    return 0;
	    else {
		    goto restart;
    	}
	}while(true);
    return 0;
}


回复

上一页1 页 / 共 1下一页
我就路过看看我就路过看看

教你一个,C++的万能头

#include<bits/stdc++.h>
using namespace std;
int main
{

}

在你的程序里,头程序就已经占了3行

不过

看好了

点赞0


评论


有栖川無限有栖川無限

万年历可以再做的简单一点,不过精细的一点是年份用了long

点赞0


评论