#include <stdio.h>
//C=12.01 N=1.008 O=16.00 N=14.01
char che[101];
int cnt[4]={0}; //[0]-C, [1]-H, [2]-O, [3]-N
void count(void); //각각의 원소의 개수
int what(int a); //원소가 위치할 방번호
double cal(void); //계산
int main(void)
{
//int i;
double result;
scanf("%s", &che);
count();
/*for(i=0; i<4; i++)
{
printf("[%d]=%d\n", i, cnt[i]);
}*/
result=cal();
printf("%.3lf", result);
return 0;
}
void count(void)
{
int i, w=-1, n=0;
for(i=0; che[i]!='\0'; i++)
{
if(('A'<=che[i] && che[i]<='Z') || ('a'<=che[i] && che[i]<='z')) //원소 기호
{
if(w!=-1)
{
if(n==0) //뒤에 숫자가 없을 경우
cnt[w]++;
else
cnt[w]+=n;
}
w=what(i);
if(che[i+1]=='\0') //뒤에 숫자가 없고 마지막인 경우
cnt[w]++;
n=0;
}
else //숫자
{
n=n*10+(che[i]-'1'+1);
if(che[i+1]=='\0') //뒤에 숫자가 없고 마지막인 경우
cnt[w]+=n;
}
}
}
int what(int a)
{
if(che[a]=='C' || che[a]=='c')
return 0;
else if(che[a]=='H' || che[a]=='h')
return 1;
else if(che[a]=='O' || che[a]=='o')
return 2;
else if(che[a]=='N' || che[a]=='n')
return 3;
}
double cal(void)
{
double sum=0.00;
sum+=(12.01)*cnt[0];
sum+=(1.008)*cnt[1];
sum+=(16.00)*cnt[2];
sum+=(14.01)*cnt[3];
return sum;
}
2018.06.02 10:04
정올 - 실전대비 - 질량계산(1091)
조회 수 350 추천 수 0 댓글 0