C/C++日历

来源:互联网 发布:mac 如何设置密码 编辑:程序博客网 时间:2024/06/05 10:43
#include<iostream>
using namespace std;

 int menu() {
  int s;
 cout<< "〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓" << endl;
 cout<< "〓  1.查询某年某月某日是该年的第几天  〓" << endl;
 cout<< "〓  2.查询某日是星期几                〓" << endl;
 cout<< "〓  3.查询某月的月历                  〓" << endl;
 cout<< "〓  4.查询某年的年历                  〓" << endl;
 cout<< "〓  5.帮助                            〓" << endl;
 cout<< "〓  6.退出                            〓" << endl;
 cout<< "〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓" << endl;
 cout<< "请输入序号:" ;
 cin>> s;
 return s;
} //菜单

 int Day(int setyear, int setmonth, int setday) {  //计算时间为该年的第几天;
  int  month, day, sumday = 0;
  for (month = 1;month <= setmonth;month++) {
   if (month>1)
    sumday = sumday + day;
   switch (month)
   {
   case 1:case 3:case 5:case 7:case 8:case 10:case 12:
    day = 31;break;
   case 4:case 6:case 9:case 11:
    day = 30;break;
   case 2:
    if ((setyear % 4 == 0 && setyear % 100 != 0) || setyear % 400 == 0)
     day = 29;
    else
     day = 28;break;
   }
  
  }
  sumday = sumday + setday;
  return sumday;
 
 }

 int Week(int setyear, int setmonth, int setday) {  //计算日期的星期
  int sum = 0, week;
  week = ((setyear - 1) + (setyear - 1) / 4 - (setyear - 1) / 100 + (setyear - 1) / 400 + Day(setyear, setmonth, setday)) % 7;
  return week;
 }

 void monthCalendar(int setyear, int setmonth,int setday){ // 实现月历
  int day,d;
  d = setday;
  setday = 1;
  switch (setmonth)
  {
  case 1:case 3:case 5:case 7:case 8:case 10:case 12:
   day = 31;break;
  case 4:case 6:case 9:case 11:
   day = 30;break;
  case 2:
   if ((setyear % 4 == 0 && setyear % 100 != 0) || setyear % 400 == 0)
    day = 29;
   else
    day = 28;break;
  }
  cout << "\n\t\t\t【" << setyear << "-" << setmonth << "】" <<endl ;
  cout << "〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓" << endl;
  cout << "星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六" << endl;
 
  int j = 0;
  for (int i = 1; i <= Week(setyear, setmonth, d);i++) {
   cout << "\t";
   j++;
  }
  for (int i = 1; i <= day;i++) {
  
   cout << i << "\t";
   j++;
   if (j % 7 == 0)
    cout << endl;
  }
  cout << "\n〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓" << endl;
 }
 int yearCalendar(int setyear, int setmonth = 1, int setday = 1) { // 实现年历
  for (setmonth;setmonth <= 12;setmonth++) {
   monthCalendar(setyear, setmonth,setday);
  
  }
  return 0;
 }
 void main() {
 bool loop = true;
 int setyear = 1, setmonth = 1, setday = 1;
 while (loop) {
  bool x = (setmonth == 1 || setmonth == 3 || setmonth == 5 || setmonth == 7 || setmonth == 8 || setmonth == 10 || setmonth == 12);
  bool y = (setmonth == 4 || setmonth == 6 || setmonth == 9 || setmonth == 11);
  bool z = (setyear % 4 == 0 && setyear % 100 != 0) || setyear % 400 == 0;
  int a = menu();
  switch (a) {
  
  case 1: {
   bool p = true;
   while (p) {
    cout << "年:";
    cin >> setyear;
    cout << "月:";
    cin >> setmonth;
    cout << "日:";
    cin >> setday;
    
    if (setmonth > 12 || setmonth <= 0) { // 判断月份是否正确
     cout << "\n\t-----请输入正确月份...-----" << endl; 
    }
    else if ((x && setday > 31) || (y && setday > 30)) {  // 判断一个月的天数是否正确
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else if (setmonth == 2 && z && setday > 29) {   // 判断闰年二月的天数是否正确
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else if (setmonth == 2 && !z && setday > 28) { // 判断平年二月的天数是否正确
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else {
     p = false;
     cout << "“" << setyear << "-" << setmonth << "-" << setday << "”" << "为当年的第:" << Day(setyear, setmonth, setday) << " 天" << endl;
     setday = 1;
     monthCalendar(setyear, setmonth, setday);
    }
   }
   break;
  }

  case 2: {
   bool p = true;
   while (p) {
    cout << "年:";
    cin >> setyear;
    cout << "月:";
    cin >> setmonth;
    cout << "日:";
    cin >> setday;
    /*bool x = (setmonth == 1 || setmonth == 3 || setmonth == 5 || setmonth == 7 || setmonth == 8 || setmonth == 10 || setmonth == 12);
    bool y = (setmonth == 4 || setmonth == 6 || setmonth == 9 || setmonth == 11);
    bool z1 = (setyear % 4 == 0 && setyear % 100 != 0) || setyear % 400 == 0;
    bool z2 = !((setyear % 4 == 0 && setyear % 100 != 0) || setyear % 400 == 0);*/
    if (setmonth > 12 || setmonth <= 0) {
     cout << "\n\t-----请输入正确月份...-----" << endl;
    }
    else if((x && setday > 31) || (y && setday > 30)){
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else if (setmonth == 2 && z && setday >29) {
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else if (setmonth == 2 && !z && setday >28) {
     cout << "\n\t-----请输入正确日期...-----" << endl;
    }
    else{
     p = false;
     cout << "“" << setyear << "-" << setmonth << "-" << setday << "”" << "为:周" << Week(setyear, setmonth, setday) << endl;
    }

   }
   break;
  }
  case 3: {
   bool p = true;
   cout << "年:";
   cin >> setyear;
   while (p)
   {
    cout << "月:";
    cin >> setmonth;
    if (setmonth <= 0 || setmonth > 12)
     cout << "\n\t-----请输入正确月份...-----" << endl;
    else {
     p = false;
     monthCalendar(setyear, setmonth, setday);
    }
   }
   break;
  }
  
  case 4: {
   
    cout << "年:";
    cin >> setyear;
    yearCalendar(setyear);
   break;
  }

  case 5: {
   cout << "“请严格按照所给的格式输入要查询的日期”\n" << endl;
   break;
  }


  case 6:
   cout<<"程序以退出......"<<endl;
   loop = false;
   break;

  default:
   cout << "“请输入要菜单前正确的序号。。。”\n" << endl;
   break;
  }
 }
}


0 0
原创粉丝点击