我正在努力使我的代码更有效率。按下滚动按钮时,函数“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