⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ai2807.cpp

📁 非常好的C++学习源码,里面包括各种算法的实现,二叉的的前中后序遍历等
💻 CPP
字号:
#include<iostream.h>
#include<cmath>

class CShape{
public:
	virtual double calcArea()=0;
	virtual double calcLength()=0;
	virtual void print()=0;
};

class CRectangle:public CShape 
{ 
private:
	double x1,y1,x2,y2; 
public :
	friend istream& operator >> (istream & input, CRectangle & rect);
	double calcArea()
	{ 
		return fabs((x2-x1)*(y2-y1));
	}
	double calcLength()
	{
		return (2*(fabs(x2-x1)+fabs(y2-y1)));
	}
	void print()
	{ 
		cout << "面积 = " << calcArea() << ", 周长 = " << calcLength() << " "
	         << " 左上角 = (" << x1 << "," << y1 << ")"<< " 右下角 = (" << x2 << "," << y2 << ")" << endl;
	}
};

istream& operator >> (istream & input, CRectangle & rect)
{
    rect.x1 = rect.y1 = rect.x2 = rect.y2 = -1;
    while(rect.x1==-1&&rect.x2==-1)
	{
		cout <<"\n请输入左上角(x1,y1)和右下角坐标(x2,y2)" << endl;
		cout <<"要求x1<x2,y1<y2" << endl;
		cout << "例如:输入5 6 7 8 表示 左上角(5,6)右下角(7,8)" << endl;
		cin >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2;
		if(rect.x1>=rect.x2 || rect.y1>=rect.y2)
		{
			rect.x1 = rect.y1 = rect.x2 = rect.y2 = -1;
			cout<<"错误!"<<endl;
    	    continue;
		}
	}
	return input;
}

class CTriangle:public CShape
{
private:
	double x1,y1,x2,y2,x3,y3;
public:
	friend istream& operator >> (istream & input, CTriangle & cir);
	double calcArea()
	{
		double s1,s2,s3,s;
		s1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		s2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
		s3=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
		s=(s1+s2+s3)/2;
		return (sqrt(s*(s-s1)*(s-s2)*(s-s3)));
	}
	double calcLenth()
	{
		double s1,s2,s3;
		s1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		s2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
		s3=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
		return (s1+s2+s3);
	}
	void print()
	{
		cout << "面积 = " << calcArea() << ", 周长 = " << calcLength() << " "
	         << "三个顶点分别是: "<<x1<<","<<y1<<" ; "<<x2<<","<<y2<<" ; "<<x3<<","<<y3<<" ; "<<endl;
	}
};

istream& operator >> (istream & input, CTriangle & rect1)
{
	double s1,s2,s3;
	s1=0;
	while (s1==0||s2==0||s3==0)
	{
		cout<<"\n请输入三个顶点的坐标(x1,y1),(x2,y2),(x3,y3)"<<endl;
		cout<<"要求三个点两两不重和且不在一条直线上"<<endl;
		cout<<"输入1 2 3 4 5 6,表示三个点坐标分别为(1,2),(3,4),(5,6)"<<endl;
		cin>>rect1.x1>>rect1.y1>>rect1.x2>>rect1.y2>>rect1.x3>>rect1.y3;
		s1=sqrt((rect1.x1-rect1.x2)*(rect1.x1-rect1.x2)+(rect1.y1-rect1.y2)*(rect1.y1-rect1.y2));
		s2=sqrt((rect1.x1-rect1.x3)*(rect1.x1-rect1.x3)+(rect1.y1-rect1.y3)*(rect1.y1-rect1.y3));
		s3=sqrt((rect1.x3-rect1.x2)*(rect1.x3-rect1.x2)+(rect1.y3-rect1.y2)*(rect1.y3-rect1.y2));
		if (s1==0||s2==0||s3==0)
		{
			s1=0;
			cout<<"错误!"<<endl;
			continue;
		}
	}
	return input;
}

class CCircle:public CShape 
{ 
private:
	double centerx ,centery, radius; 
public :
	friend istream& operator >> (istream & input, CCircle & cir);
	double calcArea()
	{ 
		return (radius*radius*3.14);
	}
	double calcLength()
	{
		return (2*radius*3.14);
	}
	void print()
	{ 
		cout << "面积 = " << calcArea() << ", 周长 = " << calcLength() << " "
	         << "圆心 = (" << centerx << "," << centery << ")"<< " 半径 =" << radius << endl; 
	}
};

istream& operator >> (istream & input, CCircle & cir)
{
    cir.radius = -1;
    while(cir.radius==-1)
	{
		cout <<"\n请输入圆心(x1,y1)和半径 r" << endl;
		cout <<"要求r是非负整数" << endl;
		cout << "例如:输入5 6 7 表示圆心(5,6)半径是 7 " << endl;
		cin >> cir.centerx >> cir.centery >> cir.radius ;
		if(cir.radius<0)
		{
			cir.radius = -1;
			cout<<"错误!"<<endl;
    		continue;
		}
	}
	return input;
}

