我正在尝试创建一个加载一个" grade.txt"文件的程序,该文件包含20个数字并计算标准偏差,并在屏幕和单独的一个单独的屏幕上输出结果标题为" result.txt"的文本文件
程序效果很好,并输出了我想要的结果。唯一的事情是,我似乎无法弄清楚如何采取计算结果(即总和,均值,标准偏差(并将其输出到文本文件中。
代码中我拥有的输出文本文件正在成功输出我发送给它的"结果"消息。
我只是不知道如何获取" void filereadernc(ifstream& file("函数和输出" void filewriter(ofstream& file("中的结果
#include <iostream>
#include <fstream>
#include <string>
// calling std name space
using namespace std;
void fileopenner(ifstream &file);
void filereader(ifstream &file);
void filecloser(ifstream &file);
void filereadernc(ifstream &file);
void filecreate(ofstream &file);
void filewriter(ofstream &file);
void filecloser(ofstream &file);
int main()
{
// declaration
ifstream ecet;
ofstream result;
// opening onc again
fileopenner(ecet);
filereadernc(ecet);
filecloser(ecet);
filecreate(result);
filewriter(result);
filecloser(result);
cout << endl;
return 0;
} // the end of the main function
void fileopenner(ifstream &file)
{
file.open("C:/Users/Desktop/grades.txt"); // file location on flash drive
}
void filereader(ifstream &file) // reading the values from the text file
{
int list[20];
for (int i = 0; i < 20; i++)
file >> list[i];
}
void filereadernc(ifstream &file) // file reader
{
float sum = 0.0, mean = 0.0, standardDeviation = 0.0;
int list[100];
int j = 0;
file >> list[j];
while (!file.eof())
{
j++;
file >> list[j];
}
cout << "The input values: "; // display the values that were in the text file to the user
for (int i = 0; i <= j; i++)
cout << list[i] << " ";
for (int i = 0; i <= j; i++) // calculate the sum and mean
{
sum += list[i];
mean = sum / 20;
}
for (int i = 0; i <= j; i++) // after finding the mean, we compute the standard deviation
standardDeviation += pow(list[i] - mean, 2);
cout << endl;
cout << "The sum of all our values = " << sum; // display the mean of all the values in the text file
cout << endl;
cout << "The mean of all our values = " << mean; // display the mean of all the values in the text file
cout << endl;
cout << "The standard deviation = " << sqrt(standardDeviation / 20); // display the standard deviation
}
void filecloser(ifstream &file)
{
file.close();
}
void filecreate(ofstream &file)
{
file.open("C:/Users/Desktop/results.txt");
}
void filewriter(ofstream &file)
{
file << "results";
}
void filecloser(ofstream &file)
{
file.close();
}
我只是不知道如何获取" void filereadernc(ifstream&amp; file("函数和输出" void filewriter(ofstream&amp; file("中的结果
我建议将程序分开,以使您有三个独立的部分:
- 阅读数据。
- 处理数据。
- 写数据。
为了能够做到这一点,您必须首先定义您的数据是什么。使用 struct
做到这一点。
struct MyData
{
// ... fill in the details.
};
功能:
void readData(std::istream& in, MyData& data)
{
// Read the contents of the stream and flesh out data
}
void readData(std::string const& file, MyData& data)
{
// Read the contents of file and flesh out data
std::ifstream in(file);
readData(in, data);
}
void processData(MyData& data)
{
// Do whatever you need to do to process the data.
}
void writeData(std:::ostream& out, MyData const& data)
{
// Write the contents of the data to the stream.
}
void writeData(std:::string const& file, MyData const& data)
{
// Write the contents of the data to the file.
std::ofstream out(file);
writeData(out, data);
}
现在main
可以:
int main()
{
MyData data;
// read the data.
readData("C:/Users/Desktop/grades.txt", data);
// Process the data.
processData(data);
// Write the data out to a file and std::cout
writeData("C:/Users/Desktop/results.txt", data);
writeData(std::cout, data);
}
如果有道理,则可以将数据分为输入数据和结果数据。
struct InputData { ... };
struct ResultsData { ... };
然后适当地更新功能。
您的代码有点复杂,但这是您可以执行的技巧,而不是每次(cout(在终端中显示结果,将其写入文件,通过调用方法filewriter并传递ARG您想在文件中写的内容。
以这种方式调用功能:
filewriter(ofstream &file, sum);
并以这种方式实现:
void filewriter(ofstream &file, float result)
{
file << results;
}