3-2分子量

bxdj發表於2024-11-24
#include<stdio.h>
#include<string.h>
#include<ctype.h>

char S[105];

double re(char c, int n)
{
	double num = 0.0;
	if (c == 'C') num = 12.01;
	if (c == 'H') num = 1.008;
	if (c == 'O') num = 16.00;
	if (c == 'N') num = 14.01;

	return (double)n * num;
}

int main()
{
	double Score = 0;
	
	scanf("%s", &S);
	for (int i = 0; i < strlen(S); i++)
	{
		
		if (isalpha(S[i]) && isdigit(S[i + 1]))
		{
			Score += re(S[i], S[i + 1] - '0');
			i++;
		}
		else
		{
			Score += re(S[i],1);
		}
	}
	printf("sumScore= %.3f", Score);
	
}