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

第一次编程作业

时间:2015-12-01 01:32:51      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

    第一编程作业代码如下:

技术分享
 1 #include <iostream>
 2 #include<cstring>
 3 #include<stdlib.h>
 4 #include <Eigen/Dense>
 5 #include<math.h>
 6 using namespace std;
 7 using namespace Eigen;    
 8 int main()
 9 {
10     float PI=3.1416;
11     int num;
12     char name[20];
13     string pdata[10];
14     string poperation[10];
15     string  data, operation;     
16     cout<<"请输入数据 ";
17     getline(cin,data);
18   /***进行字符串分割,以空格,逗号,前、后括号将字符串分割开***************************************/ 
19     char tmp1[20];
20     strncpy(tmp1,data.c_str(),data.length());
21     const char *split=", ()";    
22     char *p;
23     p=strtok(tmp1,split);
24     for(int i=0;p!=NULL;i++)
25     {
26         pdata[i]=p;
27         //cout<<pdata[i]<<endl;
28         p=strtok(NULL,split);
29         //cout<<i;
30     }
31 /****************************************************************************************************/        
32     num=atoi(pdata[1].c_str());
33     
34     RowVector2d v[num];
35     for(int i=0;i<num;i++){
36         v[i](0)=atof(pdata[2*i+2].c_str());
37         v[i](1)=atof(pdata[2*i+3].c_str());
38     }
39 
40      cout<<"请输入操作 ";
41     getline(cin,operation);
42   /***进行字符串分割,以空格,逗号,前、后括号将字符串分割开***************************************/ 
43     char tmp2[20];
44     strncpy(tmp2,operation.c_str(),operation.length());    
45     char *q;
46     q=strtok(tmp2,split);
47     for(int j=0;q!=NULL;j++)
48     {
49         poperation[j]=q;
50         //cout<<pdata[i]<<endl;
51         q=strtok(NULL,split);
52         //cout<<i;
53     }
54 /****************************************************************************************************/    
55     RowVector2d movev; 
56   
57     if(strcmp(poperation[0].c_str(),"move")==0){
58         movev(0)=atof(poperation[2].c_str());
59         movev(1)=atof(poperation[3].c_str() );
60         for(int k=0;k<num;k++){
61             v[k]+=movev;
62         }
63     
64     } 
65        
66     float deg1,deg; 
67      if(strcmp(poperation[0].c_str(),"rotate")==0){
68         deg1=atof(poperation[2].c_str());
69         Matrix2d rot;
70         deg=deg1/180*PI;
71         rot(0,0)=cos(deg);
72         rot(0,1)=sin(deg);
73         rot(1,0)=-sin(deg);
74         rot(1,1)=cos(deg);
75           for(int l=0;l<num;l++){aaaa
76             v[l]=v[l]*rot;    
77           }       
78 }
79   for(int s=0;s<num;s++){
80     cout<<v[s]<<endl;
81         
82     } 
83 } 
View Code

     运行结果如下图所示。
     执行平移操作结果:

技术分享

     执行旋转操作的结果如下图所示:

技术分享

    总结:
    为了使命令行的输入格式不用受到太多的限制,我才用了命令行分割函数将用户输入的一段命令行进行分割,并提取出有用的信息,进行操作。这样用户只要在在输入点坐标的时候,不论是采用"(x,y)"、“x,y”、“x y”、“(x y)”、[x,y]、{x,y}的形式程序都能正常运行。
      但是目前并没有用到模块化以及集成的编程方法,代码可读性比较差。

第一次编程作业

标签:

原文地址:http://www.cnblogs.com/chaoh/p/5008856.html

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