Advertisement

南京航空航天大学c++课程设计

阅读量:

point.h

复制代码
 #include<iostream>

    
 #include<math.h>
    
 #include<string>
    
 #include"Shape.h"
    
 #include<string.h>
    
 using namespace std;
    
 class Point: public Shape
    
 { 
    
 	static int count;
    
 	float x, y;
    
 public:
    
 	Point(float a=0,float b=0,int o=0, string s=""):Shape(o, s), x(a),y(b){}
    
     Point(const Point&in):Shape(in), x(in.x),y(in.y){}
    
 	float GetX() const{return x;}
    
 	float GetY() const{return y;}
    
 	float PutX (float a){x=a;}
    
 	float PutY (float b){y=b;}
    
 	static void fun(){count++;}
    
  	virtual float Area(){return 0;}
    
 	virtual void WriteToFile(ofstream &o)
    
 	{
    
 		count=0;
    
 		o<<getobj()<<"( "<<x<<","<<y<<")"<<getdes()<<endl;
    
 		fun();
    
 	}
    
 	//实现任务六,将数据写入到Point2.txt里面
    
 	//实现任务六里面的Point排序问题
    
 	friend bool operator<(const Point& p1,const Point& p2)
    
 	{
    
 		if(p1.x<p2.x)
    
 			return true;
    
 		else if(p1.x==p2.x&&p1.y<p2.y)
    
 			return true;
    
 		else
    
 			return false;
    
 	}//判断两个Point对象的大小
    
 	friend Point operator+(Point &p1,Point &p2)
    
 	{
    
 		Point p3;
    
 		p3.x=p1.x+p2.x;
    
 		p3.y=p1.y+p2.y;
    
 		return p3;
    
 	}
    
 	friend float Distance(Point &p1,Point &p2)
    
 	{
    
 		double distance;
    
 		distance=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
    
 		return sqrt(distance); 
    
 	}
    
 	//④ 类 Point 实现成员函数 Distance_R(),求解 Point 对象和 Rect 对象之间的距离(最近的两点之间的距离);
    
 	friend float Distance_R(Point& p,Point& r1,Point &r2)
    
 	{
    
 		float distancemin1,distancemin2;
    
 		distancemin1=(p.x-r1.x)*(p.x-r1.x)+(p.y-r1.y)*(p.y-r1.y);
    
 		distancemin2=(p.x-r2.x)*(p.x-r2.x)+(p.y-r2.y)*(p.y-r2.y);
    
 		if(distancemin1>distancemin2)
    
 		{
    
 			return  sqrt(distancemin2);
    
 		}
    
 		else
    
 			return sqrt(distancemin1);
    
 	}
    
 	//类 Point 实现成员函数 Distance_L(),求解 Point 对象和Line 对象之间的距离(最近的两点之间的距离) 	
    
 	friend float Distance_L(Point& p,Point& r1,Point& r2)
    
 	{
    
 		float distancemin1,distancemin2;
    
 		distancemin1=(p.x-r1.x)*(p.x-r1.x)+(p.y-r1.y)*(p.y-r1.y);
    
 		distancemin2=(p.x-r2.x)*(p.x-r2.x)+(p.y-r2.y)*(p.y-r2.y);
    
 		if(distancemin1>distancemin2)
    
 		{
    
 			return  sqrt(distancemin2);
    
 		}
    
 		else
    
 			return sqrt(distancemin1);
    
 	}	
    
 };
    
 int Point::count = 0;
    
 ostream& operator<<(ostream &o,Point &c)
    
 {
    
 	o<<c.getobj()<<'\t'<<c.GetX()<<'\t'<<c.GetY()<<'\t'<<c.getdes()<<endl;
    
 	return o;
    
 };

Line.h

