这是关于标头/规范的作用的问题,而不是代码是否起作用。
以下程序成功请求用户的日期,检查是否为闰年,检查日期的有效性,设置当月的天数,运行一系列if语句以确定如何添加一天,然后再添加一天。
问题在于,我不清楚标题是否正在做任何事情 - 我希望它(这基本上是我的第一个程序,而且重点是让标题做一些事情)。
任何人都可以就.h文件的工作原理提出建议吗?
(我知道这是很多要发布的代码,但我不确定我的错误的性质是什么)。
------ .h文件: #ifndef DATEINCREMENT_H_INCLUDED #define DATEINCREMENT_H_INCLUDED
namespace DateIncrement {
class Date
{
private:
int y, m, d;
int days_in_month;
bool lyear, y4, y100, y400;
public:
Date(); // default constructor
Date(int y, int m, int d); // parameterized constructor
bool leapyear(int); // checks whether y is a leapyear: inputs y, returns 1 or 0.
bool ydiv4(int); // within leapyear(): input y
bool ydiv100(int); // within leapyear(): input y
bool ydiv400(int); // within leapyear(): input y
bool valid_date(int, int, int); // determines days in month, checks whether a date is valid;
bool add_date(int, int, int, int, bool);
}; // class Date
} // namespace DateIncrement
//#include "main.cpp"
#endif // DATEINCREMENT_H_INCLUDED
------这是.cpp:
#include <iostream>
#include "DateIncrement.h"
using namespace std;
//using namespace DateIncrement;
//namespace DateIncrement {
int y;
int m;
int d;
bool lyear;
bool ydiv4(int y) {if (y % 4 == 0) return true; else return false; }
bool ydiv100(int y) {if (y % 100 == 0) return true; else return false; }
bool ydiv400(int y) {if (y % 400 == 0) return true; else return false; }
bool leapyear(int y)
{
cout << "Testing whether " << y << " is a leap year:" << endl;
cout << "Rule: Is it divisible by 4?";
bool y4;
y4 = ydiv4(y);
if (y4 == true) {cout << " -- Yes, so it could be." << endl;} else {cout << " -- No, so " << y << " isn't a leap year." << endl; return false;}
cout << "Possible exception: Is it divisible by 100?";
bool y100;
y100 = ydiv100(y);
if (y100 == true) {cout << " -- Yes, so it probably isn't, but:" << endl; } else {cout << " -- No, so " << y << " is a leap year." << endl; return true;}
cout << "Exception to the Exception: Is it divisible by 400?";
bool y400;
y400 = ydiv400(y);
if (y400 == true) {cout << " -- Yes, so " << y << " is a leap year after all." << endl; return true;} else {cout << " -- No, so " << y << " turns out not to be a leap year." << endl; return false;}
} // leapyear
int valid_date(int y, int m, int d, bool lyear)
{ if (d<=0 || d>31 || m<=0 || m>=13 || y<=0) return false;
cout << "On first examination, date is not invalid, but we will keep our eyes on it. Let's proceed." << endl << endl;
int days_in_month = 31;
switch (m) {
case 2: {days_in_month = lyear/*(leapyear(y))*/?29:28;
//cout << "Month #" << m << " in " << y << " has " << days_in_month << " days - [lyear=" << lyear << "]." << endl;
break;}
case 9: days_in_month = 30;
break;
case 4: days_in_month = 30;
break;
case 6: days_in_month = 30;
break;
case 11: days_in_month = 30;
break;
};
cout << endl << "The date you entered is: " << y << "-" << m << "-" << d << "." << endl << "Month #" << m << " has " << days_in_month << " days in the month." << endl;
if (d > days_in_month) {cout << endl << "Your entry " << d << " for Month #" << m << " is greater than the correct max #" << days_in_month << "." << endl; return 0;}
else {return days_in_month;}
} // valid_date
bool add_date(int y, int m, int d, int days_in_month, bool lyear)
{
cout << endl << "Adding one day to your date (" << y << "-" << m << "-" << d << ")," << " for " << days_in_month << " days in month #" << m << " and " << y << " as lyear=" << lyear << endl;
if (d<days_in_month) {d = d + 1; cout << endl << "Debugging: [case:d<days_in_month], output:" << d << "<" << days_in_month;}
else {if (m<12) {d = 1; m = m + 1; cout << endl << "Debugging: [case:m<12], output:" << m;}
else {if (m==12 && d==31) {d = 1; m = 1; y = y + 1; cout << endl << "Debugging: [case:m==12 && d=31], output:" << m << "," << d;}
else {if (d=days_in_month) {d = 1; m = m + 1; cout << endl << "Debugging: [case:d=days_in_month], output:" << d << "=" << "days_in_month";}
else {if (m==12) {d = d + 1; cout << endl << "Debugging: | [case:m==12], output:" << m;}
else cout << endl << "Debugging: [date not caught in conditions], output.";};};};};
cout << endl << endl << "The next day is: " << y << "-" << m << "-" << d << "." << endl;
}
int main()
{
int y;
int m;
int d;
int days_in_month;
cout << "Enter Year (no BC dates): ";
cin >> y;
bool lyear = leapyear(y);
cout << endl << "Enter Month (1 - 12): ";
cin >> m;
cout << "Enter Day (1 - 31): ";
cin >> d;
days_in_month = valid_date(y, m, d, lyear);
if (days_in_month == 0) {cout << "Terminating."; return false;}
add_date(y, m, d, days_in_month, lyear);
return 0;
} //main
//} // namespace DateIncrement
答案 0 :(得分:0)
传统上标题不会“做”他们声明的东西 - 它们告诉编译器期望什么,特定源文件(或二进制模块)中可用的设施。但它有点混乱,因为有时代码被放入标题中,因此它可以“实例化”到动态源文件中。但不在你的标题中。您的标题只定义了类的功能 - 让编译器知道它可以做什么。