c++ - How does structure works? -


i confuse how structure works. ask how information stored in structure number via r[i]. how value quotient initialize? how value stored in quotient/ remainder in first place via r[i]. in advance!

    // file processing + array of structures      // 1. create data file describe      //    property of struture     // 2. transfer information stored in 1     //    array of structures     // 3. process array     // 4. array output file       #include <iostream>     #include <fstream>     #include <cstdlib>     #include <ctime>     #include <iomanip>      using namespace std;      // maximum size of 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 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 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 creation failed" << endl;             exit (-1);         }          cout << "begin creation of " << filename << endl;          int size = rand () % 51 + 50;          (int = 1; <= size; i++)         {                     afile << rand () << "\t"                       << rand () + 1 << "\t"                   << "rational no " <<                   << endl;         }          afile.close ();         cout << filename << " created" << endl;     }       int filetoarray (fstream& afile, const char filename [], rationalno r [])     {         afile.open (filename, ios::in);          if (!afile)         {            cout << filename << " open reading failed" << endl;            exit (-1);         }          cout << "begin reading of " << filename << endl;          int = 0;          while (afile >> r [i].numer >> r [i].denom)         {            afile.clear ();            afile.ignore (max, '\n');             ++i;         }          afile.close ();         cout << filename << " array done" << endl;          return i;     }      void processarray (rationalno r [], int size)     {         cout << "begin process of array" << endl;          (int = 0; < 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 processed" << endl;     }       void arraytooutfile (const rationalno r [], int size,                  ofstream& outfile, const char filename [])     {         outfile.open (filename);          if (!outfile)         {             cout << filename << " opend array transfer failed" << endl;             exit (-1);         }          cout << "begin array " << filename << endl;          outfile << fixed << showpoint << setprecision (3);          (int = 0; < size; i++)         {             outfile << "rational no " << + 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 " << filename << " done" << endl;     } 

i'm going assume "begginner" level question, , don't need know compiler figure out member of struct goes or contains what.

if image struct 1 of plastic things hold bunch of tools, each 1 perfect shape 1 tool, have "hammer" shaped space, space "screwdriver", etc. in computer terms, each member of struct named "space" something.

an array of struct chest of drawers, each drawer has number, , in each drawer have 1 of plastic tool holders.

so, if pick apart 1 of statements in code:

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

the r represents entire set of "plastic tool holding things". [i] selects 1 of them, , .quotient picks "quotient shaped hole". on other side of = have code picks things out of numer , denom shaped holes in pastic tool holder.

the initialization done in line:

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

it uses >> operator afile read data r (our chest of drawer, each drawer "plastic tool holding thing", selects drawer number i, , numer , denom "holes".

(i prefer write r[i].numer, not using space between two, because in head belong together)


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -