2014-03-06 17 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

http://en.wikipedia.org/wiki/Struct_(C_programming_language) 나는 이것이 "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 

그것이 r에 데이터 (각 서랍은 "플라스틱 툴 유지 물건이"카세트 우리 가슴, 선택을 읽어 afile에서 >> 연산자를 사용

초기화이 행에서 수행 서랍 수 inumerdenom "구멍".

(나는 개인적으로 내 머리에 그들이 함께 속해 있기 때문에 r[i].numer, 둘 사이의 공간을 사용하지를 작성하는 것을 선호)

1

이는 객체 지향 프로그래밍이 작동하는 방식의 일부입니다. C의 구조는 C++의 클래스 일 뿐이고 기능에는 약간의 수정과 추가가 있습니다. 구조체는 구조체의 객체를 사용하여 여러 데이터 유형을 모두 사용할 수 있도록 여러 데이터 유형을 한 곳에 저장하는 데 사용됩니다. 자세한 내용은 이

관련 문제