要求:模仿现实生活中的挂历.
当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几).
当系统日期变到下一月时,系统自动翻页到下一月.
就是运行这个cpp他就自动显示当前系统日期,然后到下一个月会自动翻页
// WNL.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
char Content();
int Distinguish(int year, int month);
int DayNum(int year, int month);
void OutPut(int days, int year, int month);
int main()
{
int year = 0, month = 0;
char select;//用char记录选择
l:cout << "!欢迎使用万年历!" << endl;
cout << "please input the year and month you want to see:";
cin >> year;
cin >> month;
OutPut(DayNum(year, month), year, month);
m:select = Content();//将返回的字符接收
switch (select)
{
case '>':
month = month + 1;
if (month >= 13)
{
year = year + 1;
month = month % 12;
}
system("cls");
cout << year << "年" << month << "月\n";
OutPut(DayNum(year, month), year, month);
goto m;
case '<':
month = month - 1;
if (month <= 0)
{
year = year - 1;
month = month + 12;
}
system("cls");
cout << year << "年" << month << "月\n";
OutPut(DayNum(year, month), year, month);
goto m;
case '?':
system("cls"); //清屏
goto l;
case '#':
system("cls");
cout << "已退出,谢谢使用!" << endl;
break;
}
return 0;
}
int Distinguish(int year, int month)
{
int days = 0;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
if (month == 2)
{
days = 29;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
else
{
if (month == 2)
{
days = 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
return days;
}
char Content()
{
char select;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$$$$$$$ > nest month $$$$$$$" << endl;
cout << "$$$$$$$ < last month $$$$$$$" << endl;
cout << "$$$$$$$ ? resert date $$$$$$" << endl;
cout << "$$$$$$$$$$ # quit $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "Please input your select: ";
cin >> select;
return select;
}
int DayNum(int year, int month)
{
int days = 0;
int pingyear = 0, runyear = 0;
for (int i = 1; i <= year - 1; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
{
runyear++;
}
}
pingyear = year - 1 - runyear;//记得减1,因为我们计算的是全年到元年的整天数
days = pingyear * 365 + runyear * 366;
for (int i = 1; i <= month - 1; i++)//再加上输入的年的零散的几个月的天数
{
days += Distinguish(year, i);
}
return days;
}
void OutPut(int days, int year, int month)
{
int i, k = 0, n;
cout << "日" << '\t' << "一" << '\t' << "二" << '\t' << "三" << '\t' << "四" << '\t' << "五" << '\t' << "六" << '\t' << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
k = days % 7;
if (k != 6)//表示第一行开头需要有k+1个空,k+1表示上个月所占空位置数。
{
for (i = 0; i < k + 1; i++)
cout << ' ' << '\t';
}
k = k + 1 + 1;//调整k值,用于接下来的输出计数。此时,k代表下一个要输出的位置
n = Distinguish(year, month);
for (i = 1; i <= n; i++)
{
cout << i << '\t';
if (k % 7 == 0) //每行七个
{
cout << endl;
}
k = k + 1;
}
cout << endl;
}
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
char Content();//打印菜单
int Distinguish(int year, int month);/*判断月份的类型,确定此月的天数*/
int DayNum(int year, int month);// 计算从公元1月1日到所输入的时间的前一个月的最后一天的总天数
void OutPut(int days, int year, int month);
int main()
{
int year = 0, month = 0;
char select;//用char记录选择
l:cout << "!欢迎使用万年历!" << endl;
cout << "please input the year and month you want to see:";
cin >> year;
cin >> month;
OutPut(DayNum(year, month), year, month);
m:select = Content();//将返回的字符接收
switch (select)
{
case '>':
month = month + 1;
if (month >= 13)
{
year = year + 1;
month = month % 12;
}
system("cls"); //清屏
cout << year << "年" << month << "月
";
OutPut(DayNum(year, month), year, month);
goto m;//18行定义
case '<':
month = month - 1;
if (month <= 0)
{
year = year - 1;
month = month + 12;
}
system("cls"); //清屏
cout << year << "年" << month << "月
";
OutPut(DayNum(year, month), year, month);
goto m;//18行定义
case '?':
system("cls"); //清屏
goto l;
case '#':
system("cls");
cout << "已退出,谢谢使用!" << endl;
break;
}
return 0;
}
int Distinguish(int year, int month)//判断并返回所查月份的天数
{
int days = 0;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
if (month == 2)
{
days = 29;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
else
{
if (month == 2)
{
days = 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
return days;
}
char Content()
{
char select;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$$$$$$$ > nest month $$$$$$$" << endl;
cout << "$$$$$$$ < last month $$$$$$$" << endl;
cout << "$$$$$$$ ? resert date $$$$$$" << endl;
cout << "$$$$$$$$$$ # quit $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "Please input your select: ";
cin >> select;
return select;
}
int DayNum(int year, int month)
{
int days = 0;
int pingyear = 0, runyear = 0;
for (int i = 1; i <= year - 1; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
{
runyear++;
}
}
pingyear = year - 1 - runyear;//记得减1,因为我们计算的是全年到元年的整天数
days = pingyear * 365 + runyear * 366;
for (int i = 1; i <= month - 1; i++)//再加上输入的年的零散的几个月的天数
{
days += Distinguish(year, i);
}
return days;
}
void OutPut(int days, int year, int month)
{
int i, k = 0, n;
cout << "日" << ' ' << "一" << ' ' << "二" << ' ' << "三" << ' ' << "四" << ' ' << "五" << ' ' << "六" << ' ' << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
k = days % 7;
if (k != 6)//表示第一行开头需要有k+1个空,k+1表示上个月所占空位置数。
{
for (i = 0; i < k + 1; i++)
cout << ' ' << ' ';
}
k = k + 1 + 1;//调整k值,用于接下来的输出计数。此时,k代表下一个要输出的位置
n = Distinguish(year, month);
for (i = 1; i <= n; i++)
{
cout << i << ' ';
if (k % 7 == 0) //每行七个
{
cout << endl;
}
k = k + 1;
}
cout << endl;
}