Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case is given in a single line, which contains a molecular formula as a string. The chemical symbol is given by a capital letter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical symbol would be omitted when the number is 1 (2
Your program is to write to standard output. Print exactly one line for each test case. The line should contain the molar mass of the given molecular formula.
4 C C6H5OH NH2CH2COOH C12H22O11
12.010 94.108 75.070 342.296
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<iomanip>
using namespace std;
int main()
{int i,n,k,m,d,f;
double s;
char a[105][90];
while(cin>>n)
{ memset(a,0,sizeof(a));
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{ m=strlen(a[i]);
s=0;
for(k=0;k<m;k++)
{if(isalpha(a[i][k])==1&&isdigit(a[i][k+1])==1)
{{d=a[i][k+1]-'0';
for(f=1;;f++)
{if(isdigit(a[i][k+f+1])==1)d=d*10+(a[i][k+f+1]-'0');
else break;}}
switch(a[i][k])
{case'C':s+=12.01*d;break;
case'H':s+=1.008*d;break;
case'O':s+=16.00*d;break;
case'N':s+=14.01*d;break;}}
if(isalpha(a[i][k])==1&&isdigit(a[i][k+1])!=1)
switch(a[i][k])
{case'C':s+=12.01;break;
case'H':s+=1.008;break;
case'O':s+=16.00;break;
case'N':s+=14.01;break;}
if(isdigit(a[i][k])==1)
continue;
}
cout<<setiosflags(ios::fixed)<<setprecision(3)<<s<<endl;
}
}
return 0;
}