选择其成员的随机结构(Choosing a random struct by its members)

编程入门 行业动态 更新时间:2024-10-19 12:33:01
选择其成员的随机结构(Choosing a random struct by its members)

我必须在C中为周期表建模一个教育软件项目(它基本上是一个测验)。

我已经创建了一个结构并声明了所有元素。 现在我必须编写一个随机选择一个元素的函数。

这是我的结构:

typedef struct{ char name[15]; char shortname[3]; int group; int period; }element; element hydrogen={"hydrogen", "H", 1, 1}, helium={"Helium", "He", ...

我已经尝试将它们组合在一个数组中,然后生成一个随机索引:

element elements[118]; elements[1] = {"hydrogen", "H", 1, 1};

但那只是给了我错误:(

有任何想法吗?

I have to model the periodic table in C, for an educational software project (It's basically a quiz).

I already created a struct and declared all the elements. Now I have to write a function that chooses one element randomly.

This is my struct:

typedef struct{ char name[15]; char shortname[3]; int group; int period; }element; element hydrogen={"hydrogen", "H", 1, 1}, helium={"Helium", "He", ...

I already tried to combine them in an array, to then generate a random index:

element elements[118]; elements[1] = {"hydrogen", "H", 1, 1};

but that just gave me errors :(

Any ideas?

最满意答案

这种方法应该有效。 设置一个结构数组,然后取index = rand()%118。你设置结构列表的语法就是问题所在。 尝试

struct element elements[118] = { {"hydrogen", "H", 1, 1}, {"helium", "He", 2, 4}, ... etc };

The approach should work. Set up an array of structs, then take index = rand() % 118. Your syntax for seting up the list of structures is the problem. Try

struct element elements[118] = { {"hydrogen", "H", 1, 1}, {"helium", "He", 2, 4}, ... etc };

更多推荐

本文发布于:2023-07-26 00:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   结构   Choosing   members   struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!