C++實(shí)現(xiàn)萬年歷小功能
本文實(shí)例為大家分享了C++實(shí)現(xiàn)萬年歷的具體代碼,供大家參考,具體內(nèi)容如下
用C++寫了個(gè)簡(jiǎn)易的萬年歷。
具體功能如下:
1.打印指定年(用戶輸入)所有月份的年歷
2.打印指定年指定月(用戶輸入)的月份
3.打印指定日期(用戶輸入)的星期數(shù)
4.可重復(fù)輸入
貼上源碼:
#include<iostream> #include<windows.h> #include<iomanip> using namespace std; int number; //菜單鍵 int year, month, day; //年、月、日 int i, j, t; //for循環(huán)用的量 int s; //星期X char c; //存放隨機(jī)輸入的數(shù)字,以實(shí)現(xiàn)“按任意鍵返回主菜單”的功能 char months[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //平年每個(gè)月的天數(shù) void Pos(int x, int y); //光標(biāo)位置 void menu(); //主菜單函數(shù) void runnian(); //如是閏年則變第二個(gè)月天數(shù)28為29 void oneyear(); //輸出一整年的年歷 void onemonth(); //輸出一個(gè)月的月歷 void xianshiweek(); //顯示星期數(shù) void Pos(int x, int y)//光標(biāo)位置 { COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, pos); } void menu()//主菜單函數(shù) { 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 << "請(qǐng)輸入操作鍵(0~3):"; cin >> number; if (number < 0 || number>3) { system("cls"); Pos(20, 15); cout << "輸入數(shù)字無效,請(qǐng)重新輸入!" << endl; menu(); } } void runnian() //如是閏年則變第二個(gè)月天數(shù)28為29 { cin >> year; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //閏年判斷公式 { months[2] = 29; } } void oneyear() //輸出一整年的年歷 { cout << "請(qǐng)輸入年份:"; runnian(); system("cls"); //清屏 cout << "請(qǐng)輸入年份:" << year << endl << endl; s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1) % 7; //該年1月1日的星期數(shù) 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) //當(dāng)打印到星期六時(shí),換行 { cout << endl; } } cout << endl; } fflush(stdin); cout << "請(qǐng)按任意鍵返回主菜單:"; cin >> c; system("cls"); menu(); } void onemonth()//輸出一個(gè)月的月歷 { int s = 0; cout << "請(qǐng)輸入年份:"; runnian(); cout << "請(qǐng)輸入月份:"; cin >> month; system("cls"); cout << "請(qǐng)輸入年份:" << year << endl << endl; cout << "請(qǐng)輸入月份:" << month << endl << endl; for (i = 1; i <= month - 1; i++) { s = s + months[i]; //該年1月1日到所求日期前一天的天數(shù) } s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1 + s) % 7; //所求日期的星期數(shù) 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 << "請(qǐng)按任意鍵返回主菜單:"; cin >> c; system("cls"); menu(); } void xianshiweek() //顯示星期數(shù) { int s = 0; cout << "請(qǐng)輸入年份:"; runnian(); cout << "請(qǐng)輸入月份:"; cin >> month; cout << "請(qǐng)輸入日期:"; cin >> day; system("cls"); cout << "請(qǐng)輸入年份:" << year << endl << endl; cout << "請(qǐng)輸入月份:" << month << endl << endl; cout << "請(qǐng)輸入日期:" << 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 << "顯示的星期數(shù)如下:" << s << endl; cout << endl; cout << "請(qǐng)按任意鍵返回主界面:"; cin >> c; system("cls"); menu(); } int main()//主函數(shù) { setlocale(LC_ALL, "chs");//轉(zhuǎn)中文 menu(); while (number != 0) { switch (number) { case 1: { oneyear(); break; } case 2: { onemonth(); break; } case 3: { xianshiweek(); break; } } months[2] = 28; //把months[2]變?yōu)槌踔? } if (number == 0) { system("pause"); } return 0; }
運(yùn)行效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉詳解
OpenCV作為機(jī)器視覺開源庫,使用起來非常不錯(cuò),這篇文章主要給大家介紹了關(guān)于C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05MFC實(shí)現(xiàn)在文件尾追加數(shù)據(jù)的方法
這篇文章主要介紹了MFC實(shí)現(xiàn)在文件尾追加數(shù)據(jù)的方法,涉及MFC文件操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09c語言統(tǒng)計(jì)素?cái)?shù)之和的實(shí)例
這篇文章主要介紹了c語言統(tǒng)計(jì)素?cái)?shù)之和的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12使用C語言訪問51單片機(jī)中存儲(chǔ)器的核心代碼
這篇文章主要介紹了使用C語言訪問51單片機(jī)中存儲(chǔ)器的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Qt模仿Windows文件夾縮略圖的三種實(shí)現(xiàn)方式
本文講的不是簡(jiǎn)單的model/view或者widget的或者QML的基礎(chǔ)框架實(shí)現(xiàn),而是在這些框架之上的肉(文件夾縮略圖)的效果實(shí)現(xiàn),本文將以QWidget、Qt Quick(QML)、以及QGraph三種實(shí)現(xiàn)方式來講解,如何做出和Windows類似的縮略圖,需要的朋友可以參考下2024-04-04