2014-03-06 38 views
-1

我對結構的工作原理感到困惑。我想問一下如何通過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; 
    } 

回答

1

我會認爲這是一個「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,不使用兩者之間的空間,因爲在我的頭上,他們屬於一個整體)

1

這是面向對象編程如何工作的一部分。 c中的結構只不過是C++中的類,在它的功能上很少修改和增加。結構用於將多個數據類型存儲在一個地方,以便我們可以通過使用結構的對象來使用它們。 有關詳情,請http://en.wikipedia.org/wiki/Struct_(C_programming_language)