读取.txt文件,计算总和和平均值

时间:2015-10-20 22:20:24

标签: c++ sum average mean

我有一个包含数字的.txt文件,如下所示:

  

1
  2
  3

这些数字并不重要,但它们都是从一条新线开始的。

我想找到总和&文本文件中数字的平均值。

这是我到目前为止所拥有的:

#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

int main(int argc, char * argv[])
{
    std::fstream myfile("numbers.txt", std::ios_base::in);

    float a;
    while (myfile >> a)
    {
        printf("%f ", a);
    }

    getchar();

    return 0;

int sum(0);
int sumcount(0);
double average(0);
int even(0);
int odd(0);

ifstream fin;

string file_name;

int x(0);

cout<<"numbers.txt";
cin>> file_name;
fin.open(file_name.c_str(),ios::in);

if (!fin.is_open())
{
    cerr<<"Unable to open file "<<file_name<<endl;
    exit(10);

}

fin>>x;
    while (!fin.fail())
    {
        cout<<"Read integer: "<<x<<endl;
        fin>>x;
        sum=sum+x;
        sumcount++;
        if(x%2==0)
            even++;
        else
            odd++;

    }

    fin.close();
    average=(double)sum/sumcount;
    cout<<"Sum of integers: "<<sum<<endl;
    cout<<"Average: "<<average<<endl;
    cout<<"Number of even integers: "<<even<<endl;
    cout<<"Number of odd integers: "<<odd<<endl;

    return 0;
}

开始加载数字,但不会执行下一步。

我已经查看了很多其他代码并试图实现其他想法来解决问题;然而,有些人使用循环和数组以及其他东西,我不确定使用哪个。

如何让程序找到文件中数字的总和和平均值?

也欢迎任何其他帮助链接

编辑:虽然这些数字是整数,但平均值可能不是

4 个答案:

答案 0 :(得分:1)

这是您的工作代码。

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


using namespace std;

int main(int argc, char * argv[])
{
    std::fstream myfile("numbers.txt", std::ios_base::in);

    float a = 0;

    myfile >> a;

    while (!myfile.fail())
    {
        printf("%f ", a);
        myfile >> a; // here you dispay your numbers
    }

    getchar(); // waiting for input

    float sum(0);
    int x(0);
    int sumcount(0);
    double average(0);
    int even(0);
    int odd(0);

    ifstream fin;

    string file_name;

    cout<<"numbers.txt" << endl;

    cin>> file_name; // waiting for enter file name

    fin.open(file_name.c_str(),ios::in);

    if (!fin.is_open())
    {
        cerr<<"Unable to open file "<<file_name<<endl;
        exit(10);
    }

    fin >> x;

    while (!fin.fail())
    {
        cout<<"Read integer: "<<x<<endl; // display number again

        sum=sum+x;
        sumcount++;
        if(x % 2==0)  // compuing your file statistics
            even++;
        else
            odd++;

        fin>>x;
    }

    fin.close();
    average=(double)sum/sumcount;

    cout<<"Sum of integers: "<<sum<<endl; // displaying results
    cout<<"Average: "<<average<<endl;
    cout<<"Number of even integers: "<<even<<endl;
    cout<<"Number of odd integers: "<<odd<<endl;

    return 0;
}

我添加了一些最小的更改,以使您的程序正常工作。

结果

enter image description here

以及更多

enter image description here

答案 1 :(得分:0)

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


int main() {

int n=0;
int sum=0,total=0;

fstream file("numbers.txt");
while(file >> n) // or while(cin >> n) to read from stdin, commandline
{
    sum += n;
    total++;
}

int average = (float) sum/total;

cout<<"sum: " << sum << endl;
cout << "average: " << average << endl;
return 0;
}

答案 2 :(得分:0)

一旦主函数遇到语句return 0,它将退出程序并且无法执行剩余的代码。这通常只应出现在主函数中,代码块末尾

答案 3 :(得分:0)

#include <iostream>
#include<fstream>

int main()
{
    std::ifstream txtFile;
    txtFile.open("data.txt");
    //TODO: check if the file fails to open.

    double tempNum, mean(0.0), sum(0.0), count(0.0);
    while (txtFile >> tempNum)
    {
        sum += tempNum;
        ++count;
    }
    mean = sum/count;
    std::cout << "mean: " << mean << "  sum: " << sum << std::endl;

    return 0;
}