循环存储名称并跳过空白

时间:2013-07-01 15:46:37

标签: c string embedded skip

我正在努力提高代码效率。在滚动按钮按下时,函数'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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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中的名称已经是数组,那么根本不需要查找表。