复制代码
 #include<iostream>

    
 #include<string>
    
 #include"Rect.h"
    
 #include<iomanip>
    
 using namespace std;
    
 class Line:public Shape
    
 {
    
     Point p1;//左下角
    
     Point p2;//右上角
    
     double len;
    
     static int count;
    
  public:
    
     Line(float x1=0,float y1=0,float x2=0,float y2=0,int o=0,double l=0,string s=""):Shape(o,s),p1(x1,y1),p2(x2,y2),len(l){}
    
     virtual float Area(){return len;}
    
     float Getp1_x() const{return p1.GetX();}
    
     float Getp1_y() const{return p1.GetY();}
    
     float Getp2_x() const{return p2.GetX();}
    
     float Getp2_y() const{return p2.GetY();}
    
     Point Getp1(){return p1;}
    
     Point Getp2(){return p2;}
    
     static void fun(){count++;}
    
     virtual void WriteToFile(ofstream &o)
    
 	{
    
     count=0;
    
 		o<<getobj()<<setw(5)<<"("<<p1.GetX()<<","<<p1.GetY()<<")  ("<<p2.GetX()<<","<<p2.GetY()<<")     "<<len<<" "<<getdes()<<endl;
    
 		fun();
    
 	}
    
     bool operator<( const Line& in)const
    
 	{
    
 		if(len<in.len)
    
 			return true;
    
 		else
    
 			return false;
    
 	}
    
     /*friend bool operator<(Rect re1,Rect re2)
    
     {
    
     string des1,des2;
    
     des1=re1.getdes();
    
     des2=re2.getdes();
    
     if(des1<des2)
    
         return true;
    
     else
    
         return false;
    
     }*/
    
     friend float Distance (Line &l1,Line &l2)
    
     {
    
     double distancemin;
    
     double rdistance[5];
    
     rdistance[0]=(l1.Getp1_x()-l1.Getp2_x())*(l1.Getp1_x()-l1.Getp2_x())+(l1.Getp1_y()-l1.Getp1_y())*(l1.Getp1_y()-l1.Getp1_y());
    
     rdistance[1]=(l2.Getp1_x()-l2.Getp2_x())*(l2.Getp1_x()-l2.Getp2_x())+(l2.Getp1_y()-l2.Getp1_y())*(l2.Getp1_y()-l2.Getp1_y());
    
     rdistance[2]=(l1.Getp1_x()-l2.Getp2_x())*(l1.Getp1_x()-l2.Getp2_x())+(l1.Getp1_y()-l2.Getp2_y())*(l1.Getp1_y()-l2.Getp2_y());
    
     rdistance[3]=(l1.Getp1_x()-l2.Getp1_x())*(l1.Getp1_x()-l2.Getp1_x())+(l1.Getp1_y()-l2.Getp2_y())*(l1.Getp1_y()-l2.Getp1_y());
    
     rdistance[4]=(l1.Getp2_x()-l2.Getp2_x())*(l1.Getp2_x()-l2.Getp2_x())+(l1.Getp2_y()-l2.Getp1_y())*(l1.Getp2_y()-l2.Getp2_y());
    
     rdistance[5]=(l1.Getp2_x()-l2.Getp1_x())*(l1.Getp2_x()-l2.Getp1_x())+(l1.Getp2_y()-l2.Getp1_y())*(l1.Getp2_y()-l2.Getp1_y());
    
     distancemin=rdistance[0];
    
     for(int i=0;i<6;i++)
    
     {
    
         if(distancemin>rdistance[i])
    
             distancemin=rdistance[i];
    
         else
    
             continue;
    
     }
    
     return sqrt(distancemin);
    
     }
    
 };
    
 int Line::count=0;
    
 ostream& operator<<(ostream& o,Line &c)
    
 {
    
     o<<c.getobj()<<' ';
    
     o<<c.Getp1_x()<<' '<<c.Getp1_y()<<' '<<c.Getp2_x()<<' '<<c.Getp2_y()<<' '<<c.Area()<<endl;
    
 }

Point.h

