zoukankan      html  css  js  c++  java
  • 第一次编程作业

        第一编程作业代码如下:

     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}的形式程序都能正常运行。
          但是目前并没有用到模块化以及集成的编程方法,代码可读性比较差。

  • 相关阅读:
    越长大越孤单
    关于ASP.NET 启动Process的讨论
    利用selenium开发一个功能测试的框架
    开博啦(上班时间)
    利用回发 实现一个简单的AutoComplete功能
    FIFO和双端口RAM
    8位定点数开方程序(贴下来以后研究)
    Eclipse下文件读取的问题:Failed to reading file xxxxx
    Error:NgdBuild:604解决方法(添加NGC文件方法)
    Mandelbrot:美丽的分形
  • 原文地址:https://www.cnblogs.com/chaoh/p/5008856.html
Copyright © 2011-2022 走看看