【问题标题】:How to search within the structure and display it如何在结构中搜索并显示它
【发布时间】:2017-07-12 08:46:57
【问题描述】:
#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 标准,以便其他人在编译我编写的代码时不会有任何问题。

【问题讨论】:

  • Computer[j].ComputerName == "NAME" 不起作用。您需要 strcmp 来比较 C 中的字符串。

标签: c arrays search struct


【解决方案1】:

三件事:

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

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

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

【讨论】:

    【解决方案2】:

    您需要将i 传递给PrintSelective:

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

    PrintAll 也应该调用PrintSelective。

    或者,我不会打印PrintSelective 中的索引,在这种情况下,写为:

    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]);
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 2011-06-24
      • 2019-11-05
      • 2020-07-08
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多