2017-09-17 56 views
1

我的程序目前有兩個問題。當我嘗試運行我的程序時,我沒有得到正確的標準偏差,而且我不確定如何實現驗證用戶輸入的方式,如果它們輸入的不是雙精度值。我將如何去解決這個問題?謝謝。我的代碼有點雜亂,我想就如何儘可能使它更好。我將發佈的程序無法運行,因此如果您希望它編譯並運行,請取消註釋輸入並對驗證發表評論。謝謝。我得到的標準偏差總是低於實際的標準偏差。程序不輸出正確的標準偏差。另外,驗證問題

#include <iostream> 
#include <vector> 
#include <numeric> 
#include <math.h> 

double average(std::vector<double> & values) { // The Average function. Obtainin by accumulation method. 
double sum = std::accumulate(values.begin(), values.end(), 0.0); 
double average = sum/values.size(); 
return average; 
} 





double square(int x) { // Square function. Just x*x. The simplist. 
double squared = x*x; 
return squared; 
} 






double stddeviation (std::vector<double> & values) { 
double lessmean; 
double totalsquare; 
double variance; 
double standev; 
std::vector<double> lessmeanvector; // Subtracting average from each number in the vector. 
for (int i = 0; i<values.size(); i++) { 
    lessmean = (values.at(i)-average(values)); 
    lessmeanvector.push_back(lessmean); 
    } 
//std::vector<double> squaredvector; // Squaring each number in the vector. 
for (int i = 0; i<lessmeanvector.size(); i++) { 
    totalsquare+= square(lessmeanvector.at(i)); 
    } 
variance = (totalsquare/(values.size())-1); 
standev = pow(variance,0.5); 
std::cout<<"The standard deviation is "<<standev<<".\n"; 
} 







int main() { 

int total; // Decleration of main variables. 
double numbers; 
std::cout<<"How many numbers would you like to enter in?\n"; 
int total = 0; 
    while(!(std::cin >> total)){ 
     std::cin.clear(); 
     std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     std::cout << "Invalid input. Try again: "; 
    } 
//std::cin>>total; 
std::cout<<"Enter "<<total<<" numbers in now.\n"; 

std::vector<double>values; 
for(int i = 0; i<total; ++i) { 
    while(!(std::cin >> numbers)){ 
     std::cin.clear(); 
     std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     std::cout << "Invalid input. Try again: "; 
    } 
    //std::cin>>numbers; 
    values.push_back(numbers); 
    } 
std::cout<<"The average is "<<average(values)<<".\n"; 
stddeviation(values); 

} 
+0

'variance =(totalsquare /(values.size()) - 1);'你的括號在錯誤的地方。用'values.size()'分割'totalsquare',然後從結果中減去一個。而我認爲你的意思是用'values.size() - 1'來劃分'totalsquare' –

回答

1

驗證數字的一種方法是讀取數字,然後讀取下面的空格。在這一點上,你應該閱讀所有的字符。如果沒有,最後還有一些額外的字符。但是你需要從可能用完的輸入(std :: cin永遠持續下去,無需用戶干預)那樣做。

所以我做什麼是閱讀整行作爲一個字符串,並使用std::istringstream轉換和驗證輸入這樣的:

double numbers; 

for(std::string line; std::getline(std::cin, line);) 
{ 
    if((std::istringstream(line) >> numbers >> std::ws).eof()) 
     break; 

    std::cout << "Invalid input. Try again: "; 
} 

,因爲如果你只輸入它成功空格它並不完美。你需要添加一個額外的檢查。爲此,我通常使用字符串trim()函數來刪除周圍的空白字符(空格和製表符等)。

// remove surrounding whitespace 
std::string& trim(std::string& s, char const* ws = " \t") 
{ 
    s.erase(0, s.find_first_not_of(ws)); 
    s.erase(s.find_last_not_of(ws) + 1); 
    return s; 
} 

,並提供:

double numbers; 

for(std::string line; std::getline(std::cin, line);) 
{ 
    if(!trim(line).empty()) 
     if((std::istringstream(line) >> numbers).eof()) 
      break; 

    std::cout << "Invalid input. Try again: "; 
} 

現在你不需要在輸入結束跳過空格,因爲你已經修剪了。