首页 > 解决方案 > c中的重构函数

问题描述

我有一段看起来像这样的代码:

void printdatamatrix(int xp, int yp, int h)
{
   int wide;
   int x = xp;
   int y = yp;

   if(h>=40)
   {
      wide = h /9;
   }
   else
   { 
      wide = h / 4;
   }

   /*vertical - L-shape*/
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);
   x += wide;
   drawsquare(x, y, wide);

   /*horizontal - L-shape*/
   x = xp;
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);

   x = xp;
   y = yp;
   x += wide;
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);

   x = xp;
   y = yp;
   x += wide * 2;
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide * 2;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide * 2;
   drawsquare(x, y, wide);

   x = xp;
   y = yp;
   x += wide * 3;
   y -= wide * 3;
   drawsquare(x, y, wide);
   y -= wide;
   drawsquare(x, y, wide);
   y -= wide * 2;
   drawsquare(x, y, wide);
   y -= wide * 4;
   drawsquare(x, y, wide);
         .
         .
         .
        etc

drawsquare(x,y,wide) 函数创建一个正方形以形成数据矩阵(条形码)。我想知道是否可以将 printdatamatrix(int xp, int yp, int h) 函数重构为更小。我唯一想到的是为循环写 2 个并包括 L 形(一个用于垂直,另一个用于水平)。

任何帮助表示赞赏。提前致谢!

标签: cfunctionrefactoring

解决方案


我创建了一个嵌套循环来绘制存储在二维数组中的模式。它是这样的:

void printdatamatrix(int xp, int yp, int h, int dirn)
{
    int wide = _convertPixelsToMmForDataMatrix(h); 
    int x = xp; 
    int y = yp; 
    unsigned int i, j;

    static bool my_data[ROWS][COLS] = {
    { 1,1,1,1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,0,1,0,1,0 },
    { 1,1,0,1,1,1,1,0,1,1,0,1 },
    { 1,0,0,1,1,0,1,0,0,0,1,0 },
    { 1,1,0,1,0,1,0,0,1,1,0,1 },
    { 1,1,0,1,1,0,0,1,0,1,0,0 },
    { 1,0,1,1,0,1,1,0,1,0,1,1 },
    { 1,1,1,0,0,0,1,0,0,0,1,0 },
    { 1,1,0,0,0,0,0,1,1,1,1,1 },
    { 1,1,0,0,1,0,0,0,0,0,0,0 },
    { 1,1,0,0,1,0,0,1,0,1,0,1 },
    { 1,0,1,0,1,0,1,0,1,0,1,0 }
    };

    for (i = 0; i < 12; ++i)
    {
        for (j = 0; j < 12; ++j)
        {
            if (my_data[i][j])
            { 
                switch (dirn)
                {
                    case 1: // 270
                        drawsquare(x + j * wide, y + i * wide, wide);
                        break;
                    case 2: // 0
                        drawsquare(x + i * wide, y - j * wide, wide);
                        break;
                    case 3: // 90
                        drawsquare(x - j * wide, y - i * wide, wide);
                        break;
                    case 4: // 180
                        drawsquare(x - i * wide, y + j * wide, wide);
                        break;
                }
            }
       }
   }
}

推荐阅读