1.问题引入
定义会议信息类 class Conference
会议信息包括:会议编号,会议名称、部门名称、会议地点,会议日期,会议主持人,会议记录人,出席人员,会议摘要。会议信息以二进制形式存储在文件中。
要求功能:1.添加会议
2.浏览会议记录
3.查询会议
4.修改会议
5.删除会议
2.问题分析
这个题的重点是如何设计Coference类,怎么样来定义它的成员函数实现功能并在主函数中调用和怎么将会议记录用二进制存入文件中。除此之外还要有一个菜单函数来进行展示功能。
设计Coference类,先要将题目所要求的会议信息在私有成员中用变量定义,方便信息输入和储存,然后要在保护成员中申明实现功能的成员函数,成员函数要确定其返回类型和传入变量的类型,并且由于要查询和浏览所以需要一个容器来储存每次会议的记录,数组的局限性过大,所以我采用链表来进行储存。接着我将在代码中来详细讲解。
3.代码实现
总共三个文件
3.1.Coference类的定义
#pragma once
#include <iostream>
#include <fstream>
#include <cstring>
#include<iomanip>
using namespace std;
string filename = "C:\\code\\text.txt";//命名储存信息的文件
class Conference
{
public:
void get()//获取信息函数功能与构造函数类似
{
cout << "请输入会议编号:" << endl;
cin >> id;
cout << "请输入会议名称:" << endl;
cin >> conname;
cout << "请输入部门名称:" << endl;
cin >> departmentname;
cout << "请输入会议地点:" << endl;
cin >> place;
cout << "请输入会议日期:" << endl;
cin >> date;
cout << "请输入会议主持人:" << endl;
cin >> host;
cout << "请输入会议记录人:" << endl;
cin >> recorder;
cout << "请输入出席人员:" << endl;
cin >> member;
cout << "请输入会议摘要:" << endl;
cin >> abstract;
}
void display()//打印函数
{
cout << "会议编号:" << setw(12) << id << endl;
cout << "会议名称:" << setw(12) << conname << endl;
cout << "部门名称:" << setw(12) << departmentname << endl;
cout << "会议地点:" << setw(12) << place << endl;
cout << "会议日期:" << setw(12) << date << endl;
cout << "会议主持人:" << setw(12) << host << endl;
cout << "会议记录员;" << setw(12) << recorder << endl;
cout << "会议出席人员;" << setw(12) << member << endl;
cout << "会议摘要:" << setw(12) << abstract << endl;
}
~Conference();//析构函数,用来释放指针空间
void input();//将信息存储到文件中
void add();//添加会议记录函数
void look();//浏览会议记录函数
int menu();//菜单函数(查找函数使用的)
void find();//查找函数
void edit();//修改函数
void delect();//删除函数
private:
string id;//会议编号
string conname;//会议名称
string departmentname;//部门名称
string place;//会议地点
string date;//会议日期
string host;//会议主持人
string recorder;//会议记录人
string member;//出席人员
string abstract;//会议摘要
Conference *head = NULL;//头指针
Conference *next = NULL;
};
3.2.菜单函数的定义
我们用不同的数字来区分系统的不同功能,用一个整形变量来接收这个数字。
int menu()
{
int c = 0;//用来接收数据
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ 会 议 记 录 系 统 ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 1.添加会议记录 ************* " << endl;
cout << " " << endl;
cout << " ************* 2.浏览会议记录 ************* " << endl;
cout << " " << endl;
cout << " ************* 3.查询会议记录 ************* " << endl;
cout << " " << endl;
cout << " ************* 4.修改会议记录 ************* " << endl;
cout << " " << endl;
cout << " ************* 5.删除会议记录 ************* " << endl;
cout << " " << endl;
cout << " ************* 0.退出系统 ************* " << endl;
cin >> c;
return c;
}
它的运行成图为:
3.3.主函数的定义
在主函数中我们需要接收菜单函数中得到的数据,进而实现相应的功能。
int main()
{
Conference *C = new Conference();//创造一个指针并给予空间
int a = 0;//用来接收输入数字的变量
while(1)//无限循环
{
a = menu();//将菜单函数所接收的数字赋值给a
switch (a)//分情况运行功能
{
case 1:
C->add();
break;
case 2:
C->look();
break;
case 3:
C->find();
break;
case 4:
C->edit();
break;
case 5:
C->delect();
break;
case 0:
exit(1);
default:
break;
}
}
delete C;//释放空间
return 0;
}
3.4.成员函数的定义
针对类中各个成员函数的声明,我们需要对其进行定义来实现问题中所需要的功能。
3.4.1析构函数
析构函数的作用便是释放空间,释放我们在私用成员里的两个指针的空间,它的定义是这样的:
Conference::~Conference()
{
Conference *temp;
while (head != NULL)
{
temp = head;
head = head->next;
delete temp;
}
}
3.4.2文件存储的函数
本问题中要求我们以二进制的形式存入信息到文件中,所以我们要使用binary这个编码:
inline void Conference::input()
{
ofstream fout(filename,ios::binary);//以二进制形式打开文件
if(!fout)
{
cout << "File cannot be opened.";
return;
}//判断文件是否为空
get();//接收信息配合add函数使用
fout.write(reinterpret_cast<const char *>(this), sizeof(Conference));//存入信息
fout.close();//关闭文件
}
3.4.3添加函数
添加会议记录并将其存入文件中并能在后续进行浏览,我们主要就是通过链表的方式来存入会议记录,代码如下:
inline void Conference::add()
{
Conference *c = this;//用新指针来接收this指针
Conference *con;
con = new Conference();//开辟空间
while (true)
{
if (c->next == NULL)
break;
c = c->next; // 如果当前指针的下一个指针为空,则跳出循环,否则将当前指针指向下一个指针
}
//将新创建的conference对象的地址赋给c的next指针
c->next = con; // 将当前指针的下一个指针指向新创建的指针
con->input();//调用新指针的input函数,将信息存入文件中
}
3.4.4浏览函数
将所有的会议记录打印出来:
void Conference::look()
{
//遍历整个链表,将所有会议记录打印出来
Conference *c = this->next;
cout << "所有会议记录为:" << endl;
while (c!=NULL)
{
c->display();//将会议记录打印
c = c->next;
}
}
3.4.5寻找函数
查找特定的会议记录,能通过多种方式进行查找,如会议编号等,所以我们可以定义一个查找的菜单函数,用接收的数字来分类进行查找,并将查找结果用打印函数打印出来,这里我们用编号查找和名称查找来示范。
首先我们来设计一下这个菜单函数,它的结构和主函数的菜单函数大致相同:
int Conference::menu()
{
int c = 0;//定义一个变量来接收数字
cout << " ************* 1.会议编号 ************* " << endl;
cout << " " << endl;
cout << " ************* 2.会议名称 ************* " << endl;
cout << " " << endl;
cout << " ************* 3.部门名称 ************* " << endl;
cout << " " << endl;
cout << " ************* 4.会议地点 ************* " << endl;
cout << " " << endl;
cout << " ************* 5.会议日期 ************* " << endl;
cout << " " << endl;
cout << " ************* 6.会议主持人 ************* " << endl;
cout << " " << endl;
cout << " ************* 7.会议记录人 ************* " << endl;
cout << " " << endl;
cout << " ************* 8.会议成员 ************* " << endl;
cout << " " << endl;
cout << " ************* 9.会议摘要 ************* " << endl;
cout << " " << endl;
cout << " ************* 0.退出 ************* " << endl;
cin >> c;
return c;
}
接着我们来根据不同数字来进行分类查找,这里可以用which-case的结构:
void Conference::find()
{
// 定义一个字符串变量s,用于存储用户输入的查询信息
string s;
// 定义一个整型变量a,用于存储用户输入的查询类型
int a = 0;
cout<< "输入查询的信息:" << endl;
// 调用menu()函数,获取用户输入的查询类型
a = menu();
// 定义一个指向Conference对象的指针c,指向当前对象的下一个对象
Conference *c = this->next;
cout << "请输入:" << endl;
// 接收用户输入的查询关键字
cin >> s;
cout << "输出查询结果:" << endl;
// 根据用户输入的查询类型(数字),进行不同的查询操作
switch (a)
{
// 根据会议ID查询
case 1:
// 遍历链表,查找与用户输入的会议ID相同的对象
while(c!=NULL)
{
// 如果找到,调用display()函数,输出该对象的信息
if(c->id==s)
{
c->display();
// 跳出循环
break;
}
// 如果没有找到,输出提示信息
else if(c->next==NULL){
cout << "\t\t没有找到您需要的会议!" << endl;
}
// 将指针c指向下一个对象
c = c->next;
}
// 跳出switch语句
break;
// 根据会议名称查询
case 2:
// 遍历链表,查找与用户输入的会议名称相同的对象
while (c!=NULL)
{
// 如果找到,调用display()函数,输出该对象的信息
if(c->conname==s)
{
c->display();
// 跳出循环
break;
}
// 如果没有找到,输出提示信息
else if (c->next == NULL)
{
cout << "\t\t没有找到您需要的会议!" << endl;
}
// 将指针c指向下一个对象
c = c->next;
}
// 跳出switch语句
break;
// 默认情况,不进行查询操作
default:
break;
}
}
其他的信息查询以此类推。
3.4.6修改函数
修改函数的实现主要在于能准确找到要修改的记录并将其进行修改:
void Conference::edit()
{
string s;
string str;
// 将当前对象的地址赋值给指针c
Conference *c = this;
int a = 0;
// 调用find()函数
find();
// 输出提示信息
cout << "请输入想修改的信息:" << endl;
// 调用menu()函数并赋值给a
a = menu();
// 输入字符串变量str
cin >> str;
// 根据a的值进行switch语句
switch (a)
{
// 如果a等于1,将str的值赋给c的id成员变量
case 1:
c->id = str;
break;
// 如果a等于2,将str的值赋给c的conname成员变量
case 2:
c->conname = str;
break;
// 否则不执行任何操作
default:
break;
}
}
3.4.7删除函数
通过查询找到要删除信息进而来对此会议记录进行删除(这里我们以编号查询举例):
string s;
Conference *c = this;
cout << "请输入要删除的会议记录:" << endl;
cin >> s;
while (c != NULL)
{
if (c->id == s)
{
if (c->head != NULL)
c->head = c->head->next;
if (c->next != NULL)
c->next = c->head->next;
return;
}
c = c->next;
}
通过这样就可以进行删除了
4总结与分析
设计的程序整体能实现题目所要求的功能,尽量避免了重复冗长,但是还是存在了诸多问题,如存储信息还不能很好的存入文字信息,修改和删除也不是很灵敏,有些麻烦,文件的存入也有些问题,欢迎各位大佬莅临指导,谢谢大家。