【问题标题】:Find standard deviation from file C++从文件 C++ 中查找标准差
【发布时间】:2021-07-07 13:25:11
【问题描述】:

我有编码来查找提供的文件的标准偏差。我的讲师建议在函数中进行。这是我尝试过的编码。但是在主函数中调用函数有错误。它说 std 不能隐蔽浮动。我应该怎么做才能找到文件中的标准差?

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

float calculateSD(float namefile);

int main()
{
   string input;  
   ifstream namefile("payment.txt"); 

   if (!namefile)
       return (cout << " ERROR : cannot open file.\n"), 1; 

   while (getline(namefile, input)) 
        cout << input << endl;

   namefile.clear();   
   namefile.seekg(0);  

   int a {}, b {}, c {};
   nameFile >> a >> b >> c ;

   calculateSD(nameFile >> a >> b >> c);
}

float calculateSD(float nameFile)
{
   float sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
   float mean1, mean2, mean3;
   float standardDeviation1 = 0.0, standardDeviation2 = 0.0, standardDeviation3 = 0.0;

    for (int a {}, b {}, c {}; namefile >> a >> b >> c; )
  {
    sum1 += a;
    sum2 += b;
    sum3 += c;
  }

    mean1 = sum1/10;
    mean2 = sum2/10;
    mean3 = sum3/10;

   for (int a {}, b {}, c {}; nameFile >> a >> b >> c ; )
  {
        standardDeviation1 += pow(a - mean1, 2);
        standardDeviation2 += pow(b - mean2, 2);
        standardDeviation3 += pow(c - mean3, 2);
  }

 cout << "Standard Deviation 1  = " << sqrt(standardDeviation1 / 10) << endl;
 cout << "Standard Deviation 2 = " << sqrt(standardDeviation2 / 10)<< endl;
 cout << "Standard Deviation 3 = " << sqrt(standardDeviation3 / 10)<< endl;

return 0;
}

如果你们能帮助我,真的很感激

【问题讨论】:

  • 请不要解释错误,但在问题中包含编译器错误消息
  • float nameFile 这看起来很奇怪。
  • float calculateSD(std::iostream&amp;);?

标签: c++ standard-deviation


【解决方案1】:

你犯了很多错误。首先是你的功能

float calculateSD(float nameFile);

出于某种原因接受float nameFile,但您可能指的是ifstream&amp; nameFile(尽管您命名此变量的方式仍然令人困惑)。

main() 之后你尝试像这样调用这个函数

int a {}, b {}, c {};
nameFile >> a >> b >> c ;

calculateSD(nameFile >> a >> b >> c);

为什么在开始计算标准差之前从文件中读取 6 个数字?现在你的结果可能不正确。更不用说你有一个错字 - 你将你的流定义为ifstream namefile("payment.txt");,注意它被称为namefile,没有大写F,但后来你尝试将它用作nameFile变量,它不存在.你可能打算这样传递它:

int main()
{
   string input;  
   ifstream namefile("payment.txt"); 

   if (!namefile)
       return (cout << " ERROR : cannot open file.\n"), 1; 

   while (getline(namefile, input)) 
        cout << input << endl;

   namefile.clear();   
   namefile.seekg(0);  
   calculateSD(namefile);
}

【讨论】:

  • Alexey,我能知道我的函数原型应该是什么吗?因为在 calculateSD(namefile) 仍然错误无法转换为浮点数
  • float calculateSD(ifstream&amp; nameFile); 正如我已经说过的,正如你在 cmets 中被告知的那样
  • 好的,我运行编码,没有更多错误。但是,我得到所有标准差的 0
  • 这是因为您尝试重新读取文件,但您没有将流的位置重置为 0,因此更改 standardDeviation1 的循环不会 no 迭代(nameFile 不再产生值)
【解决方案2】:

您正在尝试拆分以发挥作用,这很好。问题是您无法掌握应该传递给函数的内容。

试试这个方法:

std::vector<double> loadValues(std::istream& f) {
     double x;
     std::vector<double> v;
     while(f >> x) v.push_back(x);
     return v;
}

double standardDeviationFrom(const std::vector<double>& v)
{
     ... // do it yourself
}

double standardDeviationFrom(std::istream& f)
{
    return standardDeviationFrom(loadValues(f))
}

double standardDeviationFrom(std::filesystem::path p)
{
    std::ifstream f{p};
    if (!f) {
        std::perror("standardDeviationFrom");
        return std::nan("1");
    }
    return standardDeviationFrom(f);
}

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多