c语言 分子量
作者:互联网
给出一种物质的分子式(不带括号),求分子量。本题中的分子式只包含4种原子,分别为C, H, O, N,原子量分别为12.01, 1.008, 16.00, 14.01(单位:g/mol)。例如,C6H5OH的分子量为94.108g/mol
*#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define maxn 100
char alpha[4] = { 'C','H','O','N' };
int main()
{
char str[maxn];
int num[4] = { 0 };
scanf("%s", str);
int n = strlen(str);
int p=0,j=0;
for (int i = 0;i < n;i++)
{
if (isalpha(str[i]))
{
for (j = 0;j < 4;j++)
{
if (str[i] == alpha[j])
break;
}
p = j;
num[p] += 1;
}
else
{
int sum = 0;
for (;isdigit(str[i]);i++)
{
sum = sum * 10 + (str[i]-'0');
}
sum -=1;i=i-1;
num[p] =num[p]+sum;
}
}
printf("%f", num[0] * 12.01 + num[1] * 1.008 + num[2] * 16.00 + num[3] * 14.01);
return 0;
}
标签:语言,int,分子量,++,num,str,include,sum 来源: https://blog.csdn.net/sherryrothchild/article/details/122003830