鸾凤吧 关注:64贴子:2,633
  • 3回复贴,共1

【情人节】写的一点东西

只看楼主收藏回复

一共一百一十一行


IP属地:美国1楼2011-02-15 13:21回复
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    typedef double single;
    class SinglePersonProblem : protected exception
    {
    public:
       SinglePersonProblem(string reason = "The biggest problem is that you are Single") throw():m_reason(reason){};
       ~SinglePersonProblem()throw(){};
       const char* what() const throw(){return m_reason.c_str();};
    private:
       mutable string m_reason;
    };
    class Person
    {
    private:
       static const single MagicianStartAge;
       single single_age;
       bool isMagician;
       void AddAge(single age);
    public:
       explicit Person(single age = 21);
       Person operator ++(int);
       Person& operator ++();
       Person& operator + (single age);
       double operator+ (const Person& anotherSinglePerson);
       double operator* (const Person& anotherSinglePerson);
       bool operator==(const Person& anotherSinglePerson) const;
       bool operator!=(const Person& anotherSinglePerson) const;
    };
    const single Person::MagicianStartAge = 30;
    Person::Person(single age): single_age(0), isMagician(false)
    {
       if (age < 0)
       {
       SinglePersonProblem problem("An \"anti-single\" person should be a dead person");
       throw problem;
       }
       AddAge(age);
    }
    Person Person::operator ++(int)
    {
       Person earlierPerson(single_age);
       AddAge(1);
       return earlierPerson;
    }
    Person& Person::operator ++()
    {
       AddAge(1);
       return *this;
    }
    Person& Person::operator + (single age)
    {
       AddAge(age);
       return *this;
    }
    double Person::operator+ (const Person& anotherSinglePerson)
    {
       SinglePersonProblem problem("Two Single Person all together are still Two Single Person! It will never return a double!");
       throw problem;
    }
    double Person::operator* (const Person& anotherSinglePerson)
    {
       SinglePersonProblem problem("Two Single Person multiple each other are still Two Single Person! It will never return a double!");
       throw problem;
    }
    bool Person::operator==(const Person& anotherSinglePerson) const
    {
       bool ret = false;
       if (NULL != &anotherSinglePerson)
       if (this->single_age == anotherSinglePerson.single_age)
       {
       cout << "Good, you got a buddy now!" << endl;
       ret = true;
       }
       return ret;
    }
    bool Person::operator!=(const Person& anotherSinglePerson) const
    {
       return !(*this == anotherSinglePerson);
    }
    void Person::AddAge(single age)
    {
       if (age < 0)
       {
       SinglePersonProblem problem("Forget about that! Don't try to decrease your single age!");
       throw problem;
       }
       single_age += age;
       if (single_age >= MagicianStartAge && !isMagician)
       {
       isMagician = true;
       cout << "Congratulations! You're a " << single_age << "-year-old Magician Now!" << endl;
       }
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
       try
       {
       Person a, b, c, d(25);
       d++;
       a*b;
       }
       catch (SinglePersonProblem& e)
       {
       cout<< "Exception: " << e.what() << endl;
       }
       return 0;
    }


    IP属地:美国2楼2011-02-15 13:24
    回复


      IP属地:广东3楼2011-02-15 18:14
      回复
        判断是不是魔法师的程序?
        没有很仔细看


        IP属地:湖北4楼2011-07-23 01:48
        回复