求C++高手编个小程序

2024年11月23日 07:28
有1个网友回答
网友(1):

#include 

using namespace std;

typedef double number;

inline double sqr(const double &x){ return x*x; };

class point

{

    private:

        number x,y;

    public:

        point():x(0),y(0){}//用0分别初始化x,y,就是point(){ x=0;y=0 }下同 

        point(number _x,number _y):x(_x),y(_y){}

        point(const point &another):x(another.x),y(another.y){}

        ~point(){}//没在堆内申请空间,不用管析构函数,甚至可以不写 

        

        friend number length(const point &a,const point &b){ return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)); }

        //因为line里面求两点距离要访问点内的数据,干脆加个友元 ,sqrt是cmath里的,sqr是才定义的 

};

class line

{

    private:

        point beg,end;

    public:

        line(point b,point e):beg(b),end(e){}

        ~line(){}

        

        number distance(){ return length(beg,end); }

};

class complex

{

    private:

        number r,ir;//实,虚 

    public:

        complex():r(0),ir(0){}//初始化 

        complex(number _r,number _ir=0):r(_r),ir(_ir){}//实数情况_ir=0 例complex a(3.5); 

        complex(const complex &another):r(another.r),ir(another.ir){}

        ~complex(){}

        

        complex operator+(const complex &b){ return complex(r+b.r,ir+b.ir); }//重载运算符 a.operator+(b) 就是a+b 

        complex operator-(const complex &b){ return complex(r-b.r,ir-b.ir); }

};

#include 

class people

{

    private:

        int number;

        string name;

        int sex;//1 for male,2 for famale

        class Date{//format:****.**.**

            public:

                unsigned year,month,day;

                Date(unsigned y,unsigned m,unsigned d):year(y),month(m),day(d){}

        } birthday;//同时定义类和申请对象 相当于class Date{......};Data birthday; 因为内嵌懒得用private了 

    public:

        people():number(-1),name(""),sex(0),birthday(0,0,0){}//Date的构造函数有三参数

        people(const people &b):number(b.number),name(b.name),sex(b.sex),birthday(b.birthday.year,b.birthday.month,b.birthday.day){}

        //成员函数 

        people &set(int n,const string &nm,int s,unsigned year,unsigned month,unsigned day)

        {

            number=n;

            name=nm;

            sex=s;

            birthday.year=year;

            birthday.month=month;

            birthday.day=day;

            return(*this);

        }

        people &show()

        {//就用C的printf算了,用ostream的写在下面 

            string _sex;

            switch(sex)

            {

                case 0:_sex="未知";break;

                case 1:_sex="男  ";break;

                case 2:_sex="女  ";break;

            }

            printf("%d %s %s 出生日期:%d/%d/%d",number,&name[0],&_sex[0],birthday.year,birthday.month,birthday.day);

            return (*this);

        }

        //运算符重载 

        friend ostream &operator<<(ostream &o,const people&p)

        {

            o<

            //用三元运算符 ?:作判断省去if或switch

            return o;

        }

};

int main()

{

    people a;

    a.set(7,"kevin",1,1992,4,12);

    people b(a);

    b.show();

    cout<

    system("pause");

    return 0;

//不会吧,我用devcpp编译成功运行正确

//你把便一起编译器、错误都给我吧