Molar mass (UVA - 1586)

第二周题单

B - Molar mass (UVA - 1586)

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 th 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 C_3H_4O_3 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 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 NameCarbonHydrogenOxygenNitrogn
Standard Atomic Weight12.01 g/mol1.008 g/mol16.00 g/mol14.01 g/mol

For example, the molar mass of a molecular formula C 6 H 5 O H C_6H_5OH C6H5OH is 94.108 g/mol which is computed by 6 × ( 12.01 g / m o l ) + 6 × ( 1.008 g / m o l ) + 1 × ( 16.00 g / m o l ) 6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol) 6×(12.01g/mol)+6×(1.008g/mol)+1×(16.00g/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 ) 1 (2 ≤ n ≤ 99) 1(2n99).

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.

Simple1

Input

4
C
C6H5OH
NH2CH2COOH
C12H22O11

Output

12.010
94.108
75.070
342.296

题目大意

计算相对分子质量

题解过程

读到字母后,往后找,记录数字的长度,然后计算数字的大小,将这个数字乘以元素的相对分子质量加到总质量上,重复这个过程,要防止越界还有要处理没有数字的情况
虽然题目中说了数字的大小最大也只有两位数,但是本程序也预留了大于两位数的计算,而且今后如果要添加其他元素的种类,只需定义新元素的相对分子质量并在mass函数中更新新的元素即可
如果只是为了做题的话完全可以把两位数的情况直接写出来(看了代码就明白什么意思了),不需要这么麻烦,相似部分直接CV就好(毕竟就四种元素)

可维护的↓

代码部分(cpp)
#include <bits/stdc++.h>
using namespace std;
const double C = 12.01;
const double H = 1.008;
const double O = 16.00;
const double N = 14.01;
double mass(char c)
{
    switch (c)
    {
    case 'C':
        return C;
    case 'H':
        return H;
    case 'O':
        return O;
    case 'N':
        return N;
    }
    return 0;
}
bool isdigit(char c)
{
    if (c <= '9' && c >= '0')
        return 1;
    else
        return 0;
}
int count(string s, int l, int r)
{
    int i = r;
    int num = 0;
    while (i >= l)
    {
        num += (s[i] - 48) * pow(10, r - i);
        i--;
    }
    return num;
}
void solution()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        double total_mass = 0;
        for (int i = 0; i < s.length(); i++)
        {
            int num = 0;
            while (i + 1 < s.length() && isdigit(s[i + 1]))
            {
                i++;
                num++;
            }
            if (num == 0)
                total_mass += mass(s[i]);
            else
                total_mass += mass(s[i - num]) * count(s, i - num + 1, i);
        }
        cout << fixed << setprecision(3) << total_mass << endl;
    }
}
int main(int argc, char const *argv[])
{
    solution();
    return 0;
}

无脑写的↓

代码部分(cpp)
#include <iostream>
#include <stdio.h>
using namespace std;
const double C = 12.01;
const double H = 1.008;
const double O = 16.00;
const double N = 14.01;
void solution()
{
    int c = 0, h = 0, o = 0, n = 0;
    int t;
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        string s;
        cin >> s;
        int len = s.length();
        double M = 0;
        for (int j = 0; j < len; j++)
        {
            if (s[j] == 'C')
            {
                if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                {
                    j++;
                    if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                    {
                        j++;
                        M += ((s[j - 1] - 48) * 10 + (s[j] - 48)) * C;
                    }
                    else
                    {
                        M += (s[j] - 48) * C;
                    }
                }
                else
                {
                    M += C;
                }
            }
            if (s[j] == 'H')
            {
                if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                {
                    j++;
                    if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                    {
                        j++;
                        M += ((s[j - 1] - 48) * 10 + (s[j] - 48)) * H;
                    }
                    else
                    {
                        M += (s[j] - 48) * H;
                    }
                }
                else
                {
                    M += H;
                }
            }
            if (s[j] == 'O')
            {
                if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                {
                    j++;
                    if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                    {
                        j++;
                        M += ((s[j - 1] - 48) * 10 + (s[j] - 48)) * O;
                    }
                    else
                    {
                        M += (s[j] - 48) * O;
                    }
                }
                else
                {
                    M += O;
                }
            }
            if (s[j] == 'N')
            {
                if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                {
                    j++;
                    if (j + 1 < len && s[j + 1] <= '9' && s[j + 1] >= '0')
                    {
                        j++;
                        M += ((s[j - 1] - 48) * 10 + (s[j] - 48)) * N;
                    }
                    else
                    {
                        M += (s[j] - 48) * N;
                    }
                }
                else
                {
                    M += N;
                }
            }
        }
        printf("%.3lf\n", M);
    }
}
int main(int argc, const char **argv)
{
    solution();
    return 0;
}

刚开始学习,欢迎指正

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值