基于控制台的万年历的查询系统:
#include
#include //setw函数所用头文件
using namespace std;
int judge = 1, loop = 1; //一些用到的全局变量
int mod, start, end, year, month, day, startday;
int dayw(int year, int month, int day) { //计算星期几的函数(公式计算)
if (month <= 2) {
month += 12;
year--;
}
int w = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; //公式
if (w + 1 == 7)
w = -1;
return w + 1;
}
void dw(int year, int month, int day) { //输出星期几的函数
int x = dayw(year, month, day);
if (x == 1)
cout << "星期一" << endl;
if (x == 2)
cout << "星期二" << endl;
if (x == 3)
cout << "星期三" << endl;
if (x == 4)
cout << "星期四" << endl;
if (x == 5)
cout << "星期五" << endl;
if (x == 6)
cout << "星期六" << endl;
if (x == 0)
cout << "星期日" << endl;
}
void calendar(int year, int st, int en) { //输出日历用的函数
int i, j, last;
for (i = st; i <= en; i++) {
if (i == 2) {
if (year % 400 == 0 || (year & 4 == 0 && year % 100 != 0))
last = 29;
else
last = 28;
}
else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
last = 31;
else
last = 30;
int s, day = 1, jud = 0;
s = dayw(year, i, 1);
jud = s;
cout << endl << i << "月的日历如下:" << endl;
cout << endl << setw(6) << year << "年" << i << "月" << "1日 ";
dw(year, i, 1);
cout << endl;
cout << "星期日" << setw(6) << "一" << setw(6) << "二" << setw(6) << "三" << setw(6) << "四" << setw(6) << "五" << setw(6) << "六" << endl;//setw函数控制输出格式
cout << "------------------------------------------------------" << endl;
for (j = 1; j <= s; j++) //用于输出每月第一天前面的空格
cout << " ";
for (int k = 1; k <= last; k++) {
cout << setw(6) << k;
jud++;
if (jud == 7) {
jud = 0;
cout << endl << setw(6);
}
}
cout << endl;
cout << "------------------------------------------------------" << endl;
}
}
void welcome1() { //首次进入系统输出的欢迎函数
cout << endl << endl << endl;
cout << " -------------------------------------------" << endl;
cout << " * *" << endl;
cout << " 欢迎进入万年历查询系统" << endl;
cout << " * by:贾岱华 *" << endl;
cout << " -------------------------------------------" << endl;
cout << endl;
}
void welcome2() { //输出本程序功能的函数
cout << endl << endl;
cout << "请输入您的查询内容:" << endl << endl;
cout << " 1.显示一年的日历;" << endl;
cout << " 2.显示一月的日历;" << endl;
cout << " 3.显示某一天是星期几;" << endl;
cout << " 0.退出;" << endl;
cout << "请选择按键(0~3):" << endl;
}
int wrongyear(int year) { //输入了错误的年份
while (loop) {
if (year<0) {
cout << "年份错误,请重新输入:";
cin >> year;
}
else
return year;
}
return month;
}
int wrongmonth(int month) { //输入了错误的月份
while (loop) {
if (month>12 || month <= 0) {
cout << "月份错误,请重新输入:";
cin >> month;
}
else if (month >= 1 && month <= 12)
return month;
}
return month;
}
int wrongday(int day, int year, int month) { //输入了错误的日期
while (loop) {
if (day>29 && month == 2 && year % 4 == 0 && year % 100 == 0) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else if (day >29 && month == 2 && year % 400 == 0) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else if (day >28 && month == 2) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day > 31) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else if (day>30) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else if (day<0) {
cout << "日期错误,请重新输入:";
cin >> day;
}
else
return day;
}
return day;
}
int main()
{
while (judge) {
if (judge == 1) {
welcome1();
}
welcome2();
cin >> mod;
judge++;
if (mod == 0)
judge = 0;
else if (mod == 1) {
cout << endl << "请输入年份:";
cin >> year;
year = wrongyear(year);
calendar(year, 1, 12);
}
else if (mod == 2) {
cout << endl << "请输入年份:";
cin >> year;
year = wrongyear(year);
cout << "请输入月份:";
cin >> month;
month = wrongmonth(month);
calendar(year, month, month);
}
else if (mod == 3) {
cout << "请输入年份:";
cin >> year;
year = wrongyear(year);
cout << "请输入月份(1~12)";
cin >> month;
month = wrongmonth(month);
if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 == 0)))
cout << "请输入此年此月相应的日期(1~29)";
else if (month == 2)
cout << "请输入此年此月相应的日期(1~28)";
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
cout << "请输入此年此月相应的日期(1~31)";
else
cout << "请输入此年此月相应的日期(1~30)";
cin >> day;
day = wrongday(day, month, year);
cout << "该天是:";
dw(year, month, day);
}
}
return 0;
}