#define _CRT_SECURE_NO_WARNINGS
#include "Dynamic_storage_management.h"
#define UI_LENGTH 500
#define PUTS_CMD_HINT printf("请根据提示输入键入命令>:")
char g_sz_ary[STR_ARRAY_LENGTH] = {0}; //全局字符数组
STR_MANGE g_str_mange_ary[MANGE_ARY_LEN] = {0}; //全局字符管理结构体数组
int g_n_cur_mange_count = 0; //全局变量 表示现在有几个字符串
int g_n_identifier = 0;
//==========================1 2 级 ui===========================
char sz_main_ui[] =
{
"*-----------------Dynamic_storage_management-----------\r\n"
"*\r\n"
"* 1 添加字符串\r\n"
"* 2 删除字符串\r\n"
"* 3 修改字符串\r\n"
"* 4 查询\r\n"
"* 5 统计字母出现的比例次数\r\n"
"* 6 显示存储信息\r\n"
"* 7 碎片整理\r\n"
"* 8 结束程序\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//添加UI
char sz_add_ui[] =
{
"*-----------------add str-------------------------------\r\n"
"*\r\n"
"* 1 输入要添加的字符串\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//删除UI
char sz_del_ui[] =
{
"*-----------------del str-------------------------------\r\n"
"*\r\n"
"* 1 按内容删除字符串\r\n"
"* 2 按编号删除字符串\r\n"
"* 3 按地址删除字符串\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//修改UI
char sz_modify_ui[] =
{
"*-----------------modify str-------------------------------\r\n"
"*\r\n"
"* 1 按内容修改字符串\r\n"
"* 2 按编号修改字符串\r\n"
"* 3 按地址修改字符串\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//查询UI
char sz_search_ui[] =
{
"*-----------------search str-------------------------------\r\n"
"*\r\n"
"* 1 按内容查询字符串\r\n"
"* 2 按编号查询字符串\r\n"
"* 3 按地址查询字符串\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//统计功能UI
char sz_statistics_ui[] =
{
"*-------------------------statistics---------------------\r\n"
"*\r\n"
"* 1 输入字符统计输次数比例\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//显示内存分布UI
char sz_storage_information_ui[] =
{
"*-----------------show_storage_information-----------\r\n"
"*\r\n"
"* 1 显示内存分布\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
//碎片整理UI
char sz_Defragmentation_ui[] =
{
"*-----------------Defragmentation-----------\r\n"
"*\r\n"
"* 1 开始碎片整理\r\n"
"* 0 返回\r\n"
"*\r\n"
"*-------------------------------------------------------\r\n"
};
char sz_exit_ui[] =
{
"exit\r\n"
};
char *sz_ui_ary[] =
{
sz_main_ui,
sz_add_ui,
sz_del_ui,
sz_modify_ui,
sz_search_ui,
sz_statistics_ui,
sz_storage_information_ui,
sz_Defragmentation_ui,
sz_exit_ui
};
//==========================1 2 级 ui end ===========================
//==========================3级 UI===================================
//------del---------
char sz_content_del_ui[] =
{
"1 删除第一个匹配的字符串\r\n"
"2 删除所有匹配的字符串\r\n"
"0 返回\r\n"
};
char sz_del_return[] =
{
""
};
char sz_identifier_del_ui[] =
{
"1 请输入要删除的编号\r\n"
"0 返回\r\n"
};
char sz_address_del_ui[] =
{
"1 请输入要删除的字符串地址\r\n"
"0 返回\r\n"
};
//删除界面的ui数组
char* sz_del_ui_ary[] =
{
sz_del_return,
sz_content_del_ui,
sz_identifier_del_ui,
sz_address_del_ui
};
//-----modify-----------
char sz_content_modify_ui[] =
{
"1 修改第一个匹配的字符串\r\n"
"2 修改所有匹配的字符串\r\n"
"0 返回\r\n"
};
char sz_modify_return[] =
{
""
};
char sz_identifier_modify_ui[] =
{
"1 请输入要修改字符串编号\r\n"
"0 返回\r\n"
};
char sz_address_modify_ui[] =
{
"1 请输入要修改字符串地址\r\n"
"0 返回\r\n"
};
//修改界面的ui数组
char* sz_modify_ui_ary[] =
{
sz_modify_return,
sz_content_modify_ui,
sz_identifier_modify_ui,
sz_address_modify_ui
};
//------search------------
char sz_content_search_ui[] =
{
"1 查询第一个匹配的字符串\r\n"
"2 查询所有匹配的字符串\r\n"
"0 返回\r\n"
};
char sz_search_return[] =
{
""
};
char sz_identifier_search_ui[] =
{
"1 请输入要查询字符串编号\r\n"
"0 返回\r\n"
};
char sz_address_search_ui[] =
{
"1 请输入要查询字符串地址\r\n"
"0 返回\r\n"
};
//修改界面的ui数组
char* sz_search_ui_ary[] =
{
sz_search_return,
sz_content_search_ui,
sz_identifier_search_ui,
sz_address_search_ui
};
//===========================3级UI==================================
//管理功能跑起来
int mange_run()
{
int n_ui_num = 0;
int n_cmd = 0;
printf(sz_main_ui); //展示主界面
while (1)
{
system("cls");
printf(sz_ui_ary[n_ui_num]); //打印UI
PUTS_CMD_HINT;
scanf("%1d", &n_cmd); //读入命令 防止溢出
fflush(stdin); //刷新缓冲区
if (ui_add > n_cmd ||ui_show_all < n_cmd)
{
continue; //健壮检查
}
switch (n_cmd)
{
case ui_add:
n_ui_num = call_add(); //调用添加字符串的UI接口函数
break;
case ui_del:
n_ui_num = call_del(); //调用删除字符串的UI接口函数
break;
case ui_mofify:
n_ui_num = call_modify(); //调用修改字符串的UI接口函数
break;
case ui_search:
n_ui_num = call_search(); //调用查询字符串的UI接口函数
break;
case ui_storage_informatio:
n_ui_num = call_storage_information(); //调用显示内存的UI接口函数
break;
case ui_statistics:
n_ui_num = call_statistics(); //调用统计UI接口函数
break;
case ui_Defragmentation:
n_ui_num = call_Defragmentation(); //调用碎片整理UI接口函数
break;
case ui_show_all:
show_all_str();
system("pause");
n_ui_num = ui_main; //调试看字符串的代码
break;
case ui_exit:
exit(0); //退出
default:
break;
}
}
}
//调用添加 UI和功能的接口函数
//返回值为再次进入的界面,枚举enum order表示各个界面的下标
int call_add()
{
int n_ret = 0;
int n_cmd = -1;
int n_identifier = 0;
char sz_ary[STR_ARRAY_LENGTH] = { 0 };
while (1)
{
system("cls");
printf(sz_ui_ary[ui_add]);
PUTS_CMD_HINT; //输出提示信息
scanf("%1d", &n_cmd); //读入命令
fflush(stdin);
if (add_return == n_cmd) //如果选择了返回
{
return ui_main;
}
if (add_exec != n_cmd)
{
continue;
}
puts("请输入要输入的字符串");
scanf("%1023s", sz_ary); //防止溢出
fflush(stdin); //刷新缓冲
n_ret = add_str(sz_ary, &n_identifier); //调用添加函数
if (-1 == n_ret)
{
puts("添加失败, 参数为空"); //失败报错
continue;
}
else if (-2 == n_ret)
{
PRINT_MEMORY_ERROR; //内存不足报错返回 用户决定是否碎片整理
return ui_Defragmentation;
}
printf("添加成功 编号为 -- %d\r\n", n_identifier);
system("pause");
}
}
//调用删除 UI和功能的接口函数
int call_del()
{
int n_ret = 0;
int n_cmd = -1;
int n_del_cmd = 0;
int n_start_index = 0;
int n_del_identifier = 0;
char* p_address = 0;
char sz_ary[STR_ARRAY_LENGTH] = { 0 };
while (1)
{
system("cls");
printf(sz_ui_ary[ui_del]);
PUTS_CMD_HINT; //输出提示信息
scanf("%1d", &n_cmd); //读入命令
fflush(stdin);
n_start_index = 0;
if (del_return > n_cmd || del_address < n_cmd)
{
continue;
}
system("cls");
printf(sz_del_ui_ary[n_cmd]);
switch (n_cmd)
{
case del_return:
return ui_
Dynamic_storage_management.zip_c/C++_增删改查 变长字符串_字符串管理
版权申诉
16 浏览量
2022-09-21
00:38:09
上传
评论
收藏 23KB ZIP 举报
JonSco
- 粉丝: 69
- 资源: 1万+
评论0