측면 길이 정다각형을 입력하고 측면 길이 정삼각형, 이등변 다각형, 사각형 둘레 및 면적을 계산함으로써.
다음과 같이 코드는 다음과 같습니다
Calculate.h
#pragma once
#include<iostream>
using namespace std;
class Calculate
{
public:
Calculate(double , int) ;
void Calculates();
virtual void area() = 0;
protected:
double Clong;int Cnum;
};
#pragma region 多边形
class Polygon :public Calculate
{
public:
Polygon(double,int);
void area(void);
};
#pragma endregion
#pragma region 正方形
class Square :public Calculate
{
public:
Square(double);
void area(void);
};
#pragma endregion
#pragma region 三角形
class Triangle :public Calculate
{
public:
Triangle(double);
void area(void);
};
#pragma endregion
MAIN.CPP
#include "Calculate.h"
void main(){
Calculate* CLL ;
cout << "请输入三角形正方形多边形的边长,以及多边形的边长数量";
double x;
int y;
cin >> x >> y;
Polygon P(x,y);//等边多边形
CLL = &P;
CLL->area();
Square S(x);//正方形
CLL = &S;
CLL->area();
Triangle T(x);//等边三角形
CLL = &T;
CLL->area();
}
Calculate.cpp
#include"Calculate.h"
#include<math.h>
Calculate::Calculate(double longing = 0, int numing = 0) {
Clong = longing;
Cnum = numing;
}
Polygon::Polygon(double d, int i) :Calculate(d, i) {
this->Clong = d;
this->Cnum = i;
}
void Polygon::area(void) {
cout << "多边形周长为" << Cnum * Clong << endl;
const double PI = 3.1415926;
//(边数*边长*边长)/(4*tan(π/边数))
cout<< "多边形面积为" << (Cnum * Clong*Clong)/(4*tan(PI/ Cnum)) << endl;
};
Square::Square(double d) :Calculate(d) {
this->Clong = d;
}
void Square::area(void) {
cout << "正方形周长为" << 4 * Clong << endl;
cout << "正方形面积为" << Clong * Clong << endl;
};
Triangle::Triangle(double d) :Calculate(d) {
this->Clong = d;
}
void Triangle::area(void) {
cout << "三角形周长为" << 3 * Clong << endl;
//等边三角形的面积计算公式可以取0.433倍边长的平方
cout << "三角形面积为" << 0.433 * Clong * Clong << endl;
};
결과는 다음과 같습니다 :