C++求高手!定义一个三角形类Ctriangle,求三角形的面积和周长。我写了一个不知哪错,请附上答案或改正的

#include <iostream>
#include <cmath>
using namespace std;
class Ctriangle
{
public:
void get_value();
double area();
float circumference();
void display1();
void display2();
public:
double l1;
double l2;
double l3;
double s;
};
void Ctriangle::get_value()
{
cout<<"20124140李子莹计算机2班"<<endl;
cout<<"请输入三角形的三边长:";
cin>>l1;
cin>>l2;
cin>>l3;
}
double Ctriangle::area()
{
return(sqrt(s*(s-l1)*(s-l2)*(s-l3)));
}
float Ctriangle::circumference()
{
return(l1+l2+l3);
}
void Ctriangle::display1()
{
cout<<area()<<endl;
}
void Ctriangle::display2()
{
cout<<circumference()<<endl;
}
double main()
{
double area();
double l1;
double l2;
double l3;
double s;
if(l1+l2>l3&&l2+l3>l1&&l1+l3>l2)
{
s=(l1+l2+l3)/2;
area=sqrt(s*(s-l1)*(s-l2)*(s-l3));
}
Ctriangle ctriangle;
ctriangle.get_value();
ctriangle.area();
cout<<"三角形的面积:";
ctriangle.display1();
ctriangle.circumference();
cout<<"三角形的周长:";
ctriangle.display2();
return 0;
}

#include <iostream>

#include <cmath>

using namespace std;

class Ctriangle

{

public:

 void get_value();

 double area();

 float circumference();

 void display1();

 void display2();

public:

 double l1;

 double l2;

 double l3;

 double s;

};

void Ctriangle::get_value()

{

 cout<<"20124140李子莹计算机2班"<<endl;

 cout<<"请输入三角形的三边长:";

 cin>>l1;

 cin>>l2;

 cin>>l3;

}

double Ctriangle::area()

{

 double s=(l1+l2+l3)/2;

 return(sqrt(s*(s-l1)*(s-l2)*(s-l3)));

}

float Ctriangle::circumference()

{

 return(l1+l2+l3);

}

void Ctriangle::display1()

{

 cout<<area()<<endl;

}

void Ctriangle::display2()

{

 cout<<circumference()<<endl;

}

double main()

// double area1;

// double l1;

// double l2;

 //double l3;

/// double s;

// if(l1+l2>l3&&l2+l3>l1&&l1+l3>l2)

 //{

  //s=(l1+l2+l3)/2;

  //area1=sqrt(s*(s-l1)*(s-l2)*(s-l3));

 //}

 Ctriangle ctriangle;

 ctriangle.get_value();

 ctriangle.area();

 cout<<"三角形的面积:";

 ctriangle.display1();

 ctriangle.circumference();

 cout<<"三角形的周长:";

 ctriangle.display2();

 return 0;

}


感觉你上面写了很多无用功,注释掉了,area加一个s变量,就可以运行了

温馨提示:答案为网友推荐,仅供参考
相似回答