结构是如何工作的

How does structure works?

本文关键字:工作 何工作 结构      更新时间:2023-10-16

我对结构的工作方式感到困惑。我想问一下,通过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;
}

我假设这是一个"begginner"级别的问题,实际上你不需要知道编译器做了什么来确定struct的哪个成员去了哪里或包含了什么。

如果你把struct想象成一个装着一堆工具的塑料物体,每个工具的形状都很完美,所以你有一个"锤子"形状的空间,另一个空间放一把"螺丝刀",等等。用计算机术语来说,struct的每个成员都是一个命名的"空间"。

struct的阵列就像一个抽屉柜,每个抽屉都有一个数字,每个抽屉里都有一种塑料工具架。

因此,如果我们拆开代码中的一条语句:

r [i].quotient = r [i].numer / r [i].denom;

r代表您的整套"塑料工具夹持物"。[i]选择其中一个,.quotient选择"商形孔"。在=的另一侧,我们有一个代码,可以从馅饼刀架上的numerdenom形状的孔中挑选东西。

初始化在以下行中完成:

afile >> r [i].numer >> r [i].denom

它使用afile>>运算符将数据读取到r(我们的抽屉柜,每个抽屉都是一个"塑料工具夹持物",选择抽屉编号i,以及numerdenom"孔"。

(我个人更喜欢写r[i].numer,而不是在两者之间使用空格,因为在我的脑海中,它们属于一起)

这是面向对象编程工作原理的一部分。c中的结构只不过是c++中的类,在其功能中几乎没有修改和添加。Structure用于将多个数据类型存储在一个地方,这样我们就可以通过使用Structure的对象来使用它们。有关更多详细信息,请查看http://en.wikipedia.org/wiki/Struct_(C_programming_language)