Molar Mass ACM/ICPC sroul 2007, UVa1586

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
/*
* === FUNCTION ======================================================================
* Name: get_g
* Description:
* =====================================================================================
*/
float get_g ( char a )
{
switch(a){
case 'C':
return 12.01;
break;
case 'H':
return 1.008;
break;
case 'O':
return 16.00;
break;
case 'N':
return 14.01;
break;
default:
return -1.0;
}
}
int main ( int argc, char *argv[] ) {
char buf[101]={};
int flag = 0;
float sum = 0.0;
int i = 0 ;
int j = 0;
int count = 0;
while( (scanf("%s", buf)!=EOF) ){
flag = 0;
for (i = 0; buf[i]!='\0'; i++) {
count = 0;
for( j=i+1;isdigit(buf[j])&&buf[j];j++ ){
count = count * 10 + (buf[j]-'0');
}
if (count==0) {
count=1;
}
if (buf[i]!='C'
&& buf[i]!='N'
&& buf[i]!='O'
&& buf[i]!='H'
) {
flag = 1;
}
sum += count * get_g(buf[i]);
i=j-1;
}
if (!flag){
printf ( "sum is %f\n",sum );
}else{
// printf ( "input error\n" );
}
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

Comments