大一下学期第二次上机

不用STL版

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<cmath> 
#include<algorithm>
using namespace std;

class Point {
    
    
	private:
		int x , y , z;
	public:
		Point() {
    
    }
		Point & operator = (const Point &_a) {
    
     x=_a.x; y=_a.y; z=_a.z; }
		Point(int _x , int _y , int _z) {
    
     x = _x; y = _y; z = _z; }
		int getx() {
    
     return x; }
		int gety() {
    
     return y; }
		int getz() {
    
     return z; }
};

class Segment : public Point {
    
    
	private:
		Point a , b;
		double len;
	public :
		Segment() {
    
    }
		Segment & operator = (const Segment &_a) {
    
     a = _a.a; b = _a.b; len = _a.len; }
		Segment(Point _a , Point _b) {
    
    
			a = _a; b = _b;
			int ax = a.getx() , bx = b.getx();
			int ay = a.gety() , by = b.gety();	
			int az = a.getz() , bz = b.getz();
			len = sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)+(az-bz)*(az-bz));
		}
		double getlen() {
    
     return len; }
		void print() {
    
    
			cout<<"起点:"<<a.getx()<<" "<<a.gety()<<" "<<a.getz()<<" 终点:"<<b.getx()<<" "<<b.gety()<<" "<<b.getz()<<endl; 
		}
};

class Polygons : public Segment {
    
    
	private:
		Segment s[105];
	public :
		Polygons() {
    
    }
		Polygons(Segment _x[] , int _c) {
    
    
			for(int i=1; i<=_c; i++)
				s[i] = _x[i];
		}
};

Point p[105];
Segment s[105];
Polygons d;
int cnt = 0 , num = 0 , x1 , x2 , x3;
char _in[25];
double ans;
bool vis[105][105][105];

bool Push(int a , int b , int c) {
    
    
	if(vis[a][b][c]==true)
		throw false;
	vis[a][b][c] = true;
	p[++cnt] = Point(x1,x2,x3); 
	return true;
}

void Sort() {
    
    
	int flag;
	for(int i=1; i<=num; i++) {
    
    
		flag = 0;
		for(int j=1; j<=num-i; j++)
			if(s[j].getlen()>s[j+1].getlen()) {
    
    
				flag = 1;
				Segment _temp = s[j+1];
				s[j+1] = s[j];
				s[j] = _temp;
			}
		if(!flag)
			break;
	}
}

int main() {
    
    
	ifstream in("points.txt",ios::in); 
	ofstream out("output1.txt",ios::out);
	memset(vis , false , sizeof(vis));
	while(in>>x1>>x2>>x3) {
    
    
		try {
    
     Push(x1,x2,x3); }
		catch(bool) {
    
     cout<<"("<<x1<<","<<x2<<","<<x3<<")坐标点重复"<<endl; }
	}
	for(int i=1; i<cnt; i++)
		s[++num] = Segment(p[i] , p[i+1]);
	s[++num] = Segment(p[cnt] , p[1]);
	d = Polygons(s , num);
	Point temp(101,102,103);
	s[9] = Segment(p[9] , temp);
	s[++num] = Segment(temp , p[10]);
	for(int i=1; i<=num; i++)
		ans += s[i].getlen();
	cout<<"周长是:"<<ans<<endl;
	cout<<"多边形的线段数:"<<num<<endl;
	for(int i=1; i<=num; i++) {
    
    
		cout<<"第"<<i<<"条线段长度为"<<s[i].getlen()<<" ";
		s[i].print();
	}
	Sort();
	for(int i=1; i<=num; i++)
		out<<"第"<<i<<"条线段长度为"<<s[i].getlen()<<endl;
	in.close();
	out.close();
	return 0;
}

用STL版

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<cmath> 
#include<algorithm>
#include<vector>
using namespace std;

class Point {
    
    
	private:
		int x , y , z;
	public:
		Point() {
    
    }
		Point & operator = (const Point &_a) {
    
     x=_a.x; y=_a.y; z=_a.z; }
		Point(int _x , int _y , int _z) {
    
     x = _x; y = _y; z = _z; }
		int getx() {
    
     return x;}
		int gety() {
    
     return y;}
		int getz() {
    
     return z;}
};

class Segment : public Point {
    
    
	private:
		Point a , b;
		double len;
	public :
		Segment() {
    
    }
		Segment & operator = (const Segment &_a) {
    
     a = _a.a; b = _a.b; len = _a.len; }
		Segment(Point _a , Point _b) {
    
    
			a = _a; b = _b;
			int ax = a.getx() , bx = b.getx();
			int ay = a.gety() , by = b.gety();	
			int az = a.getz() , bz = b.getz();
			len = sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)+(az-bz)*(az-bz));
		}
		double getlen() {
    
     return len; }
		void print() {
    
    
			cout<<"起点:"<<a.getx()<<" "<<a.gety()<<" "<<a.getz()<<" 终点:"<<b.getx()<<" "<<b.gety()<<" "<<b.getz()<<endl; 
		}
		bool operator < (const Segment &a) {
    
     return len<a.len; }
};

class Polygons : public Segment {
    
    
	private:
		Segment s[105];
	public :
		Polygons() {
    
    }
		Polygons(Segment _x[] , int _c) {
    
    
			for(int i=1; i<=_c; i++)
				s[i] = _x[i];
		}
};

vector<Point>p;
Segment s[105];
Polygons d;
int cnt = 0 , num = 0 , x1 , x2 , x3;
char _in[25];
double ans;
bool vis[105][105][105];

bool Push(int a , int b , int c) {
    
    
	if(vis[a][b][c]==true)
		throw false;
	vis[a][b][c] = true;
	p.push_back(Point(x1,x2,x3));
	return true;
}

int main() {
    
    
	ifstream in("points.txt",ios::in); 
	ofstream out("output2.txt",ios::out);
	memset(vis , false , sizeof(vis));
	while(in>>x1>>x2>>x3) {
    
    
		try {
    
     Push(x1,x2,x3); }
		catch(bool) {
    
     cout<<"("<<x1<<","<<x2<<","<<x3<<")坐标点重复"<<endl;  }
	}
	cnt = p.size();
	for(int i=0; i<(cnt-1); i++)
		s[++num] = Segment(p[i] , p[i+1]);
	s[++num] = Segment(p[cnt-1] , p[0]);
	d = Polygons(s , num);
	Point temp(101,102,103);
	s[9] = Segment(p[8] , temp);
	s[++num] = Segment(temp , p[9]);
	for(int i=1; i<=num; i++)
		ans += s[i].getlen();
	cout<<"周长是:"<<ans<<endl;
	cout<<"多边形的线段数:"<<num<<endl;
	for(int i=1; i<=num; i++) {
    
    
		cout<<"第"<<i<<"条线段长度为"<<s[i].getlen()<<" ";
		s[i].print();
	}
	sort(s+1,s+num+1);
	for(int i=1; i<=num; i++)
		out<<"第"<<i<<"条线段长度为"<<s[i].getlen()<<endl;
	in.close();
	out.close();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cls1277/article/details/115254387