Skip to main content
Fixed some code formatting.
Source Link
user1118321
  • 11.8k
  • 1
  • 19
  • 46
#include <iostream>
#include <string>

using namespace std;

class date
{
public:
    date() = default;
    date(istream &is) { is >> year >> month >> day; }

    bool organizeInfo();
    bool organize();
    ostream &outputInfo(std::ostream &os) { os << year << " " << month << " " << day; return os; }

private:

    bool checkValid();
    bool checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3);
    void switchItems(unsigned &item1, unsigned &item2) { unsigned tempItem1 = item1; item1 = item2; item2 = tempItem1; }

    unsigned year = 0;
    unsigned month = 0;
    unsigned day = 0;
};

inline bool date::checkValid()
{
    if (month <= 12 && day <= 31)
        return 1;
    return 0;
}

inline bool date::checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3)
{
    switchItems(item1, item2);
    if (checkValid())
        return 1;
    else
    {
            switchItems(item1, item2);
            switchItems(item2, item3);
            if (checkValid())
            return 1;
        else
        {
            switchItems(item2, item3);
            switchItems(item1, item3);
            if (checkValid())
                return 1;
            else
            {
                return 0;
            }
        }
    }
}

inline bool date::organize()
{
    if (checkValid())
        return 1;
    else
    {
        if (checkSwitch(day, month, year))
            return 1;
        else if (checkSwitch(year, month, day))
            return 1;
        else if (checkSwitch(day, year, month))
            return 1;
        return 0;
    }
}

bool date::organizeInfo()
{
    if (checkValid())
        return 1;
    else
    {
        if (organize())
            return 1;
        else
            return 0;
    }
    return 0;
}

int main()
{
    cout << "Enter year, month, date and ill try and organize it" << endl;
    date dawg(cin);
    if(dawg.organizeInfo())
        dawg.outputInfo(cout) << endl;
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

class date
{
public:
    date() = default;
    date(istream &is) { is >> year >> month >> day; }

    bool organizeInfo();
    bool organize();
    ostream &outputInfo(std::ostream &os) { os << year << " " << month << " " << day; return os; }

private:

    bool checkValid();
    bool checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3);
    void switchItems(unsigned &item1, unsigned &item2) { unsigned tempItem1 = item1; item1 = item2; item2 = tempItem1; }

    unsigned year = 0;
    unsigned month = 0;
    unsigned day = 0;
};

inline bool date::checkValid()
{
    if (month <= 12 && day <= 31)
        return 1;
    return 0;
}

inline bool date::checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3)
{
    switchItems(item1, item2);
    if (checkValid())
        return 1;
    else
    {
            switchItems(item1, item2);
            switchItems(item2, item3);
            if (checkValid())
            return 1;
        else
        {
            switchItems(item2, item3);
            switchItems(item1, item3);
            if (checkValid())
                return 1;
            else
            {
                return 0;
            }
        }
    }
}

inline bool date::organize()
{
    if (checkValid())
        return 1;
    else
    {
        if (checkSwitch(day, month, year))
            return 1;
        else if (checkSwitch(year, month, day))
            return 1;
        else if (checkSwitch(day, year, month))
            return 1;
        return 0;
    }
}

bool date::organizeInfo()
{
    if (checkValid())
        return 1;
    else
    {
        if (organize())
            return 1;
        else
            return 0;
    }
    return 0;
}

int main()
{
    cout << "Enter year, month, date and ill try and organize it" << endl;
    date dawg(cin);
    if(dawg.organizeInfo())
        dawg.outputInfo(cout) << endl;
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

class date
{
public:
    date() = default;
    date(istream &is) { is >> year >> month >> day; }

    bool organizeInfo();
    bool organize();
    ostream &outputInfo(std::ostream &os) { os << year << " " << month << " " << day; return os; }

private:

    bool checkValid();
    bool checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3);
    void switchItems(unsigned &item1, unsigned &item2) { unsigned tempItem1 = item1; item1 = item2; item2 = tempItem1; }

    unsigned year = 0;
    unsigned month = 0;
    unsigned day = 0;
};

inline bool date::checkValid()
{
    if (month <= 12 && day <= 31)
        return 1;
    return 0;
}

inline bool date::checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3)
{
    switchItems(item1, item2);
    if (checkValid())
        return 1;
    else
    {
        switchItems(item1, item2);
        switchItems(item2, item3);
        if (checkValid())
            return 1;
        else
        {
            switchItems(item2, item3);
            switchItems(item1, item3);
            if (checkValid())
                return 1;
            else
            {
                return 0;
            }
        }
    }
}

inline bool date::organize()
{
    if (checkValid())
        return 1;
    else
    {
        if (checkSwitch(day, month, year))
            return 1;
        else if (checkSwitch(year, month, day))
            return 1;
        else if (checkSwitch(day, year, month))
            return 1;
        return 0;
    }
}

bool date::organizeInfo()
{
    if (checkValid())
        return 1;
    else
    {
        if (organize())
            return 1;
        else
            return 0;
    }
    return 0;
}

int main()
{
    cout << "Enter year, month, date and ill try and organize it" << endl;
    date dawg(cin);
    if(dawg.organizeInfo())
        dawg.outputInfo(cout) << endl;
    return 0;
}
Tweeted twitter.com/StackCodeReview/status/678325855408537600
edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
edited title
Source Link
user34073
user34073

Enter year, date, and month in antany order ill, I'll organize it

I would like some advice on how I could make it less codyrepetitive and more use ofmore functions and classes.

Enter year, date, month in ant order ill organize it

I would like some advice on how I could make it less cody and more use of functions and classes.

Enter year, date, and month in any order, I'll organize it

I would like some advice on how I could make it less repetitive and use more functions and classes.

Source Link
Loading