نتائج غير صحيحة من بعض الفانكشنز في الكلاس

#ifndef dateType_H
#define dateType_H

class dateType
{
 public:
     void setMonth(int&);
     void setDay(int&);
     void setYear(int&);
     int getMonth()const;
     int getDay()const;
     int getYear()const;
     bool isLeap();
     int numOfDaysInMonth();
     int numOfDaysPassedInYear();
     int numOfDaysRemainingInYear();
     void newDate(int&);
 //get the new date after adding the input number of day in the parameter, postcondition the new date is returned

     dateType();
     dateType(int, int, int);
 private:
    int month;
    int day;
    int year;

};

#endif
//end class defention

#include <iostream>
#include "dateType.h"
using namespace std;

void dateType::setMonth(int& m)
{
    if (m > 0 && m <= 12)
        month = m;
    else
    {
        cout << "enter a correct number of month " << endl;
        cin >> m;
        setMonth(m);
    }
}
void dateType::setDay(int& d)
{
    if (d > 0 && d <= numOfDaysInMonth())
        day = d;
    else
    {
        cout << "enter a correct number of day" << endl;
        cin >> d;
        setDay(d);
    }
}
void dateType::setYear(int& y)
{
    if (y > 0)
        year = y;
    else
    {
        cout << "enter a correct number of year" << endl;
        cin >> y;
        setYear(y);
    }
}
int dateType::getMonth()const
{
    return month;
}
int dateType::getDay()const
{
    return day;
}
int dateType::getYear()const
{
    return year;
}
bool dateType::isLeap()
{
    return (year % 4 == 0);
}
int dateType::numOfDaysInMonth()
{
    switch (month)
    {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 2:
            {
            if (isLeap())
                return 29;
            return 28;
            }
            return 30;
    }
}
int dateType::numOfDaysPassedInYear() //wrong output
{
    int m = month;
    int counter = 0;
    for(month = 1; month < m; month++)
        counter += numOfDaysInMonth();
    month = m;
    return counter + day;
}
int dateType::numOfDaysRemainingInYear() //wrong output
{
    if(isLeap())
        return 366 - numOfDaysPassedInYear();
    return 365 - numOfDaysPassedInYear();
}
void dateType::newDate(int& num) //wrong output
{
    while (num > 0)
    {
        if (day < numOfDaysInMonth())
            day++;
        else if(month < 12)
        {
           day = 1;
           month++;
        }
        else
        {
            day = 1;
            month = 1;
            year++;
        }
        num--;
    }
    cout << "the new date is " << month << "-" << day << "-" << year << endl;
}
dateType::dateType()
{
    month = 1;
    day = 1;
    year = 1990;
}
dateType::dateType(int m, int d, int y)
{
    setMonth(m);
    setDay(d);
    setYear(y);
}
 // end implement

#include <iostream>
#include "dateType.h"

using namespace std;

int main()
{
    dateType object;
    int month, day, year, num;

    cout << "enter the month, day, year respectively ";
    cin >> month >> day >> year;
    cout << endl;
    object.setMonth(month);
    object.setYear(year);
    object.setDay(day);
    cout << "the date you've entered is " << object.getMonth() << "-" << object.getDay() << "-" << object.getYear() << endl;
    if (object.isLeap())
        cout << "this year is leap year" << endl;
    else
        cout << "this year is not leap year" << endl;
    cout << "the month " << object.getMonth() << " consists of " << object.numOfDaysInMonth() << " days" << endl;
    cout << "the number of days passed in this year " << object.getYear() << " is " << object.numOfDaysPassedInYear() << endl;
    cout << "the number of days remaining in this year " << object.getYear() << " is " << object.numOfDaysRemainingInYear() << endl;
    cout << "enter a number to get the new date after passing this number ";
    cin >> num;
    cout << endl;
    object.newDate(num);



    return 0;
}


//end main

 

 
الفنكشنز المسئولة عن إخراج عدد الايام الباقية, عدد الايام الماضية, و التاريخ الجديد لا يخرجون نتائج صحيحة

أضف اجابة

أضف اجابة

‫تصفح