我必须在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 };更多推荐
发布评论