第五周项目 - 三角形类雏形2345

Posted _ZHRui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第五周项目 - 三角形类雏形2345相关的知识,希望对你有一定的参考价值。

2)在上面程序的基础上,重新定义Triangle类,其中逻辑特别简单的set和get成员函数,要处理为内联成员函数,直接在类内定义。

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()  
{  
    Triangle tri1;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义三角形类的一个实例(对象)  </span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> x,y,z;  
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入三角形的三边:"</span>;  
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span>>>x>>y>>z;  
    tri1.setA(x);  
    tri1.setB(y);  
    tri1.setC(z);   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//为三边置初值  </span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(tri1.isTriangle())  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//isTriangle()返回值为布尔型,用于判断给出的三边是否能构成一个三角形</span>
    {  
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"三条边为:"</span><<tri1.getA()<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">','</span><<tri1.getB()<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">','</span><<tri1.getC()<<endl;  
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"三角形的周长为:"</span><< tri1.perimeter()<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\t'</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"面积为:"</span><< tri1.area()<<endl;  
    }  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>  
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"不能构成三角形"</span><<endl;  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;  
</code><p><code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">}  </code><code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">
</code><code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"></code><pre name="code" class="cpp">#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
    void setA(double x)
    {
        a=x;
    };
    void setB(double y)
    {
        b=y;
    };
    void setC(double z)
    {
        c=z;
    };

    double getA()
    {
        return a;
    }
    double getB()
    {
        return b;
    }
    double getC()
    {
        return c;
    }
    bool isTriangle();//判断是否构成三角形
    double perimeter(void);//计算三角形的周长
    double area(void);//计算并返回三角形的面积
private:
    double a,b,c; //三边为私有成员数据
};
int main()
{
    Triangle tri1;  //定义三角形类的一个实例(对象)
    double x,y,z;
    cout<<"请输入三角形的三边:";
    cin>>x>>y>>z;
    tri1.setA(x);
    tri1.setB(y);
    tri1.setC(z);   //为三边置初值
    if(tri1.isTriangle())
    {
        cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
        cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
    }
    else
        cout<<"无法构成三角形"<<endl;
    return 0;
}
//请在下面定义Triangle类中的各个成员函数
bool Triangle::isTriangle()
{
    return (a+b>c&&a+c>b&&b+c>a);
}

double Triangle::perimeter()
{
    return a+b+c;
}

double Triangle::area()
{
    double p=(a+b+c)/2;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}

技术分享
技术分享



3)设计三角形类带参数的构造函数,即Triangle(double x, double y, double z),三边长在调用时由实参直接给出,使对象在定义时能够进行初始化。需要的测试函数是:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    Triangle Tri(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>);    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义三角形类的一个实例(对象)</span>
    Tri.showMessage(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义一个能显示三角形信息的成员函数</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code>
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
    Triangle(double x, double y, double z)//(1)使用带参数构造函数
    {
        a=x;
        b=y;
        c=z;
    }
    double perimeter();//计算三角形的周长
    double area();//计算并返回三角形的面积
    void showMessage();
private:
    double a,b,c; //三边为私有成员数据
};

double Triangle::perimeter()
{
    return (a + b + c);
}

