C++實現(xiàn)萬年歷小功能的方法-創(chuàng)新互聯(lián)

小編給大家分享一下C++實現(xiàn)萬年歷小功能的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為輪臺企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站建設(shè),輪臺網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

具體內(nèi)容如下

用C++寫了個簡易的萬年歷。

具體功能如下:

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;         //存放隨機輸入的數(shù)字,以實現(xiàn)“按任意鍵返回主菜單”的功能
char months[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };   //平年每個月的天數(shù)
 
void Pos(int x, int y);   //光標(biāo)位置
void menu();        //主菜單函數(shù)
void runnian();       //如是閏年則變第二個月天數(shù)28為29
void oneyear();       //輸出一整年的年歷
void onemonth();      //輸出一個月的月歷
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 << "請輸入操作鍵(0~3):";
 cin >> number;
 if (number < 0 || number>3)
 {
 system("cls");
 Pos(20, 15);
 cout << "輸入數(shù)字無效,請重新輸入!" << endl;
 menu();
 }
}
void runnian()   //如是閏年則變第二個月天數(shù)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日的星期數(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)打印到星期六時,換行
  {
  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日到所求日期前一天的天數(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 << "請按任意鍵返回主菜單:";
 cin >> c;
 system("cls");
 menu();
}
void xianshiweek()   //顯示星期數(shù)
{
 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 << "顯示的星期數(shù)如下:" << s << endl;
 cout << endl;
 cout << "請按任意鍵返回主界面:";
 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;
}

運行效果如下:

C++實現(xiàn)萬年歷小功能的方法

C++實現(xiàn)萬年歷小功能的方法

C++實現(xiàn)萬年歷小功能的方法

C++實現(xiàn)萬年歷小功能的方法

C++實現(xiàn)萬年歷小功能的方法

以上是“C++實現(xiàn)萬年歷小功能的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:C++實現(xiàn)萬年歷小功能的方法-創(chuàng)新互聯(lián)
文章來源:http://www.muchs.cn/article4/ceoooe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站建設(shè)、品牌網(wǎng)站制作小程序開發(fā)、搜索引擎優(yōu)化、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)