如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
How can I sort the numbers in my input file in ascending order, and find the range and median of them?
我正在开发一个程序,该程序从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";
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 在基于范围的for循环中使用结构化绑定声明
- 按对象的特定方法按升序排列的C++优先级队列
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值