关于打印月历的题目求教
打印月历,经过调试之后情况是闰年没几个月是对的
平年的7月和8月有错误,其他都是对的
有那么几种情况:
1.firstday函数里面的公式有问题
2.basicMouth函数里的月基数有问题
(参考:http://wenku.baidu.com/link?url=HHFg4LY8ygZbTQVbyA5HhZAem1oGJeSuXpBaL6ALdLtd_tyQASSOb3th6gRu45r48DqXKXLSqDaI741QnkP7jObWjv7neE9I-_PQ3WoOOni)
3.display1函数有问题
最大可能是3,但是找不出来哪里有问题,请教各位
代码如下:
#include <iostream>
#include <string>
void display0(std::string enmouth[], int year, int mouth);
int firstday(int mouth, int year);
int days(int mouth, int year);
void display1(int W, int day, int mouth, int year);
int basicMouth(int mouth, int baseYear);
using namespace std;
int main(int argc, char *argv[]) {
cout << "Enter full year (e.g. 2010) : ";
int year;
cin >> year;
cout << "Enter mouth in number between 1 and 12 : ";
int mouth;
cin >> mouth;
string enmouth[12] = {"January" ,"February" ,"March" ,"April" ,"May" ,"June" ,"July" ,"August" ,"September" ,"October" ,"November" ,"December"};
display0(enmouth, year, mouth);
int W;
W = firstday(mouth, year);
int day;
day = days(mouth, year);
display1(W, day, mouth, year);
}
void display0(std::string enmouth[], int year, int mouth)
{
for (int i = 1; i <= 43; i++)
{
cout << "-";
}
cout << endl;
for (int i = 1; i <= 2; i++)
{
cout << "\t";
}
cout << enmouth[mouth - 1] << " " << year << endl;
for (int i = 1; i <= 43; i++)
{
cout << "-";
}
cout << endl;
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
}
int firstday(int mouth, int year)
{
int baseYear, baseMouth;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) baseYear = 2;
else baseYear = 1;
baseMouth = basicMouth(mouth, baseYear);
int W;
W = (year + year / 4 + year / 400 - year / 100 - baseYear + baseMouth + 1) % 7;
return W;
}
int basicMouth(int mouth, int baseYear)
{
int baseMouth;
if (baseYear == 1)
{
switch(mouth){
case 1:
baseMouth = 0;
break;
case 2:
baseMouth = 3;
break;
case 3:
baseMouth = 3;
break;
case 4:
baseMouth = 6;
break;
case 5:
baseMouth = 1;
break;
case 6:
baseMouth = 4;
break;
case 7:
baseMouth = 0;
break;
case 8:
baseMouth = 3;
break;
case 9:
baseMouth = 5;
break;
case 10:
baseMouth = 0;
break;
case 11:
baseMouth = 3;
break;
case 12:
baseMouth = 5;
break;
}
}
if (baseMouth == 2)
{
switch(mouth){
case 1:
baseMouth = 0;
break;
case 2:
baseMouth = 3;
break;
case 3:
baseMouth = 4;
break;
case 4:
baseMouth = 0;
break;
case 5:
baseMouth = 2;
break;
case 6:
baseMouth = 5;
break;
case 7:
baseMouth = 0;
break;
case 8:
baseMouth = 3;
break;
case 9:
baseMouth = 6;
break;
case 10:
baseMouth = 1;
break;
case 11:
baseMouth = 4;
break;
case 12:
baseMouth = 6;
break;
}
}
return baseMouth;
}
int days(int mouth, int year)
{
int day;
switch (mouth){
case 1:
day = 31;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) day = 29;
else day = 28;
break;
case 3:
day = 31;
break;
case 4:
day = 30;
break;
case 5:
day = 31;
break;
case 6:
day = 30;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 9:
day = 30;
break;
case 10:
day = 31;
break;
case 11:
day = 30;
break;
case 12:
day = 31;
break;
}
return day;
}
void display1(int W, int day, int mouth, int year)
{
cout << " ";
for (int i = 1; i <= W; i++)
{
cout << " ";
}
int i;
i = 1 + W;
for (int j = 1; j <= day; j++)
{
cout << j;
if (j <= 8) cout << " ";
else cout << " ";
if (i % 7 == 0)
{
cout << endl;
if (j < 9) cout << " ";
else cout << " ";
}
i++;
}
}
#include <string>
void display0(std::string enmouth[], int year, int mouth);
int firstday(int mouth, int year);
int days(int mouth, int year);
void display1(int W, int day, int mouth, int year);
int basicMouth(int mouth, int baseYear);
using namespace std;
int main(int argc, char *argv[]) {
cout << "Enter full year (e.g. 2010) : ";
int year;
cin >> year;
cout << "Enter mouth in number between 1 and 12 : ";
int mouth;
cin >> mouth;
string enmouth[12] = {"January" ,"February" ,"March" ,"April" ,"May" ,"June" ,"July" ,"August" ,"September" ,"October" ,"November" ,"December"};
display0(enmouth, year, mouth);
int W;
W = firstday(mouth, year);
int day;
day = days(mouth, year);
display1(W, day, mouth, year);
}
void display0(std::string enmouth[], int year, int mouth)
{
for (int i = 1; i <= 43; i++)
{
cout << "-";
}
cout << endl;
for (int i = 1; i <= 2; i++)
{
cout << "\t";
}
cout << enmouth[mouth - 1] << " " << year << endl;
for (int i = 1; i <= 43; i++)
{
cout << "-";
}
cout << endl;
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
}
int firstday(int mouth, int year)
{
int baseYear, baseMouth;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) baseYear = 2;
else baseYear = 1;
baseMouth = basicMouth(mouth, baseYear);
int W;
W = (year + year / 4 + year / 400 - year / 100 - baseYear + baseMouth + 1) % 7;
return W;
}
int basicMouth(int mouth, int baseYear)
{
int baseMouth;
if (baseYear == 1)
{
switch(mouth){
case 1:
baseMouth = 0;
break;
case 2:
baseMouth = 3;
break;
case 3:
baseMouth = 3;
break;
case 4:
baseMouth = 6;
break;
case 5:
baseMouth = 1;
break;
case 6:
baseMouth = 4;
break;
case 7:
baseMouth = 0;
break;
case 8:
baseMouth = 3;
break;
case 9:
baseMouth = 5;
break;
case 10:
baseMouth = 0;
break;
case 11:
baseMouth = 3;
break;
case 12:
baseMouth = 5;
break;
}
}
if (baseMouth == 2)
{
switch(mouth){
case 1:
baseMouth = 0;
break;
case 2:
baseMouth = 3;
break;
case 3:
baseMouth = 4;
break;
case 4:
baseMouth = 0;
break;
case 5:
baseMouth = 2;
break;
case 6:
baseMouth = 5;
break;
case 7:
baseMouth = 0;
break;
case 8:
baseMouth = 3;
break;
case 9:
baseMouth = 6;
break;
case 10:
baseMouth = 1;
break;
case 11:
baseMouth = 4;
break;
case 12:
baseMouth = 6;
break;
}
}
return baseMouth;
}
int days(int mouth, int year)
{
int day;
switch (mouth){
case 1:
day = 31;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) day = 29;
else day = 28;
break;
case 3:
day = 31;
break;
case 4:
day = 30;
break;
case 5:
day = 31;
break;
case 6:
day = 30;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 9:
day = 30;
break;
case 10:
day = 31;
break;
case 11:
day = 30;
break;
case 12:
day = 31;
break;
}
return day;
}
void display1(int W, int day, int mouth, int year)
{
cout << " ";
for (int i = 1; i <= W; i++)
{
cout << " ";
}
int i;
i = 1 + W;
for (int j = 1; j <= day; j++)
{
cout << j;
if (j <= 8) cout << " ";
else cout << " ";
if (i % 7 == 0)
{
cout << endl;
if (j < 9) cout << " ";
else cout << " ";
}
i++;
}
}
用的是C++,但是除了cin cout还有字符串数组和必要的头文件描述之外,其余都是C语言的内容