415改编的日记系统(不可以读版)
2018-02-26 13:05
127 查看
#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; 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; 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; 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改编后的日记系统可读版
- 社交系统ThinkSNS+研发日记四:如何做到 Laravel 配置可以网站后台配置
- android系统下可以实时语音对讲的软件应用。
- 编写一个学生信息管理系统:学生信息包括有(学生学号、姓名、性别、手机号码),系统以学生对象数组的方式存储学生信息 (初始数组为10个元素,每当数组存储满时,需要进行扩容,规则可以为翻倍也可以固定增长
- 《可以量化的经济学》系统思考
- Windows Vista系统实用命令行大全(实验证明在windows 7下一样可以使用)
- 系统思考——《可以量化的经济学》
- shellexec、exec、shellexecute以及批处理cmd中可以得到的系统值
- 关于 Overtrue 的拼音库 overtrue/pinyin 为何 travis 为 error【社交系统研发日记十】
- C++下个标准,可以考虑把GC系统加进去
- 日记 [2006年12月29日]终于可以YUM拉
- Linux就这个范儿 第16章 谁都可以从头再来--从头开始编译一套Linux系统 nsswitch.conf配置文件
- fsarchiver创建系统镜像(dd命令也可以)
- 【ITOO】--迎新系统,你可以做的更好
- SAP 断线后,若系统提示程序正在被编辑,可以使用T-CODE:SM12
- EditText禁用系统键盘,光标可以继续使用
- Windows环境下,输入(Chkntfs /X C:)命令可以取消系统每次启动对C盘的磁盘扫描程序
- ubuntu有线无法上网(双系统下windows可以)--可以连接无法上网
- MonoRail学习笔记十八:在VM中可以使用哪些系统变量
- python可以调用外部系统命令