정리필요2

아씹...못끝냈어....

ShineWithMe 2008. 9. 1. 23:41

#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)// 이진파일에 쓰기
{
 // 채워야 할 부분
 BinOut.write((char*) &sno, sizeof(int));
 BinOut.write(sname.c_str(), MAXNAME);
 BinOut.write((char*) &score, sizeof(float));
}

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

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(Type *data, int n);
};

template <class Type>
void Sort<Type>::bubble(Type *data, int n)
{
 int i=0, j=0;
 for(i=0; i<n; i++)
  for(j=0; j<n; j++)
   if(data[i] < data[j])
    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;
 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);

 KeyIndex K[MAXSIZE];

 for(int k=0; k<i; k++)
 {
  S[k].readFromFile(binin);
  S[k].printRecord(cout);
 }
 binin.close(); // 텍스트파일을 읽어 이진파일로 저장, 출력


 for(int m=0; m<i; m++)
 {
  K[m].Set(S[m].getSno, m);
 }

 Sort<KeyIndex> sort;
 sort.bubble(K, i)


 textin.clear();
 textin.seekg(0, ios::beg);

 return 0;
}