-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmolar_mass.cpp
More file actions
59 lines (51 loc) · 1.26 KB
/
molar_mass.cpp
File metadata and controls
59 lines (51 loc) · 1.26 KB
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
double getMolarMass( string formula ){
double ret = 0;
stringstream formula_stream(formula);
char c;
while( formula_stream >> c ){
int qty = 1;
double density;
switch( c ){
case 'C':
density = 12.01;
break;
case 'H':
density = 1.008;
break;
case 'O':
density = 16.00;
break;
case 'N':
density = 14.01;
break;
}
int peek = formula_stream.peek();
if( peek != 'C' && peek != 'H' && peek != 'O' && peek != 'N' )
formula_stream>>qty;
ret += (double)qty * density;
}
return ret;
}
int main(){
#ifndef ONLINE_JUDGE
ifstream cin("entrada.txt");
ofstream cout("saida.txt");
#endif
string line;
stringstream line_stream;
getline(cin,line);
line_stream = stringstream(line);
int T;
line_stream>>T;
cout.precision(3);
for( auto t = 0 ; t < T ; t++ ){
getline(cin,line);
cout << fixed << getMolarMass(line) << endl;
}
}