坚持为学校计算C ++日历代码

时间:2015-05-08 23:45:40

标签: c++

我要将日历的代码作为学校项目制作,我几乎已经完成了。但是,运行应用程序时,该月的天数将继续,并且不会在最后一天停止。我认为它与发出月中日期的部分有关(int numDaysMonth)。我需要将它保存为数组,因为这是一个标题。

这是主要的:

#include <cstdlib>
#include <iostream>
#include "LeapYear.h"
#include "calendar.h"
using namespace std;

int main(int argc, char *argv[])
{
functions();
system("PAUSE");
return EXIT_SUCCESS;
}

这是创建日历的地方(链接到其他人的单独文件)......

#include <iostream>
#include <iomanip>
#include "LeapYear.h"
#include"calendar.h"
using namespace std;



/**********************************************************************
* This function will call all the functions necessary to make a calendar
* for any given month and year.
***********************************************************************/
int functions()
{
int numDays;
int offset;
int month;
int year;

month = getMonth(month);

year = getYear(year);

offset = computeOffset(year, month);

display(year, month, offset);

return 0;
}

/***********************************************************************
* Gets the month number.
**********************************************************************/
int getMonth(int month)
{
cout << "Enter a month number: ";
cin >> month;

while ( month < 1 || month > 12)
{
cout << "Month must be between 1 and 12.\n"
<< "Enter a month number: ";
cin >> month;
}

return month;
}

/***********************************************************************
* Gets the year.
**********************************************************************/
int getYear(int year)
{
cout << "Enter year: ";
cin >> year;

while ( year < 1753)
{
cout << "Year must be 1753 or later.\n"
<< "Enter year: ";
cin >> year;
}
return year;
}

/***********************************************************************
* Computes the offset.
**********************************************************************/

int computeOffset(int year, int month)
{
int offset = 0;
int count = year - 1753;
for ( int iYear = 0; iYear < count; iYear++)
{
offset = ( offset + 365 + isLeapYear(year)) % 7;
}

for ( int iMonth = 1; iMonth < month; iMonth++)
{
offset = ( offset + numDaysMonth(year, iMonth)) % 7;
}

return offset;
}



/***********************************************************************
* Computes the number of days in the given year.
**********************************************************************/
int numDaysYear(int year)
{
int daysYear = 365 + isLeapYear(year);
return daysYear;
}

/***********************************************************************
* Gets the number of days in the given month.
**********************************************************************/
int numDaysMonth(int year, int month)
{
int daysMonth[13];
daysMonth[0] = 31;
daysMonth[1] = 29;
daysMonth[2] = 28;
daysMonth[3] = 31;
daysMonth[4] = 30;
daysMonth[5] = 31;
daysMonth[6] = 30;
daysMonth[7] = 31;
daysMonth[8] = 31;
daysMonth[9] = 30;
daysMonth[10] = 31;
daysMonth[11] = 30;
daysMonth[12] = 31;
if ( month == 1)
daysMonth[0];
else if ( month == 2)
{
if (isLeapYear(year) == true)
daysMonth[1];
else
daysMonth[2];
}
else if ( month == 3)
daysMonth[3];
else if ( month == 4)
daysMonth[4];
else if ( month == 5)
daysMonth[5];
else if ( month == 6)
daysMonth[6];
else if ( month == 7)
daysMonth[7];
else if ( month == 8)
daysMonth[8];
else if ( month == 9)
daysMonth[9];
else if ( month == 10)
daysMonth[10];
else if ( month == 11)
daysMonth[11];
else if ( month == 12)
daysMonth[12];

}


/**********************************************************************
* Displays the calender table.
**********************************************************************/
void display(int year, int month, int offset)
{
int dayOfWeek;
int day;

cout << endl;
if ( month == 1)
cout << "January";
else if ( month == 2)
cout << "February";
else if ( month == 3)
cout << "March";
else if ( month == 4)
cout << "April";
else if ( month == 5)
cout << "May";
else if ( month == 6)
cout << "June";
else if ( month == 7)
cout << "July";
else if ( month == 8)
cout << "August";
else if ( month == 9)
cout << "September";
else if ( month == 10)
cout << "October";
else if ( month == 11)
cout << "November";
else if ( month == 12)
cout << "December";


cout << ", " << year << "\n";
// Display month header
cout << " Su Mo Tu We Th Fr Sa\n";

// Gets the correct offset width and end the line on the right
//day of the week
if (offset == 0)
{
day = 2;
cout << setw(6);
}
else if (offset == 1)
{
day = 3;
cout << setw(10);
}
else if (offset == 2)
{
day = 4;
cout << setw(14);
}
else if (offset == 3)
{
day = 5;
cout << setw(18);
}
else if (offset == 4)
{
day = 6;
cout << setw(22);
}
else if (offset == 5)
{
day = 7;
cout << setw(26);
}
else if (offset == 6)
{
day = 1;
cout << setw(2);
}
else
cout << "Error offset must be >= 0 and <=6\n";

// The loop for displaying the days and ending the line in the right place
for ( dayOfWeek = 1; dayOfWeek <= numDaysMonth(year, month); dayOfWeek++ )
{
cout << " " << setw(2) << dayOfWeek;
++day;
if (day == 8)
{
cout << "\n";
day = 1;
}
}
if ( day >= 2 && day <= 7)
cout << "\n";

}

然后我有一个单独的文件链接到那个:

#include <iostream>
#include <iomanip>
#include "LeapYear.h"
#include"calendar.h"

using namespace std;

/***********************************************************************
* Determines if given year is a leap year.
**********************************************************************/
bool isLeapYear(int year)
{
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}

然后是第一个头文件......

bool isLeapYear(int year);

第二个头文件......

int getMonth(int month);
int getYear(int year);
int computeOffset(int year, int month);
int numDaysYear(int year);
int numDaysMonth(int year, int month);
bool isLeapYear(int year);
void display(int year, int month, int offset);

2 个答案:

答案 0 :(得分:1)

您遗漏了numDaysMonth()中的所有返回语句。

如果您在编译器的额外警告开启的情况下编译代码,那么您肯定会从“没有值的函数返回”类型的诊断中获得。

答案 1 :(得分:1)

就在这里:

for (dayOfWeek = 1; dayOfWeek <= numDaysMonth(year, month); dayOfWeek++)

您依赖numDaysMonth()的返回值来停止显示日历。如果检查该函数,它不会返回您想要的内容,因为您没有明确指定返回。

由于您需要使用数组 - 您可以通过以下方式重构:

int numDaysMonth(int year, int month)
{
  const int LEAP_FEBRUARY = 13;
  int daysMonth[13];
  daysMonth[0] = 31;
  daysMonth[1] = 28;
  daysMonth[2] = 31;
  daysMonth[3] = 30;
  daysMonth[4] = 31;
  daysMonth[5] = 30;
  daysMonth[6] = 31;
  daysMonth[7] = 31;
  daysMonth[8] = 30;
  daysMonth[9] = 31;
  daysMonth[10] = 30;
  daysMonth[11] = 31;
  daysMonth[12] = 31;
  daysMonth[LEAP_FEBRUARY] = 29;

  if (month == 2) {
    if (isLeapYear(year)) {
      return daysMonth[LEAP_FEBRUARY];
    }
    else {
      return daysMonth[1];
    }
  } 

  return daysMonth[month - 1];
}