ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 실습 4
    정리필요2 2008. 9. 2. 15:49

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    const int MAXSIZE = 10;

    class StudentRecord
    {
    private:
     int sno;
     string sname;
     float score;
    public:
     StudentRecord() {;}
     StudentRecord(int no, string name, float s) {sno=no, sname=name, score=s;}

     void setSno(int no) {sno = no;}
     void setSname(string name) {sname = name;}
     void setScore(float s) {score = s;}

     int getSno() const {return sno;}
     string getSname() const {return sname;}
     float getScore() const {return score;}

     void readFromFile(ifstream&);
     void writeToFile(ofstream&);
     void printRecord(ostream&);
    };

    void exchange(StudentRecord &x, StudentRecord &y)
    {
       StudentRecord tmp =  x;
       x = y;
       y = tmp;
    }

    void bubble(StudentRecord a[], int n)
    {
       for (int i = 0; i < n - 1; i++)
          for (int j = n - 1; j > i; j--)
             if (a[j-1].getSname() > a[j].getSname())
                exchange(a[j-1], a[j]);
    }


    void StudentRecord::writeToFile(ofstream& BinOut)// 이진파일에 쓰기
    {
     // 채워야 할 부분
     int namelen = (int)sname.length();
     BinOut.write((char*) &sno, sizeof(int));
     BinOut.write((char*) &namelen, sizeof(int));
     BinOut.write(sname.c_str(), (std::streamsize)namelen);
     BinOut.write((char*) &score, sizeof(float));
    }

    void StudentRecord::readFromFile(ifstream& Bin)// 이진파일 읽기
    {
     // 채워야 할 부분
     int len;
     char* name = new char[20];
     Bin.read((char*) &sno, sizeof(int));
     Bin.read((char*) &len, sizeof(int));
     Bin.read((char*) name, (std::streamsize)len);
     name[len] = '\0';
     sname = name;
     Bin.read((char*) &score, sizeof(float));
    }

    void StudentRecord::printRecord(ostream&)// 화면에 출력
    {
     // 채워야 할 부분
     cout << sno <<'\t'<< sname <<'\t'<< score <<endl;
    }
    int main()
    {
     StudentRecord S[MAXSIZE], Odd[MAXSIZE], Even[MAXSIZE];
     string sname;
     int sno;
     float score;

     ifstream TextIn("student.txt", ios::in);// student 파일 오픈

     int i=0, n=0;  // 루프 변수

     // student 파일에서 레코드 읽어 배열에 저장
     while (TextIn.good() && TextIn.peek() != EOF)
     {
      TextIn >> sno >> sname >> score;
      TextIn.ignore(300, '\n');
      S[i].setSno(sno);
      S[i].setSname(sname);
      S[i].setScore(score);
      i++;
     }
     

     int numOfStudents = i;  // 읽어 들인 학생 수
     TextIn.close();

     for (n=0; n<numOfStudents; n++) // 읽어 들인 학생 레코드를 콘솔에 출력
      S[n].printRecord(cout);
     cout << endl;

     bubble(S , numOfStudents);

     ofstream BinOutOdd("studentOdd.bin", ios::out | ios::trunc | ios::binary);
     ofstream BinOutEven("studentEven.bin", ios::out | ios::trunc | ios::binary);

     int odd=0 , even=0;
     for(int j=0; j<numOfStudents; j++)
     {
      if(j%2 == 0)
      {S[j].writeToFile(BinOutOdd); odd++;}
      else
      {S[j].writeToFile(BinOutEven); even++;}
     }

     //cout << odd << even << endl;
     
     BinOutEven.close();
     BinOutOdd.close();
     // 채워야 할 부분
     // 이진 파일에 저장된 학생 레코드를 오픈하고 레코드를 읽어 콘솔에 출력
     ifstream BinOdd("studentOdd.bin");
     ifstream BinEven("studentEven.bin");

     for(n=0; n<odd; n++)// 파일에서읽기
      Odd[n].readFromFile(BinOdd);

     for(n=0; n<even; n++)// 파일에서읽기
      Even[n].readFromFile(BinEven);

     cout << "홀수파일에서 읽은 데이터" << endl;
     for (n=0; n<odd; n++) // 읽어 들인 학생 레코드를 콘솔에 출력
      Odd[n].printRecord(cout);
     cout << endl;

     cout << "짝수파일에서 읽은 데이터" << endl;
     for (n=0; n<even; n++) // 읽어 들인 학생 레코드를 콘솔에 출력
      Even[n].printRecord(cout);

     BinOdd.close();
     BinEven.close();

     return 0;
    }

Designed by Tistory.