如何在结构中搜索并显示它 #包括 #包括 结构规范{ int计算机ID; 字符计算机名[50]; 浮动价格; }; void PrintAll(整数计数,结构PCSpec计算机[100]); int输入数据(int计数,结构PCSpec计算机[100]); 无效搜索(整数计数,结构PCSpec计算机[100]); 无效搜索ID(整数计数,结构PCSpec计算机[100]); 无效搜索名称(整数计数,结构PCSpec计算机[100]); void主菜单(整数计数,结构PCSpec计算机[100]); 无效选项1菜单(); 无效选项2菜单(); void PrintSelective(结构PCSpec计算机[100]); main() { 整数计数; 结构PCSpec计算机[100]; printf(“请输入计算机数量(最大为100)”); scanf(“%d”、&count); 系统(“cls”); 选项1菜单(); 主菜单(计数、计算机); } 整数输入数据(整数计数,结构PCSpec计算机[100]) { int i; 对于(i=0;i
三件事:如何在结构中搜索并显示它 #包括 #包括 结构规范{ int计算机ID; 字符计算机名[50]; 浮动价格; }; void PrintAll(整数计数,结构PCSpec计算机[100]); int输入数据(int计数,结构PCSpec计算机[100]); 无效搜索(整数计数,结构PCSpec计算机[100]); 无效搜索ID(整数计数,结构PCSpec计算机[100]); 无效搜索名称(整数计数,结构PCSpec计算机[100]); void主菜单(整数计数,结构PCSpec计算机[100]); 无效选项1菜单(); 无效选项2菜单(); void PrintSelective(结构PCSpec计算机[100]); main() { 整数计数; 结构PCSpec计算机[100]; printf(“请输入计算机数量(最大为100)”); scanf(“%d”、&count); 系统(“cls”); 选项1菜单(); 主菜单(计数、计算机); } 整数输入数据(整数计数,结构PCSpec计算机[100]) { int i; 对于(i=0;i,c,arrays,search,struct,C,Arrays,Search,Struct,三件事: 使用scanf(“%s”,&NAME)可以将字符串读入单个字符。非空字符串始终需要至少两个字符:至少一个用于字符串内容,另一个用于字符串终止符。49个字符的字符串需要50个字符的数组 “NAME”是一个文本字符串,而不是变量NAME 使用Computer[j].ComputerName==“NAME”可以比较两个指针。使用Computer[j].ComputerName==NAME可以将指针与单个char值进行比较。如果将NAME更改为数组,则再次比较两个指针。要比较字符串,需要使用
scanf(“%s”,&NAME)
可以将字符串读入单个字符。非空字符串始终需要至少两个字符:至少一个用于字符串内容,另一个用于字符串终止符。49个字符的字符串需要50个字符的数组
“NAME”
是一个文本字符串,而不是变量NAME
Computer[j].ComputerName==“NAME”
可以比较两个指针。使用Computer[j].ComputerName==NAME
可以将指针与单个char
值进行比较。如果将NAME
更改为数组,则再次比较两个指针。要比较字符串,需要使用您需要将
i
传递到PrintSelective
:
void PrintAll(int count,struct PCSpec Computer[100])
{
int j;
for(j=0;j<count;j++)
{
printf("Computer Package %d\n", j);
printf("Computer ID = \t\t%d\n",Computer[j].ComputerID);
printf("Computer Name = \t%s\n",Computer[j].ComputerName);
}
}
void PrintSelective(struct PCSpec Computer[100])
{
int i;
printf("Computer Package %d\n", i);
printf("Computer ID = \t\t%d\n",Computer[i].ComputerID);
printf("Computer Name = \t%s\n",Computer[i].ComputerName);
}
void Option1Menu()
{
printf("\n1] Create a Record\n");
printf("2] Display Records\n");
printf("3] Search a Record\n");
printf("4] Exit");
}
void Option2Menu()
{
printf("\n1] Search using ComputerID\n");
printf("2] Search using ComputerNAME\n");
printf("3] Back to Main Menu\n");
printf("4] Exit the Program\n");
}
然后称之为:
void PrintSelective(struct PCSpec Computer[100], int i)
{
printf("Computer Package %d\n", i);
printf("Computer ID = \t\t%d\n",Computer[i].ComputerID);
printf("Computer Name = \t%s\n",Computer[i].ComputerName);
}
PrintAll
也应调用PrintSelective
或者,我不会在PrintSelective
中打印索引,在这种情况下,写为:
PrintSelective(Computer, i);
并称为:
void PrintSelective(struct PCSpec *pComputer)
{
printf("Computer ID = \t\t%d\n",pComputer->ComputerID);
printf("Computer Name = \t%s\n",pComputer->ComputerName);
}
Computer[j]。ComputerName==“NAME”
无法工作。您需要strcmp
来比较C中的字符串。
void PrintSelective(struct PCSpec Computer[100], int i)
{
printf("Computer Package %d\n", i);
printf("Computer ID = \t\t%d\n",Computer[i].ComputerID);
printf("Computer Name = \t%s\n",Computer[i].ComputerName);
}
PrintSelective(Computer, i);
void PrintSelective(struct PCSpec *pComputer)
{
printf("Computer ID = \t\t%d\n",pComputer->ComputerID);
printf("Computer Name = \t%s\n",pComputer->ComputerName);
}
PrintSelective(&Computer[i]);