gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:15:59 25 4
gpt4 key购买 nike

我正在开发一个程序,它从一个 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;
}

最佳答案

我建议您从文件中读取所有值并将它们放入 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";
}
}

关于c++ - 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58712331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com