请各位帮我看看什么问题 或者我没有考虑的情况
别贴网址啊 都能搜到的就不来问了
至少可以指出下我的代码中没有考虑到的输入案例
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
bool is_legal(std::string &str) {
if (str.front() != '-'&& !(isdigit(str.front())))
return false;
int i = str.find('.');
if (i < (int)str.size() - 3 && i != -1)
return false;
std::stringstream ss(str);
std::string temp;
double n;
ss >> n >> temp;
if (temp.size() > 0 || (n < -1000 || 1000 < n))
return false;
return true;
}
int main()
{
size_t n;
std::cin >> n;
std::vector<std::string> vec(n);
std::vector<double> output;
for (size_t i{ 0 }; i < n; ++i) {
std::cin >> vec[i];
}
for (size_t i{ 0 }; i < n; ++i) {
if (is_legal(vec[i])) {
std::stringstream ss(vec[i]);
double temp;
ss >> temp;
output.push_back(temp);
}
else {
std::cout << "ERROR: " << vec[i] << " is not a legal number" << '\n';
}
}
std::cout << "The average of " << output.size() << " numbers is ";
if (output.size() == 0)
std::cout << "Undefined";
else {
double sum{ 0 };
for (size_t i{ 0 }; i < output.size(); ++i) {
sum += output[i];
}
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(2) << sum / (double)output.size();
}
return 0;
}