double Triangle::area()
{
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

void Triangle::showMessage()
{
    cout<<"三角形的三边长分别为:"<<a<<' '<<b<<' '<<c<<endl;
    cout<<"该三角形的周长为"<<perimeter()<<",面积为"<<area()<<endl<<endl;
}


int main()
{
    Triangle Tri(7,8,9);    //定义三角形类的一个实例(对象)
    Tri.showMessage();
    return 0;
}

技术分享

(4)使用有默认参数的构造函数,不给定实参时,默认边长为1;注意——只需要一个构造函数即可。需要的测试函数是:

<code class="hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">int <span class="hljs-function" style="box-sizing: border-box;">main()</span>
{
    Triangle Tri1;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义边长为1 1 1的三角形类实例</span>
    Tri1<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    Triangle <span class="hljs-function" style="box-sizing: border-box;">Tri2(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.5</span>)</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义边长为1.5 1 1的三角形类实例</span>
    Tri2<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    Triangle <span class="hljs-function" style="box-sizing: border-box;">Tri3(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.5</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.5</span>)</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义边长为1.5 1.5 1的三角形类实例</span>
    Tri3<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    Triangle <span class="hljs-function" style="box-sizing: border-box;">Tri4(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>)</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义边长为7 8 9的三角形类实例</span>
    Tri4<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    return 0;
}</code>
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
    Triangle(double x=1, double y=1, double z=1);
    double perimeter();//计算三角形的周长
    double area();//计算并返回三角形的面积
    void showMessage();
private:
    double a,b,c; 
};

Triangle::Triangle(double x, double y, double z)
{
    a=x;
    b=y;
    c=z;
}

double Triangle::perimeter()
{
    return (a + b + c);
}

double Triangle::area()
{
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

void Triangle::showMessage()
{
    cout<<"三角形的三边长分别为:"<<a<<' '<<b<<' '<<c<<endl;
    cout<<"该三角形的周长为"<<perimeter()<<",面积为"<<area()<<endl<<endl;
}

int main()
{
    Triangle Tri1;   //定义边长为1 1 1的三角形类实例
    Tri1.showMessage();
    Triangle Tri2(1.5);//定义边长为1.5 1 1的三角形类实例
    Tri2.showMessage();
    Triangle Tri3(1.5,1.5);//定义边长为1.5 1.5 1的三角形类实例
    Tri3.showMessage();
    Triangle Tri4(7,8,9); //定义边长为7 8 9的三角形类实例
    Tri4.showMessage();
    return 0;
}

技术分享

(5)使用复制构造函数,能用一个已有的三角形对象,复制出新的三角形对象:

<code class="hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">int <span class="hljs-function" style="box-sizing: border-box;">main()</span>
{
    Triangle <span class="hljs-function" style="box-sizing: border-box;">Tri1(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)</span>;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定义边长为1 1 1的三角形类实例</span>
    Tri1<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    Triangle <span class="hljs-function" style="box-sizing: border-box;">Tri2(Tri1)</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//调用复制构造函数</span>
    Tri2<span class="hljs-class" style="box-sizing: border-box;">.showMessage</span>();
    return 0;
}</code>
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
    Triangle(double x=1, double y=1, double z=1);
    Triangle(const Triangle &t);
    double perimeter();  //计算三角形的周长
    double area();   //计算返回三角形的面积
    void showMessage();
private:
    double a,b,c;
};

Triangle::Triangle(double x, double y, double z)
{
    a=x;
    b=y;
    c=z;
}

Triangle::Triangle(const Triangle &t)
{
    a=t.a;
    b=t.b;
    c=t.c;
}

double Triangle::perimeter()
{
    return (a + b + c);
}

double Triangle::area()
{
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

void Triangle::showMessage()
{
    cout<<"三角形的三边长分别为:"<<a<<' '<<b<<' '<<c<<endl;
    cout<<"该三角形的周长为"<<perimeter()<<",面积为"<<area()<<endl<<endl;
}
int main()
{
    Triangle Tri1(3,4,5);   //定义边长为1 1 1的三角形类实例
    Tri1.showMessage();
    Triangle Tri2(Tri1); //调用复制构造函数
    Tri2.showMessage();
    return 0;
}

技术分享














以上是关于第五周项目 - 三角形类雏形2345的主要内容,如果未能解决你的问题,请参考以下文章

软件项目管理第五周作业

20165236 第五周学习总结

201671010139 徐楠 第五周总结

第五周学习总结

20165318 2017-2018-2 《Java程序设计》第五周学习总结

(第五周)团队项目1