1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Element {
string name;
float weight;
};
string data =
"H 1.008, He 4.003, Li 6.941, Be 9.012, B 10.811, C 12.011, N 14.007, "
"O 15.999, F 18.998, Ne 20.180, Na 22.990, Mg 24.305, Al 26.982, Si 28.086, "
"P 30.974, S 32.065, Cl 35.453, Ar 39.948, K 39.098, Ca 40.078, Sc 44.956, "
"Ti 47.867, V 50.942, Cr 51.996, Mn 54.938, Fe 55.845, Co 58.933, Ni 58.693, "
"Cu 63.546, Zn 65.39, Ga 69.723, Ge 72.64, As 74.922, Se 78.96, Br 79.904, "
"Kr 83.80, Rb 85.468, Sr 87.62, Y 88.906, Zr 91.224, Nb 92.906, Mo 95.94, "
"Ru 101.07, Rh 102.91, Pd 106.42, Ag 107.87, Cd 112.41, In 114.82, Sn 118.71, "
"Sb 121.76, Te 127.60, I 126.90, Xe 131.29, Cs 132.91, Ba 137.33, La 138.91, "
"Ce 140.12, Pr 140.91, Nd 144.24, Sm 150.36, Eu 151.96, Gd 157.25, Tb 158.93, "
"Dy 162.50, Ho 164.93, Er 167.26, Tm 168.93, Yb 173.04, Lu 174.97, Hf 178.49, "
"Ta 180.95, W 183.84, Re 186.21, Os 190.23, Ir 192.22, Pt 195.08, Au 196.97, "
"Hg 200.59, Tl 204.38, Pb 207.2, Bi 208.98, Po 209, At 210, Rn 222, Fr 223, "
"Ra 226, Ac 227, Th 232.04, Pa 231.04, U 238.03";
int main()
{
Element elements[200];
stringstream weights(data);
string item;
int count = 0;
while (getline(weights,item,',')) // comma is the delimiter
{
stringstream sitem(item);
sitem >> elements[count].name >> elements[count].weight;
count++;
}
for (int i=0; i<count; i++)
cout << i+1 << " " << elements[i].name << " " << elements[i].weight << endl;
string FirstElement;
cout << "Please enter the name of the element.\n";
cin >> FirstElement;
int find;
for (find=0; find<count; find++)
if (elements[find].name == FirstElement)
break;
if (find == count)
cout << "Element " << FirstElement << " was not found" << endl;
else
cout << "Element " << FirstElement
<< " weight = " << elements[find].weight << endl;
return 0;
}
|