【Uva 1586】 Molar mass

来源:互联网 发布:新手做淘宝客服怎么干 编辑:程序博客网 时间:2024/05/10 15:04

Description

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements.
这里写图片描述
When an organic compound is given as a molecular formula, Dr. CHON wants to find its molar mass. A molecular formula, such as C 3 H 4 O 3   , 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 element, 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’.

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

For example, the molar mass of a molecular formula C 6 H 5 OH  is 94.108 g/mol which is computed by 6 × (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

Input

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

Output

Your program is to write to standard output. Print exactly one line for each test case. The line should contain 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

解题思路

【organic compound】有机化合物
【molecules】分子
【molar mass】摩尔质量
【formula】式子,准则
【constituent】构成的
【subscript】下标

给定一个只含C、H、O、N的化学式,求其相对分子质量。
读入字符串后,逐个扫描累加,由于字符中只含有字母和数字,我们可以借助ctype.h中的函数(isalpha\isdigit)帮助识别。

参考代码

#include <stdio.h>#include <string.h>#include <ctype.h>#define LEN strlen(str)const int MAX = 100;int main(){   int n,i;    char str[MAX];    double SAW[128];    SAW['C'] = 12.01,SAW['H'] = 1.008;    SAW['O'] = 16.00,SAW['N'] = 14.01;//保存相对原子质量    scanf("%d",&n);    while (n--){        scanf("%s",str);        double ans = 0;        for (i = 0;i < LEN;i++){            if (isalpha(str[i]))//当前为字母                ans += SAW[str[i]];//加上对应的质量            else{//若为数字,注意到(2 <= n <= 99)                if (isdigit(str[i+1])){//则还需判断下一位是否也为数字                    ans += SAW[str[i-1]]*(10*(str[i]-'0')+str[i+1]-'0'-1);//注意要-1,因为上一位已经累加过一次了                    i++;//跳过对下一位的扫描                }                   else                    ans += SAW[str[i-1]]*(str[i]-'0'-1);            }        }        printf("%.3lf\n",ans);    }    return 0;}
0 0
原创粉丝点击