C:我怎么会写一个搜索功能来寻找一个结构阵列匹配,并返回(打印),整个结构是相匹配? [英] C: how would I write a search function to look for a match in a struct array, and return (print) the entire struct it matched?

查看:109
本文介绍了C:我怎么会写一个搜索功能来寻找一个结构阵列匹配,并返回(打印),整个结构是相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#定义记录了10

下面的功能是什么,我要求的帮助。

 静态字符searchforRecordbystate(字符输入[3])
{

有关/而/ if循环

搜索结构数组成员

如果发现匹配

收益率(打印),其中发现匹配的整个结构

 返回0;
}

主要功能 - 有史以来第一次使用指针,(X code未与其抱怨设置为尽可能严格),但大家都欢迎的抱怨,尤其是当我提出一个巨大的监督。

  INT的main(){
    typedef结构{
        字符*的firstName [记录]
        字符* lastName的[记录]
        字符*街头[记录]
        字符*城市[记录]
        字符*状态[记录]
        为int *拉链[记录]
        字符*手机[记录]
        为int *帐户ID [记录]
    }客户;    的typedef诠释记录;
    记录I = 0;

有关数据输入阵列循环

 客户custArray [记录]
    的printf(=============================================== ================= \\ n);
    对于(i = 0; I<记录; ++ I)
    {
        的printf(请输入用户%d个\\ n个数据,我+ 1);
        的printf(请输入姓名,姓氏,手机\\ n);
        scanf函数(%s%s%S,* custArray [I] .firstName,* custArray [I] .lastName,* custArray [I] .phone);
        的printf(请输入地址(街道市州邮政编码));
        scanf函数(%s%s%S * C%D,* custArray [I] .street,* custArray [I]。城市,* custArray [I] .STATE,* custArray [I] .zip文件);
        打破;
    }
    炭输入[3];
    的printf(输入状态搜索客户一个客户记录:\\ n);
    scanf函数(%S,输入);
    sea​​rchforRecordbystate(输入);}

没有错误检查必要的,只是想爬进此刻学习c。而且不会有在成员国重复的数据。希望这是更容易些。


解决方案

  

我怎么会写一个搜索功能来查找匹配的结构
  阵列和回报(printf的)整个结构是匹配的?



  1. 声明结构数据类型的功能,所以它的可见整个模块之外。

  2. 创建一个功能,即能pretty打印一个结构:

    无效CustomerPrint(常量客户* toPrint){
        ...
    }


  3. 创建通过数组迭代进行比较给定参数搜索功能:

    客户* CustomerFind(为const char *名称){
    ...
    }


  4. 这两个功能模块通过调用连接 CustomerFind ,并在情况下,结果是不是 NULL 调用 CustomerPrint 功能。


当然的接口是唯一建议,可能有改变。如果你有关于该提案的细节发表评论任何问题,如果你愿意,我会解释的很详细。

其他的想法

虽然重读我的职务,我意识到,我的一些决定,我在上面提出的提案需要一个交代呢:

CustomerPrint 拍摄的指针是'常量?因为该功能是不会改变结构的任何领域。因此,我们告诉我们不会改变任何东西的编译器。

CustomerFind 预计对所有搜索领域的争论。 (所以你鼓励延长签字)我会建议由指针全部采取比较值,然后让调用这些指针是 NULL 这是不相关的搜索。 (例如,如果你有名称城市你可以离开城市 NULL以仅搜索名称第一次出现。

函数本身通过记录的阵列上运行,相比不在 NULL 字段。如果找到,则返回指向该元素(收益及(myRecords [N]); )。如果函数涉及到数组的结尾,它将返回 NULL 来表示没有记录相匹配。

还有,如果你想拥有你可以引入一个概念搜索 - 搜索下一个功能。让我知道如果你是在一个概念intrested为过。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RECORDS 10  

The function below is what I am asking for help with.

static char searchforRecordbystate(char input[3])
{   

for / while /if loop

search struct array members

if a match is found

return (print) the entire struct where a match was found

    return 0;
}   

Main function - first time ever using pointers, (xcode is not complaining with it set to be as strict as possible) but all of you are welcome to complain, especially if I am making a huge oversight.

int main() {
    typedef struct {
        char *firstName[RECORDS];
        char *lastName[RECORDS];
        char *street[RECORDS];
        char *city[RECORDS];
        char *state[RECORDS];
        int *zip[RECORDS];
        char *phone[RECORDS];
        int *accountId[RECORDS];
    } Customer ;    

    typedef int records;
    records i = 0;  

array loop for data entry

    Customer custArray[RECORDS];
    printf("================================================================\n");
    for(i = 0; i < RECORDS; ++i)
    {
        printf("Enter data for customer %d\n", i + 1);
        printf("Enter firstname, last name, phone\n");
        scanf("%s %s %s", *custArray[i].firstName, *custArray[i].lastName, *custArray[i].phone);
        printf("Enter Address (Street City State ZIP)");
        scanf("%s %s %s*c %d", *custArray[i].street, *custArray[i].city, *custArray[i].state, *custArray[i].zip);
        break;
    }
    char input[3];
    printf("Enter in state to search for customer a customer record:\n");
    scanf("%s", input); 


    searchforRecordbystate(input);  

}   

No error checking necessary, just trying to crawl into learning c at the moment. And there will not be duplicate data in the state member. Hope that makes this easier.

解决方案

how would I write a search function to look for a match in a struct array and return (printf) the entire struct it matched?

  1. Declare the struct datatype outside of the function so it's "visible" to the whole module.
  2. Create a function that is able to pretty-print a struct:

    void CustomerPrint(const Customer *toPrint) { ... }

  3. Create a search function that iterates through the array comparing given arguments:

    Customer *CustomerFind(const char *name) { ... }

  4. Connect the two function blocks by calling CustomerFind and in case the result is not NULL call the CustomerPrint function.

Of course the interfaces are only proposal and are subject to be changed. If you've got any questions regarding the details of the proposal leave a comment, I'll explain it in great detail if you like.

Additional thoughts

While rereading my post I realized that some of my decisions I've made in above proposal need an explaination anyway:

In CustomerPrint the pointer taken is `const? because this function is not going to modify any field of the struct. Therefore we tell the compiler that we are not going to change anything.

CustomerFind is expected to have arguments for all searchable fields. (So you are encouraged to extend the signature) I'd propose to take all the "compare" values by pointer and let the caller those pointers be NULL which are not relevant for the search. (e.g. if you have name and city you can leave city NULL in order to only search for the first occurence of name.

The function itself runs through the array of records and compares the fields that are not NULL. In case it finds one, it returns the pointer to that element (return &(myRecords[n]);). If the function comes to the end of the array, it will return NULL to indicate no record matched.

There is also a concept you can introduce if you want to have "search - search next" capabilities. Let me know if you are intrested in a concept for that too.

这篇关于C:我怎么会写一个搜索功能来寻找一个结构阵列匹配,并返回(打印),整个结构是相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