编写一个c++程序,要求输入年份月份,并且输入该月的1号为周几,输出该月的日历

2025-04-07 04:36:35
推荐回答(2个)
回答1:

#include 
class Calendar {
public:
 enum Week
 {
  Monday=1,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
 };
 enum Month {
  January=1,
  February,
  March,
  April,
  May,
  June,
  July,
  August,
  September,
  October,
  November,
  December
 };
 int year;
 Month month;
 Week week;
 bool isLeapYear()const{
  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ;
 }
 /*初始化*/
 Calendar(int year_, Month month_,Week week_):
 year(year_),month(month_),week(week_){
 }
 void print() const{
  //获得月份时间
  int month_size = day_[month];
  if (month==Month::February) {
   if (isLeapYear()) {
    month_size = 29;
   }
  }
  int week_ = int(week);
  //
  std::cout << "year:"  << year <  std::cout << "month:" << int(month) << std::endl;
  for (auto ii = 0; ii < month_size;++ii) {
   std::cout << "day:" << (ii+1)<<"is:";
   std::cout << week_;
   std::cout << std::endl;
   ++week_;
   if (week_>Sunday) {
    week_ = int(Monday);
   }
  }
 }
private:    
 const static int day_[13] ;
};
//                           0  1   2 3  4   5  6  7  8  9 10 11 12
const int Calendar::day_[13]{-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
 Calendar c(200, Calendar::Month::April,Calendar::Week::Friday);
 c.print();
#ifdef _MSC_VER
 system("pause");
#endif
}

回答2:

#include
#include
using namespace std;int main(){
int year; int month; int monthcodearr[12]={1,4,4,7,2,5,7,3,6,1,4,6};
char week[7][10]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int day;
printf("input the date(YYYY-MM-DD)\n");
scanf("%d-%d-%d",&year,&month,&day); //这里主要是格式化输入。2011-6-20要按照这个格式输入。
int yearcode;
int monthcode;
if(year > 2000) {
yearcode = 4-2*((year-2000)/4)+(year-2000)%4+1; }
else if(year == 2000) {
yearcode = 4;
}
else {
yearcode = 4+2*(2000-year)/4-(2000-year)%4;
}
monthcode = monthcodearr[month-1];
int weekcode = (yearcode+monthcode+day)%7;
cout< cout< cout< cout< cout<}