zoukankan      html  css  js  c++  java
  • 实时软件控制设计第一次作业

      1 #include <iostream>
      2 #include <string>
      3 #include <stdlib.h> 
      4 #include <math.h>
      5 #include <Eigen/Dense>
      6 
      7 #define PN 100//可保存的点的数目 
      8 #define LN 100//可保存线的数目 
      9 #define RN 100//可保存三角形的数目 
     10 #define PI 3.1415926
     11 
     12 using namespace Eigen;
     13 using namespace std;
     14 
     15 void add ();
     16 void addPoint();
     17 void addLine();
     18 void addRect();
     19 void move();
     20 void rotate();
     21 
     22 
     23 
     24 //模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)  
     25 template <class Type>  
     26 Type stringToNum(const string& str)  
     27 {  
     28     istringstream iss(str);  
     29     Type num;  
     30     iss >> num;  
     31     return num;      
     32 }  
     33 
     34 class Point {
     35     
     36     private :
     37         Vector2d v;    
     38         string name;    
     39     public:
     40         
     41         //构造函数
     42         Point(Vector2d v1){
     43             v = v1;
     44         }
     45         
     46         Point(Vector2d v1 , string pname) {
     47             v = v1;
     48             name = pname;
     49         } 
     50         Point() {};
     51         
     52         //设置点的x,y坐标 
     53         void setPoint(Vector2d v1){
     54             v = v1;
     55         }
     56                 
     57         
     58         Vector2d getPoint() {
     59             return v;
     60         }
     61         
     62         string getName() {
     63             return name;
     64         }
     65         
     66         void setName(string pname) {
     67             name = pname;
     68         }
     69 };
     70 
     71 class Line {
     72     private :
     73         Vector2d v1;
     74         Vector2d v2;
     75         string name;
     76         
     77     public :
     78         //构造函数
     79         Line() {};
     80          
     81         void setLine(Vector2d vv1,Vector2d vv2) {
     82             v1 = vv1;
     83             v2 = vv2;
     84         }
     85         
     86         Vector2d getStartPoint() {
     87             return v1;
     88         }
     89         
     90         Vector2d getEndPoint() {
     91             return v2;
     92         }
     93         
     94         void setName(string lname) {
     95             name = lname;
     96         }
     97         
     98         string getName() {
     99             return name;
    100         }
    101         
    102         
    103 };
    104 
    105 class Rect {
    106     private :
    107         Vector2d v1;
    108         Vector2d v2;
    109          Vector2d v3;
    110          string name;
    111          
    112     public:
    113         
    114         Rect() {};
    115         
    116     void setRect(Vector2d vv1,Vector2d vv2,Vector2d vv3) {
    117         v1 = vv1;
    118         v2 = vv2;
    119         v3 = vv3;
    120     }
    121     
    122     Vector2d getPoint1() {
    123         return v1;
    124     }
    125     
    126     Vector2d getPoint2() {
    127         return v2;
    128     }
    129     
    130     Vector2d getPoint3() {
    131         return v3;
    132     }
    133     
    134     void setName(string rname) {
    135         name = rname;
    136     }
    137     
    138     string getName() {
    139         return name;
    140     }
    141 };
    142 
    143 string a[5];
    144 int pcnt = 0;
    145 Point point[PN]; 
    146 int lcnt = 0;
    147 Line line[LN];
    148 int rcnt = 0;
    149 Rect rect[RN];
    150 
    151 
    152 int main(int argc,char *argv[]) {
    153     
    154     while(1) {
    155         string str ; //定义一个字符串去接收命令行传入的值
    156         getline(cin,str);//接收命令行的字符串 
    157         
    158     //将命令行的字符串按空格分割存入a[5]中 
    159         for(int i=0,j=0;i<str.size();i++) {
    160             int pos;
    161             pos = str.find(' ',i);
    162             if(pos!=std::string::npos) {
    163                 a[j] = str.substr(i,pos-i);
    164                 j++;
    165                 i=pos;
    166             } else {
    167                 a[j] = str.substr(i,str.size()-i);
    168                 break;
    169             }
    170             
    171          }
    172          
    173          if(a[0]=="move") {
    174              move();
    175         } else if(a[0]=="rotate") {
    176             rotate();
    177         } else {
    178             add();
    179         }
    180     }
    181     
    182 }
    183 
    184 void add() {
    185     if(a[1]=="1") {
    186         addPoint();
    187     } else if(a[1]=="2") {
    188         addLine();
    189     } else if(a[1]=="3") {
    190         addRect();
    191     } else {
    192         cout<<"输入格式有误,请按照正确的格式输入!"<<endl;
    193     }
    194     
    195 }
    196 
    197 void addPoint() {
    198     Vector2d v;
    199     string m1,m2; 
    200     string s = a[2];
    201     double x,y;
    202     int pos;
    203     pos=s.find(',',0);
    204     if(pos!=std::string::npos) {
    205         m1 = s.substr(0,pos);
    206         m2 = s.substr(pos+1,s.size()-pos);
    207         x = stringToNum<double>(m1);
    208         y = stringToNum<double>(m2);
    209      } else {
    210          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    211          cout<<"p1 1 5.2,6.4"<<endl;
    212          exit(0); 
    213      }
    214      v<<x,y;
    215      
    216     point[pcnt].setPoint(v);
    217     point[pcnt].setName(a[0]);
    218     pcnt++;
    219     cout<<"添加了一个点:"<<a[0]<<endl;
    220     cout<<"坐标为:"<<v.transpose()<<endl; 
    221     
    222 }
    223 
    224 void addLine() {
    225     Vector2d v1,v2;
    226     string m1,m2,m3,m4;
    227     string s1 = a[2];
    228     string s2 = a[3];
    229     double x1,x2,y1,y2;
    230     int pos1,pos2;
    231         pos1=s1.find(',',0);
    232         
    233     if(pos1!=std::string::npos) {
    234         m1 = s1.substr(0,pos1);
    235         m2 = s1.substr(pos1+1,s1.size()-pos1);
    236         x1 = stringToNum<double>(m1);
    237         y1 = stringToNum<double>(m2);
    238      } else {
    239          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    240          cout<<"l1 2 1,2 3,4"<<endl;
    241          exit(0); 
    242     }
    243     
    244     pos2=s2.find(',',0);
    245     if(pos2!=std::string::npos) {
    246         m3 = s2.substr(0,pos2);
    247         m4 = s2.substr(pos2+1,s2.size()-pos2);
    248         x2 = stringToNum<double>(m3);
    249         y2 = stringToNum<double>(m4);
    250      } else {
    251          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    252          cout<<"l1 2 1,2 3,4"<<endl;
    253          exit(0); 
    254     }
    255     
    256     v1<<x1,y1;
    257     v2<<x2,y2;
    258     line[lcnt].setLine(v1,v2);
    259     line[lcnt].setName(a[0]);
    260     lcnt++;
    261     cout<<"添加了一条线:"<<a[0]<<endl;
    262     cout<<"起点坐标为:"<<v1.transpose()<<endl;
    263     cout<<"终点坐标为:"<<v2.transpose()<<endl; 
    264 }
    265 
    266 void addRect() {
    267     Vector2d v1,v2,v3;
    268     string m1,m2,m3,m4,m5,m6;
    269     string s1 = a[2];
    270     string s2 = a[3];
    271     string s3 = a[4];
    272     double x1,x2,x3,y1,y2,y3;
    273     int pos1,pos2,pos3;
    274     
    275     pos1=s1.find(',',0);    
    276     if(pos1!=std::string::npos) {
    277         m1 = s1.substr(0,pos1);
    278         m2 = s1.substr(pos1+1,s1.size()-pos1);
    279         x1 = stringToNum<double>(m1);
    280         y1 = stringToNum<double>(m2);
    281      } else {
    282          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    283          cout<<"l1 2 1,2 3,4 5,6"<<endl;
    284          exit(0); 
    285     }
    286     
    287     pos2=s2.find(',',0);
    288     if(pos2!=std::string::npos) {
    289         m3 = s2.substr(0,pos2);
    290         m4 = s2.substr(pos2+1,s2.size()-pos2);
    291         x2 = stringToNum<double>(m3);
    292         y2 = stringToNum<double>(m4);
    293      } else {
    294          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    295          cout<<"l1 2 1,2 3,4"<<endl;
    296          exit(0); 
    297     }
    298     
    299     pos3=s3.find(',',0);
    300     if(pos3!=std::string::npos) {
    301         m5 = s3.substr(0,pos3);
    302         m6 = s3.substr(pos3+1,s3.size()-pos3);
    303         x3 = stringToNum<double>(m5);
    304         y3 = stringToNum<double>(m6);
    305      } else {
    306          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    307          cout<<"l1 2 1,2 3,4 5,6"<<endl;
    308          exit(0); 
    309     }
    310     
    311     v1<<x1,y1;
    312     v2<<x2,y2;
    313     v3<<x3,y3;
    314     rect[rcnt].setRect(v1,v2,v3);
    315     rect[rcnt].setName(a[0]);
    316     rcnt++;
    317     cout<<"添加了一个三角形:"<<a[0]<<endl;
    318     cout<<"端点一坐标为:"<<v1.transpose()<<endl;
    319     cout<<"端点二坐标为:"<<v2.transpose()<<endl;
    320     cout<<"端点三坐标为:"<<v3.transpose()<<endl;
    321      
    322 }
    323 
    324 void move() {
    325     string s;
    326     s = a[1];
    327     Vector2d v;
    328     bool find = false;
    329     string s1 = a[2];
    330     double x,y;
    331     int pos;
    332     string m1,m2;
    333     
    334     pos=s1.find(',',0);
    335     if(pos!=std::string::npos) {
    336         m1 = s1.substr(0,pos);
    337         m2 = s1.substr(pos+1,s.size()-pos);
    338         x = stringToNum<double>(m1);
    339         y = stringToNum<double>(m2);
    340      } else {
    341          cout<<"指令输入有误,要添加点请按照如下结构输入:"<<endl;
    342          cout<<"move XX 2,3"<<endl;
    343          exit(0); 
    344      }
    345      v<<x,y;
    346     
    347     for(int i=0;i<PN;i++) {
    348         if(point[i].getName()==s) {
    349             find = true;
    350             Vector2d v1 = point[i].getPoint();
    351              v1=v1+v;
    352              cout<<s<<"平移后的点的坐标为:"<<v1.transpose()<<endl;    
    353         } 
    354     }
    355     
    356     for(int i=0;i<LN;i++) {
    357         if(line[i].getName()==s) {
    358             find = true;
    359             Vector2d v1 = line[i].getStartPoint();
    360             Vector2d v2 = line[i].getEndPoint();
    361              v1 = v1+v;
    362              v2 = v2+v; 
    363              cout<<"直线"<<s<<"平移后起点的坐标为:"<<v1.transpose()<<endl;
    364              cout<<"直线"<<s<<"平移后终点的坐标为:"<<v2.transpose()<<endl;    
    365         } 
    366     }
    367     
    368     for(int i=0;i<RN;i++) {
    369         if(rect[i].getName()==s) {
    370             find = true;
    371             Vector2d v1 = rect[i].getPoint1();
    372             Vector2d v2 = rect[i].getPoint2();
    373             Vector2d v3 = rect[i].getPoint3();
    374              v1 = v1+v;
    375              v2 = v2+v; 
    376              v3 = v3+v;
    377              cout<<"三角形"<<s<<"点1平移后的坐标为:"<<v1.transpose()<<endl;
    378              cout<<"三角形"<<s<<"点2平移后的坐标为:"<<v2.transpose()<<endl;
    379              cout<<"三角形"<<s<<"点3平移后的坐标为:"<<v2.transpose()<<endl;    
    380         } 
    381     }
    382     
    383     if(find==false) {
    384         cout<<"没有找到对应的点线面,请先添加点线面!"<<endl; 
    385     }
    386 }
    387 
    388 void rotate() {
    389     string s;
    390     string s1 = a[2];
    391     s = a[1];
    392     MatrixXd rot(2,2);
    393     bool find = false;
    394     double deg,avg;
    395     deg = stringToNum<double>(s1);
    396     avg = deg*PI/180;
    397     rot(0,0)=cos(avg);
    398     rot(0,1)=sin(avg);
    399     rot(1,0)=-sin(avg);
    400     rot(1,1)=cos(avg);
    401    for(int i=0;i<PN;i++) {
    402         if(point[i].getName()==s) {
    403             find = true;
    404             Vector2d v1 = point[i].getPoint();
    405              v1=rot*v1;
    406              cout<<""<<s<<"绕原点旋转后的坐标为:"<<v1.transpose()<<endl;    
    407         } 
    408     }
    409 
    410     for(int i=0;i<LN;i++) {
    411         if(line[i].getName()==s) {
    412             find = true;
    413             Vector2d v1 = line[i].getStartPoint();
    414             Vector2d v2 = line[i].getEndPoint();
    415              v1 = rot*v1;
    416              v2 = rot*v2; 
    417              cout<<"直线"<<s<<"旋转后起点的坐标为:"<<v1.transpose()<<endl;
    418              cout<<"直线"<<s<<"旋转后终点的坐标为:"<<v2.transpose()<<endl;    
    419         } 
    420     }    
    421     
    422         for(int i=0;i<RN;i++) {
    423         if(rect[i].getName()==s) {
    424             find = true;
    425             Vector2d v1 = rect[i].getPoint1();
    426             Vector2d v2 = rect[i].getPoint2();
    427             Vector2d v3 = rect[i].getPoint3();
    428              v1 = rot*v1;
    429              v2 = rot*v2; 
    430              v3 = rot*v3;
    431              cout<<"三角形"<<s<<"点1旋转后的坐标为:"<<v1.transpose()<<endl;
    432              cout<<"三角形"<<s<<"点2旋转后的坐标为:"<<v2.transpose()<<endl;
    433              cout<<"三角形"<<s<<"点3旋转后的坐标为:"<<v2.transpose()<<endl;    
    434         } 
    435     }
    436     
    437     if(find==false) {
    438         cout<<"没有找到对应的点线面,请先添加点线面!"<<endl;
    439     } 
    440 }

    结果截图:

    点线面的创建:

    点线面的平移:

    点线面的旋转:

    输入错误的提示:

    解题思路:

    1、考虑到程序的可拓展性,所以选择建立了点、线、面类,然后用对象数组来保存点线面的对象(本来应该用容器更好,但是容器的知识不太会)。

    2、首先应该将输入字符串拆分,以空格为界限,得到多个子数组用字符串数组保存。

    3、保存的字符串要根据其类型进行转换,然后通过if语句判断进入对应的程序段即可

    难点:

    1、函数和类都应该先定义后使用,这一点和java不一样,所以一开始很不习惯。

    2、模版函数

    template <class Type>
    Type stringToNum(const string& str)
    {
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;
    }

    用于字符串向各种基本类型的转换,很实用。

    3、这份代码其实冗余度还比较高,需要完善。

      

  • 相关阅读:
    SQL Server 隐式转换引发的死锁
    C# List按某对象的属性分组 IGrouping
    C# 正则表达式获取json字符串中的键值
    .NET程序修改 ConfigurationManager 后,不需要重启IIS也可刷新Web.config配置文件
    相同结构的多个表合并到一个表的实现方法
    WCF系列_WCF影响客户端导出Excel文件的实现
    WCF系列_WCF如何选择不同的绑定
    WCF系列_WCF常用绑定选择
    JS生成URL二维码
    win 常用CMD命令备忘
  • 原文地址:https://www.cnblogs.com/maowei/p/5008286.html
Copyright © 2011-2022 走看看