#include<string>
#include<iostream>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<fstream>
using namespace std;
int ju = 0;
class List;
class Person {
friend class List;
friend ostream& operator<<(ostream& os, Person* k) {
os << "----------------------------" << endl;
os << "the name: " << k->name << endl;
os << "the age: " << k->age << endl;
os << "the address: " << k->address << endl;
os << "the telephone: " << k->telephone << endl;
os << "-----------------------------" << endl;
return os;
}
protected:
string name;
int age;
string address;
string telephone;
Person* next;
Person* ptr;
public:
Person(string n, int a, string aa, string t) {
name = n;
age = a;
address = aa;
telephone = t;
}
~Person() {}
virtual void print() {
cout << "----------------------------" << endl;
cout << "the name: " << this->name << endl;
cout << "the age: " << this->age << endl;
cout << "the address: " << this->address << endl;
cout << "the telephone: " << this->telephone << endl;
cout << "-----------------------------" << endl;
}
virtual void insert() {
ptr = new Person(name, age, address, telephone);
}
virtual int judge() {
return 0;
}
};
class Student :public Person {
friend class List;
friend ostream& operator<<(ostream& os, Student* p) {
os << "----------------------------" << endl;
os << "the name: " << p->name << endl;
os << "the age: " << p->age << endl;
os << "the address: " << p->address << endl;
os << "the telephone: " << p->telephone << endl;
os << "the level: " << p->level << endl;
os << "the grade_point_average: " << p->grade_point_average << endl;
os << "-----------------------------" << endl;
return os;
}
private:
int level;
float grade_point_average;
public:
Student(string s1, int a, string s2, string s3, int l, float g) :Person(s1, a, s2, s3) {
level = l;
grade_point_average = g;
}
~Student() {}
void print();
void insert();
int judge() {
return 1;
}
};
void Student::insert()
{
ptr = new Student(name, age, address, telephone, level, grade_point_average);
}
void Student::print() {
cout << "----------------------------" << endl;
cout << "the name: " << this->name << endl;
cout << "the age: " << this->age << endl;
cout << "the address: " << this->address << endl;
cout << "the telephone: " << this->telephone << endl;
cout << "the level: " << this->level;
cout << "the grade_point_average: " << this->grade_point_average << endl;
cout << "-----------------------------" << endl;
}
class Teacher :public Person {
friend class List;
friend ostream& operator<<(ostream& os, Teacher* p) {
os << "----------------------------" << endl;
os << "the name: " << p->name << endl;
os << "the age: " << p->age << endl;
os << "the address: " << p->address << endl;
os << "the telephone: " << p->telephone << endl;
os << "the salary: " << p->salary << endl;
os << "-----------------------------" << endl;
return os;
}
private:
float salary;
public:
Teacher(string s1, int a, string s2, string s3, float s) :Person(s1, a, s2, s3) {
salary = s;
}
~Teacher() {}
void print();
void insert();
int judge() {
return 2;
}
};
void Teacher::insert() {
ptr = new Teacher(name, age, address, telephone, salary);
}
void Teacher::print() {
cout << "----------------------------" << endl;
cout << "the name: " << this->name << endl;
cout << "the age: " << this->age << endl;
cout << "the address: " << this->address << endl;
cout << "the telephone: " << this->telephone << endl;
cout << "the salary: " << this->salary << endl;
cout << "-----------------------------" << endl;
}
class Staff :public Person {
friend class List;
friend ostream& operator<<(ostream& os, Staff* p) {
os << "----------------------------" << endl;
os << "the name: " <<p->name << endl;
os << "the age: " << p->age << endl;
os << "the address: " << p->address << endl;
os << "the telephone: " << p->telephone << endl;
os << "the hourly_wages: " << p->hourly_wages << endl;
os << "-----------------------------" << endl;
return os;
}
private:
float hourly_wages;
public:
Staff(string s1, int a, string s2, string s3, float h) :Person(s1, a, s2, s3) {
hourly_wages = h;
}
~Staff() {}
void print();
void insert();
int judge() {
return 3;
}
};
void Staff::insert() {
ptr = new Staff(name, age, address, telephone, hourly_wages);
}
void Staff::print() {
cout << "----------------------------" << endl;
cout << "the name: " << this->name << endl;
cout << "the age: " << this->age << endl;
cout << "the address: " << this->address << endl;
cout << "the telephone: " << this->telephone << endl;
cout << "the hourly_wages: " << this->hourly_wages << endl;
cout << "-----------------------------" << endl;
}
class List {
private:
Person* root;
public:
List() {
root = NULL;
}
~List() {}
void insert_list(Person* node);
void print_list()const;
void readfiles();
void writefiles();
};
void List::insert_list(Person* node) {
string s = node->name;
Person* middle = root;
Person* previous = NULL;
while (middle && s < middle->name) {
previous = middle;
middle = middle->next;
}
node->insert();
node->ptr->next = middle;
if (previous == NULL) {
root = node->ptr;
}
else
previous->next = node->ptr;
}
void List::print_list()const {
Person* r = root;
while (r) {
cout << r;
r = r->next;
}
}
void List::readfiles()
{
cout << "请输入读取的文件名\n";
string ss;
cin >> ss;
ifstream InFiles;
InFiles.open(ss.c_str());
if (!InFiles.is_open()) {
cout << "文件不存在\n";
exit(EXIT_FAILURE);
}
else {
while (!InFiles.eof()) {
char ch;
InFiles >> ch;
cout << ch << endl;
if (ch == 'S') {
string a;int b;string c, d;int f; float e=0.0;
InFiles >> a >> c >> d >> b >> e >> f;
if (InFiles.fail() == true) {
cout << "发现输入流挂起,这一整行数据被忽略\n";
InFiles.clear();InFiles.ignore(1024, '\n');
}
else {
Student* middle = new Student(a, b, c, d, f, e);
this->insert_list(middle);
InFiles.get();
}
}
else if (ch == 'E') {
string a; int b; string c, d; float e;
InFiles >> a >> c >> d >> b >> e;
if (InFiles.fail() == true) {
cout << "发现输入流挂起,这一整行数据被忽略\n";
InFiles.clear();InFiles.ignore(1024, '\n');
}
else {
Staff* middle = new Staff(a, b, c, d, e);
this->insert_list(middle);
InFiles.get();
}
}
else if (ch == 'T') {
string a; int b; string c, d; float e;
InFiles >> a >> c >> d >> b >> e;
if (InFiles.fail() == true) {
cout << "发现输入流挂起,这一整行数据被忽略\n";
InFiles.clear();InFiles.ignore(1024, '\n');
}
else {
Teacher* middle = new Teacher(a, b, c, d, e);
this->insert_list(middle);
InFiles.get();
}
}
else if (ch == 'P') {
string a; int b; string c, d;
InFiles >> a >> c >> d >> b;
if (InFiles.fail() == true) {
cout << "发现输入流挂起,这一整行数据被忽略\n";
InFiles.clear();InFiles.ignore(1024, '\n');
}
else {
Person* middle = new Person(a, b, c, d);
this->insert_list(middle);
InFiles.get();
}
}
else {
cout << "发现不匹配类型,这一整行数据被忽略\n";
InFiles.ignore(1024,'\n');
}
}
}
InFiles.close();
}
void List::writefiles()
{
cout << "请输入要写入的文件名\n";
string s; cin >> s;
ofstream OutFiles(s);
if (!OutFiles.is_open()) {
cout << "文件无法打开\n";
exit(EXIT_FAILURE);
}
else {
Person* h = root;
while (h) {
if (h->judge() == 1) {
Student* pr = (Student*)h;
OutFiles << pr;
}
else if (h->judge() == 2) {
Teacher* pr = (Teacher*)h;
OutFiles << pr;
}
else if (h->judge() == 3) {
Staff* pr = (Staff*)h;
OutFiles << pr;
}
else if (h->judge() == 0) {
OutFiles << h;
}
h = h->next;
}
}
OutFiles.close();
}
int main()
{
List people;
people.readfiles();
people.readfiles();
people.writefiles();
system("pause");
}
```cpp
自己做的一个小型异质链表加文件的读写(BY期末大作业)
最新推荐文章于 2021-09-30 19:01:55 发布