我正在用c ++编写一个程序,它将打印全年的日历。该程序将询问用户他们想要进入哪一年,以便确定它是否是闰年。然后它会要求用户询问他们希望1月1日 st 从哪一天开始计数。然后它应该在1月的每一天cout然后移动到2月然后计算每一天然后继续行军等。但它不会每个月正确打印。我还需要不使用%
运算符。任何可以使我的程序打印出正确日历的帮助都将非常感谢,请不要发布使用%
运算符的建议。
#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;
}
答案 0 :(得分:0)
便宜的黑客:
在start_month
中,请确保打印出要移动信息流的字符
cout << setw(2) << " "; // was setw(3), but we added a manual space.
因为我们即将在setw(2)
中使用listdays
来为每天的数字打印出2个字符,这将消除setw
中的start_month
cout << setw(2) << oneday << " ";
然后根据需要通过添加空格和填充来确保所有内容都排成一行。
行。也许这令人困惑。问题在于:cout << oneday <<" ";
打印出一个整数。前9个是一位数宽。接下来的20个左右是2位数宽。为了将1位数字与2位数字对齐,1位数字必须占用2位数字。
所以,让我们这样做,不管吗?
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 << setw(2) << oneday <<" ";
mostart++;
}
else{
cout << endl;
mostart=0;
start_month(mostart);
cout << setw(2) << oneday <<" ";
mostart++;
}
};
cout << endl;
}
但这会打破start_month
,因为cout << setw(2) << oneday <<" ";
只是要重置cout << setw(3);
和他的朋友设置的宽度。快速解决方案:我们需要设置写入宽度,然后编写一个字符来占用该空间。
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(2) << " ";
}
else if (mostart == 1){
cout << setw(6) << " ";
}
else if (mostart == 2){
cout << setw(10) << " ";
}
else if (mostart == 3){
cout << setw(14) << " ";
}
else if (mostart == 4){
cout << setw(18) << " ";
}
else if (mostart == 5){
cout << setw(22) << " ";
}
else if (mostart == 6){
cout << setw(26) << " ";
}
else if (mostart == 7){
mostart = 1;
}
}
现在,如果你真的想踢,你说,&#34;拧紧所有这些if
/ else if
块&#34;并使用数学。看看2号,6号,10号,14号,18号,22号,26号以上的宽度数字。看模式? 2 + mostart * 4
。不要为此不需要施加任何功能!我可能有错误的凝视状态或步伐。这很容易解决。改变一个号码。
OP的大多数条件逻辑可以用简单的数学代替。这就是为什么我把这个答案的原始剪辑称为廉价黑客。
其余的是重新排列所有字符串文字。如果OP无法解决这个问题,我无法帮助他们。