如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值

How can I sort the numbers in my input file in ascending order, and find the range and median of them?

本文关键字:范围 升序 何按 输入 文件 数字 排序      更新时间:2023-10-16

我正在开发一个程序,该程序从txt文件中读取信息,然后计算其平均值、中值、模式、范围、标准差和方差。如何将输入文件中的数字按升序排序,并找到这些值的中值和范围?input.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;
}

我建议您从文件中读取所有值,并将它们放在std::vector中,以便于计算。

这是一种可以通过一些如何排序、求和和和计算均值和中值的例子来实现的方法。

#include <algorithm> // std::sort
#include <fstream>
#include <iostream>
#include <iterator>  // std::istream_iterator, std::back_inserter
#include <numeric>   // std::accumulate
#include <string>
#include <vector>    // std::vector
// a function to read all values of a certain type (T) from a stream
// and put them in a vector
template<typename T>
std::vector<T> extract_data_from_stream(std::istream& is) {
std::vector<T> retval;
// copy all values from the stream and put them in a vector
std::copy(std::istream_iterator<T>(is), std::istream_iterator<T>(),
std::back_inserter(retval));
// not at end-of-file, extraction ran into problems 
if(is.eof() == false)
throw std::runtime_error("extraction failed");
return retval;
}
// a wrapper function to open a file and call the above function
template<typename T>
std::vector<T> extract_data_from_file(const std::string& filename) {
// open the file
std::ifstream fin(filename);
// if the file was opened ok, extract all data and return it
if(fin) return extract_data_from_stream<T>(fin);
// else throw
throw std::runtime_error("Could not open " + filename + " for reading");
}
int main() {
try {
// get all int:s from a file as a std::vector<int>
auto data = extract_data_from_file<int>("/home/anomaly/Desktop/arsenic.txt");
// sort the data
std::sort(data.begin(), data.end());
// print the data
std::copy(data.begin(), data.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << "n";
// sum of the data
int datSum = std::accumulate(data.begin(), data.end(), 0);
std::cout << "number of values: " << data.size() << "n";
std::cout << "sum:              " << datSum << "n";
if(data.size()) { // mean and median must have data
// get the mean value
float datMean =
static_cast<float>(datSum) / static_cast<float>(data.size());
// find/calculate the median value
float datMedian;
size_t mid = data.size() / 2;
if(data.size() % 2) // odd number of values
datMedian = static_cast<float>(data[mid]);
else                // even number of values
datMedian = static_cast<float>(data[mid - 1] + data[mid]) / 2.f;
std::cout << "mean:             " << datMean << "n";
std::cout << "median:           " << datMedian << "n";
}
} catch(const std::exception& ex) {
std::cerr << ex.what() << "n";
}
}