C++實現(xiàn)簡易萬年歷
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
      
本文實例為大家分享了C++實現(xiàn)簡易的萬年歷,供大家參考,具體內容如下
代碼如下:
/*
*文件名稱:萬年歷.cpp
*作  者:chenghan
*完成日期:2019/1/10
*版 本 號:1.0
*問題描述:制作一個簡單的萬年歷 
*/ 
#include<iostream>
#include <string>
using namespace std;
//判斷一年是否為閏年,是返回true 否返回false
bool isleapyear(int year); 
//兔子圖案 
void Rabbit();  
//封裝時間類 私有數(shù)據(jù)成員包括年月日 
class Date
{
 private:
  int year, month, day; //私有數(shù)據(jù)成員 
 public:
  Date(){} //無參的構造函數(shù) 
  Date(int year, int month, int day); //有參的構造函數(shù) 
  void Disp_Date();  //顯示星期數(shù) 
  void set(); //用戶輸入時間 
  int week(); //判斷星期的函數(shù) 
 void show(); //顯示日歷的函數(shù) 
};
//主函數(shù) 
int main()
{
  Date t; //創(chuàng)建一個Date類對象 
  string N="yes";
  Rabbit(); 
 while(N=="yes"){
 t.set(); //調用設置時間函數(shù) 
 t.Disp_Date(); //顯示星期 
   t.show();  //展示日歷畫面 
   cout<<"\n是否繼續(xù)查詢,是(yes)否(no)\n";
   cin>>N;
 } 
  
  return 0;
}
//判斷一年是否為閏年,是返回true 否返回false
bool isleapyear(int year)
{
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    return true;
  else
    return false;
}
//兔子圖案
void Rabbit()  
{
 cout<<endl;
  cout<<"         ┏━┓ ┏━┓"<<endl;
  cout<<"作者:chenghan  ★│┃ ┃│┃"<<endl;
  cout<<"         ┃│┗灬┛│┃"<<endl;
  cout<<"版本:1.0     ┃     ┃"<<endl;
  cout<<"         ┃ ^   ^ ┃"<<endl;
  cout<<"時間:2019/1/10   ﹌ ˇ ﹌  "<<endl;
  cout<<"         ┗○━━━○┛"<<endl;
  
  cout<<"---------------------------------\n";
  
  cout<<"歡 來 萬 歷"<<endl;
  cout<<" 迎 到 年 !"<<endl;
  
   cout<<"---------------------------------------------------\n";
}
//有參的構造函數(shù) 
Date::Date(int year, int month, int day) //有參的構造函數(shù) 
{ 
    this->year = year;
    this->month = month;
    this->day = day;
}
//顯示星期數(shù)
void Date::Disp_Date(){    
    cout << year << "年" << month << "月" << day << "日 星期" ;
    switch(this->week()){
     case 0:
     cout<<"日\n";
     break;
     case 1:
     cout<<"一\n";
     break;
     case 2:
     cout<<"二\n";
     break;
     case 3:
     cout<<"三\n";
     break;
     case 4:
     cout<<"四\n";
     break;
     case 5:
     cout<<"五\n";
     break;
     case 6:
     cout<<"六\n";
     break;
 }
  }
  
//用戶設置時間
void Date::set()
{  
  cout<<"請輸入您所想要查找的年、月、日:";
  cin>>year>>month>>day;
}
//判斷星期的函數(shù) 
int Date::week(){
 int C,y,d,M;
 if(this->month==1||this->month==2){
 C = (this->year-1)/100;
 y = (this->year-1)%100;
 M = this->month+12; 
 d = this->day; 
 } 
 else{
 C = this->year/100; //C世紀數(shù)減一
 y = this->year%100; //y年份后兩位 
 d = this->day; //d是日
 M = this->month;
 }
 int W = C/4 - 2*C + y + y/4 + 13 * (M+1) / 5 + d - 1; //判斷星期的蔡勒公式 
 if (W < 0) /* 如果w是負數(shù),則計算余數(shù)方式不同 */
 {
    W = 7 - (-W) % 7;
    return W; //返回值1~6對應星期一到六 0對應七 
 }
 else return W%7;
}
//顯示日歷的函數(shù) 
void Date::show(){   
 Date temp;
 temp.year = this->year;
 temp.month = this->month;
 temp.day = 1;
 
 int count = temp.week();
 cout<<"---------------------------------------------------"<<endl;  //粗制濫造的界面 
 cout<<"---------------------"<<this->year<<"年"<<this->month<<"月"<<"---------------------\n"; 
 cout<<"日 一 二 三 四 五 六\n";
 for(int i=0;i<count;i++){
 cout<<" \t";
 }
 if(temp.month == 1 ||temp.month == 3 ||temp.month == 5 || temp.month ==7 || temp.month ==8 || temp.month ==10 ||temp.month == 12){
 for(int j=1;j<32;j++){
  if(j<10)cout<<" "<<j<<"\t";
  else cout<<j<<"\t";
  if(count==6){
  cout<<"\n";
  count = 0;
  }
  else count++;
 }
 }
 else if(temp.month == 4 ||temp.month == 6 ||temp.month == 9 ||temp.month == 11){
 for(int j=1;j<31;j++){
  if(j<10)cout<<" "<<j<<"\t";
  else cout<<j<<"\t";
  if(count==6){
  cout<<"\n";
  count = 0;
  }
  else count++;
 }
 }
 else if(temp.month==2){
 if(isleapyear(this->year)){
  for(int j=1;j<30;j++){
  if(j<10)cout<<" "<<j<<"\t";
  else cout<<j<<"\t";
  if(count==6){
   cout<<"\n";
   count = 0;
  }
  else count++;
  }
 }
 else{
  for(int j=1;j<29;j++){
  if(j<10)cout<<" "<<j<<"\t";
  else cout<<j<<"\t";
  if(count==6){
   cout<<"\n";
   count = 0;
  }
  else count++;
  }
 } 
 }
 cout<<"\n---------------------------------------------------";
} 
運行結果:
代碼中沒有檢查輸入錯誤的機制,寫的比較粗糙,有許多錯誤之處望指正。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
 - 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
 - 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見的關鍵字含義
 - 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
 - 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
 - 01-10使用C++實現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10用C++實現(xiàn)DBSCAN聚類算法
 - 01-10深入全排列算法及其實現(xiàn)方法
 


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
 
隨機閱讀
- 04-02jquery與jsp,用jquery
 - 01-10C#中split用法實例總結
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 01-10delphi制作wav文件的方法
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-11ajax實現(xiàn)頁面的局部加載
 


