정리필요2

실습 6

ShineWithMe 2008. 9. 1. 23:35

냥냥...


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

const int MAXNAME = 20;
const int MAXSIZE = 10;

class StudentRecord
{
private:
 int sno;
 string sname; // 또는 char sname[MAXNAME] 사용
 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&);
 int size();
};
void StudentRecord::writeToFile(ofstream& BinOut)// 이진파일에 쓰기
{
 // 고정길이 레코드(28byte)로 이진파일에 쓰기
 BinOut.write((char*) &sno, sizeof(int)); //고정길이 4
 BinOut.write(sname.c_str(), MAXNAME);  //고정길이 20
 BinOut.write((char*) &score, sizeof(float));//고정길이 4
}

void StudentRecord::readFromFile(ifstream& Bin)// 이진파일 읽기
{
 // 고정길이 레코드로 이진파일 읽기
 char *name = new char[MAXNAME];
 Bin.read((char*) &sno, sizeof(int)); //고정길이 4
 Bin.read((char*) name, MAXNAME);  //고정길이 20
 sname = name;
 Bin.read((char*) &score, sizeof(float));//고정길이 4
}

void StudentRecord::printRecord(ostream&)// 화면에 출력
{
 // 채워야 할 부분
 cout << sno <<'\t'<< sname <<'\t'<< score <<endl;
}

int StudentRecord::size()
{
 return sizeof(sno) + MAXNAME + sizeof(score);
}

class KeyIndex
{
private:
 int key;
 int RRN;
public:
 KeyIndex() {;}
 KeyIndex(int k, int r) {key = k, RRN = r;}
 void Set(int k, int r) {key = k, RRN = r;}
 int getKey() const {return key;}
 int getRRN() const {return RRN;}
};


template <class Type>
class Sort
{
private:
 void exchange(Type &a, Type &b);
public:
 void bubble(KeyIndex *data, int n);
};

template <class Type> //버블소트로 인덱스정렬
void Sort<Type>::bubble(KeyIndex *data, int n)
{

 int i=0, j=0;
  for(i=0; i<n; i++)
   for(j=0; j<n; j++)
    if(data[i].getKey() < data[j].getKey())
     exchange(data[i], data[j]);
}

template <class Type> // 교환함수
void Sort<Type>::exchange(Type &a, Type &b)
{
 Type c;
 c=a;
 a=b;
 b=c;
}


int main(int argc, char* argv[])
{
 StudentRecord S[MAXSIZE], R[MAXSIZE];
 char sname[20]; // 레코드저장할임시변수
 int sno;
 float score;

 // 루프 변수

 ifstream textin(argv[1]);

 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++;
 }

 if(!textin.is_open())
 {
  cout << "파일을 열수 없습니다. " << endl;
  return 0;
 }

 ofstream binout("binout.bin", ios::binary);
 for(int j=0; j<i; j++)
  S[j].writeToFile(binout);
 binout.close();
 // 이진파일 생성했다.

 ifstream binin("binout.bin", ios::binary);

 cout << "이진파일로 바꾼내용을 출력" << endl;
 for(int k=0; k<i; k++)
 {
  R[k].readFromFile(binin);
  R[k].printRecord(cout);
 }
 binin.close(); // 이진파일 출력

 KeyIndex *K = new KeyIndex[i]; // 인덱스정렬 할 배열

 for(int m=0; m<i; m++)
 {
  K[m].Set(R[m].getSno(), m); // 키필드와 RRN을 인덱스 배열에 넣기
 }

 Sort<KeyIndex> sort;
 sort.bubble(K, i); // 인덱스 배열정렬
 
 ifstream bintest("binout.bin", ios::binary);
 StudentRecord *TEST = new StudentRecord[i];
 for(int k=0; k<i; k++)
  TEST[k].readFromFile(bintest);
 
 cout << "정렬된 인덱스배열을 이용하여 파일의 레코드 출력" << endl;
 for(int x=0; x<i; x++)
 {
  cout << TEST[K[x].getRRN()].getSno() << '\t'<< TEST[K[x].getRRN()].getSname() << '\t'
   << TEST[K[x].getRRN()].getScore() << endl;
 }

 cout << "레코드길이 : " << TEST->size() << endl; // 레코드 길이 시험 출력

 bintest.close();

 //delete TEST;
 delete K;
 return 0;
}