【发布时间】:2016-12-02 06:10:33
【问题描述】:
我正在用 c++ 编写一个程序,它将打印一整年的日历。该程序将询问用户他们想输入哪一年,以便确定它是否是闰年。然后它会要求用户询问他们希望从 1 月 1 日st 的哪一天开始计数。然后它应该在一月的每一天计算,然后移动到二月,然后计算每一天,然后继续前进等。但是它不能正确打印每个月。我还被要求不要使用 % 运算符。任何可以使我的程序打印正确日历的帮助将不胜感激,请不要发布使用 % 运算符的建议。
#include<iostream>
#include<iomanip>
using namespace std;
/****************************************************************************************
Prototypes
****************************************************************************************/
void getyear(int& year , int& days_per_yr);
void list_months(int imonth, int iyear);
void firstday(int& mostart);
void start_month(int& mostart);
int get_numdays(int xmonth, int numdays, int year);
void listdays(int numdays, int& mostart);
bool isLeapYear(int year);
void printMonth();
/****************************************************************************************
Main
****************************************************************************************/
int main(){
printMonth();
}
void printMonth(){ // calls all the functions needed to print the calendar
int year;
int day;
int month;
int mostart;
int days_per_yr;
int numdays; // number of days per month is stored here
cout << "What year do you want a calendar for? ";
getyear(year, days_per_yr);
cout << "What day of the week does January 1 fall ";
cout <<"on (0 for Sunday, 1 for Monday, etc.)? ";
firstday(mostart);
cout << year << "\n" <<endl;
for (month=1; month <= 12; ++month){
list_months(month, year);
start_month(mostart);
numdays=get_numdays(month, numdays, year);
listdays(numdays, mostart);
}
}
void getyear(int& year, int& days_per_yr){ //gets the year from the user and sends it back to the printMonth function
// this function is vital for the isLeapyear function to work properly
cin >> year;
if (isLeapYear(year) == true){
days_per_yr = 366;}
else{
days_per_yr = 365;}
}
void list_months(int imonth, int iyear){ // lists every month needed with the days spaced out correctly
switch (imonth){
case 1: cout<< " January" << endl;
cout << " S M T W T F S "<< endl;
cout <<"-----------------------"<< endl;
break;
case 2: cout<< " Febuarry" << endl;
cout << " S M T W T F S "<< endl;
cout <<"-----------------------"<< endl;
break;
case 3: cout<< " March" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 4: cout<< " April" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 5: cout<< " May" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 6: cout<< " June" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 7: cout<< " July" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 8: cout<< " August" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 9: cout<< " September" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 10: cout<< " October" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 11: cout<< " November" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
case 12: cout<< " December" << endl;
cout << " S M T W T F S "<< endl;
cout <<"------------------------"<< endl;
break;
}
}
void firstday(int& mostart){ // gets the input from the user for the start_month(); function
cin >> mostart;
cout <<"\n";
}
void start_month(int& mostart){ //does the spacing so that the desired day entered by the user is displayed correctly
if(mostart==0){
cout << setw(3);
}
else if (mostart == 1){
cout << setw(6);
}
else if (mostart == 2){
cout << setw(9);
}
else if (mostart == 3){
cout << setw(12);
}
else if (mostart == 4){
cout << setw(15);
}
else if (mostart == 5){
cout << setw(18);
}
else if (mostart == 6){
cout << setw(21);
}
else if (mostart == 7){
mostart = 1;
}
}
int get_numdays(int xmonth, int numdays, int year){ // sets correct amount of days per month. If there is a leap year febuarry will have 29 days.
if ( xmonth == 1)
numdays = 31;
else if ( xmonth == 2){if (isLeapYear(year) == true)
numdays = 29;
else
numdays = 28;}
else if ( xmonth == 3)
numdays = 31;
else if ( xmonth == 4)
numdays = 30;
else if ( xmonth == 5)
numdays = 31;
else if ( xmonth == 6)
numdays = 30;
else if ( xmonth == 7)
numdays = 31;
else if ( xmonth == 8)
numdays = 31;
else if ( xmonth == 9)
numdays = 30;
else if ( xmonth == 10)
numdays = 31;
else if ( xmonth == 11)
numdays = 30;
else if ( xmonth == 12)
numdays = 31;
return numdays;
}
void listdays(int numdays, int& mostart){ //This function counts out every single day depending on the month.
int oneday;
for (oneday=1; oneday <= numdays; ++oneday){
if(mostart <= 6){
cout << oneday <<" ";
mostart++;
}
else{
cout << endl;
mostart=0;
start_month(mostart);
cout << oneday <<" ";
mostart++;
}
};
cout << endl;
}
bool isLeapYear(int year){ //determines if the year entered is a leapyear or not if it is a leap year it will return true
// if true is returned then it will execute a condition that will make febuary a day longer.
// If the condition returns false then febuary will continue to be 28 days.
if (year % 400 == 0){
return true;
}
if (year % 100 == 0){
return false;
}
if (year % 4 == 0){
return true;
}
return false;
}
【问题讨论】:
-
题外话:
cout << " S M T W T F S "<< endl; cout <<"-----------------------"<< endl;通用于所有情况,可以拉出来为你节省一大堆冗余代码 -
isLeapYear使用%运算符,所以...为什么您要求不要发布使用它的解决方案?测验是为您准备的,还是为我们所有人准备的?此外,1 月 1 日的工作日完全由年份决定。 -
只需使用 Julian Day Number 公式,不要理会那些复杂的逻辑细节 - en.wikipedia.org/wiki/Julian_day
-
@LuisColorado 如果年份是整数,则不需要
%:bool isLeapYear(int year) { return (year/400*400==year || year/100*100!=year) && year/4*4==year; } -
@JerryJeremiah,我知道,我的问题是因为 OP 希望我们努力不使用
%运算符,但随后他在他发布的代码中使用它。这个世界没有正义吗? :)
标签: c++