码迷,mamicode.com
首页 > 其他好文 > 详细

UVA1586 UVALive3900 Molar mass

时间:2016-07-29 15:42:23      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

Regionals 2007 >> Asia - Seoul

问题链接:UVA1586 UVALive3900 Molar mass基础练习题,用C++语言编写程序。

这个问题是根据分子式,求分子量。

原子量使用map表来存储,所以用C++来编程。

程序中,使用函数getchar()处理输入流,需要更高的编程技巧。

AC的C++语言程序如下:

/* UVA1586 UVALive3900 Molar mass */

#include <iostream>
#include <map>
#include <cstdio>
#include <cctype>

using namespace std;

map<char, double> aweight;

int main()
{
    int t, count, mflag;
    char c, molar;
    double molarmass;

    aweight['C'] = 12.01;
    aweight['H'] = 1.008;
    aweight['O'] = 16.00;
    aweight['N'] = 14.01;

    cin >> t;
    getchar();
    while(t--) {
        molarmass = 0.0;

        mflag = 0;
        count = 0;
        while((c=getchar()) != '\n' && c != EOF) {
            if(isalpha(c)) {
                if(mflag)
                    molarmass += ((count==0) ? 1 : count) * aweight[molar];
                molar = c;
                count = 0;
                mflag = 1;
            } else if(isdigit(c))
                count = count * 10 + c - '0';
        }
        if(mflag)
            molarmass += ((count==0) ? 1 : count) * aweight[molar];


        // 输出结果
        if(mflag)
            printf("%.3f\n", molarmass);
    }

    return 0;
}


UVA1586 UVALive3900 Molar mass

标签:

原文地址:http://blog.csdn.net/tigerisland45/article/details/52054445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!