uvaoj 1586-molar mass

/*
An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements.

\epsfbox{p3900.eps}
When an organic compound is given as a molecular formula, Dr. CHON wants to find its molar mass. A molecular formula, such as C3 H4 O3 , identifies each constituent element by its chemical symbol and indicates the number of atoms of each element found in each discrete molecule of that compound. If a molecule contains more than one atom of a particular element, this quantity is indicated using a subscript after the chemical symbol.

In this problem, we assume that the molecular formula is represented by only four elements, `C' (Carbon), `H' (Hydrogen), `O' (Oxygen), and `N' (Nitrogen) without parentheses.

The following table shows that the standard atomic weights for `C', `H', `O', and `N'.

Atomic Name	 Carbon	 Hydrogen	 Oxygen	 Nitrogen
Standard Atomic Weight	 12.01 g/mol	 1.008 g/mol	 16.00 g/mol	 14.01 g/mol

For example, the molar mass of a molecular formula C6 H5 OH is 94.108 g/mol which is computed by 6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol).

Given a molecular formula, write a program to compute the molar mass of the formula.

Input 

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$ \le$n$ \le$99) .

Output 

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.

Sample Input 

4 
C 
C6H5OH 
NH2CH2COOH 
C12H22O11
Sample Output 

12.010 
94.108 
75.070 
342.296


*/

#include<stdio.h>
#include<string.h>
int main()
{
	int n,i=0,a,e,p=1;char b,d;
	char c[200]={};
	double s;
	scanf("%d",&n);
	while(n--)
	{	
		s=0;
		i=0;
		memset(c,0,sizeof(c));
		scanf("%s",c);
		for(int j=1;j<=strlen(c);j++)
	 {
	 	//printf("1");
		 //printf("%d",i);
	    if(c[i-1]=='\0'&&i!=0) break;
	   // printf("%d",c[i]);
	   if(p) b=c[i++];
	    p=1;
	   if(c[i]!='\0')
	  {
		
		d=c[i++]; 
		//printf("%c ",c[i]);
		if(d>57) 
		{
			//printf("%d,%d",d,i);
			a=1;
			switch(b)
			{
				
				case'C':s+=a*12.01;break;
				case'H':s+=a*1.008;break;
				case'O':s+=a*16.00;break;
				case'N':s+=a*14.01;break;
				
			}
			//printf("%f,%d!",s,i);
			b=d;
			p=0;	//printf("*");
		}
		else if(c[i]>57||c[i]=='\0')
		{
			//printf("%d",c[i]);
		//	printf("%d",i);
			a=d-48;  //printf("%d,%d!",a,i);
			switch(b)
				{
				
					case'C':s+=a*12.01;break;
					case'H':s+=a*1.008;break;
					case'O':s+=a*16.00;break;
					case'N':s+=a*14.01;break;
					
				}
			//printf("%f,%d!",s,i);
		}
		else if(c[i]<=57&&c[i]>=48)
		{
			e=c[i]-48;
			a=(d-48)*10+e;
			switch(b)
				{
				
					case'C':s+=a*12.01;break;
					case'H':s+=a*1.008;break;
					case'O':s+=a*16.00;break;
					case'N':s+=a*14.01;break;
				
				}	
			i++;
		}
	  }
	  else {
	  				a=1;
				switch(b)
				{
				
					case'C':s+=a*12.01;break;
					case'H':s+=a*1.008;break;
					case'O':s+=a*16.00;break;
					case'N':s+=a*14.01;break;
					
				}
				i++;
				
	       }
	 }
	 	printf("%.3f\n",s);
    }
	return 0;
	
}
/*  终于ac了,本题差点一下弄死了我,调试了5个小时了,条件嵌套太复杂。 求分子量但是化学式中字母和数字混在一起
我想不到一个合适的方法让系统知道这是字母,这是数字。一开始想到了scanf("%d",&n);如果是数字就不是0,如果是字母就是0;
但是知道这条有什么用呢?字母被读成0以后我就不知道怎么还原回来再用这个字母了。
结果我用了getchar从头读到尾,再用ascll码把字符数字转换成真正的数字,但是要知道数字是多少位,而且每行末尾是\0 ,要做判断,而且复杂的i变量变化,都极其耗头脑。*/ 

你可能感兴趣的:(uvaoj 1586-molar mass)