码迷,mamicode.com
首页 > 其他好文 > 详细

Code for the Homework1 改进

时间:2015-12-11 01:29:11      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 #include <iostream>
  2 #include <vector>
  3 #include "shape.h"
  4 //using namespace std;
  5 //using namespace Eigen;  在shape.h中 
  6 const double PI=3.141592653;
  7 vector<SHAPE*> sv;
  8 vector<SHAPE*>::iterator itr;
  9 POINT p;
 10 LINE l;
 11 TRIANGLE t;
 12 void Shape_Save(void);
 13 void Shape_Handle(void);
 14 int main()
 15 {    
 16     int flag;
 17     while (1)
 18     {
 19         cout << "存图形请输入0,操作图形请输入1" << endl;
 20         cin >> flag;
 21         switch (flag)
 22         { 
 23         case 0:Shape_Save();break; 
 24         case 1:Shape_Handle(); break;
 25         default:cout << "输入错误!" << endl; break;     
 26         }
 27     }
 28     
 29     return 0;
 30 }
 31 
 32 void Shape_Save()
 33 {
 34     string name;
 35     int num;
 36     cout << "请输入图形名称 点数 坐标" << endl;
 37     cin >> name;
 38     cin >> num;
 39     switch (num)
 40     {
 41     case 1:
 42     {
 43         p.name=name;
 44         p.get_cin_point();
 45         sv.push_back(&p);
 46         break;
 47     }
 48     case 2:
 49     {
 50         l.name=name;
 51         l.get_cin_line();
 52         sv.push_back(&l);
 53         break;
 54     }
 55     case 3:
 56     {
 57         t.name=name;
 58         t.get_cin_triangle();
 59         sv.push_back(&t);
 60         break;
 61     }
 62     default: {cout << "点数输入错误" << endl; break; }
 63     }
 64     
 65 }
 66 void Shape_Handle(void)
 67 {
 68     string cmd,name1;
 69     char a, b, c;
 70     Vector2d vec;
 71     double x,y,angle;
 72     cout << "请输入操作指令" << endl;
 73     cin >> cmd;
 74     cin>> name1;
 75     if (cmd == "move")
 76     {
 77         cin >> a >> x >> b >> y >> c;
 78         vec[0]=x;vec[1]=y;
 79 //        for(int i=0;i<sv.size();i++)
 80 //        {
 81 //            if(name1 == sv[i]->name)
 82 //            {
 83 //                cout<<"i="<<i<<endl;
 84 //                sv[i]->move(vec);
 85 //                cout << sv[i]->name<<"平移后为:";
 86 //                sv[i]->display(); 
 87 //            }
 88 //        }
 89         for (itr = sv.begin(); itr != sv.end(); itr++)  //迭代器方式 
 90         {
 91             if(name1== (*itr)->name) 
 92             {
 93                 (*itr)->move(vec);
 94                 cout << (*itr)->name<<"平移后为:";
 95                 (*itr)->display();
 96             }
 97            
 98          } 
 99     }
100     else if (cmd == "rotate")
101     {
102         cin >> angle;
103 //        for(int i=0;i<sv.size();i++)
104 //        {
105 //            if(name1 == sv[i]->name)
106 //            {
107 //                sv[i]->rotate(angle);
108 //                cout << sv[i]->name<<"旋转后为:";
109 //                sv[i]->display(); 
110 //            }
111 //        }
112         for (itr = sv.begin(); itr != sv.end(); itr++)  //迭代器方式 
113         {
114             if(name1== (*itr)->name)
115             {
116                 (*itr)->rotate(angle);
117                 cout << (*itr)->name<<"旋转后为:";
118                 (*itr)->display();
119             }
120          } 
121     }
122     else cout << "comand is error!" << endl;
123 } 
main.cpp
技术分享
 1 #include <Eigen/Dense>
 2 #include <iostream>
 3 #include <vector>
 4 using namespace Eigen;
 5 using namespace std;
 6 extern const double PI;
 7 class SHAPE
 8 {
 9     public:
10     string name;
11     virtual void display()=0;
12 //    {
13 //        cout<<1; 
14 //        return; 
15 //    };
16     virtual void rotate(double &angle)=0;
17 //    {
18 //        cout<<1;
19 //        return;
20 //    }
21     virtual void move(Vector2d &vec)=0;
22 //    {
23 //        cout<<1;
24 //        return;
25 //    }
26 };
27 class POINT:public SHAPE
28 {
29     public:
30         double x, y;
31         POINT(){
32         };
33         POINT(string nam,double xx,double yy){
34             name=nam;
35             x=xx;
36             y=yy;
37         }
38         POINT(const POINT &p){
39             name=p.name;
40             x=p.x;
41             y=p.y;
42         }
43         void copyto(POINT &p);
44         void get_cin_point(void);
45         void display();
46         void rotate(double &angle);
47         void move(Vector2d &vec);
48 };
49 class LINE:public SHAPE
50 {
51     public:
52         POINT p_start,p_end;
53         LINE(){
54         }
55         LINE(string nam,POINT s,POINT e)
56         {
57             name=nam;
58             s.copyto(p_start);
59             e.copyto(p_end);
60         }
61         void get_cin_line(void);
62         void display();
63         void rotate(double &angle);
64         void move(Vector2d &vec);
65 };
66 class TRIANGLE:public SHAPE
67 {
68     public:
69         POINT p1,p2, p3;
70         TRIANGLE(){
71         } 
72         TRIANGLE(string nam,POINT pp1,POINT pp2,POINT pp3)
73         {
74             name=nam;
75             pp1.copyto(p1);
76             pp2.copyto(p2);
77             pp3.copyto(p3);
78         }
79         void get_cin_triangle(void);
80         void display();
81         void rotate(double &angle);
82         void move(Vector2d &vec);
83 }; 
shape.h
技术分享
 1 #include "shape.h" 
 2 /*****POINT类成员函数的实现****/ 
 3 void POINT::copyto(POINT &p){
 4     p.name=name;
 5     p.x=x;
 6     p.y=y;
 7 }
 8 void POINT::get_cin_point(void){
 9     char a, b, c;
10     cin >> a >> x >> b >> y >> c ;
11 }
12 void POINT::display() {
13     cout << "(" << this->x << "," << this->y << ")" << endl;
14 }
15 void POINT::rotate(double &angle){  //逆时针为正
16     double x, y,ang;
17     x = this->x;
18     y = this->y;
19     ang = angle*PI/ 180;
20     this->x = x*cos(ang) - y*sin(ang);
21     this->y = x*sin(ang) + y*cos(ang);
22 }
23 
24 void POINT::move(Vector2d &vec){
25     this->x+=vec[0];
26     this->y+=vec[1];
27 }
28 
29 /*****LINE类成员函数的实现****/
30 void LINE::get_cin_line(void){
31     char a, b, c;
32     cin >> a >>p_start.x>> b >> p_start.y >> c >> a >>p_end.x>> b >> p_end.y >> c;
33 }
34 void LINE::display(){
35             cout<<"起点";
36             p_start.display();
37             cout<<"终点";
38             p_end.display();
39 }
40 void LINE::rotate(double &angle){
41             p_start.rotate(angle);
42             p_end.rotate(angle);
43 }
44 void LINE::move(Vector2d &vec){
45             p_start.move(vec);
46             p_end.move(vec);
47 }
48 
49 /*****TRIANGLE类成员函数的实现****/
50 void TRIANGLE::get_cin_triangle(void){
51     char a, b, c;
52     cin >> a >>p1.x>> b >> p1.y >> c >> a >>p2.x>> b >> p2.y >> c>> a >>p3.x>> b >> p3.y >> c;
53 }
54 void TRIANGLE::display() {
55     cout<<"顶点1";
56     p1.display();
57     cout<<"顶点2";
58     p2.display();
59     cout<<"顶点3";
60     p3.display();
61 }
62 void TRIANGLE::rotate(double &angle){
63     p1.rotate(angle);
64     p2.rotate(angle);
65     p3.rotate(angle);
66 }
67 void TRIANGLE::move(Vector2d &vec){
68     p1.move(vec);
69     p2.move(vec);
70     p3.move(vec);
71 }
shape.cpp

运行结果:

技术分享

技术分享

Code for the Homework1 改进

标签:

原文地址:http://www.cnblogs.com/yixu/p/5037740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!