复制代码
 #include<iostream>

    
 #include<math.h>
    
 #include<string>
    
 #include"Shape.h"
    
 #include<string.h>
    
 using namespace std;
    
 class Point: public Shape
    
 { 
    
 	static int count;
    
 	float x, y;
    
 public:
    
 	Point(float a=0,float b=0,int o=0, string s=""):Shape(o, s), x(a),y(b){}
    
     Point(const Point&in):Shape(in), x(in.x),y(in.y){}
    
 	float GetX() const{return x;}
    
 	float GetY() const{return y;}
    
 	float PutX (float a){x=a;}
    
 	float PutY (float b){y=b;}
    
 	static void fun(){count++;}
    
  	virtual float Area(){return 0;}
    
 	virtual void WriteToFile(ofstream &o)
    
 	{
    
 		count=0;
    
 		o<<getobj()<<"( "<<x<<","<<y<<")"<<getdes()<<endl;
    
 		fun();
    
 	}
    
 	//实现任务六,将数据写入到Point2.txt里面
    
 	//实现任务六里面的Point排序问题
    
 	friend bool operator<(const Point& p1,const Point& p2)
    
 	{
    
 		if(p1.x<p2.x)
    
 			return true;
    
 		else if(p1.x==p2.x&&p1.y<p2.y)
    
 			return true;
    
 		else
    
 			return false;
    
 	}//判断两个Point对象的大小
    
 	friend Point operator+(Point &p1,Point &p2)
    
 	{
    
 		Point p3;
    
 		p3.x=p1.x+p2.x;
    
 		p3.y=p1.y+p2.y;
    
 		return p3;
    
 	}
    
 	friend float Distance(Point &p1,Point &p2)
    
 	{
    
 		double distance;
    
 		distance=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
    
 		return sqrt(distance); 
    
 	}
    
 	//④ 类 Point 实现成员函数 Distance_R(),求解 Point 对象和 Rect 对象之间的距离(最近的两点之间的距离);
    
 	friend float Distance_R(Point& p,Point& r1,Point &r2)
    
 	{
    
 		float distancemin1,distancemin2;
    
 		distancemin1=(p.x-r1.x)*(p.x-r1.x)+(p.y-r1.y)*(p.y-r1.y);
    
 		distancemin2=(p.x-r2.x)*(p.x-r2.x)+(p.y-r2.y)*(p.y-r2.y);
    
 		if(distancemin1>distancemin2)
    
 		{
    
 			return  sqrt(distancemin2);
    
 		}
    
 		else
    
 			return sqrt(distancemin1);
    
 	}
    
 	//类 Point 实现成员函数 Distance_L(),求解 Point 对象和Line 对象之间的距离(最近的两点之间的距离) 	
    
 	friend float Distance_L(Point& p,Point& r1,Point& r2)
    
 	{
    
 		float distancemin1,distancemin2;
    
 		distancemin1=(p.x-r1.x)*(p.x-r1.x)+(p.y-r1.y)*(p.y-r1.y);
    
 		distancemin2=(p.x-r2.x)*(p.x-r2.x)+(p.y-r2.y)*(p.y-r2.y);
    
 		if(distancemin1>distancemin2)
    
 		{
    
 			return  sqrt(distancemin2);
    
 		}
    
 		else
    
 			return sqrt(distancemin1);
    
 	}	
    
 };
    
 int Point::count = 0;
    
 ostream& operator<<(ostream &o,Point &c)
    
 {
    
 	o<<c.getobj()<<'\t'<<c.GetX()<<'\t'<<c.GetY()<<'\t'<<c.getdes()<<endl;
    
 	return o;
    
 };

Rect.h

复制代码
 #include<iostream>

    
 #include<string>
    
 #include"Point.h"
    
 using namespace std;
    
 class Rect:public Shape
    
 {
    
     Point lp;
    
     Point rp;
    
     static int count;
    
     float aarea;
    
 public:
    
     Rect(float x1=0,float y1=0,float x2=0,float y2=0,float area=0,int o=0,string s=""):Shape(o,s),lp(x1,y1),rp(x2,y2),aarea(area){}
    
     virtual float Area(){return aarea;}
    
     static void fun(){count++;}
    
     float Getlp_x() const{return lp.GetX();}
    
     float Getlp_y() const{return lp.GetY();}
    
     float Getrp_x() const{return rp.GetX();}
    
     float Getrp_y() const{return rp.GetY();}
    
     Point Getlp(){return lp;}
    
     Point Getrp(){return rp;}
    
     friend bool operator<( Rect& r1, Rect& r2)
    
     {
    
     if(r1.getdes()<r2.getdes())
    
         return true;
    
     else
    
         return false;
    
     }
    
     virtual void WriteToFile(ofstream &o)
    
 	{
    
 		count=0;
    
 		o<<getobj()<<" ("<<lp.GetX()<<","<<lp.GetY()<<") ("<<rp.GetX()<<","<<rp.GetY()<<") "<<getdes()<<endl;
    
 		fun();
    
 	}
    
     friend Rect operator+(Rect& R1,Rect& R2)
    
     {
    
     Rect R3;
    
     if(R1.lp<R2.lp)
    
     {
    
         R3.lp=R1.lp;
    
     }
    
     else
    
         R3.lp=R2.lp;
    
     if(R1.rp<R2.rp)
    
     {
    
         R3.rp=R2.rp;
    
     }
    
     else
    
         R3.rp=R1.rp;
    
     return R3;
    
     }
    
     friend float Distance (Rect &r1,Rect &r2)
    
     {
    
     double distancemin;
    
     double rdistance[5];
    
     rdistance[0]=(r1.Getlp_x()-r1.Getrp_x())*(r1.Getlp_x()-r1.Getrp_x())+(r1.Getlp_y()-r1.Getlp_y())*(r1.Getlp_y()-r1.Getlp_y());
    
     rdistance[1]=(r2.Getlp_x()-r2.Getrp_x())*(r2.Getlp_x()-r2.Getrp_x())+(r2.Getlp_y()-r2.Getlp_y())*(r2.Getlp_y()-r2.Getlp_y());
    
     rdistance[2]=(r1.Getlp_x()-r2.Getrp_x())*(r1.Getlp_x()-r2.Getrp_x())+(r1.Getlp_y()-r2.Getrp_y())*(r1.Getlp_y()-r2.Getrp_y());
    
     rdistance[3]=(r1.Getlp_x()-r2.Getlp_x())*(r1.Getlp_x()-r2.Getlp_x())+(r1.Getlp_y()-r2.Getlp_y())*(r1.Getlp_y()-r2.Getlp_y());
    
     rdistance[4]=(r1.Getrp_x()-r2.Getrp_x())*(r1.Getrp_x()-r2.Getrp_x())+(r1.Getrp_y()-r2.Getrp_y())*(r1.Getrp_y()-r2.Getrp_y());
    
     rdistance[5]=(r1.Getrp_x()-r2.Getlp_x())*(r1.Getrp_x()-r2.Getlp_x())+(r1.Getrp_y()-r2.Getlp_y())*(r1.Getrp_y()-r2.Getlp_y());
    
     distancemin=rdistance[0];
    
     for(int i=0;i<6;i++)
    
     {
    
         if(distancemin>rdistance[i])
    
             distancemin=rdistance[i];
    
         else
    
             continue;
    
     }
    
     return sqrt(distancemin);
    
     }
    
 };
    
 int Rect::count=0;
    
 ostream& operator<<(ostream &o,Rect c)
    
 {
    
     o<<c.getobj()<<'\t'<<c.Getlp_x()<<'\t'<<c.Getlp_y()<<'\t'<<c.Getrp_x()<<'\t'<<c.Getrp_y()<<'\t'<<c.Area()<<'\t'<<c.getdes()<<endl;
    
     return o;
    
 }

Shape.h

复制代码
 #include<iostream>

    
 #include<string>
    
 using namespace std;
    
 class Shape
    
 {
    
     int obj_id;
    
     string des;
    
 public:
    
     Shape(int o, string s):obj_id(o), des(s){}
    
     int getobj(){return obj_id;}
    
     void change(string s)
    
     {
    
     des = s;
    
     }
    
     const string getdes(){return des;}
    
     virtual float Area()=0;
    
     virtual void WriteToFile(ofstream &o)=0;
    
 };

main.cpp

复制代码
 #include<iostream>

    
 #include<string>
    
 #include<fstream>
    
 #include<vector>
    
 #include"Line.h"
    
 #include<algorithm>
    
 using namespace std;
    
 void menu()
    
 {
    
     cout<<"功能列表:"<<endl;
    
     cout<<"      功能一:获取各个类的数量—————————(请扣1)              "<<endl;
    
     cout<<"      功能二:任意给一个Point对象,输出所有和它的des相同的Rect对象————————(请扣2)"<<endl;
    
     cout<<"      功能三:任意给一个Rect对象,输出所有和它的des相同的Rect对象(请扣3)"<<endl;
    
     cout<<"      功能四:将Area最小的Rect和Area最大的Rect写进Rect_data.txt,将Line最小的Line和Line最大的Line写进Line_data.txt:————————(请扣4)"<<endl;
    
     cout<<"      功能五:对类 Point 实现运算符重载 operator<,并演示p1 < p2: p1.x < p2.x or p1.x = p2.x and p1.y < p2.y————————(请扣5)"<<endl;
    
     cout<<"      功能六:对类 Point、类 Rect 实现运算符重载 operator+ Point: x1+x2, y1+y2Rect: lp = min(lp1, lp2), rp = max(rp1, rp2)"<<endl;
    
     cout<<"      任意给出两个 Point 和 Rect 对象,演示 operator+的功能————————(请扣6)"<<endl;
    
     cout<<"      ① 类 Point 实现成员函数 Distance(),求解两个点之间的距离 "<<endl;
    
     cout<<"      ② 类 Rect 实现成员函数 Distance(),求解距离(最近的两点之间的距离)"<<endl;
    
     cout<<"      ③ 类 Line 实现成员函数 Distance(),求解距离(最近的两点之间的距离)"<<endl;
    
     cout<<"      ④ 类 Point 实现成员函数 Distance_R(),求解 Point 对象和 Rect 对象之间的距离(最近的两点之间的距离)"<<endl;
    
     cout<<"      类 Point 实现成员函数 Distance_L(),求解 Point 对象和Line 对象之间的距离(最近的两点之间的距离)"<<endl;
    
     cout<<"      演示上述功能————————(请扣7)"<<endl;
    
 }
    
 int Getcount(char* filename)
    
 {
    
     string Point_count;
    
     vector<string>PointVector;
    
     fstream file;
    
 	file.open(filename,ios::in);
    
     if(file.fail()!=false)
    
     {
    
     cout<<"文件打开错误"<<endl;
    
     exit(0);
    
     }
    
     while(getline(file,Point_count))
    
     {
    
     PointVector.push_back(Point_count);
    
     }
    
     file.close();
    
     int i=1;
    
     for(vector<string>::iterator it=PointVector.begin();it!=PointVector.end();++it)
    
     {
    
     if(*it==" ")
    
         i=i-1;
    
     i++;//输出Point对象的个数;
    
     }
    
     
    
     return i-1;
    
 };
    
 void WriteData(Shape* s, ofstream& out_file)
    
 {
    
     s->WriteToFile(out_file);
    
 };
    
 /*Getcount函数,获取各个类的对象的数量*/
    
 int main()
    
 {//任务一之模ban:
    
     menu();
    
     float Pointx,Pointy;
    
     int Point_obj; 
    
     string des;
    
     ifstream Pointfile;   
    
     vector<Point>PointVector;
    
     Pointfile.open("Point.txt");
    
     if(Pointfile.fail()==true)
    
     {
    
     cout<<"Point.txt打开错误"<<endl;
    
     exit(0);
    
     }
    
     while(!Pointfile.eof())
    
     {
    
     Pointfile>>Point_obj>>Pointx>>Pointy>>des;//将Point文件里的数据写入到对象的成员变量里面;
    
     Point p(Pointx,Pointy,Point_obj,des);
    
     PointVector.push_back(p);
    
     p.fun();
    
     }
    
     vector<Point>::iterator iter = PointVector.begin();
    
     for(; iter!=PointVector.end(); iter++)
    
     {
    
        // cout<<*iter;
    
     }
    
     Pointfile.close();
    
     float m1,n1,m2,n2;
    
     int Lineobj; 
    
     double len;
    
     int i=0;
    
     ifstream Linefile;
    
     vector<Line>LineVector;
    
     Linefile.open("Line.txt");
    
     if(Linefile.fail()==true)
    
     {
    
     cout<<"Line.txt打开错误"<<endl;
    
     exit(0);
    
     }
    
     while(!Linefile.eof())
    
     {  
    
     Linefile>>Lineobj>>m1>>n1>>m2>>n2>>len;//将Line文件里的数据写入到对象的成员变量里面; 
    
     Line L(n1,m1,n2,m2,Lineobj,len);
    
     LineVector.push_back(L);
    
     L.fun();
    
     }
    
     vector<Line>::iterator iterLine = LineVector.begin();
    
     for(; iterLine!=LineVector.end();iterLine++)
    
     {
    
    // cout<<*iterLine;
    
     }
    
     Linefile.close();
    
     float x1,y1,x2,y2;
    
     float RectArea;
    
     int Rect_obj;
    
     string RectDes;
    
     ifstream RectFile;
    
     vector<Rect>Rectvector;
    
     RectFile.open("Rect.txt");
    
     if(RectFile.fail()==true)
    
     {
    
     cout<<"Rect.txt文件打开错误"<<endl;
    
     exit(0);
    
     }
    
     while(!RectFile.eof())
    
     {
    
     RectFile>>Rect_obj>>x1>>y1>>x2>>y2>>RectArea>>RectDes;
    
     Rect R(x1,y1,x2,y2,RectArea,Rect_obj,RectDes);
    
     Rectvector.push_back(R);
    
     R.fun();
    
     }
    
     vector<Rect>::iterator iterRect=Rectvector.begin();
    
     for(;iterRect!=Rectvector.end();iterRect++)
    
     {
    
     //cout<<*iterRect;
    
     }
    
     RectFile.close();
    
      //任务一之获取对象的数量:
    
     //上述功能是将各个类的对象存入vertor里面
    
     
    
     while(1)
    
     {
    
     cout<<"是否退出功能模块:"<<endl;
    
     cout<<"是请输入8"<<endl;
    
     cout<<"否请输入9"<<endl;
    
     int stystem;
    
     cin>>stystem;
    
     if(stystem==8)
    
         break;
    
     menu();
    
     cout<<" 请输入数字来实现您所需要实现的功能:"<<endl; 
    
     int FunctionCount;
    
     cin>>FunctionCount;
    
     switch (FunctionCount)
    
     {
    
         case(1):
    
         {
    
             int count;
    
             cout<<"请输入您所要查询对象数量的文件名称:"<<endl;
    
             int i=0;
    
             char filename[32];
    
             cin>>filename;
    
             while(1)
    
             {
    
                 count=Getcount(filename);
    
                 cout<<count<<endl;
    
                 int yes_no;
    
                 cout<<"是否继续查询,是请输入1,否请输入0"<<endl;
    
                 cin>>yes_no;
    
                 if(yes_no==0)
    
                     break;
    
                 if(yes_no==1)
    
                 {
    
                     cout<<"请输入您所要查询对象数量的文件名:"<<endl;
    
                     cin>>filename;
    
                     continue;
    
                 }       
    
             } 
    
             break;
    
         }
    
         case(2):
    
         {   //此功能是查询各个对象类的数量
    
             //任务二之分别找出与Point类有相同des的Rect类和找出与Rect类有相同des的Point类:
    
             cout<<"任意给一个Point对象,输出所有和它的des相同的Rect对象:格式:1 2 3 HOUSE"<<endl;
    
             float a,b;
    
             int c;
    
             string stringsearch;
    
             cin>>a>>b>>c>>stringsearch;
    
             Point search(a,b,c,stringsearch);
    
             for(int i=0; i<Rectvector.size(); i++)
    
             {
    
                 if(stringsearch == Rectvector[i].getdes())
    
                     cout<<Rectvector[i];
    
             }
    
             break;
    
         }
    
         case(3):
    
         {
    
             cout<<"任意给一个Rect对象,输出所有和它的des相同的Point对象:格式:1 2 3 4 5 6 HOUSE"<<endl;
    
             float Recta,Rectb,Rectc,Rectd,Rectarea;
    
             int Rectcount;
    
             string Rect_string_Search;
    
             cin>>Recta>>Rectb>>Rectc>>Rectd>>Rectarea>>Rectcount>>Rect_string_Search;
    
             Rect Rectsearch(Recta,Rectb,Rectc,Rectd,Rectarea,Rectcount,Rect_string_Search);
    
             for(int j=0;j<PointVector.size();j++)
    
             {
    
                 if(Rect_string_Search==PointVector[j].getdes())
    
                 cout<<PointVector[j];
    
             }
    
             break;
    
         }
    
         case(4):
    
         {
    
             //任务三:
    
             Rect Area_max=Rectvector[1];
    
             Rect Area_min=Rectvector[1];
    
             for(int i=0;i<Rectvector.size(); i++)
    
                 {
    
                     if(Rectvector[i].Area()>Area_max.Area())
    
                         Area_max=Rectvector[i];
    
                     if(Rectvector[i].Area()<Area_min.Area())
    
                         Area_min=Rectvector[i];
    
                 }
    
  
    
             fstream file;
    
             file.open("Rect_data.txt",ios::out);
    
             if(file.fail()!=false)
    
             {
    
                 cout<<"文件打开错误"<<endl;
    
                 exit(0);
    
             }     
    
             file<<"Area最大的Rect:"<<Area_max;
    
             file<<"Area最小的Rect:"<<Area_min<<endl;
    
             file.close();
    
             //找出Area最大的Rect和Area最小的Rect;
    
             Line Line_max=LineVector[1];
    
             Line Line_min=LineVector[1];
    
             for(int i=0;i<LineVector.size(); i++)
    
             {
    
                 if(LineVector[i].Area()>Line_max.Area())               
    
                     Line_max=LineVector[i];
    
                 if(LineVector[i].Area()<Line_min.Area())
    
                     Line_min=LineVector[i];
    
                 }
    
             fstream linefile;
    
             linefile.open("Line_data.txt",ios::out);
    
             if(linefile.fail()!=false)
    
             {
    
                 cout<<"文件打开错误"<<endl;
    
                 exit(0);
    
             }     
    
             linefile<<"Line最大的Line:"<<Line_max;
    
             linefile<<"Line最小的Line:"<<Line_min<<endl;
    
             linefile.close();
    
             break;
    
         } 
    
         //任务四:
    
         case(5):
    
         {//①对类 Point 实现运算符重载 operator<,并演示
    
         //p1 < p2: p1.x < p2.x or p1.x = p2.x and p1.y < p2.y 
    
             //演示:
    
             cout<<"对类 Point 实现运算符重载 operator<,并演示:p1 < p2: p1.x < p2.x or p1.x = p2.x and p1.y < p2.y "<<endl;
    
             cout<<"演示如下:"<<endl;
    
             cout<<endl;
    
             cout<<"Point p1(1,2,3,HOUSe),Point p2(3,4,5,HOUSE)    p1<p2:"<<endl;
    
             Point p1(1,2,3,"HOUSE");
    
             Point p2(3,4,5,"HOUSE");
    
             if(p1<p2==1)
    
                 cout<<"true"<<endl;
    
             else
    
                 cout<<"false"<<endl;
    
             cout<<"Point p3(1,2,3,HOUSE);Point p4(1,2,4,HOUSE)    p3<p4:"<<endl;
    
             Point p3(1,2,3,"HOUSE");
    
             Point p4(1,2,4,"HOUSE");
    
             if(p3<p4==1)
    
                 cout<<"true"<<endl;
    
             else
    
                 cout<<"false"<<endl;
    
             break;
    
         }
    
         case(6):
    
         {   /*②  对类 Point、类 Rect 实现运算符重载 operator+ 
    
             Point: x1+x2, y1+y2
    
             Rect: lp = min(lp1, lp2), rp = max(rp1, rp2) 
    
             任意给出两个 Point 和 Rect 对象,演示 operator+的功能 */ 
    
             cout<<"任意给两个Point对象,演示operator+的功能:格式p1:1 2 3 HOUSE"<<endl;
    
             cout<<"Point1:"<<endl;
    
             float p1x,p1y,p2x,p2y;
    
             int p1count,p2count;
    
             string p1des,p2des;
    
             cin>>p1count>>p1x>>p1y>>p1des;
    
             cout<<"Point2:"<<endl;
    
             cin>>p2count>>p2x>>p2y>>p2des;
    
             Point p1new(p1x,p1y,p1count,p1des);
    
             Point p2new(p2x,p2y,p2count,p2des);
    
             Point p3new=p1new+p2new;
    
             cout<<"Point3:";
    
             cout<<p3new<<endl;
    
             cout<<"任意给两个Rect对象,演示operator+的功能:格式R1:1 2 3 4 5 6 HOUSE"<<endl;
    
             cout<<"Rect1:"<<endl;
    
             float R1x1,R2x1,R1y1,R2y1,R1x2,R2x2,R1y2,R2y2,R1x3,R2x3;
    
             int R1count,R2count;
    
             string R1des,R2des;
    
             cin>>R1x1>>R1y1>>R1x2>>R1y2>>R1x3>>R1count>>R1des;
    
             cout<<"Rect2:"<<endl;
    
             cin>>R2x1>>R2y1>>R2x2>>R2y2>>R2x3>>R2count>>R2des;
    
             Rect R1(R1x1,R1y1,R1x2,R1y2,R1x3,R1count,R1des);
    
             Rect R2(R2x1,R2y1,R2x2,R2y2,R2x3,R2count,R2des);
    
             Rect R3=R1+R2;
    
             cout<<"Rect3:";
    
             cout<<R3<<endl;
    
             break;
    
         }
    
         case(7):
    
         {
    
             /*任务 5:
    
             ① 类 Point 实现成员函数 Distance(),求解两个点之间的距离 
    
             ② 类 Rect 实现成员函数 Distance(),求解距离(最近的两点之间的距离)
    
             ③ 类 Line 实现成员函数 Distance(),求解距离(最近的两点之间的距离)
    
             ④ 类 Point 实现成员函数 Distance_R(),求解 Point 对象和 Rect 对象之间的距离(最近的两点之间的距离);
    
             ⑤ 类 Point 实现成员函数 Distance_L(),求解 Point 对象和Line 对象之间的距离(最近的两点之间的距离) 
    
             编写程序演示上述功能 
    
             */
    
         //演示任务五:
    
             cout<<"演示任务五:"<<endl;
    
             cout<<"① 类 Point 实现成员函数 Distance(),求解两个点之间的距离 :p1(1,2,3,HOUSE),P2(1,2,3,HOUSE)"<<endl;
    
             Point p1distance(1,2,3,"HOUSE"),p2distance(1,2,3,"HOUSE");
    
             cout<<endl; 
    
             cout<<"Distance:"<<Distance(p1distance,p2distance)<<endl;
    
             cout<<endl;
    
             cout<<"② 类 Rect 实现成员函数 Distance(),求解距离(最近的两点之间的距离):"<<endl;
    
             cout<<"R1(4943,40395,9128,40430,9158,1050,HOUSE)"<<endl;
    
             cout<<"R2(4944,40710,9102,40763,9120,954,HOUSE)"<<endl;
    
             Rect r1distance(4943,40395,9128,40430,9158,1050,"HOUSE"),r2distance(4944,40710,9102,40763,9120,954,"HOUSE");
    
             cout<<endl;
    
             cout<<"Distance:"<<Distance(r1distance,r2distance)<<endl;
    
             cout<<endl;
    
             cout<<"③ 类 Line 实现成员函数 Distance(),求解距离(最近的两点之间的距离):"<<endl;
    
             cout<<"L1(14,20223,16490,20280,16605,128.351)"<<endl;
    
             cout<<"L2(16,20280,16686,20237,16885,203.593)"<<endl;
    
             Line L1distance(14,20223,16490,20280,16605,128.351),L2distance(16,20280,16686,20237,16885,203.593);
    
             cout<<endl; 
    
             cout<<"Distance:"<<Distance(L1distance,L2distance)<<endl;
    
             cout<<endl;
    
             cout<<"④ 类 Point 实现成员函数 Distance_R(),求解 Point 对象和 Rect 对象之间的距离(最近的两点之间的距离):"<<endl;
    
             cout<<"p1(1,2,3,HOUSE),R1(4943,40395,9128,40430,9158,1050,HOUSE)"<<endl;
    
             Point Point_Rect(1,2,3,"HOUSE");
    
             Rect Rect_Point(4943,40395,9128,40430,9158,1050,"HOUSE");
    
             Point Rect_Point1=Rect_Point.Getlp(),Rect_Point2=Rect_Point.Getrp();
    
             cout<<endl;
    
             cout<<"Distance_R:"<<Distance_R(Point_Rect,Rect_Point1,Rect_Point2)<<endl;
    
             cout<<endl;
    
             cout<<"⑤ 类 Point 实现成员函数 Distance_L(),求解 Point 对象和Line 对象之间的距离(最近的两点之间的距离):"<<endl;
    
             cout<<"P1(1,2,3,HOUSE),L1(16,20280,16686,20237,16885,203.593)"<<endl;
    
             Line Line_Point(16,20280,16686,20237,16885,203.593);
    
             Point Point_Line(1,2,3,"HOUSE");
    
             Point Line_Point1=Line_Point.Getp1(),Line_Point2=Line_Point.Getp2();
    
             cout<<endl;
    
             cout<<"Distance_L:"<<Distance_L(Point_Line,Line_Point1,Line_Point2)<<endl;
    
             cout<<endl;
    
         }
    
     }    
    
     }
    
     cout<<"任务 6:①体现多态性,分别在 Point、Rect 和 Line 中实现 WriteToFile()虚函数将数据写入到文件,格式为"<<endl;
    
     cout<<"Point: ID (x, y) des     Rect: ID (x1, y1) (x2, y2) des        Line: ID (x1, y1) (x2, y2) len"<<endl;
    
     cout<<"要求: 在 main 函数中调用 riteData(Shape* s, ofstream& out_file)  void WriteData(Shape* s,ofstream& out_file)"<<endl;
    
     cout<<"1.将所有 Point 对象的字符串变量 des 变成小写,按坐标排序后,利用 sort 排序,p1<p2<p3…<pn)写入到 Point2.txt;"<<endl;
    
     cout<<"2.将 Rect 对象按照 des(字符串排序),写入到 Rect2.txt;"<<endl;
    
     cout<<"3.将 Line 对象按照 len 递增数序排序,写入到文件 Line2.txt"<<endl;
    
     ofstream Point2file;
    
     Point2file.open("Point2.txt",ios::out);
    
     if(Point2file.fail()==true)
    
     {
    
     cout<<"Point2.txt打开错误"<<endl;
    
     exit(0);
    
     }
    
     sort(PointVector.begin(),PointVector.end());
    
     for(int i=0;i<PointVector.size(); i++)
    
     {    
    
     string a=PointVector[i].getdes();
    
     transform(a.begin(), a.end(), a.begin(), ::tolower);
    
     PointVector[i].change(a);
    
     }
    
     Shape *sha;
    
     for(int i=0;i<PointVector.size();i++)
    
     {  
    
     sha=&PointVector[i];
    
     WriteData(sha,Point2file);
    
     }
    
     Point2file.close();
    
     //任务六:1
    
     Shape *sha1;
    
     ofstream Rect2file;
    
     Rect2file.open("Rect2.txt",ios::out);
    
     if(Rect2file.fail()==true)
    
     {
    
     cout<<"Rect2.txt打开错误"<<endl;
    
     exit(0);
    
     }
    
     //sort(Rectvector.begin(),Rectvector.end());
    
     for(int i=0; i<Rectvector.size(); i++)
    
     {
    
             sha1 = &Rectvector[i];
    
             WriteData(sha1,Rect2file);
    
     }
    
     Rect2file.close();
    
     //任务六:2;
    
     ofstream Line2file;
    
     Line2file.open("Line2.txt",ios::out);
    
     if(Line2file.fail()==true)
    
     {
    
     cout<<"Line2.txt打开错误"<<endl;
    
     exit(0);
    
     }
    
     Shape *sha2;
    
     sort(LineVector.begin(),LineVector.end());
    
     for(int i=0;i<LineVector.size();i++)
    
     {
    
     sha2=&LineVector[i];
    
     WriteData(sha2,Line2file);
    
     }
    
     Line2file.close();
    
      return 0;
    
 }

全部评论 (0)

还没有任何评论哟~