自考新教材-p328_5(1)

源程序:

#include <iostream>
#include <fstream>
using namespace std;

class triangle
{
private:
int a, b, c;
double area;
public:
triangle(int aa, int bb, int cc)
{
a = aa;
b = bb;
c = cc;
}
double triangle_area()
{
int l = (a + b + c) / 2;
area = sqrt(l*(l-a)*(l-b)*(l-c));
return area;
}
void show()
{
cout << "三角形的面积为:"<<area << endl;
}
void file_write()
{
ofstream txt1;
txt1.open("c:\\tmp\\mytxt1.txt",ios::out);
txt1 << "三角形的面积为:";
txt1 << area << endl;
}
};
int main()
{
//triangle tri;
int a1, b1, c1;
cout << "请输入三角形三条边:";

cin >> a1 >> b1 >> c1;
while ((a1 + b1) <= c1 || (a1 + c1) <= b1 || (c1 + b1) <= a1)
{
cout << "三解形三边不合理,请重新输入!" << endl;
cin >> a1 >> b1 >> c1;
}
triangle trian(a1,b1,c1);
//trian.input();
trian.triangle_area();
trian.show();
trian.file_write();
system("pause");
return 1;
}

运行结果:

自考新教材-p328_5(1)

 

 写入文件:

自考新教材-p328_5(1)

 

上一篇:计算机图形学(六):1. Triangle类,以及Ray Triangle的求交,画出一个红红的三角形来


下一篇:变换