C++初学:图形管理类
功能:实现圆、椭圆、矩形、三⾓形、正多边形和任意多边形的管理,实现计算所有图形⾯积、长度,添加图形,删除图形,获取指定位置图形,输出图形信息等。
头⽂件1:stdafx.h 存放⽤到的头⽂件
#ifndef STDAFX_H_INCLUDED
#define STDAFX_H_INCLUDED
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#endif
头⽂件2:point2d.h 点类
#ifndef POINT2D_H_INCLUDED
#define POINT2D_H_INCLUDED
#include "stdafx.h"
//点类
class point2d {
public:
point2d():m_x(0),m_y(0){}
point2d(double x,double y):m_x(x),m_y(y){}
point2d(const point2d& p){
m_x = p.m_x;
m_y = p.m_y;
}
void rewritex(double x){ m_x = x;}
void rewritey(double y){ m_y = y;}
double getx(){return m_x;}
double gety(){return m_y;}
private:
double m_x;
double m_y;
};
#endif
头⽂件3:shape.h 图形类
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include "stdafx.h"
#include "point2d.h"
#define PI 3.1416926
//图形基类
class shape {
public:
virtual double calCirc()=0;
virtual double calCirc()=0;
virtual double calArea()=0;
virtual void info()=0;
virtual shape*expand()=0;
};
//---------------------------圆类
class circle :public shape {
public:
circle():m_r(1){}
circle(double r):m_r(r){}
virtual double calCirc(){
swallow
return2* PI * m_r;
}
virtual double calArea(){
return PI * m_r * m_r;
}
virtual void info(){
cout <<"圆"<<" ";
cout <<" 半径:"<< m_r;
cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<< endl << endl;
}
virtual circle*expand(){
cout <<"输⼊半径:";
float r =0;
cin >> r;
circle* temp =new circle(r);
return temp;
}
英汉字典在线翻译private:
double m_r;
};
//---------------------------椭圆类
air dropclass oval :public shape {
public:
oval():m_a(1),m_b(2){}
oval(double a,double b):m_a(max(a, b)),m_b(min(a, b)){}
virtual double calCirc(){
return2*min(m_a, m_b)+4*(max(m_a, m_b)-min(m_a, m_b));
}
virtual double calArea(){
return PI * m_a * m_b;
}
void info(){
cout <<"椭圆"<<" ";
cout <<"长轴半径:"<<max(m_a, m_b)<<" 短轴半径:"<<min(m_a, m_b); cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<< endl << endl;
}
virtual oval*expand(){
cout <<"输⼊长轴、短轴:";
float a =0, b =0;
cin >> a >> b;
oval* temp =new oval(a, b);
return temp;
}
private:
double m_a;
double m_b;
double m_b;
};
//---------------------------矩形类
class rectangle :public shape {
public:
rectangle():m_a(1),m_b(1){}
rectangle(double a,double b):m_a(max(a, b)),m_b(min(a, b)){}
virtual double calCirc(){
return4* m_a * m_b;
}
virtual double calArea(){
return m_a * m_b;
}
virtual void info(){
cout <<"矩形"<<" ";
cout <<"长:"<<max(m_a, m_b)<<" 宽:"<<min(m_a, m_b);
cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<< endl << endl;
}
virtual rectangle*expand(){
cout <<"输⼊长、宽:";
float a =0, b =0;
cin >> a >> b;
rectangle* temp =new rectangle(a, b);
return temp;
}
private:
double m_a;
double m_b;
};
//---------------------------三⾓形类
class triangle :public shape {
public:
triangle():m_a(1),m_b(1),m_c(1){}
triangle(double a,double b,double c):m_a(a),m_b(b),m_c(c){}
virtual double calCirc(){
return m_a + m_b + m_c;
}
virtual double calArea(){
return(double)1/(double)4*sqrt((m_a + m_b + m_c)*(m_a + m_b - m_c)*(m_a + m_c - m_b)*(m_b + m_c - m_a)); }
virtual void info(){
cout <<"三⾓形"<<" ";
cout <<" 三边长:"<< m_a <<" "<< m_b <<" "<< m_c;
cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<< endl << endl;
}
virtual triangle*expand(){
cout <<"输⼊三边(⾃觉符合规范):";
float a =0, b =0, c =0;
cin >> a >> b >> c;
triangle* temp =new triangle(a, b, c);
return temp;
}
private:
double m_a;
double m_b;
double m_c;
};
/
/---------------------------任意正多边形类
//---------------------------任意正多边形类brainstorming
class regularShape :public shape {
public:
regularShape():m_n(3),m_l(1){}
regularShape(int n,double l):m_n(n),m_l(l){}
virtual double calCirc(){
return m_n * m_l;
}
virtual double calArea(){
return pow(calCirc(),2)/((tan(PI / m_n))*(4*(double)m_n));
}
virtual void info(){
cout <<"正多边形"<<" ";
cout <<" 边数:"<< m_n <<" 边长:"<< m_l;
cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<< endl << endl;
}
virtual regularShape*expand(){
cout <<"输⼊边数、边长:";
float a =0;
int b =0;
cin >> a >> b;
regularShape* temp =new regularShape(b, a);
return temp;
}
the color of the nightprivate:
int m_n;
double m_l;
};
//---------------------------任意多边形类
class irregularShape :public shape {diet是什么意思
public:
irregularShape(){
point2d p1(0,0);
point2d p2(0,1);
point2d p3(1,1);
point2d p4(1,0);
all_point2d.push_back(p1);
all_point2d.push_back(p2);
初吻 主题曲all_point2d.push_back(p3);
all_point2d.push_back(p4);
}
irregularShape(vector<point2d> point2d):all_point2d(point2d){}
~irregularShape(){}
virtual double calCirc(){troll
double Circ =0;
for(unsigned int i =0; i < all_point2d.size(); i++){
if(i != all_point2d.size()-1){ Circ +=sqrt(pow((all_point2d[i].getx()- all_point2d[i +1].getx()),2)+pow((all_point2d[i].gety()- all_point2d[i +1].gety()),2)) ;}
el{ Circ +=sqrt(pow((all_point2d[i].getx()- all_point2d[0].getx()),2)+pow((all_point2d[i].gety()- all_point2d[0].gety()),2));}
}
return Circ;
}
virtual double calArea(){
double Area =0;
for(unsigned int i =0; i < all_point2d.size(); i++){
if(i != all_point2d.size()-1){ Area +=(all_point2d[i].getx()* all_point2d[i +1].gety()- all_point2d[i +1].getx()* all_point2d[i].gety());}
el{ Area +=(all_point2d[i].getx()* all_point2d[0].gety()- all_point2d[0].getx()* all_point2d[i].gety());}
}
return abs(Area)/2;
return abs(Area)/2;
}
virtual void info(){
cout <<"任意多边形"<<" "<<"边数:"<< all_point2d.size();
cout <<" 周长:"<<calCirc()<<" ⾯积:"<<calArea()<<" 坐标:";
for(unsigned int i =0; i < all_point2d.size(); i++){
cout <<" x"<< i +1<<"="<< all_point2d[i].getx()<<" "<<"y"<< i +1<<"="<< all_point2d[i].gety(); }
cout << endl << endl;
}
virtual irregularShape*expand(){
all_point2d.clear();
cout <<"你想构造⼏边形?(>=3)";
int nums =0;
do{
cin >> nums;
}while(nums <3);
for(int i =0; i < nums; i++){
cout <<"按顺时针输⼊第"<< i +1<<"个点的横、纵坐标,空格隔开:";
double x =0, y =0;
cin >> x >> y;
point2d p(x, y);
all_point2d.push_back(p);
}
irregularShape* temp =new irregularShape(all_point2d);
return temp;
}
public:
vector<point2d> all_point2d;//存放所有点
心理自我
};
#endif// SHAPE_H_INCLUDED
头⽂件4:shapeManage.h 图形管理类
#ifndef SHAPEMANAGE_H_INCLUDED
#define SHAPEMANGE_H_INCLUDED
#include "stdafx.h"
#include "shape.h"
//图形管理类,实现计算所有图形⾯积、长度,添加图形,删除图形,获取指定位置图形,输出图形信息class shapeManage {
public:
shapeManage(){
circle* circle1 =new circle();
oval* oval1 =new oval();
rectangle* rectangle1 =new rectangle();
triangle* triangle1 =new triangle();
regularShape* regularShape1 =new regularShape();
irregularShape* irregularShape1 =new irregularShape();
all_shapes.push_back(circle1);
all_shapes.push_back(oval1);
all_shapes.push_back(rectangle1);
all_shapes.push_back(triangle1);
all_shapes.push_back(regularShape1);
all_shapes.push_back(irregularShape1);
}
~shapeManage(){
for(unsigned int i =0; i < all_shapes.size(); i++){
if(all_shapes[i]){
delete all_shapes[i];
all_shapes[i]=nullptr;
}
}
>the rundown