-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPeriodicFile.cpp
164 lines (160 loc) · 5.34 KB
/
PeriodicFile.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<string.h>
#include<fstream>
#include<cstdlib>
using namespace std;
/**************************************************/
// CLASS DEFINATION FOR THE ELEMENT
/*************************************************/
class Element{
public: int atno;
int group, period, K, L, M, N, O, P, Q;
float mass, density;
char name[20], symbol[4];
// CONSTRUCTOR OF THE CLASS //
/*Element()
{ atno=period=1;
K=0,L=0,M=0,N=0,P=0,Q=0;
group=0;
mass=0;
strcpy(name,"Hydrogen");
strcpy(symbol,"H");
} */
void def_atno()
{ static int x=1;
atno=x;
++x;
}
// FUNCTION RETURNS ATOMIC NUMBER
int get_atno()
{
return atno;
}
// FUNCTION THAT DISPLAYS PROPERTIES OF ELEMENTS
void Display()
{ cout<<"\nElement name:"<<name;
cout<<"\nSymbol:"<<symbol;
cout<<"\nAtomic Number:"<<atno;
cout<<"\nGroup:"<<group;
cout<<"\nPeriod:"<<period;
cout<<"\nMass:"<<mass;
cout<<"\nElectrons in shell";
cout<<"\n K L M N O P Q\n";
cout<<K<<" "<<L<<" "<<M<<" "<<N<<" "<<O<<" "<<P<<" "<<Q;
}
void getele()
{ cout<<"\nEnter Element's Name:";cin>>name;
cout<<"\nEnter Atomic Number:";cin>>atno;
cout<<"\nEnter Element's Symbol:";cin>>symbol;
cout<<"\nEnter Element's Group:";cin>>group;
cout<<"\nEnter Element's Period:";cin>>period;
cout<<"\nEnter Element's Mass:";cin>>mass;
cout<<"\nElectrons in shell";
cout<<"K:";cin>>K;
cout<<"L:";cin>>L;
cout<<"M:";cin>>M;
cout<<"N:";cin>>N;
cout<<"O:";cin>>O;
cout<<"P:";cin>>P;
cout<<"Q:";cin>>Q;
}
};
/**************************************************************/
// FUNCTION SORT USED TO SHOW A THOSE ELEMENTS IN A PART
// YOU HAVE CHOOSEN A RANGE OF IT
/**************************************************************/
void Sort(int a,int b,Element A[118])
{ for(int i=a-a;i<=b-a;++i)
{ for(int j=a-a;j<b-a-i;++j)
{ if (strcmpi(A[a+j].name,A[a+j+1].name)>0)
{ char temp[20];
strcpy(temp, A[j+a].name);
strcpy(A[j+a].name, A[j+a+1].name);
strcpy(A[j+a+1].name,temp);
}
}
}
cout<<"\n Selected Range of Elemets Sorted Alphabetically:\n" ;
for(int k=a;k<=b;++k)
{
cout<<A[k].name<<endl;
}
}
/**********************************************************************/
// FUNCTION THAT TAKE ELEMENTS' PROPERTIES FROM THE FILE AND STORE
// THEM IN THE ARRAY OF OBJECT A[ ] OF CLASS ELEMENTS
/**********************************************************************/
void definition(Element A[118])
{ ifstream table("Periodic.txt");
table.seekg(0);
for(int i=0;i<118;i++)
{
table.read((char*)&A[i], sizeof(A[i]));
}
table.close();
}
/**********************************************/
// MAIN FUNCTION CONTROL WHOLE PROGRAM
/**********************************************/
int main()
{ Element E[120];
char ch='i'; int i=0;
while(ch!='5')
{
definition(E);
cout<<"\n\t \t 1.Search element by Name/ Symbol";
cout<<"\n\t \t 2.Search element by Atomic Number";
cout<<"\n\t \t 3.Sort Alphabetically";
cout<<"\n\t \t 4.Display Periodic Table";
cout<<"\n\t \t 5.EXIT";
cout<<"\n Enter your choice(1-5):"; cin>>ch;
switch(ch)
{ case '1': { char Sname[20];
cout<<"\nEnter element Name / Symbol:"; cin>>Sname;
for(i=0; i<118; i++)
{
if(_strcmpi(Sname,E[i].name)==0 || _strcmpi(Sname,E[i].symbol)==0)
{
E[i].Display();
}
}
}
break;
case '2': { int Ano;
cout<<"\nEnter atomic number:"; cin>>Ano;
for(i=0;i<118;i++)
{ if(Ano==E[i].get_atno())
{
E[i].Display();
}
}
}
break;
case '3': int min,max;
do{
cout<<"\nEnter Range of Atomic Numbers:";
cin>>min>>max;
}while(min<1||max>118);
Sort(min-1,max-1,E);
break;
case '4': cout<<"\nSTILL WORK GOING ON";
break;
case '5': exit(0);
break;
case 'i': { i=0; char ans='y';
ofstream table;
table.open("Periodic.txt",ios::out|ios::app);
while(ans=='y')
{ E[i].getele();
table.write((char*)&E[i], sizeof(E[i]));
cout<<"\nWant to Enter More Elements?(Y/N)..";
cin>>ans; i++;
}
table.close();
}
break;
default: { cout<<"\nINVALID CHOICE";}
}
}
return 0;
}