最近在想好好学学C++,手头有本纸质的Primer,但想着之前的Plus还没看完,然后继续看,对于第六章第二题一开始不是很会,上网看了下都不是很符合要求,基本都有小差错,所以自己也发个博客
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
bool judge(string line)
{
int len;
len = line.size();
for (int i = 0; i < len; i++)
{
if (false == isdigit(line[i]) && '.' != line[i])//判断数字和.字符
{
return false;
}
}
return true;
}
int main()
{
double sum = 0, average = 0;
double a[10];
double tempdouble;
string line;
int i = 0, total = 0;
for (; i < 10; i++)
{
getline(cin, line, '\n');
if (false==isdigit(line[0]))//如果不是数字就跳出,结束输入
{
break;
}
if (false==judge(line))
{
break;//如果判断失败(数字和。字符)就跳出,结束输入
}
a[i] = atof(line.c_str());
sum += a[i];
}
if (0!=i)
{
average = sum / i;
}
for (int j = 0; j < i; j++)
{
if (a[j]>average)
{
total++;
}
}
cout << "There numbers' average is " << average << endl;
cout << "This are " << total << " number(s) biger than average.";
cin.get();
cin.get();
return 0;
}