Question

I am confuse with how structure works. I would like to ask how is the information stored in structure number via r[i]. How is the value quotient initialize? how is the value stored in quotient/ remainder in the first place via r[i]. Thanks in advance!

    // 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;
    }
Était-ce utile?

La solution

I'm going to assume this is a "begginner" level question, and you don't actually really need to know what the compiler does to figure out which member of a struct goes where or contains what.

If you image a struct as one of those plastic things that hold a bunch of tools, each one perfect shape for one tool, so you have a "hammer" shaped space, another space for a "screwdriver", etc. In computer terms, each member of a struct is a named "space" for a something.

An array of struct is like a chest of drawers, where each drawer has a number, and in each drawer you have one of those plastic tool holders.

So, if we pick apart one of the statements in the code:

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

The r represents your entire set of "plastic tool holding things". [i] selects one of them, and .quotient picks the "quotient shaped hole". On the other side of the = we have code that picks things out of the numer and denom shaped holes in the pastic tool holder.

The initialization is done in this line:

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

it uses the >> operator from afile to read data into r (our chest of drawer, where each drawer is a "plastic tool holding thing", selects the drawer number i, and the numer and denom "holes".

(I personally prefer to write r[i].numer, not using a space between the two, because in my head they belong together)

Autres conseils

That is a part of how object oriented programming works. Structure in c is nothing but class in C++,with few modifications and additions in it's functionality. Structure is used to store multiple datatypes in one place so that we can use them all by using structure's object. For more details check http://en.wikipedia.org/wiki/Struct_(C_programming_language)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top