class CSector:public CShape
{
private:
	double centerx ,centery, radius,angle; 
public :
	friend istream& operator >> (istream & input, CSector & cir);
	double calcArea()
	{ 
		return (radius*radius*3.14*(angle/360));
	}
	double calcLength()
	{
		return (2*radius*3.14*(angle/360)+2*radius);
	}
	void print()
	{ 
		cout << "面积 = " << calcArea() << ", 周长 = " << calcLength() << " "
	         << "圆心 = (" << centerx << "," << centery << ")"<< " 半径 =" << radius << " 夹角 ="<<angle<<endl; 
	}
};

istream& operator >> (istream & input, CSector & cir)
{
    cir.radius = -1;
    while(cir.radius==-1)
	{
		cout <<"\n请输入圆心(x1,y1),半径 r,夹角angle度" << endl;
		cout <<"要求r是正整数,0<angle<360" << endl;
		cout << "例如:输入5 6 7 160表示圆心(5,6)半径是 7 夹角160度 " << endl;
		cin >> cir.centerx >> cir.centery >> cir.radius >>cir.angle;
		if(cir.radius<=0||cir.angle<=0||cir.angle>=360)
		{
			cir.radius = -1;
			cout<<"错误!"<<endl;
    		continue;
		}
	}
	return input;
}

class CTrapezia:public CShape
{
private:
	double x1,y1,x2,y2,x3,y3,x4,y4; 
public :
	friend istream& operator >> (istream & input, CTrapezia & rect);
	double calcArea()
	{ 
		return ((fabs(x1-x2)+fabs(x3-x4))*fabs(y2-y3)/2);
	}
	double calcLength()
	{
		return (sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4))+sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3))+fabs(x1-x2)+fabs(x3-x4));
	}
	void print()
	{ 
		cout << "面积 = " << calcArea() << ", 周长 = " << calcLength() << " "
	         << " 左上角 = (" << x1 << "," << y1 << ")"<< " 右上角 = (" << x2 << "," << y2 << ")"
			 << " 右下角 = (" << x3 << "," << y3 << ")"<< " 左下角 = (" << x4 << "," << y4 << ")"<<endl;
	}
};

istream& operator >> (istream & input, CTrapezia & rect)
{
    rect.x1 = rect.y1 = rect.x2 = rect.y2 = -1;
    while(rect.x1==-1&&rect.x2==-1)
	{
		cout <<"\n请输入左上角(x1,y1),和右上角坐标(x2,y2),右下角坐标(x3,y3),左下角坐标(x4,y4)" << endl;
		cout <<"要求x1<x2,x3>x4,y1=y2,y3=y4,y1>y3" << endl;
		cout << "例如:输入2 6 3 6 8 3 1 3 表示 左上角(2,6)右上角(3,6)右上角(8,3)左下角(1,3)" << endl;
		cin >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2>>rect.x3 >> rect.y3 >> rect.x4 >> rect.y4;
		if(rect.x1>=rect.x2 || rect.y1<=rect.y3 || rect.x3<=rect.x4 || rect.y3!=rect.y4 || rect.y1!=rect.y2)
		{
			rect.x1 = rect.y1 = rect.x2 = rect.y2 = -1;
			cout<<"错误!"<<endl;
    	    continue;
		}
	}
	return input;
}

int main()
{
	char ch;
	CShape *shapes[100];  
	int nShape=0,i;  
	while (1)
	{
		cout<<"请输入几何体的形状(用数字1-5表示,输入e(E)表示结束):"<<endl;
		cout<<"1、矩形   2、三角型   3、圆形   4、扇型   5、梯形"<<endl;
		cin>>ch;
		if ((ch=='e')||(ch=='E'))
			break;
		if (ch=='1')
		{
			CRectangle *test1;
			test1=new CRectangle;
			cin>>*test1;
			test1->print();
			shapes[nShape]=test1;
			nShape++;
			cout<<endl;
		}
		if (ch=='2')
		{
			CTriangle *test2;
			test2=new CTriangle;
			cin>> *test2;
			test2->print();
			shapes[nShape]=test2;
			nShape++;
			cout<<endl;
		}
		if (ch=='3')
		{
			CCircle *test3;
			test3=new CCircle;
			cin>> *test3;
			test3->print();
			shapes[nShape]=test3;
			nShape++;
			cout<<endl;
		}
		if (ch=='4')
		{
			CSector *test4;
			test4=new CSector;
			cin>> *test4;
			test4->print();
			shapes[nShape]=test4;
			nShape++;
			cout<<endl;
		}
		if (ch=='5')
		{
			CTrapezia *test5;
			test5=new CTrapezia;
			cin>> *test5;
			test5->print();
			shapes[nShape]=test5;
			nShape++;
			cout<<endl;
		}
	}
	for (i=0;i<nShape;i++)
	{
		shapes[i]->print();
		delete shapes[i];
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -