200317////////作业

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;
class student
{
public:
    char *name;
    string sex;
    int num;
    int age;
    int score1,score2,score3;
    int avg;
public:
    student(char *name_,string sex_,int num_,int age_,int score1_,int score2_,int score3_)
    {
        name=new char[strlen(name_)+1];
        strcpy(name,name_);
        sex=sex_;
        num=num_;
        age=age_;
        score1=score1_;
        score2=score2_;
        score3=score3_;
    }
    ~student()
    { delete[]name;}
    student(const student&stu);
    void AVG()
    {
        avg=(score1+score2+score3)/3;
        cout<<"平均分= "<<avg<<endl;
    }
    void output()
    {
        cout<<"姓名:"<<name<<" "<<"性别:"<<sex<<' '<<"学号: "<<num<<' '<<"年龄: "<<age<<endl;
        cout<<"三科分数:"<<score1<<" "<<score2<<' '<<score3<<endl;
    }
};
student::student(const student&stu)
{
    this->score1=stu.score1;
    this->score2=stu.score2;
    this->score3=stu.score3;
}
int main()
{
    student stu1("QWE","M",1313,17,90,92,94),avg(stu1);
    stu1.output();
    avg.AVG();
}

在这里插入图片描述

法一:


#include<iostream>
using namespace std;
#include<cmath>
class point
{
    double x,y;
public:
    point(double xx=0,double yy=0)
    { x=xx;y=yy; }
    void setxy(double xx,double yy)
    { x=xx;y=yy; }
    void getxy()
    {cout<<x<<" "<<y<<endl; }
    double getx(){return x;}
    double gety(){return y;} };
class line
{
    point point1,point2;
    double len; double k;
public:
    void length(point a,point b)
    { len=sqrt((a.getx()-b.getx())*(a.getx()-b.getx())+ (a.gety()-b.gety())*(a.gety()-b.gety())); }
    void getlen() { cout<<len<<endl; }
    void getline(point a,point b)
    { k=(a.gety()-b.gety())*1.0/(a.getx()-b.getx()); cout<<"y-"<<a.gety()<<"=" <<k<<"(x-"<<a.getx()<<")"<<endl; } };
int main()
{
    point point11;
    point11.getxy();
    point point22(4,3);
    point22.getxy();
    line line1;
    line1.length(point11,point22);
    line1.getlen();
    line1.getline(point11,point22);
}


法二:第一次做的时候长度结果一直是0,后来才发现是因为两个点的值没有传到计算的函数中。


#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
    int x,y;
public:
    int setx(){return x;}
    int sety(){return y;}
    Point(int xx=0,int yy=0);
    Point(Point&p);
    void ShowPoint();
};
Point ::Point(int xx,int yy)
{ x=xx;y=yy;}
Point::Point(Point&p)
{
    x=p.x;
    y=p.y;
}
void Point::ShowPoint()
{ cout<<"x= "<<x<<" "<<"y= "<<y<<endl;}
class line
{
private:
    Point p1,p2;
    double len;
public:
    line(Point xp1,Point xp2)
    {Point p1(xp1);Point p2(xp2);}
    line(line&l)
    { len=l.len;}
    double Getlen(Point&p1,Point &p2)//注意这里
    {
        len=sqrt((p1.setx()-p2.setx())*(p1.setx()-p2.setx())-(p1.sety()-p2.sety())*(p1.sety()-p2.sety()));
        cout<<len<<endl;
    }
};
int main()
{
    Point p11(2,1),p22(1,1);
    p11.ShowPoint();
    p22.ShowPoint();
    line l(p11,p22);
    l.Getlen(p11,p22);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值