我正在开发一个程序,它从 txt 文件中读取信息,然后计算平均值、中位数、众数、范围、标准差和方差。如何将输入文件中的数字按升序排序并找到这些值的中位数和范围?输入 .txt 文件中的所有数字都是随机顺序。
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
int main()
{
// Define Variables
string null;
int input=0, counter=0, numAscend=0;
float datSum, datMean;
const string SPCHAR= " ";
// File Algorithms
// Input File and Output File
ifstream fin; // <--- Reading from this File
ofstream myfile; // <--- Writing to this File
// Output File Computing
// Place the directory to the file you want to write to here.
myfile.open("/home/anomaly/Desktop/finaldata.txt");
// What is being wrote to the output file.
myfile << "Writing this to a file.\n";
fin.open("/home/anomaly/Desktop/arsenic.txt");
// Main Operation of Program
fin >> input;
getline(fin, null);
while (fin.good())
{
datSum += input;
fin >> input;
getline(fin, null);
counter++;
}
// Data Configurations
datMean = (datSum / counter);
cout << "There are " << counter << " item(s)" << endl;
cout << setprecision(1) << input << endl;
cout << fixed << setprecision(2) << SPCHAR << "The mean is " << datMean << SPCHAR << SPCHAR << SPCHAR << SPCHAR << SPCHAR << SPCHAR << " The variance is " << endl;
cout << fixed << setprecision(2) << SPCHAR << SPCHAR << "The median is " << datSum << SPCHAR << SPCHAR << SPCHAR << " The Stand. Deviation is " << endl;
cout << fixed << setprecision(2) << SPCHAR << SPCHAR << SPCHAR << "The mode is " << datSum << SPCHAR << SPCHAR << SPCHAR << SPCHAR << SPCHAR << SPCHAR << " The range is " << endl;
myfile << setprecision(2) << datSum << endl;
return 0;
}