简体   繁体   English

将结构中的数据存储到另一个结构中

[英]Store data from a struct on another struct

Im doing something like a supermarket simulator using C. I have a struct that keeps data from hundreds of fictional clients like so: 我使用C做类似超级市场模拟器的操作。我有一个结构可以保留来自数百个虚构客户的数据,如下所示:

typedef struct clients {
int ID;
char Name[50];
}Clients;

I imported a .txt file containing these clients. 我导入了包含这些客户端的.txt文件。 It basically looks like this: 基本上看起来像这样:

1234 Peter Parker
4724 Barack Obama
3851 John Wick
9428 Donald Trump
...

So now I have to simulate a supermarket, i have a timer that updates itself, picking random clients from the struct and put them inside the supermarket, i need to keep track how many of them and which of them are inside the supermarket. 因此,现在我必须模拟一个超级市场,我有一个可以自我更新的计时器,从结构中挑选随机的客户并将其放入超级市场,我需要跟踪其中有多少个客户以及其中哪些在超级市场中。 The thing is, i don't know how to like put the clients inside the market and store the data of those clients. 问题是,我不知道如何喜欢将客户放入市场并存储这些客户的数据。 For example, imagine client A and B enter the supermarket, how do I store those specific clients in the market, so that like i could search which of them are inside the market? 例如,假设客户A和客户B进入超市,我该如何在市场中存储这些特定客户,以便像我一样搜索其中哪些在市场内? Should I make a struct containing data from the struct Clients? 我是否应该构建一个包含来自结构客户端的数据的结构? I have no idea. 我不知道。 Glad if you could help me. 很高兴能帮到我。

You can create an array of your clients struct. 您可以创建一个客户结构体数组。

Here are some resources I found. 这是我找到的一些资源。 http://www.asic-world.com/scripting/structs_c.html http://www.c4learn.com/c-programming/c-array-structure/ http://www.asic-world.com/scripting/structs_c.html http://www.c4learn.com/c-programming/c-array-structure/

First thing that comes to mind is to use some kind of directed (or bidirected) list containing references to your Client structure. 首先想到的是使用某种定向(或双向)列表,其中包含对您的Client结构的引用。 For instance, 例如,

typedef struct {
  Client *client;
  Visitor *next;
  Visitor *prev;
} Visitor;

may be one of the nodes of that list. 可能是该列表的节点之一。 In the case of head or tail of the list, you can specify NULL values to distinguish different corner cases, but that depends on realization. 对于列表的开头或结尾,您可以指定NULL值以区分不同的极端情况,但这取决于实现。

The list itself: 列表本身:

typedef struct {
    Visitor *head;
    Visitor *tail;
} VisitorList;

The drawback of such structure is that getting number of visitors has linear complexity which may be not that you want. 这种结构的缺点是,获取访问者的数量具有线性复杂性,这可能不是您想要的。

The answer is that there are many ways to do this, with pros and cons to each. 答案是,这样做的方法很多,各有各的利弊。 If you care about performance and your data gets larger than hundreds and into the upper thousands then you probably start worrying about what type of data structure you're using, which then becomes a question about data structures and how you'll be searching for the names. 如果您关心性能,并且数据越来越多,甚至成千上万,那么您可能会开始担心所使用的数据结构类型,这将成为有关数据结构以及如何搜索数据结构的问题。名称。 If you're not so worried about performance, you can go for an array implementation where you create an array of Clients like so: which declares an array of 200 clients that you can assign values to. 如果您不太担心性能,则可以选择一个数组实现,在其中创建一个Clients数组,如下所示:它声明一个200个客户的数组,您可以为其分配值。

Clients array[200];

The down side of this is if you get more Clients than you've allocated memory for, you'll have to either malloc more memory to fit it. 这样做的不好的一面是,如果你得到更多的Clients比你分配的内存,你就必须要么malloc更多的内存,以适应它。

Alternatively you can go with the linked list implementation and create some sort of struct that represents a node with corresponding functions to manipulate it. 另外,您可以使用链表实现并创建某种struct ,该struct表示具有相应功能的节点来对其进行操作。

typedef struct {
  Client *client;
  node *next;
  node *prev;
} node;

But this would require writing more code to add and remove nodes at minimum. 但这将需要编写更多代码以最少添加和删除节点。

So, its really question of what YOU want. 因此,这确实是您想要的问题。

EDIT: If you just want to add a way of knowing if some client is in the super market why not create an enum and add it into the client struct ? 编辑:如果您只想添加一种方法来了解某个客户是否在超市中,为什么不创建一个enum并将其添加到client struct呢? I prefer this over bool because then if you want to put someone in another place in the future, then you have the option to just add it to the enum . 我比较喜欢bool,因为如果将来您想将某人放在另一个地方,那么您可以选择将其添加到enum

enum place {
    supermarket,
    none
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM