415改编后的日记系统可读版
2018-02-26 11:13
92 查看
#include"stdafx.h" #include<iostream> #include<string> #include<fstream> using namespace std; struct Diary { string date; string weather; string feeling; string substance; bool operator== (const Diary &x) { return date == x.date; } }; ostream & operator << (ostream& os, const Diary & x) { os << "日期:" << x.date << endl; os << "天气:" << x.weather << endl; os << "心情:" << x.feeling << endl; os << "内容:" << x.substance << endl; os << endl; return os; } 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()) 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<Diary> 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; Diary 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.date >> x.weather >> x.feeling >> x.substance; list.push_front(x); } cout << "日记已经插入完毕!" << endl; } else { for (int i = 1; i <= num; i++) { cout << "请依次输入日记的日期,天气,心情,内容!" << endl; cin >> x.date >> x.weather >> x.feeling >> x.substance; list.push_back(x); } cout << "日记已经插入完毕" << endl; } if (flag1 == false) flag1 = true; cout << endl; } else if (m == 2) { int i; Diary y; cout << "请输入要删除的日记的日期" << endl; cin >> y.date; 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; 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; Diary z; string num; bool flag2 = false; cout << "请输入你想查询的日记的日期!" << endl; cin >> num; for (i = 1; i <= list.size(); ++i) { z = list.getLocate(i); if (z.date == num) { flag2 = true; break; } } if (flag2) { cout << "该日记信息存在日记表!该日记信息如下:" << endl; cout <<"日期:"<< z.date << endl; cout <<"天气:"<< z.weather << endl; cout <<"心情:"<< z.feeling << endl; cout <<"内容:"<< z.substance << 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; } } }
相关文章推荐
- 415改编的日记系统(不可以读版)
- 知识管理系统Data Solution研发日记之十五 数据存储技术 LLBL Gen ORM + Db4o
- 【机器学习小白日记】Win10+Ubuntu16.04双系统安装
- 基于 SquashFS 构建 Linux 可读写文件系统
- 以可读写可追加的方式显示系统时间
- 基于 Laravel 开发 社群系统ThinkSNS+ 中前端的抉择(webpack/Vue)踩坑日记
- 新申请的blog 第一天 把自己以前开发的一个系统的开发日记贴上来 见笑了
- 马尔可夫模型日记——如何模拟真正的概率系统
- ThinkPHP内容管理系统开发日记(二)--本地环境
- ThinkPHP内容管理系统开发日记(四)-- 配置信息与开发Longin模块以及模板
- 基于 SquashFS 构建 Linux 可读写文件系统
- 挂载android只读文件系统为可读写
- 基于 SquashFS 构建 Linux 可读写文件系统
- 系统升级日记(2)- 升级到SharePoint Server 2013
- 银行系统改编版
- 我的材质系统---RenderScript的设计日记 (2005-8-08)
- Windows Server 2008 r2系统安全学习日记 — shadow security scanner
- ThinkPHP内容管理系统开发日记(一)--规划数据库。
- 知识管理系统Data Solution研发日记之十一 数据绑定技术的应用