구조가 어떻게 작동하는지 혼동합니다. r [i]를 통해 구조 번호에 저장된 정보가 어떻게되는지 묻고 싶습니다. 가치 지수는 어떻게 초기화됩니까? r [i]를 통해 몫/나머지에 저장된 값은 어떻게됩니까? 미리 감사드립니다!구조는 어떻게 작동합니까?
// File processing + array of structures
// 1. Create a data file to describe the
// property of a struture
// 2. Transfer information stored in 1 to an
// array of structures
// 3. Process the array
// 4. From array to an output file
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// The maximum size of the array
const int MAX = 100;
struct RationalNo
{
int numer;
int denom;
int quotient;
int remainder;
float value;
};
// Task 1
void createInputFile (fstream&, const char []);
// Task 2
int fileToArray (fstream&, const char [], RationalNo []);
// Task 3
void processArray (RationalNo [], int);
// Task 4
void arrayToOutfile (const RationalNo [], int, ofstream&, const char []);
int main()
{
fstream afile;
char fileName [MAX];
cout << "Enter file name to be created: ";
cin >> fileName;
createInputFile (afile, fileName);
cout << "---------------------------------" << endl;
RationalNo r [MAX];
int size = fileToArray (afile, fileName, r);
cout << "---------------------------------" << endl;
processArray (r, size);
cout << "---------------------------------" << endl;
ofstream outfile;
cout << "Enter array to output file name: ";
cin >> fileName;
arrayToOutfile (r, size, outfile, fileName);
}
void createInputFile (fstream& afile, const char fileName [])
{
afile.open (fileName, ios::out);
if (!afile)
{
cout << fileName << " opened for creation failed" << endl;
exit (-1);
}
cout << "Begin the creation of " << fileName << endl;
int size = rand() % 51 + 50;
for (int i = 1; i <= size; i++)
{
afile << rand() << "\t"
<< rand() + 1 << "\t"
<< "Rational No " << i
<< endl;
}
afile.close();
cout << fileName << " successfully created" << endl;
}
int fileToArray (fstream& afile, const char fileName [], RationalNo r [])
{
afile.open (fileName, ios::in);
if (!afile)
{
cout << fileName << " open for reading failed" << endl;
exit (-1);
}
cout << "Begin reading of " << fileName << endl;
int i = 0;
while (afile >> r [i].numer >> r [i].denom)
{
afile.clear();
afile.ignore (MAX, '\n');
++i;
}
afile.close();
cout << fileName << " to array done" << endl;
return i;
}
void processArray (RationalNo r [], int size)
{
cout << "Begin the process of array" << endl;
for (int i = 0; i < size; i++)
{
r [i].quotient = r [i].numer/r [i].denom;
r [i].remainder = r [i].numer % r [i].denom;
r [i].value = 1.0 * r [i].numer/r [i].denom;
}
cout << "Array was processed" << endl;
}
void arrayToOutfile (const RationalNo r [], int size,
ofstream& outfile, const char fileName [])
{
outfile.open (fileName);
if (!outfile)
{
cout << fileName << " opend for array transfer failed" << endl;
exit (-1);
}
cout << "Begin from array to " << fileName << endl;
outfile << fixed << showpoint << setprecision (3);
for (int i = 0; i < size; i++)
{
outfile << "Rational no " << i + 1 << ": "
<< r [i].numer << "\t"
<< r [i].denom << "\t"
<< r [i].quotient << "\t"
<< r [i].remainder << "\t"
<< r [i].value
<< endl;
}
outfile.close();
cout << "Array to " << fileName << " done" << endl;
}