结构如何运作?

时间:2014-03-06 07:16:46

标签: c++ arrays module structure multiplication

我对结构如何运作感到困惑。我想问一下如何通过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;
    }

2 个答案:

答案 0 :(得分:1)

这是面向对象编程如何工作的一部分。 c中的结构只不过是C ++中的类,它的功能很少有修改和补充。结构用于在一个地方存储多个数据类型,以便我们可以通过使用结构的对象来使用它们。 有关详细信息,请查看http://en.wikipedia.org/wiki/Struct_(C_programming_language)

答案 1 :(得分:1)

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

如果你将struct塑造成一个带有一堆工具的塑料物品,每个工具都有一个完美的形状,所以你有一个“锤子”形状的空间,另一个用于“螺丝刀”的空间在计算机术语中,struct的每个成员都是某个名称的“空间”。

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

所以,如果我们选择代码中的一个语句:

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

r代表你的整套“塑料工具拿着东西”。 [i]选择其中一个,.quotient选择“商形孔”。在=的另一边,我们的代码可以从pastic工具架中的numerdenom形状的孔中挑选出来。

初始化在这一行完成:

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

它使用>>的{​​{1}}运算符将数据读入afile(我们的抽屉柜,每个抽屉都是“塑料工具固定物”,选择抽屉编号{ {1}}以及ri“漏洞”。

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

相关问题