习题3-2 分子量

分子量

给出一个物质的分子式(不带括号),求分子量。本题中的分子只包含4种原子,分别为C,H,O,N,原子量分别为12.01,1.008,16.00,14.01(单位:g/mol)。例如,C6H5OH的分子量为94.08g/mol。

#include <iostream>
#include <string>
using namespace std;

int main()
{
	char s[100];
	double result = 0;
	scanf_s("%s", s, sizeof(s));
	for (int i = 0; i < strlen(s); i++)
	{
		if (isdigit(s[i + 1]))
		{
			if (s[i] == 'C')
			{
				result += 12.01*(s[i + 1] - '0');
				i++;
			}
			else if (s[i] == 'H')
			{
				result += 1.008*(s[i + 1] - '0');
				i++;
			}
			else if (s[i] == 'O')
			{
				result += 16.00*(s[i + 1] - '0');
				i++;
			}
			else if (s[i] == 'N')
			{
				result += 14.01*(s[i + 1] - '0');
				i++;
			}
		}
		else
		{
			if (s[i] == 'C')
			{
				result += 12.01;
			}
			else if (s[i] == 'H')
			{
				result += 1.008;
			}
			else if (s[i] == 'O')
			{
				result += 16.00;
			}
			else if (s[i] == 'N')
			{
				result += 14.01;
			}
		}
	}
	cout << result << endl;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值