gpt4 book ai didi

c - 如何在结构内搜索并显示它

转载 作者:行者123 更新时间:2023-11-30 18:48:41 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>

struct PCSpec{
int ComputerID;
char ComputerName[50];
float Price;
};


void PrintAll(int count,struct PCSpec Computer[100]);
int InputData(int count,struct PCSpec Computer[100]);
void Search(int count,struct PCSpec Computer[100]);
void SearchID(int count,struct PCSpec Computer[100]);
void SearchNAME(int count,struct PCSpec Computer[100]);
void MainMenu(int count,struct PCSpec Computer[100]);
void Option1Menu();
void Option2Menu();
void PrintSelective(struct PCSpec Computer[100]);

main()
{
int count;
struct PCSpec Computer[100];
printf("Please enter the Number Of Computer (Max is 100) ");
scanf("%d",&count);
system("cls");
Option1Menu();
MainMenu(count,Computer);
}

int InputData(int count,struct PCSpec Computer[100])
{
int i;
for(i=0;i<count;i++)
{
printf("Enter the ComputerID = ");
scanf("%d", &Computer[i].ComputerID);
printf("Enter the ComputerName, Maximum 50 Characters = ");
scanf(" %s",Computer[i].ComputerName);
}
printf("Data was SUCCESSFULLY recorded!");
}

void MainMenu(int count,struct PCSpec Computer[100])
{
while (1)
{
int option;
printf("\nEnter your choice : ");
scanf("%d", &option);
switch (option)
{
case 1:
system("cls");
InputData(count,Computer);
system("cls");
Option1Menu();
break;
case 2:
system("cls");
PrintAll(count,Computer);
Option1Menu();
break;
case 3:
system("cls");
Search(count,Computer);
case 4:
exit(0);
}
}
}


void Search(int count,struct PCSpec Computer[100])
{
int Option;
Option2Menu();
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&Option);
switch(Option)
{
case 1:
system("cls");
SearchID(count,Computer);
Option2Menu();
break;
case 2:
system("cls");
SearchNAME(count,Computer);
Option2Menu();
break;
case 3:
system("cls");
MainMenu(count,Computer);
case 4:
exit(1);
}
}
}

抱歉代码太长,下面的部分是我遇到问题的部分。我能够成功搜索,但无法显示搜索到的结构。我最初的计划是搜索 ComputerID,如果找到,我希望它也显示 ComputerName。

如果找到 ID,我想要得到的输出。计算机 ID = 21计算机名称=桌面

void SearchID(int count,struct PCSpec Computer[100])
{
int i,id;
printf("Please Enter Computer ID ");
scanf("%d",&id);
for(i=0;i<count;i++)
{
if(Computer[i].ComputerID == id)
{
printf("%d is found at %d",id,i);
//PrintSelective(Computer);
break;
}
}
if( i == count )
printf("Unable to locate ComputerID\n");
}

对于这一部分,我的意图与上一部分相同,但是这次我想搜索ComputerName,发现它也会打印ComputerID。

现在我面临的问题是,找不到我输入的输入。

void SearchNAME(int count,struct PCSpec Computer[100])
{
int j;
char NAME;
printf("Warning CASE Senstitive!\n");
printf("Please Enter Computer Name ");
scanf("%s",&NAME);
for(j=0;j<count;j++)
{
if(Computer[j].ComputerName == "NAME")
{
printf("%s is found at %d",NAME,j);
//PrintSelective(Computer);
break;
}
}
if( j == count )
printf("Unable to locate ComputerID\n");
}

其余部分几乎是声明

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");
}

对我之前的帖子感到抱歉,我的问题不清楚,因为我还是 C 新手,感谢所有对我的编码的反馈!另一件事是,你们使用哪个编译器?或 C 标准,以便其他人在编译我编写的代码时不会出现任何问题。

最佳答案

三件事:

  1. 使用scanf("%s",&NAME),您可以将字符串读入单个字符。非空字符串始终需要至少两个字符:至少一个用于字符串内容加上一个字符串终止符。 49 个字符的字符串需要 50 个 char数组

  2. "NAME" 是一个文字字符串,而不是变量 NAME

  3. 使用 Computer[j].ComputerName == "NAME" 您正在比较两个指针。使用 Computer[j].ComputerName == NAME 您将比较指针与单个 char 值。如果将 NAME 更改为数组,那么您将再次比较两个指针。要比较字符串,您需要使用 strcmp .

关于c - 如何在结构内搜索并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45052763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com