chaper3_exerise_Uva1568_Molar_Mass_分子量

 1 #include<iostream>
 2 #include<iomanip> 
 3 #include<string>
 4 #include<cctype>
 5 using namespace std;
 6 const int maxn = 1010;
 7 
 8 void get_num(int &num,string c,int j)
 9 {
10     while (isdigit(c[j]) && c[j])
11     {
12         num = num*10 + (c[j]-'0');
13         j++;
14     }
15 }
16 
17 int main(void)
18 {
19     string c;
20     int T;
21     cin >> T;
22     for (int i = 0; i < T; i++)
23      {
24          double count = 0;
25          cin >> c;
26         for (int j = 0; j < c.size(); j++)
27         {
28             if (isalpha(c[j]))
29             {
30                 if (isdigit(c[j+1]))
31                 {
32                     int num = 0;
33                     get_num(num,c,j+1); 
34                     switch(c[j])
35                     {
36                         case 'C' : count += 12.01*num; break;
37                         case 'H' : count += 1.008*num; break;
38                         case 'O' : count += 16.00*num; break;
39                         case 'N' : count += 14.01*num; break;
40                     }
41                 }
42                 else {
43                         switch(c[j])
44                         {
45                             case 'C' : count += 12.01; break;
46                             case 'H' : count += 1.008; break;
47                             case 'O' : count += 16.00; break;
48                             case 'N' : count += 14.01; break;
49                         }
50                 }    
51             }
52         }
53         cout << fixed << setprecision(3) << count << endl;
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/douzujun/p/5584856.html