64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <conio.h>
using namespace std;
class Student
{
private:
string m_name;
int m_age;
double m_mark;
string m_address;
string m_number;
public:
Student(string name = "#", int age = 0, double mark = 100, string add = "#", string number = "#");
Student(Student &std)
{
m_name = std.m_name;
m_age = std.m_age;
m_mark = std.m_mark;
m_address = std.m_address;
m_number = std.m_number;
}
void FileRead(ofstream &file) const;
void FileWrite(ifstream &file) const;
friend ostream &operator<<(ostream &dist, Student &std);
friend istream &operator >> (istream &sour, Student &std);
bool operator>(Student &std);
bool operator==(Student &std);
};
Student::Student(string name , int age , double mark , string add , string number)
{
m_name = name;
m_age = age;
m_mark = mark;
m_address = add;
m_number = number;
}
bool Student::operator>(Student &std)
{
return m_mark > std.m_mark;
}
bool Student::operator==(Student &std)
{
return m_mark == std.m_mark;
}
ostream &operator<<(ostream &dist, Student &std)
{
cout << "学生姓名:" << '\t' << std.m_name << endl;
cout << "学生年龄:" << '\t' << std.m_age << endl;
cout << "学生成绩:" << '\t' << std.m_mark << endl;
cout << "学生住址:" << '\t' << std.m_address << endl;
cout << "学生学号:" << '\t' << std.m_number << endl;
return dist;
}
void Student::FileRead(ofstream &dist) const
{
dist.write(m_name.c_str(), sizeof(m_name));
dist.write((char *)&m_age, sizeof(int));
dist.write((char *)&m_mark, sizeof(int));
dist.write(m_address.c_str(), sizeof(m_address));
dist.write(m_number.c_str(), sizeof(m_number));
}//由于编译器不支持c99标准,因此调整书上的方法
istream &operator >> (istream &sour, Student &std)
{
cout << "请输入学生姓名" << endl;
cin >> std.m_name;
cout << "请输入学生年龄" << endl;
cin >> std.m_age;
cout << "请输入学生成绩" << endl;
cin >> std.m_mark;
cout << "请输入学生住址" << endl;
cin >> std.m_address;
cout << "请输入学生学号" << endl;
cin >> std.m_number;
return sour;
}
void Student::FileWrite(ifstream &sour)const
{
sour.read((char *)&m_name, sizeof(m_name));
sour.read((char *)&m_age, sizeof(int));
sour.read((char *)&m_mark, sizeof(m_mark));
sour.read((char *)&m_address, sizeof(m_address));
sour.read((char *)&m_number, sizeof(m_number));
}
template<typename T>class Array
{
private:
T *m_element;
int m_maxsize;
int m_last;
ifstream m_fin;
ofstream m_fout;
public:
Array(int max = 3);
~Array();
void Ordinsert(T &std);
bool IsFull(void);
void Renews(void);
void ShowInfo(void);
};
template<typename T>Array<T>::Array(int max)
{
m_element = new T[3];
assert(m_element != NULL);
m_maxsize = max;
m_last = -1;
m_fout.open("save info.data",ios::binary);
if (!m_fout)
{
cout << "文件创建失败,退出程序" << endl;
m_fout.close();
m_fout.clear();
system("pause");
exit(0);
}
else
{
while (!m_fout.eof())//这里无法读取到文件尾
{
T temp;
temp.FileRead(m_fout);
if(m_fout.eof()==0)
Ordinsert(temp);
}
m_fout.close();
m_fout.clear();
}
}
template<typename T>Array<T>::~Array()
{
int i;
m_fin.open("save info.data",ios::binary);
if (!m_fin)
{
cout << "找不到文件,退出程序" << endl;
m_fin.close();
m_fin.clear();
system("pause");
exit(0);
}
else
{
for (i = 0; i <= m_last; i++)
{
m_element[i].FileWrite(m_fin);
cout << "存储成功" << endl;
}
delete[]m_element;
m_fin.close();
}
}
template<typename T>bool Array<T>::IsFull(void)
{
return m_last == m_maxsize - 1;
}
template<typename T>void Array<T>::Renews(void)
{
int i;
T *temp = m_element;
m_element = new Student[2 * m_maxsize];
assert(temp != NULL);
m_maxsize = m_maxsize * 2;
for (i = 0; i <= m_last; i++)
{
m_element[i] = temp[i];
}
delete[]temp;
}
template<typename T>void Array<T>::Ordinsert(T &std)
{
int i, j;
if (IsFull())
{
Renews();
}
for (i = 0; i <= m_last; i++)
{
if (m_element[i] > std)
break;
}
if (i == m_last)
{
m_element[++m_last] = std;
}
else
{
for (j = i + 1; j <= m_last; j++)
{
m_element[j] = m_element[j - 1];
}
m_element[i] = std;
m_last++;
}
}
template<typename T>void Array<T>::ShowInfo(void)
{
int i;
cout << "现已登记的学生信息" << endl;
for (i = 0; i <= m_last; i++)
{
cout << m_element[i];
cout << endl;
}
}
int main(void)
{
Array<Student> mylist;
Student student;
char option;
cout << "是否登记新学生信息?Y or N" << endl;
option = _getch();
while (1)
{
if (option == 'Y')
{
cin >> student;
mylist.Ordinsert(student);
cin.ignore(1024, '\n');
cout << "是否继续输入?Y or N" << endl;
option = _getch();
}
else
{
mylist.ShowInfo();
cout << "退出程序" << endl;
break;
}
}
mylist.~Array();
system("pause");
return 0;
}