UVa 1586/uvalive 3900 Molar Mass (string)

Source: Internet
Author: User

Molar Mass
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

An organic compound are any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound are the mass of one mole of the organic compound. The molar mass of an organic compound can is computed from the standard atomic weights of the elements.

Organic compound is given as a molecular formula, Dr. Chon wants to find its molar mass. A molecular formula, such as C3H4O3, identifies each constituent element by its CH Emical 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 was indicated using a subscript after the Chemical symbol.

In this problem, we assume the molecular formula are represented by only four elements, 'C' (Carbon), 'h< /c1> ' (hydrogen), 'O' (Oxygen), and 'N' (nitrogen) without parentheses.

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

/tbody>
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 C6H5OH are 94.108 g/mol which is comp Uted by 6 x (12.01 g/mol) + 6 x (1.008 g/mol) + 1 x (16.00 g/mol).

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

Input

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

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
Approximate test instructions: The molecular formula (without parentheses) of a substance is given to calculate the molecular weight. The formula in this problem only contains 4 kinds of atoms, respectively, C, O, H, N, the atomic weight is 12.01, 16.00, 1.008, 14.01 (unit: g/mol).
1#include <cstdio>2#include <cstring>3#include <cctype>4 DoubleChCharmol)5 {6     Switch(MOL)7     {8          Case 'C':return 12.01;9          Case 'H':return 1.008;Ten          Case 'O':return 16.00; One          Case 'N':return 14.01; A     } -     return 0.0; - } the intMain () - { -     intT; -     Charstr[ -]; +scanf"%d\n",&T); -      while(t--) +     { A gets (str); at         DoubleCount=0.0; -         intlen=strlen (str); -         intnpos=0; -          while(npos<len) -         { -             Doublenum=CH (str[npos]); in             Doublesum=num; -             if(npos+1<len && IsDigit (str[npos+1])) toSum *= str[++npos]-'0'; +             if(npos+1<len && IsDigit (str[npos+1])) -sum = (sum*Ten+ num* (str[++npos]-'0')); thenpos++; *count+=sum; $         }Panax Notoginsengprintf"%.3f\n", count); -     } the}

UVa 1586/uvalive 3900 Molar Mass (string)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.