当前位置:编程学习 > C/C++ >>

自定义--三角形类

[cpp]  
/*   
* 程序的版权和版本声明部分   
* Copyright (c)2013, 烟台大学计算机学院学生   
* All rightsreserved.   
* 文件名称:score.cpp                              
* 作    者:张浩                               
* 完成日期:2013年3月22日   
* 版本号: v1.0         
* 输入描述:  
* 问题描述:设计求三角形面积和周长的类  
* 程序输出:三角形的面积和周长   
*/      
#include<iostream>  
#include<Cmath>  
using namespace std;  
class Triangle  
{  
private:  
    double a,b,c;//三边为私有成员数据  
public:  
    void getABC(double *x,double *y,double *z);//取三边的值  
    double perimeter(void);//计算三角形的周长  
    double area(void);//计算并返回三角形的面积  
  
};  
void Triangle::getABC(double *x,double *y,double *z)  
{  
    a=*x;b=*y;c=*z;  
}  
      
double Triangle::perimeter()  
{  
    double m;  
    m=a+b+c;  
    return m;  
}  
double Triangle::area(void)  
{  
    double s,p;  
    p=(a+b+c)*0.5;  
    s=sqrt(p*(p-a)*(p-b)*(p-c));  
return s;  
}  
int main()  
{  
    Triangle tri1;  
  
    double x=0,y=0,z=0;  
    cout<<"请由小到大输入三角形的三条边长:"<<endl;  
    cin>>x>>y>>z;  
    while((x>y||x>z||y>z)&&(z<(x+y)&&y>(z-x)&&x>(z-y)))  
    {  
        cout<<"你输入的不符合规定或不构成三角形!重新输入吧......"<<endl;  
        cin>>x>>y>>z;   
    }  
    tri1.getABC(&x,&y,&z);  
  
    cout<<"三条边为:"<<x<<'\t'<<y<<'\t'<<z<<endl;  
    cout<<"三角形的周长为:"<<tri1.perimeter()<<'\n'<<"三角形的面积为:"<<tri1.area()<<endl;   
    return 0;  
}  
 
运行结果:
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,