UVa1586-Molar Mass(分子量)

Anorganic compoundis any member of a large class of chem-ical compounds whose molecules contain carbon. Themolarmassof an organic compound is the mass of one mole of theorganic compound. The molar mass of an organic compoundcan be computed from the standard atomic weights of theelements.

When an organic compound is given as amolecular for-
mula
, Dr. CHON wants to find its molar mass. A molecular
formula, such as
C3H4O3, identifies each constituent element by
its chemical symbol and indicates the number of atoms of each
element found in each discrete molecule of that compound. If
a molecule contains more than one atom of a particular ele-
ment, this quantity is indicated using a subscript after the chemical symbol.

In this problem, we assume that the molecular formula is represented by only four elements, ‘C’(Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’ (Nitrogen) without parentheses.

The following table shows that the standard atomic weights for ‘C’, ‘H’, ‘O’, and ‘N’.

For example, the molar mass of a molecular formulaC6H5OHis 94.108 g/mol which is computed by6×(12.01 g/mol) + 6×(1.008 g/mol) + 1×(16.00 g/mol).

Given a molecular formula, write a program to compute the molar mass of the formula.

Atomic Name

Carbon

Hydrogen

Oxygen

Nitrogen

Standard Atomic Weight

12.01 g/mol

1.008 g/mol

16.00 g/mol

14.01 g/mol


Input

Your program is to read from standard input. The input consists ofTtest cases. The number of testcasesTis given in the first line of the input. Each test case is given in a single line, which containsa molecular formula as a string. The chemical symbol is given by a capital letter and the length ofthe string is greater than 0 and less than 80. The quantity numbernwhich is represented after thechemical symbol would be omitted when the number is 1 (2n99).

Output

Your program is to write to standard output. Print exactly one line for each test case. The line shouldcontain the molar mass of the given molecular formula.

Sample Input

4
C
C6H5OH
NH2CH2COOH
C12H22O11

Sample Output

12.010
94.108
75.070
342.296


题目大意:给出一个化学分子式,这些化学分子式都属于简单的分子式,都没有括号,并且该化学分子式是由C、H、O、N四种元素组成(注意,这些分子式字母都是大写的)。给出四种原子的原子质量,根据这些原子质量求出分子式的分子量。

#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;

#define Carbon 12.01
#define Hydrogen 1.008
#define Oxygen 16.00
#define Nitrogen 14.01

//字符串转数字
int str2num(string s)
{
    int num;
    stringstream ss(s);
    ss >> num;
    return num;
}
int main(int argc, const char * argv[]) {
    int T;
    cin >> T;
    string s;
    while (T--) {
        cin >> s;
        double result = 0; //保存最终结果
        for (int i = 0; i < s.length(); i++) { //遍历化学式(字符串)
            char c = s[i];
            if (s[i] < 65 && s[i] > 90) { //如果是数字,直接结束本次循环跳到下一次循环,只有是大写字符的时候才继续执行代码
                continue;
            }
            string s1;   //定义字符串变量s1,保存每个大写字符后面的数字
            int sum;
            for (int j = i + 1; j < s.length(); j++) { //从大写字符的下一个字符开始搜索
                if (s[j] >= 65 && s[j] <= 90) { //遇到下一个字符,跳出循环
                    i = j - 1;
                    break;
                }else { //只要是数字,就添加到s1中
                    s1 = s1 + s[j];
                    i = j - 1;
                }
            }
            if (s1.empty()) { //如果s1为空,说明该大写字符下一个字符仍然是大写字符,该字符的数量就是1了
                sum = str2num("1"); //字符转换成数字
            } else {
                sum = str2num(s1);  //字符转换成数字
            }
            //计算分子量
            if (c == 'C') {
                result =result + Carbon * sum;
            } else if (c == 'H') {
                result = result + Hydrogen * sum;
            } else if (c == 'N') {
                result = result + Nitrogen *sum;
            } else if(c == 'O'){
                result = result + Oxygen *sum;
            }
        }
        //格式化输出结果
        printf("%.3lf\n", result);
    }
    return 0;
}


发布了78 篇原创文章 · 获赞 12 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章