您的位置:首页 > 其它

第3周项目3--多文件组织三角形

2015-03-21 10:16 211 查看
/*  
 * Copyright (c) 2015, 烟台大学计算机学院  
 * All rights reserved.  
 * 文件名称:test.cpp  
 * 作    者:姜甜甜  
 * 完成日期:2015年 3 月 19日  
 * 版 本 号:v1.0  
 *  
 * 问题描述: 利用多文件组织,实现项目2主文件:
  main.cpp,用于定义main()函数
  头文件: triangle.h,头文件,声明类,定义内置成员函数类
  定义文件: triangle.cpp,用于定义类Triangle中其他成员函数
  注意,将3个set函数和3个get函数设计成内置成员函数,其他函数不作为内置函数。

main.cpp

#include <iostream>
#include "student.h"
using namespace std;

int main()
{
    Triangle tri1; //定义三角形类的一个实例(对象)
    double x,y,z;
    cout<<"请输入三角形的三边:";
    cin>>x>>y>>z;
    tri1.setA(x);
    tri1.setB(y);
    tri1.setC(z); //为三边置初值
    if(tri1.isTriangle())
    {
        cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
        cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
    }
    else
        cout<<"不能构成三角形"<<endl;
    return 0;
}

student.cpp

#include "student.h"
#include<cmath>
double  Triangle::perimeter()
{
    return x+y+z;
}
double Triangle::area()
{
    double p;
    p=(x+y+z)/2;
    p=sqrt(p*(p-x)*(p-y)*(p-z));
   return p;
}
bool Triangle::isTriangle()
{
    if((x+y)>z&&(x+z)>y&&(y+z)>x)
        return true;
else return false;
}

student.h

#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

class Triangle
{
private:
    double x,y,z;
public:
    inline void setA(double a)
    {
        x=a;
    }
    inline void setB(double b)
    {
        y=b;
    }
   inline  void setC(double c)
    {
        z=c;
    }
   inline double getA()
    {
       return x;
    }
   inline double getB()
    {
        return y;
    }
   inline double getC()
    {
         return z;
    }
    double perimeter();
   double area();
    bool isTriangle();
};

#endif // STUDENT_H_INCLUDED




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: