1054

作者: 峡迩 | 来源:发表于2017-09-04 16:13 被阅读0次
// PATn.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>


using namespace std;

bool check_number(string &s)
{
    const string s_f = "0123456789-.";

    for (auto r : s)//判断是否含有除数字、负号、点号外的字符!
    {
        if (s_f.find(r) == string::npos)
            return false;
    }

    if (s.find_first_of('.') != s.find_last_of('.'))//判断是否含有多个字符‘.’
        return false;

    if (s.find('.')!=string::npos)
    {
        if ((s.size() - s.find('.')) > 3)//判断小数点的位数是否大于2!
            return false;
    }

    double tmp = stod(s);

    if (tmp < -1000 || tmp>1000)//判断是否越界!
        return false;

    return true;            //通过所有判断后则满足条件!
}

int main()
{
    
    unsigned n;
    cin >> n;

    string tmp;
    double sum=0;
    unsigned count = 0;
    for (unsigned i = 0; i < n; ++i)
    {
        cin >> tmp;
        if (check_number(tmp))
        {
            sum = sum + stod(tmp);
            count = count + 1;
        }
        else
        {
            cout << "ERROR: " << tmp << " is not a legal number" << endl;
        }
    }

    cout.setf(ios::fixed);
    if(count == 0)
        cout <<"The average of 0 numbers is Undefined";
    else
    {
        auto out = sum / (count*1.0);
        if (count == 1)
            cout << "The average of " << count << " number is " << setprecision(2) << out;
        else
            cout  << "The average of " << count << " numbers is " << setprecision(2)<<out;
    }
        
    system("pause");
    return 0;
}

相关文章

  • 1054

  • 1054

    8月14日,农历七月十七,周日 高温40度,现在室外温度35度,晴 娃爸在家,我就可以做甩手掌柜。 早上他骑电驴子...

  • 1054

    今天下午去参加老板的小会,然后带着口罩下去,时不时的说两句,结果居然开始咳嗽,会开到晚上7.49,我收拾一下回家给...

  • 1054, "Unknown column '*********

    1054, "Unknown column '*********.receive' in 'field list'...

  • B18. Alarm Messages-6&B19. V

    1054 Exasmoc controller was normally terminated %s(Exa...

  • PAT 1054

    1.isdigit函数原型:extern int isdigit(int c);用法:#include

  • 亲子(1054)

    2020.2.19 星期三 晴 一天的忙碌终于结束,刚刚吃过晚饭,奖励俩孩子看会儿动画片。 而我,也终于...

  • 日记1054

    2022年11月7日 星期一 晴 经过两天的网课和周末的调休,今天终于结束了,孩子们回到学校有些兴奋,同学间有...

  • 世界史学习(二百三十八)

    -1054年,双方最终分裂,教会历史上的分水岭,被称为“东西方教会大分裂”。双方的裂痕至今存在。-1054年,双方...

  • 1054 The Dominant Color(map 以及 S

    1054 The Dominant Color (20 分) Behind the scenes in the c...

网友评论

      本文标题:1054

      本文链接:https://www.haomeiwen.com/subject/pbuejxtx.html