C语言函数比较多且丰富,全部记住是不可能的,请设计一个C语言函数查询系统帮助大家快速熟悉和掌握这些函数的用途和使用方法。
要求设计的类中至少包含:函数编号、函数名、函数功能、所在头文件名字、功能类型编号。
要求:
1、读入装有C函数数据的文件;
2、增加新的C语言函数;
3、删除指定的C语言函数;
4、修改指定的C语言函数;
5、查询指定的C语言函数(可以提供两种不同的查询方式);
6、显示功能类型相同的所有C语言函数(例如:查询所有输出函数的列表);
7、显示所有的C语言函数到屏幕上;
8、系统的各个功能模块要求用函数的形式实现;
9、界面友好(良好的人机交互),程序加必要的注释。
#include<iostream>
#include <fstream>
#include <cstring>
#include<stdlib.h>
using namespace std;
class C_fun {
friend class C_funOperate;//友元函数
private:
int C_id;//函数编号
char C_name[20];//函数名
string C_function;//函数功能
string C_head;//头文件
int C_funID;//功能类型编号
public:
C_fun* next;
void input();//输入基本信息
void ReadFile(ifstream& in);//读取输入信息
void readC();
};
//1.读入装有C函数的文件
void C_fun::readC() {
//读文件步骤如下:
//1. 包含头文件
//2. 创建流对象
ifstream ifs;
//3. 打开文件并判断文件是否打开成功
ifs.open("C函数查询.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//4. 读数据
string buf;
while (getline(ifs, buf))
{
cout << buf << endl;
}
//5. 关闭文件
ifs.close();
}
//2、增加新的C语言函数;
void C_fun::input()
{
cout << "请输入函数编号:";
cin >> C_id;
cout << "请输入函数名:";
cin >> C_name;
cout << "请输入函数功能:";
cin >> C_function;
cout << "请输入头文件:";
cin >> C_head;
cout << "请输入功能类型编号:";
cin >> C_funID;
return;
}
void C_fun::ReadFile(ifstream& in)
{
/*从文档中读取值到结点*/
in >> C_id >> C_name >> C_function >> C_head >> C_funID ;
cout << endl;
}
class C_funOperate {
friend class C_fun;//友元类
private:
/* C_funOperate类可以看作是以 C_fun类为结点的链表*/
C_fun* head; //链表头
C_fun* end; //链表尾
ifstream in;
ofstream out;
public:
C_funOperate();
~C_funOperate();
void menu();//菜单
void Add_Node();//添加结点
void Del_node();//删除结点
void showSame();//显示相同功能函数
void Display();//显示所有函数
void Find();//查找特定函数
void Modify();//修改函数
void Save();//保存修改信息
void Clean();//清空函数
};
//构造函数
C_funOperate::C_funOperate()
{
/*初始化,头尾结点*/
head = new C_fun;
head->next = new C_fun;
end = head->next;
ifstream in("file.txt"); //原有的学生信息是放在文件file.txt中的
/*Important Part!*/
/*下面很关键,不这样做的话达不到效果。首先要先向文件读一下,才能判断
文件是否为空,因为eof()不能用来判断文件是否为空,eof()是根据文件的状态标识
来判断文件是否为空的,当读取文件时,遇到结尾时,系统会改变文件的状态标识
,这时候才会返回EOF,才能用eof()判断。不先读一下的话,in.eof()是不会返回
正值的,即使文件为空*/
char ch = in.get();
if (in.eof())
{
cout << "file is empty" << endl;
}
/*若文件非空*/
while (!in.eof())
{
end->ReadFile(in);
if (end->C_name[0] == '\0')break; //这里也是比较关键的,不加这一句的话,程序会把文件最后结束的标记也都出来
end->next = new C_fun;
end = end->next;
}
}
//析构函数
C_funOperate::~C_funOperate()
{
Save(); //当正常退出程序的时候,保存数据到file.txt中
C_fun* pos = head->next;
C_fun* temp;
while (pos != end)
{
temp = pos;
pos = pos->next;
delete temp;
}
delete head, end;
}
//显示菜单
void C_funOperate::menu()
{
cout << endl;
cout << endl;
cout << "-----------------------欢迎使用C语言函数查询管理系统-------------------------" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
cout << "|------------------ 1、读入装有C函数数据的文件 -----------------|" << endl;
cout << "|------------------ 2、增加新的C语言函数 -----------------|" << endl;
cout << "|------------------ 3、删除指定的C语言函数 -----------------|" << endl;
cout << "|------------------ 4、修改指定的C语言函数 -----------------|" << endl;
cout << "|------------------ 5、查询指定的C语言函数 -----------------|" << endl;
cout << "|------------------ 6、显示功能类型相同的所有C语言函数 -----------------|" << endl;
cout << "|------------------ 7、显示所有的C语言函数到屏幕上 -----------------|" << endl;
cout << "|------------------ 0、退出C语言函数查询管理系统 -----------------|" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
cout << endl;
cout << endl;
}
//增加节点
void C_funOperate::Add_Node()
{
char quit;
while (1)
{
end->input();
end->next = new C_fun;
end = end->next;
cout << "continue or not?,Y/N" << endl;
cin >> quit;
switch (quit)
{
case'Y':
case'y':break;
case'n':
case'N':return; break;
default:cout << "error input,again,please..Y/N";
cin >> quit;
}
}
cout << "add node success!" << endl;
system("pause");
}
//删除节点
void C_funOperate::Del_node()
{
int del_flag = 0;
char name[20];
if (head->next == end)
{
cout << "Empty...." << endl; return;
}
cout << "please enter the name you want to delete:";
cin >> name;
C_fun* p = head->next;
C_fun* q = head;
for (p; p != end; p->next)
{
if (strcmp(p->C_name, name) == 0)
{
del_flag = 1;
q->next = p->next;
delete p;
break;
}
else
{
q = q->next;
p = p->next;
}
}
if (del_flag == 0)
{
cout << "Can not Find the name you want." << endl;
}
cout << "del node success!" << endl;
system("pause");
}
void C_funOperate::showSame()
{
if (head->next == end)
{
cout << "Empty..." << endl;
}
C_fun* pos;
pos = head->next;
cout << "C语言函数功能分类如下:" << endl;
cout << "--------------------------------------------" << endl;
cout << "1.数学函数" << endl;
cout << "2.字符函数" << endl;
cout << "3.字符串函数" << endl;
cout << "4.输入输出函数" << endl;
cout << "5.动态存储分配函数" << endl;
cout << "6.其他功能函数" << endl;
cout << "--------------------------------------------" << endl;
cout << "请输入你要查找的函数功能类型编号:";
int ID;//功能类型编号
cin >> ID;
for (pos; pos != end; pos = pos->next)
{
if (ID == pos->C_funID == 1) {
cout << "数学函数有:" << pos->C_name << endl;
}
else if (ID == pos->C_funID == 2) {
cout << "字符函数有:" << pos->C_name << endl;
}
else if (ID == pos->C_funID == 3) {
cout << "字符串函数有:" << pos->C_name << endl;
}
else if (ID == pos->C_funID == 4) {
cout << "输入输出函数有:" << pos->C_name << endl;
}
else if (ID == pos->C_funID == 5) {
cout << "动态存储分配函数有:" << pos->C_name << endl;
}
else if (ID == pos->C_funID == 6) {
cout << "其他功能函数有:" << pos->C_name << endl;
}
else {
cout << "不存在此函数!" << endl;
}
}
cout << "功能类型相同的所有C语言函数显示完成!" << endl;
system("pause");
}
//显示结点
void C_funOperate::Display()
{
if (head->next == end)
{
cout << "Empty..." << endl;
}
C_fun* pos;
pos = head->next;
for (pos; pos != end; pos = pos->next)
{
cout << endl;
cout << "C_id:" << pos->C_id << endl;
cout << "C_name:" << pos->C_name << endl;
cout << "C_function:" << pos->C_function << endl;
cout << "C_head:" << pos->C_head << endl;
cout << "C_funID:" << pos->C_funID << endl;
cout << endl;
}
system("pause");
}
//查找结点
void C_funOperate::Find()
{
char name[20];
cout << "please enter name you want to find:";
cin >> name;
C_fun* pos = head->next;
if (pos == end)
{
cout << "C_fun Message is Empty..." << endl;
return;
}
for (pos; pos != end; pos = pos->next)
{
if (strcmp(pos->C_name, name) == 0)
{
cout << "Find it!" << endl;
cout << "C_id:" << pos->C_id << endl;
cout << "C_name:" << pos->C_name << endl;
cout << "C_function:" << pos->C_function << endl;
cout << "C_head:" << pos->C_head << endl;
cout << "C_funID:" << pos->C_funID << endl;
return;
}
}
cout << "Can not Find the name...";
}
//修改结点
void C_funOperate::Modify()
{
char name[20];
cout << "enter the name you want to modify:";
cin >> name;
C_fun* pos = head->next;
for (pos; pos != end; pos = pos->next)
{
if (strcmp(name, pos->C_name) == 0)
{
pos->input();
break;
}
}
cout << "Modify success!..." << endl;
system("pause");
}
//保存结点
void C_funOperate::Save()
{
out.open("file.txt");
C_fun* pos = head->next;
if (head->next == end)
{
cout << "StudentMessage List is Empty...." << endl;
return;
}
else
{
for (pos; pos != end; pos = pos->next)
{
out << '\t' << pos->C_name << '\t' << pos->C_id << pos->C_function << pos->C_head << pos->C_funID;
/*格式最好这样写,因为一开始会把文件读一下,以判断文件是否为空,所以前面最后用‘\t’来增加几个空格,给他读一个空格,
以不读到正常数据,后面就不要有空格以为当程序从file.txt读信息时,可能会把空格也当是一个数据结点读进程序 */
}
}
out.close();
cout << "Save node success!" << endl;
}
//清空文件
void C_funOperate::Clean()
{
ofstream out("file.txt", ios::trunc);
out.close();
exit(0);
//用exit使程序退出程序的时候,不调用 C_funOperate 类的析构函数,否则又会把内存的数据写到file.txt文件中
}
int main() {
//创建C函数查询管理系统
C_fun c;
C_funOperate C;
int select = 0;
while (true)
{
C.menu();
cout << "请输入对应功能的序号,并以回车键结束:";
cin >> select;
switch (select)
{
case 1: //读入装有C函数数据的文件
c.readC();
break;
case 2: //增加新的C语言函数
C.Add_Node();
break;
case 3: //删除指定的C语言函数
C.Del_node();
break;
case 4: //修改指定的C语言函数
C.Modify();
break;
case 5: //查询指定的C语言函数
C.Find();
break;
case 6: //显示功能类型相同的所有C语言函数
C.showSame();
break;
case 7: //显示所有的C语言函数到屏幕上
C.Display();
break;
case 0: //退出C语言函数查询管理系统
cout << "欢迎下次使用C语言函数查询管理系统" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}