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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章