C,H,O,N对应原子量为12.01,1.008,16.00,14.01
计算 C6H5OH =94.108
#include <stdio.h>
#include<iostream>
#include<cmath>
#include <string.h>
using namespace std;
#define maxn 10005
char s[maxn];
float f(char c){
switch(c){
case 'C':return 12.01;
case 'H':return 1.008;
case 'O':return 16.00;
case 'N':return 14.01;
}
}
int main(){
cin>>s;
char m[10];
int t=0;
int i;
float ft=1.0f;
double sum=0;
double totsum=0;
int mlen;
int len=strlen(s);
for(i=0;i<len;){
sum=0;
if(s[i]>=65&&s[i]<=122){ //字母
ft=f(s[i]);
if(s[i+1]>=48&&s[i+1]<=57){ //数字
i++;
t=0;
while(s[i]>=48&&s[i]<=57){
m[t++]=s[i];
i++;
}
mlen=t-1;
int j=0;
t=0;
while(mlen>=0){
sum+=pow(10,mlen)*(m[t]-48);
mlen--;
t++;
}
sum*=ft;
totsum+=sum;
}
else{ //字母后还是字母
while(s[i]>=65&&s[i]<=122) {
sum+=f(s[i]);
i++;
}
totsum+=sum;
}
}
}
cout<<totsum;
return 0;
}