3週目のオンサイト実践プロジェクト3——プログラムのマルチファイル組織


問題とコード
メインファイル:main.cpp
/*
 * Copyright (c) 2015,          
 * All rights reserved.
 *     :test.cpp
 *       :  
 *     :2015   3   22  
 *      :v1.0
 *
 *     :        ,           (   )。
 *     :  。
 *     :     。
 */
#include<iostream>
#include"trianale.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;
}

ヘッダファイル:triangle.h
#ifndef TRIANALE_H_INCLUDED
#define TRIANALE_H_INCLUDED
class Triangle
{
public:
    void setA(double x)
    {
        a=x;
    }
    void setB(double y)
    {
        b=y;
    }
    void setC(double z)
    {
        c=z;
    }
    bool isTriangle()
    {
        if(a+b>c&&a+c>b&&b+c>a)
            return true;
        else
            return false;
    }
    double getA()
    {
        return a;
    }
    double getB()
    {
        return b;
    }
    double getC()
    {
        return c;
    }
    double perimeter();
    double area();
private:
    double a,b,c; //         
};


#endif // TRIANALE_H_INCLUDED

クラス定義ファイル:triangle.cpp
#include"trianale.h"
#include<cmath>
double Triangle::perimeter()
{
    return a+b+c;
}
double Triangle::area()
{
    double p;
    p=(a+b+c)/2;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}