我正在尝试在线活动并遇到问题。我有关于分子的数据,我正试图建立一个分子量计算器。数据的格式如下:
{1, 1.0079, "Hydrogen", "H"},
{2, 4.0026, "Helium", "He"},
{3, 6.941, "Lithium", "Li"}
它具有原子序数,原子量,名称和缩写。
我使用了一个结构:
struct element {
int atomicNumber;
double atomicWeight;
char elementName[25];
char abbriv[5];
};
现在我需要使用全局数组来实际存储信息。我很困惑,因为我为每个元素(int,double,char)提供了三种不同的数据类型。我试过做一些研究,但找不到类似的问题。是否可以将此信息放入数组中?
另外,我只复制了上面的3个元素描述,实际上总共有109个,所以我对如何实际存储大量信息有了第二个想法。
完整清单:
{1, 1.0079, "Hydrogen", "H"},
{2, 4.0026, "Helium", "He"},
{3, 6.941, "Lithium", "Li"},
{4, 9.0122, "Beryllium", "Be"},
{5, 10.811, "Boron", "B"},
{6, 12.0107, "Carbon", "C"},
{7, 14.0067, "Nitrogen", "N"},
{8, 15.9994, "Oxygen", "O"},
{9, 18.9984, "Fluorine", "F"},
{10, 20.1797, "Neon", "Ne"},
{11, 22.9897, "Sodium", "Na"},
{12, 24.305, "Magnesium", "Mg"},
{13, 26.9815, "Aluminum", "Al"},
{14, 28.0855, "Silicon", "Si"},
{15, 30.9738, "Phosphorus", "P"},
{16, 32.065, "Sulfur", "S"},
{17, 35.453, "Chlorine", "Cl"},
{18, 39.948, "Argon", "Ar"},
{19, 39.0983, "Potassium", "K"},
{20, 40.078, "Calcium", "Ca"},
{21, 44.9559, "Scandium", "Sc"},
{22, 47.867, "Titanium", "Ti"},
{23, 50.9415, "Vanadium", "V"},
{24, 51.9961, "Chromium", "Cr"},
{25, 54.938, "Manganese", "Mn"},
{26, 55.845, "Iron", "Fe"},
{27, 58.9332, "Cobalt", "Co"},
{28, 58.6934, "Nickel", "Ni"},
{29, 63.546, "Copper", "Cu"},
{30, 65.39, "Zinc", "Zn"},
{31, 69.723, "Gallium", "Ga"},
{32, 72.64, "Germanium", "Ge"},
{33, 74.9216, "Arsenic", "As"},
{34, 78.96, "Selenium", "Se"},
{35, 79.904, "Bromine", "Br"},
{36, 83.8, "Krypton", "Kr"},
{37, 85.4678, "Rubidium", "Rb"},
{38, 87.62, "Strontium", "Sr"},
{39, 88.9059, "Yttrium", "Y"},
{40, 91.224, "Zirconium", "Zr"},
{41, 92.9064, "Niobium", "Nb"},
{42, 95.94, "Molybdenum", "Mo"},
{43, 98, "Technetium", "Tc"},
{44, 101.07, "Ruthenium", "Ru"},
{45, 102.9055, "Rhodium", "Rh"},
{46, 106.42, "Palladium", "Pd"},
{47, 107.8682, "Silver", "Ag"},
{48, 112.411, "Cadmium", "Cd"},
{49, 114.818, "Indium", "In"},
{50, 118.71, "Tin", "Sn"},
{51, 121.76, "Antimony", "Sb"},
{52, 127.6, "Tellurium", "Te"},
{53, 126.9045, "Iodine", "I"},
{54, 131.293, "Xenon", "Xe"},
{55, 132.9055, "Cesium", "Cs"},
{56, 137.327, "Barium", "Ba"},
{57, 138.9055, "Lanthanum", "La"},
{58, 140.116, "Cerium", "Ce"},
{59, 140.9077, "Praseodymium", "Pr"},
{60, 144.24, "Neodymium", "Nd"},
{61, 145, "Promethium", "Pm"},
{62, 150.36, "Samarium", "Sm"},
{63, 151.964, "Europium", "Eu"},
{64, 157.25, "Gadolinium", "Gd"},
{65, 158.9253, "Terbium", "Tb"},
{66, 162.5, "Dysprosium", "Dy"},
{67, 164.9303, "Holmium", "Ho"},
{68, 167.259, "Erbium", "Er"},
{69, 168.9342, "Thulium", "Tm"},
{70, 173.04, "Ytterbium", "Yb"},
{71, 174.967, "Lutetium", "Lu"},
{72, 178.49, "Hafnium", "Hf"},
{73, 180.9479, "Tantalum", "Ta"},
{74, 183.84, "Tungsten", "W"},
{75, 186.207, "Rhenium", "Re"},
{76, 190.23, "Osmium", "Os"},
{77, 192.217, "Iridium", "Ir"},
{78, 195.078, "Platinum", "Pt"},
{79, 196.9665, "Gold", "Au"},
{80, 200.59, "Mercury", "Hg"},
{81, 204.3833, "Thallium", "Tl"},
{82, 207.2, "Lead", "Pb"},
{83, 208.9804, "Bismuth", "Bi"},
{84, 209, "Polonium", "Po"},
{85, 210, "Astatine", "At"},
{86, 222, "Radon", "Rn"},
{87, 223, "Francium", "Fr"},
{88, 226, "Radium", "Ra"},
{89, 227, "Actinium", "Ac"},
{90, 232.0381, "Thorium", "Th"},
{91, 231.0359, "Protactinium", "Pa"},
{92, 238.0289, "Uranium", "U"},
{93, 237, "Neptunium", "Np"},
{94, 244, "Plutonium", "Pu"},
{95, 243, "Americium", "Am"},
{96, 247, "Curium", "Cm"},
{97, 247, "Berkelium", "Bk"},
{98, 251, "Californium", "Cf"},
{99, 252, "Einsteinium", "Es"},
{100, 257, "Fermium", "Fm"},
{101, 258, "Mendelevium", "Md"},
{102, 259, "Nobelium", "No"},
{103, 262, "Lawrencium", "Lr"},
{104, 261, "Rutherfordium", "Rf"},
{105, 262, "Dubnium", "Db"},
{106, 266, "Seaborgium", "Sg"},
{107, 264, "Bohrium", "Bh"},
{108, 277, "Hassium", "Hs"},
{109, 268, "Meitnerium", "Mt"}
答案 0 :(得分:2)
是的,您走在正确的轨道上,现在您可以创建一个struct
个变量数组,并立即将所有这些变量分配给它并通过循环访问它。
这样的事情:
#include<stdio.h>
#define NUM_OF_ELEMENTS 109
struct element {
int atomicNumber;
double atomicWeight;
char elementName[25];
char abbriv[5];
};
struct element elements[NUM_OF_ELEMENTS]={
{1, 1.0079, "Hydrogen", "H"},
{2, 4.0026, "Helium", "He"},
{3, 6.941, "Lithium", "Li"},
{4, 9.0122, "Beryllium", "Be"},
{5, 10.811, "Boron", "B"},
{6, 12.0107, "Carbon", "C"},
{7, 14.0067, "Nitrogen", "N"},
{8, 15.9994, "Oxygen", "O"},
{9, 18.9984, "Fluorine", "F"},
{10, 20.1797, "Neon", "Ne"},
{11, 22.9897, "Sodium", "Na"},
{12, 24.305, "Magnesium", "Mg"},
{13, 26.9815, "Aluminum", "Al"},
{14, 28.0855, "Silicon", "Si"},
{15, 30.9738, "Phosphorus", "P"},
{16, 32.065, "Sulfur", "S"},
{17, 35.453, "Chlorine", "Cl"},
{18, 39.948, "Argon", "Ar"},
{19, 39.0983, "Potassium", "K"},
{20, 40.078, "Calcium", "Ca"},
{21, 44.9559, "Scandium", "Sc"},
{22, 47.867, "Titanium", "Ti"},
{23, 50.9415, "Vanadium", "V"},
{24, 51.9961, "Chromium", "Cr"},
{25, 54.938, "Manganese", "Mn"},
{26, 55.845, "Iron", "Fe"},
{27, 58.9332, "Cobalt", "Co"},
{28, 58.6934, "Nickel", "Ni"},
{29, 63.546, "Copper", "Cu"},
{30, 65.39, "Zinc", "Zn"},
{31, 69.723, "Gallium", "Ga"},
{32, 72.64, "Germanium", "Ge"},
{33, 74.9216, "Arsenic", "As"},
{34, 78.96, "Selenium", "Se"},
{35, 79.904, "Bromine", "Br"},
{36, 83.8, "Krypton", "Kr"},
{37, 85.4678, "Rubidium", "Rb"},
{38, 87.62, "Strontium", "Sr"},
{39, 88.9059, "Yttrium", "Y"},
{40, 91.224, "Zirconium", "Zr"},
{41, 92.9064, "Niobium", "Nb"},
{42, 95.94, "Molybdenum", "Mo"},
{43, 98, "Technetium", "Tc"},
{44, 101.07, "Ruthenium", "Ru"},
{45, 102.9055, "Rhodium", "Rh"},
{46, 106.42, "Palladium", "Pd"},
{47, 107.8682, "Silver", "Ag"},
{48, 112.411, "Cadmium", "Cd"},
{49, 114.818, "Indium", "In"},
{50, 118.71, "Tin", "Sn"},
{51, 121.76, "Antimony", "Sb"},
{52, 127.6, "Tellurium", "Te"},
{53, 126.9045, "Iodine", "I"},
{54, 131.293, "Xenon", "Xe"},
{55, 132.9055, "Cesium", "Cs"},
{56, 137.327, "Barium", "Ba"},
{57, 138.9055, "Lanthanum", "La"},
{58, 140.116, "Cerium", "Ce"},
{59, 140.9077, "Praseodymium", "Pr"},
{60, 144.24, "Neodymium", "Nd"},
{61, 145, "Promethium", "Pm"},
{62, 150.36, "Samarium", "Sm"},
{63, 151.964, "Europium", "Eu"},
{64, 157.25, "Gadolinium", "Gd"},
{65, 158.9253, "Terbium", "Tb"},
{66, 162.5, "Dysprosium", "Dy"},
{67, 164.9303, "Holmium", "Ho"},
{68, 167.259, "Erbium", "Er"},
{69, 168.9342, "Thulium", "Tm"},
{70, 173.04, "Ytterbium", "Yb"},
{71, 174.967, "Lutetium", "Lu"},
{72, 178.49, "Hafnium", "Hf"},
{73, 180.9479, "Tantalum", "Ta"},
{74, 183.84, "Tungsten", "W"},
{75, 186.207, "Rhenium", "Re"},
{76, 190.23, "Osmium", "Os"},
{77, 192.217, "Iridium", "Ir"},
{78, 195.078, "Platinum", "Pt"},
{79, 196.9665, "Gold", "Au"},
{80, 200.59, "Mercury", "Hg"},
{81, 204.3833, "Thallium", "Tl"},
{82, 207.2, "Lead", "Pb"},
{83, 208.9804, "Bismuth", "Bi"},
{84, 209, "Polonium", "Po"},
{85, 210, "Astatine", "At"},
{86, 222, "Radon", "Rn"},
{87, 223, "Francium", "Fr"},
{88, 226, "Radium", "Ra"},
{89, 227, "Actinium", "Ac"},
{90, 232.0381, "Thorium", "Th"},
{91, 231.0359, "Protactinium", "Pa"},
{92, 238.0289, "Uranium", "U"},
{93, 237, "Neptunium", "Np"},
{94, 244, "Plutonium", "Pu"},
{95, 243, "Americium", "Am"},
{96, 247, "Curium", "Cm"},
{97, 247, "Berkelium", "Bk"},
{98, 251, "Californium", "Cf"},
{99, 252, "Einsteinium", "Es"},
{100, 257, "Fermium", "Fm"},
{101, 258, "Mendelevium", "Md"},
{102, 259, "Nobelium", "No"},
{103, 262, "Lawrencium", "Lr"},
{104, 261, "Rutherfordium", "Rf"},
{105, 262, "Dubnium", "Db"},
{106, 266, "Seaborgium", "Sg"},
{107, 264, "Bohrium", "Bh"},
{108, 277, "Hassium", "Hs"},
{109, 268, "Meitnerium", "Mt"}
};
void main()
{
int i;
printf("%13s\t%13s\t%25s\t%s\n", "Atomic Number", "Atomic Weight", "Element Name", "Abbrevation");
for(i = 0; i < NUM_OF_ELEMENTS; i++)
printf("%13d\t%13.2lf\t%25s\t%5s\n", elements[i].atomicNumber,
elements[i].atomicWeight,
elements[i].elementName,
elements[i].abbriv);
}
这是输出:
$ gcc prgm.c
$ ./a.out
Atomic Number Atomic Weight Element Name Abbrevation
1 1.01 Hydrogen H
2 4.00 Helium He
3 6.94 Lithium Li
4 9.01 Beryllium Be
5 10.81 Boron B
6 12.01 Carbon C
7 14.01 Nitrogen N
8 16.00 Oxygen O
9 19.00 Fluorine F
10 20.18 Neon Ne
11 22.99 Sodium Na
12 24.30 Magnesium Mg
13 26.98 Aluminum Al
14 28.09 Silicon Si
15 30.97 Phosphorus P
16 32.06 Sulfur S
17 35.45 Chlorine Cl
18 39.95 Argon Ar
19 39.10 Potassium K
20 40.08 Calcium Ca
21 44.96 Scandium Sc
22 47.87 Titanium Ti
23 50.94 Vanadium V
24 52.00 Chromium Cr
25 54.94 Manganese Mn
26 55.84 Iron Fe
27 58.93 Cobalt Co
28 58.69 Nickel Ni
29 63.55 Copper Cu
30 65.39 Zinc Zn
31 69.72 Gallium Ga
32 72.64 Germanium Ge
33 74.92 Arsenic As
34 78.96 Selenium Se
35 79.90 Bromine Br
36 83.80 Krypton Kr
37 85.47 Rubidium Rb
38 87.62 Strontium Sr
39 88.91 Yttrium Y
40 91.22 Zirconium Zr
41 92.91 Niobium Nb
42 95.94 Molybdenum Mo
43 98.00 Technetium Tc
44 101.07 Ruthenium Ru
45 102.91 Rhodium Rh
46 106.42 Palladium Pd
47 107.87 Silver Ag
48 112.41 Cadmium Cd
49 114.82 Indium In
50 118.71 Tin Sn
51 121.76 Antimony Sb
52 127.60 Tellurium Te
53 126.90 Iodine I
54 131.29 Xenon Xe
55 132.91 Cesium Cs
56 137.33 Barium Ba
57 138.91 Lanthanum La
58 140.12 Cerium Ce
59 140.91 Praseodymium Pr
60 144.24 Neodymium Nd
61 145.00 Promethium Pm
62 150.36 Samarium Sm
63 151.96 Europium Eu
64 157.25 Gadolinium Gd
65 158.93 Terbium Tb
66 162.50 Dysprosium Dy
67 164.93 Holmium Ho
68 167.26 Erbium Er
69 168.93 Thulium Tm
70 173.04 Ytterbium Yb
71 174.97 Lutetium Lu
72 178.49 Hafnium Hf
73 180.95 Tantalum Ta
74 183.84 Tungsten W
75 186.21 Rhenium Re
76 190.23 Osmium Os
77 192.22 Iridium Ir
78 195.08 Platinum Pt
79 196.97 Gold Au
80 200.59 Mercury Hg
81 204.38 Thallium Tl
82 207.20 Lead Pb
83 208.98 Bismuth Bi
84 209.00 Polonium Po
85 210.00 Astatine At
86 222.00 Radon Rn
87 223.00 Francium Fr
88 226.00 Radium Ra
89 227.00 Actinium Ac
90 232.04 Thorium Th
91 231.04 Protactinium Pa
92 238.03 Uranium U
93 237.00 Neptunium Np
94 244.00 Plutonium Pu
95 243.00 Americium Am
96 247.00 Curium Cm
97 247.00 Berkelium Bk
98 251.00 Californium Cf
99 252.00 Einsteinium Es
100 257.00 Fermium Fm
101 258.00 Mendelevium Md
102 259.00 Nobelium No
103 262.00 Lawrencium Lr
104 261.00 Rutherfordium Rf
105 262.00 Dubnium Db
106 266.00 Seaborgium Sg
107 264.00 Bohrium Bh
108 277.00 Hassium Hs
109 268.00 Meitnerium Mt
$
答案 1 :(得分:-1)
你要做的是创建一个结构数组。
例如
struct elements myArray[108];
myArray[0].atomicNumber = 1;
myArray[0].atomicWeight = 1.0079;
myArray[0].elementName = "Hydrogen";
myArray[0].abbriv = "H";
显然,您的表中有很多元素,因此您可以通过循环轻松自动执行此过程。