题目

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molarmass 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 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 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 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

代码+注释

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{int T;double k[5];string l;cin>>T;while(T--){memset(k,0,sizeof(k));      //将数组k初始化为0。cin>>l;float ans=0;int len=l.size();       //求出数组l的大小。for(int i=0;i<len;i++){if(l[i]=='C')       //分别讨论不同的元素{if((l[i+1]>=48&&l[i+1]<=57)&&(l[i+2]>=48&&l[i+2]<=57))         //讨论元素的个数(10~99)。{k[0]+=((l[i+1]-'0')*10+(l[i+2]-'0'));}else if(l[i+1]>=48&&l[i+1]<=57)        //讨论元素的个数(2~9)。{k[0]+=(l[i+1]-'0');}else k[0]+=1;         //讨论元素个数(1)}else if(l[i]=='H'){if((l[i+1]>=48&&l[i+1]<=57)&&(l[i+2]>=48&&l[i+2]<=57)){k[1]+=((l[i+1]-'0')*10+(l[i+2]-'0'));}else if(l[i+1]>=48&&l[i+1]<=57){k[1]+=(l[i+1]-'0');}else k[1]+=1;}else if(l[i]=='O'){if((l[i+1]>=48&&l[i+1]<=57)&&(l[i+2]>=48&&l[i+2]<=57)){k[2]+=((l[i+1]-'0')*10+(l[i+2]-'0'));}else if(l[i+1]>=48&&l[i+1]<=57){k[2]+=(l[i+1]-'0');}else k[2]+=1;}else if(l[i]=='N'){if((l[i+1]>=48&&l[i+1]<=57)&&(l[i+2]>=48&&l[i+2]<=57)){k[3]+=((l[i+1]-'0')*10+(l[i+2]-'0'));}else if(l[i+1]>=48&&l[i+1]<=57){k[3]+=(l[i+1]-'0');}else k[3]+=1;}}k[0]*=12.01;k[1]*=1.008;k[2]*=16;k[3]*=14.01;      //各元素的相对原子质量。for(int i=0;i<4;i++)ans+=k[i];           //求出相对分子质量。printf("%.3f\n",ans);}return 0;
}

Molar mass(计算分子量)字符转化相关推荐

  1. (UVA)1586 --Molar Mass(分子量)

    题目链接:http://vjudge.net/problem/UVA-1586 思路:统计一个分子式中CHON出现的总次数,乘上相对原子量后求和.要注意的是CH4这样的C后面的1默认不出现,以及C4H ...

  2. UVa 1586 Molar mass 分子量 题解

    英文 Description An organic compound is any member of a large class of chemical compounds whose molecu ...

  3. UVA1586 ​​​​​​​ Molar mass

    Molar mass UVA - 1586 题目传送门 题目大意:给你一个只包含C,H,O,N分子式,其中C,H,O,N的原子量分别为:12.01,1.008,16.00,14.01,求其分子量 AC ...

  4. UVa1586 - Molar mass

    //UVa1586 - Molar mass //给出一种由C, H, O, N 四种原子构成的分子式,求分子量 //#define A1 //无法处理换行问题(scanf) //#define A2 ...

  5. 已知四种原子的质量,C/H/O/N分别为12/1/16/14,输入分子式,计算分子量。例如H2O,分子量为1*2+16=18,有如HC11N2,分子量为1+12*11+14*2=161

    已知四种原子的质量,C/H/O/N分别为12/1/16/14,输入分子式,计算分子量.例如"H2O",分子量为1*2+16=18,有如HC11N2,分子量为1+12*11+14*2 ...

  6. 十六进制字符转化为十进制数字

    在进行转化之前我们需要先了解这样几点: 1.数字0~9的的值,即为0到9,然后10为'a'. 2.转化为十进制数就是将上面的这个值乘上16. 下面是进行转化的代码: int i; for (i = n ...

  7. android 关于字符转化问题

    今日在写android的客户端,发现字符转化是个大问题. 下面是Unicode转UTF-8的转化,便于以后使用 private static String decodeUnicode(String t ...

  8. android把255转换成字节,android 上传参数设置,字符转化成字节,包装流等

    jsp中的小知识点 在页面输入内容控制台可以打印 必须写入的内容 value="save"/> 这个的意思是把隐藏域的值提交给了save方法了 SetDooutput(tru ...

  9. iOS计算输入字符数

    2019独角兽企业重金招聘Python工程师标准>>> iOS计算输入字符数 本文展示在iOS下,如何正确的计算输入的字符个数. 常见编码 汉字.中文符号 UTF-8编码下,一个汉字 ...

最新文章

  1. 【Java】 leetCode 删除链表中等于给定值 val 的所有节点。
  2. 哈佛大学 NLP 组开源神经机器翻译系统 OpenNMT
  3. 软件測试基本方法(一)之软件測试
  4. Android第三十三天
  5. java calendar 转换_[java]转:String Date Calendar之间的转换
  6. 交换机连接控制器_DELL MD3200I存储单双控制器的自由切换
  7. 为什么你的网站没流量?做不大!让我来告诉你。
  8. appinventor广州服务器网页,app inventor服务器
  9. docker 安装wiki.js 和wekan
  10. 贪心算法---Huffman编码---神秘电报密码
  11. winrar破解注册
  12. 如何培养自己的商业思维能力?
  13. Android 7.0修改PMS逻辑添加权限白名单
  14. x264参数设置详解(x264 settings)
  15. 城市各种服务设施半径
  16. pytorch 12 支持任意维度数据的梯度平衡机制GHM Loss的实现(支持ignore_index、class_weight,支持反向传播训练,支持多分类)
  17. 读书百客:《拟孙权答曹操书》赏析
  18. 9.nodejs 内置模块
  19. debain10更换源和配置
  20. C++空间中一点到平面投影

热门文章

  1. 生日攻击简介与相关推导
  2. 企业架构成功之道读书笔记
  3. php中pcntl_fork详解
  4. 小程序登陆注册功能的实现
  5. 平替笔和ipad笔差别大吗?主动式电容笔推荐
  6. k8s查看pod的yaml文件_K8S教程(6)YAML资源配置清单
  7. 关于abaqus中Mises应力分量的理解
  8. Linux中使用RAID技术提升磁盘读写速度及数据安全
  9. Kotlin协程不可用(java.lang.IllegalStateException: Module with the Main dispatcher is missing)另一种可能原因
  10. mysql值比较函数_mysql 比较函数浅用