源程序:
#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;
}
运行结果:
写入文件: