0

我正在努力使我的代码更有效率。按下滚动按钮时,函数“SetName”将一个数字存储为整数iName,该整数是针对存储在内存中的 5 个名称的索引。如果内存中没有设置名称,则跳到下一个。

该代码有效,但占用了大量空间。任何建议表示赞赏。代码:

    #include <string.h>
    int iName = 0;
    int iNewName = 0;

    BYTE GetName ()
    {
     return  iName;
    }


    void SetName (int iNewName)
    {

      while (iName != iNewName)
      {

      switch (byNewName)
       {
        case 1:
        if (strlen (memory.m_nameA) == 0)
         new_name++;
        else
         iName = iNewName;
        break;

        case 2:
        if (strlen (memory.m_nameB) == 0)
         new_name++;
        else
         iName = iNewName;
        break;

        case 3:
        if (strlen (memory.m_nameC) == 0)
         new_name++;
        else
         iName = iNewName;
        break;

        case 4:
        if (strlen (memory.m_nameD) == 0)
         new_name++;
        else
         iName = iNewName;
        break;

        case 5:
        if (strlen (memory.m_nameE) == 0)
         new_name++;
        else
         iName = iNewName;
        break;

        default:
        iNewName = 1;
        break;
       } // end of case

      } // end of loop

    } // end of SetName function


void main ()
 {
  while(1)
  {
  if (Button_pushed)
   SetName(GetName+1);
  } // end of infinite loop
} // end of main
4

3 回答 3

0

new_name 可以是静态的吗?

#include <string.h>
static int iName = 0;
static int iNewName = 0;

BYTE GetName ()
{
 return  iName;
}

void shortCut(char[] m,int N) //may not work for other type of arrays because their pointers are diminished into a single pointer that cannot give "sizeof" value. But strlen function checks for a null element in string.
{
   if (strlen (m) == 0)
     new_name++;
   else
     iName = N;
}

void SetName (int iNewName)
{

  while (iName != iNewName)
  {   
    if(byNewName==1){shortCut(memory.m_nameA, iNewName);}
    else if(byNewName==2){shortCut(memory.m_nameB, iNewName);}
    else if(byNewName==3){shortCut(memory.m_nameC, iNewName);}
    else if(byNewName==4){shortCut(memory.m_nameD, iNewName);}
    else if(byNewName==5){shortCut(memory.m_nameE, iNewName);}
    else{iNewName = 1;}
  } // end of loop

} // end of SetName function

 void main ()
 {
    while(1)
    {
      if (Button_pushed)
      SetName(GetName+1);
    } // end of infinite loop
 } // end of main
于 2013-07-01T15:56:32.723 回答
0

首先,如果可以选择,我建议您避免使用全局变量。它更具可读性。
但是,在通过使用指针(以及带有逻辑的全局变量)的解决方案之后:

#include <string.h>
static int iName = 0;
static int iNewName = 0;
static int nbNames = 5; /* can be changed */
static char ** tableName = NULL;


BYTE GetName ()
{
 return  iName;
}


void shortCut(char* m, int newNameNumber)
{
   if (strlen (m) == 0)
     new_name++;
   else
     iName = newNameNumber;
}

void SetName (int iNewName)
{

  while (iName != iNewName)
  {   
    if(byNewName > 0 && byNewName <= nbNames) {
      shortCut(tableName[byNewName-1],iNewName);
    } else {
       iNewName = 1;
    }
  } // end of loop

} // end of SetName function


 void main ()
 {
     tableName = (char**)malloc(sizeof(char*)*nbNames);
    /* here check tableName != NULL */

    /* I don't know which data structure is 'memory' but you should initialize the value with a loop*/
    tableName[0] = memory.m_nameA;
    /* ... */

    while(1)
    {
      if (Button_pushed)
      SetName(GetName+1);
    } // end of infinite loop
    free(tableName);
 } // end of main
于 2013-07-01T16:26:37.993 回答
0

替换表格的所有行:

if (strlen (memory.m_nameA) == 0)

if( *memory.m_nameA == '\0' )

strlen()这将消除对和 string.h的依赖。测试空字符串是一种特殊情况,不需要您确定字符串的长度。如果您不在其他地方使用它,它将完全从代码中删除。

实际上,您在索引上选择了不必要的重复代码。改用查找表:

void SetName (int iNewName)
{
    static const char* lookup[] = { memory.m_nameA, 
                                    memory.m_nameB,
                                    memory.m_nameC,
                                    memory.m_nameD,
                                    memory.m_nameE } ;


  while (iName != iNewName)
  {

      iNewName = 1;
      if( byNewName < sizeof(lookup) / sizeof(*lookup) )
      {
          if( *lookup[byNewName] == '\0' )
          {
              new_name++;
          }
          else
          {
              iName = iNewName;
          }
      }
   } // end of loop
}

如果其中的名称memory已经是一个数组,那么您根本不需要查找表。

于 2013-07-01T17:19:54.117 回答