#include <iostream> #include <vector> #include "shape.h" //using namespace std; //using namespace Eigen; 在shape.h中 const double PI=3.141592653; vector<SHAPE*> sv; vector<SHAPE*>::iterator itr; POINT p; LINE l; TRIANGLE t; void Shape_Save(void); void Shape_Handle(void); int main() { int flag; ) { cout << "存图形请输入0,操作图形请输入1" << endl; cin >> flag; switch (flag) { :Shape_Save();break; :Shape_Handle(); break; default:cout << "输入错误!" << endl; break; } } ; } void Shape_Save() { string name; int num; cout << "请输入图形名称 点数 坐标" << endl; cin >> name; cin >> num; switch (num) { : { p.name=name; p.get_cin_point(); sv.push_back(&p); break; } : { l.name=name; l.get_cin_line(); sv.push_back(&l); break; } : { t.name=name; t.get_cin_triangle(); sv.push_back(&t); break; } default: {cout << "点数输入错误" << endl; break; } } } void Shape_Handle(void) { string cmd,name1; char a, b, c; Vector2d vec; double x,y,angle; cout << "请输入操作指令" << endl; cin >> cmd; cin>> name1; if (cmd == "move") { cin >> a >> x >> b >> y >> c; vec[]=x;vec[]=y; // for(int i=0;i<sv.size();i++) // { // if(name1 == sv[i]->name) // { // cout<<"i="<<i<<endl; // sv[i]->move(vec); // cout << sv[i]->name<<"平移后为:"; // sv[i]->display(); // } // } for (itr = sv.begin(); itr != sv.end(); itr++) //迭代器方式 { if(name1== (*itr)->name) { (*itr)->move(vec); cout << (*itr)->name<<"平移后为:"; (*itr)->display(); } } } else if (cmd == "rotate") { cin >> angle; // for(int i=0;i<sv.size();i++) // { // if(name1 == sv[i]->name) // { // sv[i]->rotate(angle); // cout << sv[i]->name<<"旋转后为:"; // sv[i]->display(); // } // } for (itr = sv.begin(); itr != sv.end(); itr++) //迭代器方式 { if(name1== (*itr)->name) { (*itr)->rotate(angle); cout << (*itr)->name<<"旋转后为:"; (*itr)->display(); } } } else cout << "comand is error!" << endl; }
main.cpp
#include <Eigen/Dense> #include <iostream> #include <vector> using namespace Eigen; using namespace std; extern const double PI; class SHAPE { public: string name; ; // { // cout<<1; // return; // }; ; // { // cout<<1; // return; // } ; // { // cout<<1; // return; // } }; class POINT:public SHAPE { public: double x, y; POINT(){ }; POINT(string nam,double xx,double yy){ name=nam; x=xx; y=yy; } POINT(const POINT &p){ name=p.name; x=p.x; y=p.y; } void copyto(POINT &p); void get_cin_point(void); void display(); void rotate(double &angle); void move(Vector2d &vec); }; class LINE:public SHAPE { public: POINT p_start,p_end; LINE(){ } LINE(string nam,POINT s,POINT e) { name=nam; s.copyto(p_start); e.copyto(p_end); } void get_cin_line(void); void display(); void rotate(double &angle); void move(Vector2d &vec); }; class TRIANGLE:public SHAPE { public: POINT p1,p2, p3; TRIANGLE(){ } TRIANGLE(string nam,POINT pp1,POINT pp2,POINT pp3) { name=nam; pp1.copyto(p1); pp2.copyto(p2); pp3.copyto(p3); } void get_cin_triangle(void); void display(); void rotate(double &angle); void move(Vector2d &vec); };
shape.h
#include "shape.h" /*****POINT类成员函数的实现****/ void POINT::copyto(POINT &p){ p.name=name; p.x=x; p.y=y; } void POINT::get_cin_point(void){ char a, b, c; cin >> a >> x >> b >> y >> c ; } void POINT::display() { cout << "(" << this->x << "," << this->y << ")" << endl; } void POINT::rotate(double &angle){ //逆时针为正 double x, y,ang; x = this->x; y = this->y; ang = angle*PI/ ; this->x = x*cos(ang) - y*sin(ang); this->y = x*sin(ang) + y*cos(ang); } void POINT::move(Vector2d &vec){ ]; ]; } /*****LINE类成员函数的实现****/ void LINE::get_cin_line(void){ char a, b, c; cin >> a >>p_start.x>> b >> p_start.y >> c >> a >>p_end.x>> b >> p_end.y >> c; } void LINE::display(){ cout<<"起点"; p_start.display(); cout<<"终点"; p_end.display(); } void LINE::rotate(double &angle){ p_start.rotate(angle); p_end.rotate(angle); } void LINE::move(Vector2d &vec){ p_start.move(vec); p_end.move(vec); } /*****TRIANGLE类成员函数的实现****/ void TRIANGLE::get_cin_triangle(void){ char a, b, c; cin >> a >>p1.x>> b >> p1.y >> c >> a >>p2.x>> b >> p2.y >> c>> a >>p3.x>> b >> p3.y >> c; } void TRIANGLE::display() { cout<<"顶点1"; p1.display(); cout<<"顶点2"; p2.display(); cout<<"顶点3"; p3.display(); } void TRIANGLE::rotate(double &angle){ p1.rotate(angle); p2.rotate(angle); p3.rotate(angle); } void TRIANGLE::move(Vector2d &vec){ p1.move(vec); p2.move(vec); p3.move(vec); }
shape.cpp
运行结果: