![](http://imgsrc.baidu.com/forum/pic/item/5782ba99a9014c086cc14c6f067b020879f4f4ac.jpg?v=tbs)
#include <iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
class point
{
public:
float x1, y1, x2, y2, x3, y3;
point(float x1, float y1, float x2, float y2, float x3, float y3)
: x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3) {
cout << a() << endl; cout << b() << endl; cout << c() << endl;
}
float a() { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); }
float b() { return (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); }
float c() { return (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); }
};
class line :public point
{
public:
line(float x1, float y1, float x2, float y2, float x3, float y3)
:point(x1, y1, x2, y2, x3, y3) {
cout << len1() << endl; cout << len2() << endl; cout << len3() << endl;
}
float len1() { return sqrt(a()); }
float len2() { return sqrt(b()); }
float len3() { return sqrt(c()); }
};
class triangle :public line
{
private:
float a = len1(), b = len2(), c = len3();
float max(float x, float y) { return x > y ? x : y; }
float min(float x, float y) { return x < y ? x : y; }
float maxside, minside, midside;
public:
triangle() :line(0, 0, 0, 0, 0, 0) {}
triangle(float x1, float y1, float x2, float y2, float x3, float y3)
:line(x1, y1, x2, y2, x3, y3) {}
bool triangle_condition();
void triangle_length_compare();
void triangle_judge();
void triangle_area();
void output_();
};
void menu_show() {
cout << "***************************************" << endl << endl
<< "*** 三角形的种类和面积-功能菜单 ***" << endl << endl
<< "* 1.输入坐标 *" << endl << endl
<< "* 2.判断三角形种类 *" << endl << endl
<< "* 3.根据构成的三角形计算三角形面积 * " << endl << endl
<< "* 4.输出结果 *" << endl << endl
<< "* 5.退出运行 *" << endl << endl
<< "***************************************" << endl << endl;
}
bool triangle::triangle_condition()//三角形构成条件
{
if (a + b > c&& a + c > b&& b + c > a)
return 1;
else
return 0;
}
void triangle::triangle_length_compare()
{
maxside = max(a, max(b, c));
minside = min(a, min(b, c));
midside = a + b + c - maxside - minside;
}
void triangle::triangle_judge() {
if (triangle_condition())
{
float result = maxside * maxside - minside * minside - midside * midside;
cout << "result:" << result << endl;
cout << "sizeof:" << sizeof(result) << endl;
if (maxside * maxside==minside * minside + midside * midside && abs(result)<= 1.15292e+15)
cout << endl << "直角三角形" << endl;
else if (maxside * maxside < minside * minside + midside * midside)
cout << endl << "锐角三角形" << endl;
else if (maxside * maxside > minside* minside + midside * midside)
cout << endl << "钝角三角形" << endl;
}
else
cout << "不能构成三角形,请重新输入" << endl;
}
void triangle::triangle_area() {//海伦公式求面积,适用所有三角形
float s, area;
if (triangle_condition())
{
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - c) * (s - b));
cout << "三角形面积=" << area << endl << endl;
}
else cout << "三角形面积不可求!" << endl << endl;
}
void triangle::output_() {
cout << "(" << x1 << "," << y1 << ")" << endl
<< "(" << x2 << "," << y2 << ")" << endl
<< "(" << x3 << "," << y3 << ")" << endl;
triangle_judge();
triangle_area();
}
void menu() {
cout << "|-------------------------------------|" << endl << "|" << endl
<< "| 三角形的种类和面积-功能菜单 |" << endl << "|" << endl
<< "| 1.输入坐标 |" << endl << "|" << endl
<< "| 2.判断三角形种类 |" << endl << "|" << endl
<< "| 3.根据构成的三角形计算三角形面积 |" << endl << "|" << endl
<< "| 4.输出结果 |" << endl << "|" << endl
<< "| 5.退出运行 |" << endl << "|" << endl
<< "|-------------------------------------|" << endl << endl;
}
/*
1)设计菜单实现功能选择
2)输入三角形的三个顶点坐标
3)判断三角形种类(锐角三角形、钝角三角形、直角三角形、或者不能构成三角形)
4)根据构成的三角形计算三角形面积
5)输出结果(包括:三个点的坐标、哪种三角形、三角形的面积值)*/
int main()
{
int menu;
float x1, x2, y1, y2, z1, z2;
while (1)
{
menu_show();
cout << "请选择菜单<1-5>:";
cin >> menu;
switch (menu)
{
case 1: {
cout << "输入三角形的三个顶点坐标:(x1, x2),(y1, y2), (z1, z2)" << endl;
cin >> x1 >> x2 >> y1 >> y2 >> z1 >> z2;
system("pause"); system("cls"); break; }
case 2: {
triangle a(x1, x2, y1, y2, z1, z2);
a.triangle_judge();
system("pause"); system("cls"); break; }
case 3: {
triangle a(x1, x2, y1, y2, z1, z2);
a.triangle_area();
system("pause"); system("cls"); break; }
case 4:
{triangle a(x1, x2, y1, y2, z1, z2);
a.output_();
system("pause"); system("cls"); break; }
case 5:system("pause"); exit(0);
default:cout << "请重新输入选择!\n"; system("pause"); system("cls"); break;
}
}
}
/*
double result = maxside * maxside - minside * minside - midside * midside ;
cout<< "result:"<<result<<endl;
if ((result < 0.0001&&result>0) ||(result>-0.0001&&result<0))*/
#include<cmath>
#include<cstdlib>
using namespace std;
class point
{
public:
float x1, y1, x2, y2, x3, y3;
point(float x1, float y1, float x2, float y2, float x3, float y3)
: x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3) {
cout << a() << endl; cout << b() << endl; cout << c() << endl;
}
float a() { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); }
float b() { return (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); }
float c() { return (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); }
};
class line :public point
{
public:
line(float x1, float y1, float x2, float y2, float x3, float y3)
:point(x1, y1, x2, y2, x3, y3) {
cout << len1() << endl; cout << len2() << endl; cout << len3() << endl;
}
float len1() { return sqrt(a()); }
float len2() { return sqrt(b()); }
float len3() { return sqrt(c()); }
};
class triangle :public line
{
private:
float a = len1(), b = len2(), c = len3();
float max(float x, float y) { return x > y ? x : y; }
float min(float x, float y) { return x < y ? x : y; }
float maxside, minside, midside;
public:
triangle() :line(0, 0, 0, 0, 0, 0) {}
triangle(float x1, float y1, float x2, float y2, float x3, float y3)
:line(x1, y1, x2, y2, x3, y3) {}
bool triangle_condition();
void triangle_length_compare();
void triangle_judge();
void triangle_area();
void output_();
};
void menu_show() {
cout << "***************************************" << endl << endl
<< "*** 三角形的种类和面积-功能菜单 ***" << endl << endl
<< "* 1.输入坐标 *" << endl << endl
<< "* 2.判断三角形种类 *" << endl << endl
<< "* 3.根据构成的三角形计算三角形面积 * " << endl << endl
<< "* 4.输出结果 *" << endl << endl
<< "* 5.退出运行 *" << endl << endl
<< "***************************************" << endl << endl;
}
bool triangle::triangle_condition()//三角形构成条件
{
if (a + b > c&& a + c > b&& b + c > a)
return 1;
else
return 0;
}
void triangle::triangle_length_compare()
{
maxside = max(a, max(b, c));
minside = min(a, min(b, c));
midside = a + b + c - maxside - minside;
}
void triangle::triangle_judge() {
if (triangle_condition())
{
float result = maxside * maxside - minside * minside - midside * midside;
cout << "result:" << result << endl;
cout << "sizeof:" << sizeof(result) << endl;
if (maxside * maxside==minside * minside + midside * midside && abs(result)<= 1.15292e+15)
cout << endl << "直角三角形" << endl;
else if (maxside * maxside < minside * minside + midside * midside)
cout << endl << "锐角三角形" << endl;
else if (maxside * maxside > minside* minside + midside * midside)
cout << endl << "钝角三角形" << endl;
}
else
cout << "不能构成三角形,请重新输入" << endl;
}
void triangle::triangle_area() {//海伦公式求面积,适用所有三角形
float s, area;
if (triangle_condition())
{
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - c) * (s - b));
cout << "三角形面积=" << area << endl << endl;
}
else cout << "三角形面积不可求!" << endl << endl;
}
void triangle::output_() {
cout << "(" << x1 << "," << y1 << ")" << endl
<< "(" << x2 << "," << y2 << ")" << endl
<< "(" << x3 << "," << y3 << ")" << endl;
triangle_judge();
triangle_area();
}
void menu() {
cout << "|-------------------------------------|" << endl << "|" << endl
<< "| 三角形的种类和面积-功能菜单 |" << endl << "|" << endl
<< "| 1.输入坐标 |" << endl << "|" << endl
<< "| 2.判断三角形种类 |" << endl << "|" << endl
<< "| 3.根据构成的三角形计算三角形面积 |" << endl << "|" << endl
<< "| 4.输出结果 |" << endl << "|" << endl
<< "| 5.退出运行 |" << endl << "|" << endl
<< "|-------------------------------------|" << endl << endl;
}
/*
1)设计菜单实现功能选择
2)输入三角形的三个顶点坐标
3)判断三角形种类(锐角三角形、钝角三角形、直角三角形、或者不能构成三角形)
4)根据构成的三角形计算三角形面积
5)输出结果(包括:三个点的坐标、哪种三角形、三角形的面积值)*/
int main()
{
int menu;
float x1, x2, y1, y2, z1, z2;
while (1)
{
menu_show();
cout << "请选择菜单<1-5>:";
cin >> menu;
switch (menu)
{
case 1: {
cout << "输入三角形的三个顶点坐标:(x1, x2),(y1, y2), (z1, z2)" << endl;
cin >> x1 >> x2 >> y1 >> y2 >> z1 >> z2;
system("pause"); system("cls"); break; }
case 2: {
triangle a(x1, x2, y1, y2, z1, z2);
a.triangle_judge();
system("pause"); system("cls"); break; }
case 3: {
triangle a(x1, x2, y1, y2, z1, z2);
a.triangle_area();
system("pause"); system("cls"); break; }
case 4:
{triangle a(x1, x2, y1, y2, z1, z2);
a.output_();
system("pause"); system("cls"); break; }
case 5:system("pause"); exit(0);
default:cout << "请重新输入选择!\n"; system("pause"); system("cls"); break;
}
}
}
/*
double result = maxside * maxside - minside * minside - midside * midside ;
cout<< "result:"<<result<<endl;
if ((result < 0.0001&&result>0) ||(result>-0.0001&&result<0))*/
![](http://imgsrc.baidu.com/forum/pic/item/5782ba99a9014c086cc14c6f067b020879f4f4ac.jpg?v=tbs)