2 ruiruiddd ruiruiddd 于 2016.04.28 15:34 提问

各位大神帮忙看看哪里写错了,修改下谢谢 C++ 入门
c++

================================================
dateType.h
================================================
#include
using namespace std;
class dateType {
private:
int year, month, day;
public:
void setDate(int month, int day, int year);
void setMonth(int month);
void setDay(int day);
void setYear(int year);
void print();
int numberOfDaysPassed();
int numberOfDaysLeft();
void incrementDays(int num);
int getMonth();
int getYear();
int getDay();
int getDaysInMonth();
int getDaysInMonth(int month);
bool isLeapYear();
dateType(int = 1, int = 1, int = 1900);
~dateType() {}
};

================================================
dateTypelmp.cpp
================================================
#include "dateType.h"

void dateType::setDate(int _month, int _day, int _year) {
year = _year;
month = _month;
day = _day;
}
dateType::dateType(int _month, int _day, int _year) {
year = _year;
month = _month;
day = _day;
}
void dateType::setMonth(int _month) {
month = _month;
}

void dateType::setDay(int _day) {
day = _day;
}

void dateType::setYear(int _year) {
year = _year;
}

void dateType::incrementDays(int num) {
int leftDays = numberOfDaysLeft();
while (num>leftDays) {
year++;
num -= leftDays;
leftDays = numberOfDaysLeft();
month = 1;
day = 0;
}

while (num>(getDaysInMonth() - day)) {

    num -= (getDaysInMonth() - day);
    month++;
    day = 0;
}
day += num;

}
int dateType::getDaysInMonth() {
return getDaysInMonth(month);
}
int dateType::getDaysInMonth(int _month) {
int days = 31;
switch (_month) {
case 2:
if (isLeapYear()) {
days = 29;
}
else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
}
return days;
}
bool dateType::isLeapYear() {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int dateType::getYear() {
return year;
}
int dateType::getMonth() {
return month;
}
int dateType::getDay() {
return day;
}
void dateType::print() {
cout << "date=" << month << "-" << day << "-" << year << endl;
}

int dateType::numberOfDaysPassed() {

int passedDays = 0;
for (int i = 1; i<month; i++) {
    passedDays += getDaysInMonth(i);
}
passedDays += day;
return passedDays;

}

int dateType::numberOfDaysLeft() {
int years = 365;
if (isLeapYear()) {
years = 366;
}
return years - numberOfDaysPassed();
}

int main(void)
{
dateType date1(3, 15, 2008);
date1.print();
date1.setDate(10, 10, 2010);
date1.print();

date1.setMonth(12);
date1.setDay(12);
date1.setYear(2012);
date1.print();

cout << "Elapesed Days since 1/1/2012 = " << date1.numberOfDaysPassed() << endl
    << "Days remaining in 2012 = " << date1.numberOfDaysLeft() << endl;
date1.incrementDays(378);
cout << date1.getMonth() << "/"
    << date1.getDay() << "/"
    << date1.getYear() << "/\n";
cout << "there are " << date1.getDaysInMonth() << " in this month\n"
    << "and this year " << (date1.isLeapYear() ? "is" : "is not ") << "a leap year\n";
cin.get();

system("pause");
return 0;

}

5个回答

ruiruiddd
ruiruiddd   2016.04.28 15:35

这个是我运行后的图片说明

ruiruiddd
ruiruiddd   2016.04.28 15:35

这个是老师给的纸上面的测试图片说明

xianfajushi
xianfajushi   2016.04.28 18:05

照抄书本的自己从头对照每行代码就是了,也是一种学习.

xialeichang
xialeichang   2016.05.03 15:42

发现一个问题,在最后的方框里你把numberOfDaysPassed()函数写为date1.numberOfDaysPassed() ,这个与你照片上不一样,但是你应该是把函数都放在类里面,这样与照片上的代码风格不同,入门时你还是把函数与书本上的对照写吧,下次贴图记得贴完整哦

xialeichang
xialeichang   2016.05.03 15:42

发现一个问题,在最后的方框里你把numberOfDaysPassed()函数写为date1.numberOfDaysPassed() ,这个与你照片上不一样,但是你应该是把函数都放在类里面,这样与照片上的代码风格不同,入门时你还是把函数与书本上的对照写吧,下次贴图记得贴完整哦

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!