本文實例為大家分享了C++實現萬年歷的具體代碼,供大家參考,具體內容如下
用C++寫了個簡易的萬年歷。
具體功能如下:
1.打印指定年(用戶輸入)所有月份的年歷
2.打印指定年指定月(用戶輸入)的月份
3.打印指定日期(用戶輸入)的星期數
4.可重復輸入
貼上源碼:
- #include<iostream>
- #include<windows.h>
- #include<iomanip>
- using namespace std;
- int number; //菜單鍵
- int year, month, day; //年、月、日
- int i, j, t; //for循環用的量
- int s; //星期X
- char c; //存放隨機輸入的數字,以實現“按任意鍵返回主菜單”的功能
- char months[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //平年每個月的天數
- void Pos(int x, int y); //光標位置
- void menu(); //主菜單函數
- void runnian(); //如是閏年則變第二個月天數28為29
- void oneyear(); //輸出一整年的年歷
- void onemonth(); //輸出一個月的月歷
- void xianshiweek(); //顯示星期數
- void Pos(int x, int y)//光標位置
- {
- COORD pos;
- HANDLE hOutput;
- pos.X = x;
- pos.Y = y;
- hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOutput, pos);
- }
- void menu()//主菜單函數
- {
- Pos(40, 3);
- cout << "***********************************" << endl;
- Pos(40, 4);
- cout << "* 歡迎使用萬年歷 *" << endl;
- Pos(40, 5);
- cout << "* ---made by pjr *" << endl;
- Pos(40, 6);
- cout << "***********************************" << endl;
- Pos(20, 8);
- cout << "操作鍵:" << endl;
- Pos(20, 9);
- cout << "1.顯示一年的年歷" << endl;
- Pos(20, 10);
- cout << "2.顯示一月的月歷" << endl;
- Pos(20, 11);
- cout << "3.顯示某一天是星期幾" << endl;
- Pos(20, 12);
- cout << "0.退出" << endl;
- Pos(20, 14);
- cout << "請輸入操作鍵(0~3):";
- cin >> number;
- if (number < 0 || number>3)
- {
- system("cls");
- Pos(20, 15);
- cout << "輸入數字無效,請重新輸入!" << endl;
- menu();
- }
- }
- void runnian() //如是閏年則變第二個月天數28為29
- {
- cin >> year;
- if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //閏年判斷公式
- {
- months[2] = 29;
- }
- }
- void oneyear() //輸出一整年的年歷
- {
- cout << "請輸入年份:";
- runnian();
- system("cls"); //清屏
- cout << "請輸入年份:" << year << endl << endl;
- s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1) % 7; //該年1月1日的星期數
- for (i = 1; i <= 12; i++)
- {
- cout << i << "月份的月歷如下:" << endl;
- cout << setw(6) << "日" << setw(6) << "一" << setw(6) << "二" << setw(6) << "三" << setw(6) << "四" << setw(6) << "五" << setw(6) << "六" << endl;
- for (j = 0; j < s; j++)
- {
- cout << setw(6) << " ";
- }
- for (t = 1; t <= months[i]; t++)
- {
- cout << setw(6) << t;
- s = (s + 1) % 7;
- if (s % 7 == 0) //當打印到星期六時,換行
- {
- cout << endl;
- }
- }
- cout << endl;
- }
- fflush(stdin);
- cout << "請按任意鍵返回主菜單:";
- cin >> c;
- system("cls");
- menu();
- }
- void onemonth()//輸出一個月的月歷
- {
- int s = 0;
- cout << "請輸入年份:";
- runnian();
- cout << "請輸入月份:";
- cin >> month;
- system("cls");
- cout << "請輸入年份:" << year << endl << endl;
- cout << "請輸入月份:" << month << endl << endl;
- for (i = 1; i <= month - 1; i++)
- {
- s = s + months[i]; //該年1月1日到所求日期前一天的天數
- }
- s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1 + s) % 7; //所求日期的星期數
- cout << month << "月份的月歷如下:" << endl;
- cout << setw(6) << "日" << setw(6) << "一" << setw(6) << "二" << setw(6) << "三" << setw(6) << "四" << setw(6) << "五" << setw(6) << "六" << endl;
- for (j = 0; j < s; j++)
- {
- cout << setw(6) << " ";
- }
- for (t = 1; t <= months[month]; t++)
- {
- cout << setw(6) << t;
- s = (s + 1) % 7;
- if (s % 7 == 0)
- {
- cout << endl;
- }
- }
- cout << endl;
- cout << "請按任意鍵返回主菜單:";
- cin >> c;
- system("cls");
- menu();
- }
- void xianshiweek() //顯示星期數
- {
- int s = 0;
- cout << "請輸入年份:";
- runnian();
- cout << "請輸入月份:";
- cin >> month;
- cout << "請輸入日期:";
- cin >> day;
- system("cls");
- cout << "請輸入年份:" << year << endl << endl;
- cout << "請輸入月份:" << month << endl << endl;
- cout << "請輸入日期:" << day << endl << endl;
- for (i = 1; i <= month - 1; i++)
- {
- s = s + months[i];
- }
- s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + day + s) % 7;
- cout << "顯示的星期數如下:" << s << endl;
- cout << endl;
- cout << "請按任意鍵返回主界面:";
- cin >> c;
- system("cls");
- menu();
- }
- int main()//主函數
- {
- setlocale(LC_ALL, "chs");//轉中文
- menu();
- while (number != 0)
- {
- switch (number)
- {
- case 1:
- {
- oneyear();
- break;
- }
- case 2:
- {
- onemonth();
- break;
- }
- case 3:
- {
- xianshiweek();
- break;
- }
- }
- months[2] = 28; //把months[2]變為初值
- }
- if (number == 0)
- {
- system("pause");
- }
- return 0;
- }
運行效果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
原文鏈接:https://blog.csdn.net/qq_36224413/article/details/73302156