您的位置:首页 > 其它

415链表联系(日记)

2018-02-26 11:12 141 查看
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct Student
{
string number;
string name;
string dob;
string sex;
string condition;

bool operator == (const Student & x)
{
return number == x.number;
}
//==操作符重载
};

ostream & operator << (ostream & os, const Student & x)        //流对象是不能复制的,所以必须传引用或者指针   第二个是指Student对象x的引用
{
os << "学号:" << x.number << endl;
os << "姓名: " << x.name << endl;
os << "出生日期: " << x.dob << endl;
os << "性别:" << x.sex << endl;
os << "身体状况:" << x.condition << endl;
os << endl;
return os;
}
/*重载输出“<<”符号,方便Student结构体的输出*/

template <typename Object>             //申明一个类函数
class List
{
private:
struct Node
{
Object data;          //数据
Node * next;         //后指针
Node * prev;       //前指针

Node(const Object & d = Object(), Node *p = NULL, Node *n = NULL) : data(d), prev(p), next(n) { }
};
//私有内嵌结构体,包括数据,前指针,后指针。

public:
List()
{
init();
}
//List构造函数,调用私有函数init生成一个默认的链表,有头结点,尾结点。
~List()
{
clear();
delete head;
delete tail;
}
//析构函数
void clear()
{
Node * p = begin();
Node * q;
while (p != end())
{
q = p;
p = p->next;
delete q;
}
theSize = 0;
}
//将链表里面的元素全部删除。
int size() const
{
return theSize;
}
//返回链表的长度。
bool empty()
{
return size() == 0;
}
//判断链表是否为空。
Node * locate(int i)        //定位函数  返回指针地址
{
if (i < 0 || i > size())
return NULL;
else
{
Node * p = head;
for (int j = 1; j <= i; ++j)
{
p = p->next;
}
return p;
}
}
//返回第i个结点。
Node * begin()
{
return head->next;
}
//返回第一个结点。
Node * end()
{
return tail;
}
//返回尾结点。
Object getLocate(int i)    //定位函数 返回元素
{
Node *p = locate(i);
return p->data;
}
//返回第i个结点储存的元素。
void push_front(const Object & x)
{
insert(begin(), x);
}
//头插入。
void push_back(const Object & x)
{
insert(end(), x);
}
//尾插入。
void insert(Node * p, const Object & x)
{
p->prev = p->prev->next = new Node(x, p->prev, p);
theSize++;
}
//在p结点前插入新的结点,其元素为x。
int search(const Object & x)
{
int i;
Node * p = begin();
for (i = 1; i <= size(); ++i)
{
if (p->data == x)
{
break;
}
p = p->next;
}
/*if( i <= size( ))
{
cout << "该学号的学生的信息存在健康表!" << endl;
}
else
cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;*/
if (i <= size())
return i;
else
return -1;
}
//查找元素x在链表的位置,如果不存在,返回-1
void remove(int i)
{
Node * p = locate(i);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
theSize--;
}
//删除第i个结点。
void output()
{
Node * p = begin();
for (int i = 1; i <= size(); ++i)
{
cout << p->data;
p = p->next;
}
cout << endl;
}
//将链表依次输出。
private:
int theSize;                                //链表的长度。
Node * head;                                //头结点。
Node * tail;                                //尾结点。

void init()
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}                                           //生成默认链表。
};

int main()
{
bool flag = false;
bool flag1 = false;
List<Student>  list;
cout << "---------------------------欢迎使用学生健康管理系统---------------------------" << endl;
cout << "健康表已经自动建立!" << endl;
int m, m1;
while (1)
{
cout << "请选择需要的操作!" << endl;
cout << "1:插入学生信息。 2:删除学生信息。 3:从文件读入信息。" << endl;
cout << "4:向文件写入信息。 5:查询。 6:输出全部信息。其他:退出。" << endl;
cout << endl;

cin >> m;

if (m == 1)
{
int num;
Student x;
cout << "请选择学生信息插入的方式:" << endl;
cout << "1:头插入。 2:尾插入。" << endl;
cin >> m1;
cout << "请输入插入学生信息的数目:" << endl;
cin >> num;
if (m1 == 1)
{
for (int i = 1; i <= num; ++i)
{
cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
list.push_front(x);
}
cout << "信息已经插入完毕!" << endl;
}
else
{
for (int i = 1; i <= num; ++i)
{
cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
list.push_back(x);
}
cout << "信息已经插入完毕!" << endl;
}
if (flag1 == false)
flag1 = true;
cout << endl;
}

else if (m == 2)
{
int i;
Student y;
cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;
i = list.search(y);
if (i > -1 && i <= list.size())
{
list.remove(i);
cout << "删除学生及其信息成功!" << endl;
}
else
cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
cout << endl;
}

else if (m == 3)
{
if (!flag)
cout << "你还没有向文件写入学生健康信息!请先写入学生健康信息后再读取" << endl;
else
{
cout << "--------------------读取开始!--------------------" << endl;
ifstream read("list.txt");
char s;
while (read.get(s))
cout << s;
cout << "--------------------读取结束!--------------------" << endl;
read.close();
}
cout << endl;
}

else if (m == 4)
{
string select;
cout << "是否已经将需要记录的学生全部记录?(该操作是依次将健康表里面的全部信息写入文件里)" << endl;
cout << "如果选择是就输入“Y”;否则输入“N”,继续将学生健康信息插入健康表里。" << endl;
cin >> select;

if (select == "Y")
{
ofstream write("list.txt");
for (int i = 1; i <= list.size(); ++i)
{
write << list.getLocate(i);
}
flag = true;
write.close();
}
else
{
cout << "请继续插入或删除操作!" << endl;
}
cout << endl;
}

else if (m == 5)
{
int i;
Student z;
string num;
bool flag2 = false;
cout << "请输入你想查询的学生的学号!" << endl;
cin >> num;
for (i = 1; i <= list.size(); ++i)
{
z = list.getLocate(i);
if (z.number == num)
{
flag2 = true;
break;
}
}
if (flag2)
cout << "该学号的学生的信息存在健康表!" << endl;
else
cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;

cout << endl;
}

else if (m == 6)
{
if (flag1)
{
cout << "输出健康表中全部学生的信息!" << endl;
list.output();
}
else
{
cout << "健康表为空,请先插入学生信息!" << endl;
}
cout << endl;
}

else
{
cout << "---------------------------谢谢使用学生健康管理系统---------------------------" << endl;
break;
}
}
}

20170405 晴天 nice 我去吃饭了
20170406 雨天 bad 我去睡觉了
20170407 白天 cool 我去跑步了

else if (m == 3)
{
if (!flag)
cout << "您还没有向文件写入日记信息!请先写入" << endl;
else
{
cout << "------------------------读取开始!------------------------" << endl;
ifstream read("list.txt");
char s;
while (read.get(s))
cout << s;
cout << "------------------------读取结束!------------------------" << endl;
read.close();
}
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: