Molar mass UVa1586

code:

//The second one
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define maxn 110

char molar[5] = "CHON";
double weight_n[4]={ 12.01, 1.008, 16.00, 14.01};

//return the weight of the molecule(such as "CHON")
double weight(char c)
{
    int num = strchr(molar, c)-molar;
    return weight_n[num];
}

//return the digit of the number after the molecule.
int sum_n(int d[], int n) { int num = n-1; int sum = 0; for(int j =0 ; j < n; j++) { sum += d[j] * (int)pow(10,num--); } return sum; } int main() { int T(0); scanf("%d", &T); while(T--) { double sum(0); char a[maxn]; scanf("%s",a); int n = strlen(a); int flag = 1; //default: everytime puch back 1 element. int i = 0; for(; i < n; i += flag) { int num[maxn]; int index = 1; int number_n(1); int count = 0; if(isdigit(a[i+1])) //if there is number after the molecule, then count the number { while(isdigit(a[i+index])) { num[count] = (a[i+index]-48); index++; count++; } number_n = sum_n(num,index-1); flag = index; } else flag = 1; sum += weight(a[i])*number_n; //add the weight of molecules to the value sum; } printf("%.3f\n",sum); } return 0; }

 

  1.  第一部分是两个函数比较重要,一个是用来计算某个元素的摩尔质量;另一个是用来返回分子数目。
  2. 时刻要保持头脑清醒。

转载于:https://www.cnblogs.com/dreamworldclark/p/9414367.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值