本文主要是介绍EGE——c++——通讯录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、简介
1、基础功能:增、查、删、改
2、界面的交互功能:鼠标和按钮的交互,按钮的点击效果,界面切换动态效果
3、以文档格式储存
对EGE中,窗口delay_fps()帧率刷新理解有一定提升。控制了重画界面的时机和次数,减小损耗;通过对redraw的不同赋值,实现界面内不同子界面的变化。
二、界面相关代码
头文件interface.h
#pragma once
#include<iostream>
using namespace std;
//控制台开关
#define SHOW_CONSOLE
#include<graphics.h>
#include<ege/sys_edit.h>
#include"addresslist.h"constexpr auto Windx = 600;
constexpr auto Windy = 400;
constexpr auto Windw = 800;
constexpr auto Windh = 600;
constexpr auto Caption = "通讯录";
constexpr auto MaxSizeNameofButton = 24;
//声明按钮类
class Button {
public:int b_x;int b_y;int b_w;int b_h;char b_name[MaxSizeNameofButton];int b_mouse_in_flag = 0;int b_down_flag = 0;Button() {}Button(int x, int y, int w, int h, const char* buttonname);void operator=(const Button& b);void draw_button();int mouse_in(mouse_msg& mouse);int mouse_down(mouse_msg& mouse);bool mouse_click(mouse_msg& mouse);
};
//初始化主按钮
void init_mainbutton(Button* main_button, int n);//画界面,以函数指针为参数,返回值0-5分别代表主界面和4个分界面
int draw_interface(int(*Interface)());
//主界面
int main_interface();
//添加界面
int add_interface();
//搜索界面
int search_interface();
//修改界面
int rec_interface();
//删除界面
int del_interface();
//动态切换效果
int dyn_interface(Button main_button[], int n);
//初始化文本输入框
void init_edit(sys_edit& edit, int x, int y, int w, int h);
//检查按钮状态
int check_button(Button* pbutton, int size, mouse_msg& mouse);
//获取联系人信息
Person get_person(sys_edit* editname, sys_edit* editage = NULL, sys_edit* edittel = NULL);
//获取联系人ID
int get_id(sys_edit& edit);
//显示联系人
void person_show(int index, int x, int y, int w, int h);
interface.cpp各个界面的实现源码
#pragma once
#include"interface.h"
#include<time.h>
//按钮构造函数
Button::Button(int x, int y, int w, int h, const char * buttonname)
{b_x = x;b_y = y;b_w = w;b_h = h;strcpy_s(b_name, buttonname);
}
//按钮=号重载,方便主界面按钮数组的初始化
void Button::operator=(const Button & b)
{b_x = b.b_x;b_y = b.b_y;b_w = b.b_w;b_h = b.b_h;strcpy_s(b_name, b.b_name);
}
//画按钮
void Button::draw_button()
{//记录原填充色和字体color_t tempcolor = getfillcolor();LOGFONT tempfont;getfont(&tempfont);//画按钮(矩形)int x = b_x, y = b_y, w = b_w, h = b_h;if (b_mouse_in_flag) {x = b_x - 2;y = b_y - 2 * h / w;w = b_w + 4;h = b_h + 4 * h / w;}else {x = b_x;y = b_y;w = b_w;h = b_h;}setfillcolor(EGEACOLOR(0x80, RGB(200, 200, 200)));ege_fillrect((float)x, (float)y, (float)w, (float)h);//按钮名称setfont(h / 2 + 5, 0, "黑体");setbkmode(TRANSPARENT);int strwidth = textwidth(b_name);int strheight = textheight(b_name);if (b_down_flag) {xyprintf(b_x + (b_w - strwidth) / 2, b_y + (b_h - strheight) / 2 + 2, b_name);}else {xyprintf(b_x + (b_w - strwidth) / 2, b_y + (b_h - strheight) / 2, b_name);}//恢复原来的填充色和字体setfillcolor(tempcolor);setfont(&tempfont);
}
//判断鼠标是否指向按钮,实现交互
int Button::mouse_in(mouse_msg& mouse)
{return mouse.x >= b_x && mouse.x <= b_x + b_w && mouse.y >= b_y && mouse.y <= b_y + b_h;
}
//判断按钮是否被按下
int Button::mouse_down(mouse_msg& mouse)
{return mouse_in(mouse) && mouse.is_left() && mouse.is_down();
}
//判断按钮是否被点击(鼠标按下后松开)
bool Button::mouse_click(mouse_msg& mouse)
{return mouse.is_up() && mouse.is_left() && b_down_flag;
}
//初始化主界面的按钮数组
void init_mainbutton(Button* main_button, int n)
{const char* Main_buttonname[] = { "添加联系人","查找联系人","修改联系人","删除联系人","退出通讯录" };int button_w = 200;int button_h = Windh / 11;int button_x = (Windw - button_w) / 2;int button_y = button_h;for (int i = 0; i < n; i++) {main_button[i] = Button(button_x, button_y + i * button_y * 2, button_w, button_h, Main_buttonname[i]);}
}
//画界面,以界面函数的指针为参数
int draw_interface(int(*pInterface)())
{return pInterface();
}
//主界面
int main_interface() {int res = 0;//标记是否重画界面,不需要重画时可减小损耗int redraw = 1;static Button main_button[5];init_mainbutton(main_button, 5);mouse_msg mouse;while (mousemsg()) {mouse = getmouse();int checkflag = check_button(main_button, sizeof(main_button) / sizeof(main_button[0]), mouse);switch (checkflag) {case 0:case 1:case 2:case 3:case 4:res = checkflag + 1;main_button[checkflag].b_down_flag = 0;main_button[checkflag].b_mouse_in_flag = 0;redraw = 1;break;case 5:res = 0;redraw = 1;break;}}if (redraw) {cleardevice();setbkcolor(EGEACOLOR(0X80, RGB(255, 255, 255)));for (int i = 0; i < 5; i++) {main_button[i].draw_button();}}return res;
}
//添加界面
int add_interface() {int res = 1;//定义一个主界面按钮,用来实现动态切换效果static Button main_button[5];//定义添加界面初始化完成标记static int initflag = 0;//动态切换效果开关static int dynflag = 0;//重画开关int redraw = 0;static sys_edit editname;static sys_edit editage;static sys_edit edittel;//添加界面标题按钮int button_w = 200;int button_h = Windh / 11;int button_x = (Windw - button_w) / 2;int button_y = button_h;Button add(button_x, button_y, button_w, button_h, "添加联系人");enum { cel, yes };static Button sec_button[2];sec_button[cel] = Button((Windw - 300) / 2 - textwidth("姓名:"), add.b_y + 380, 80, 40, "取消");sec_button[yes] = Button((Windw - 300) / 2 + 300 - 60, sec_button[cel].b_y, 80, 40, "确定");//三个输入框和主界面按钮数组初始化,一个添加界面只初始化1次if (initflag == 0) {init_mainbutton(main_button, 5);init_edit(editname, (Windw - 300) / 2, main_button[0].b_y + 100, 320, 30);init_edit(editage, (Windw - 300) / 2, main_button[0].b_y + 200, 320, 30);init_edit(edittel, (Windw - 300) / 2, main_button[0].b_y + 300, 320, 30);editname.setfocus();initflag = 1;}//动态切换效果未完成时,重画界面动态界面if (dynflag < 5) {dynflag = dyn_interface(main_button, 5);redraw = 1;}//完成时,重画一次输入界面else if (dynflag == 5) {redraw = 2;dynflag = 6;}//定义一个计时器开始标记static clock_t start;static int tip = 0;mouse_msg mouse;while (mousemsg()) {//按钮和鼠标的交互mouse = getmouse();//取消按钮和确定按钮的鼠标指向效果,重画输入界面int size = sizeof(sec_button) / sizeof(sec_button[0]);int checkflag = check_button(sec_button, size, mouse);switch (checkflag) {case 0: {res = 0;sec_button[cel].b_mouse_in_flag = 0;editname.destroy();editage.destroy();edittel.destroy();dynflag = 0;initflag = 0;}break;case 1: {res = 1;tip = 1;Person p = get_person(&editname, &editage, &edittel);if (address.work_add(p) != -1) {start = clock();}}case 2: {redraw = 2;}break;}}clock_t end = clock();if (tip == 1 && end - start >= 1000) {cout << tip << endl;//满足计时条件,关闭提示信息,重画1次界面redraw = 2;tip = 0;editname.settext("");editage.settext("");edittel.settext("");editname.setfocus();}if (redraw) {cleardevice();setbkcolor(EGEACOLOR(0X80, RGB(255, 255, 255)));switch (redraw) {case 1:for (int i = 0; i < 5; i++) {main_button[i].draw_button();}if (dynflag != 5) redraw = 0;else redraw = 1;break;case 2:add.draw_button();xyprintf(editname.getx() - textwidth("姓名:"), editname.gety() + 5, "姓名:");xyprintf(editname.getx() - textwidth("年龄:"), editage.gety() + 5, "年龄:");xyprintf(editname.getx() - textwidth("电话:"), edittel.gety() + 5, "电话:");editname.visible(true);editage.visible(true);edittel.visible(true);sec_button[cel].draw_button();sec_button[yes].draw_button();if (tip) {xyprintf(sec_button[cel].b_x + sec_button[cel].b_w + ((sec_button[yes].b_x - sec_button[cel].b_x - sec_button[cel].b_w) - textwidth("添加成功")) / 2, sec_button[cel].b_y, "添加成功");}redraw = 0;break;}}return res;
}
//搜索界面
int search_interface() {int res = 2;//定义一个主界面按钮,用来实现动态切换效果static Button main_button[5];//定义添加界面初始化完成标记static int initflag = 0;//动态切换效果开关static int dynflag = 0;//重画开关int redraw = 0;static sys_edit edit;//添加界面标题按钮int button_w = 200;int button_h = Windh / 11;int button_x = (Windw - button_w) / 2;int button_y = button_h;Button search(button_x, button_y, button_w, button_h, "搜索联系人");enum { cel, yes };static Button sec_button[2];//输入框和主界面按钮数组初始化,一个添加界面只初始化1次int printx = 0;if (initflag == 0) {init_mainbutton(main_button, 5);init_edit(edit, (Windw - 360) / 2, main_button[0].b_y + 150, 360, 30);edit.setfocus();sec_button[cel] = Button(search.b_x - 80, search.b_y + 380, 80, 40, "取消");sec_button[yes] = Button(search.b_x + search.b_w, sec_button[cel].b_y, 80, 40, "确定");initflag = 1;}//动态切换效果未完成时,重画界面动态界面if (dynflag < 5) {dynflag = dyn_interface(main_button, 5);redraw = 1;}//完成时,重画一次输入界面else if (dynflag == 5) {redraw = 2;dynflag = 6;}static int index = -1;mouse_msg mouse;while (mousemsg()) {mouse = getmouse();int size = sizeof(sec_button) / sizeof(sec_button[0]);int checkflag = check_button(sec_button, size, mouse);switch (checkflag) {case 0: {res = 0;sec_button[cel].b_mouse_in_flag = 0;sec_button[yes].b_down_flag = 0;strcpy_s(sec_button[yes].b_name, "确定");intArray.init();edit.destroy();dynflag = 0;initflag = 0;}break;case 1: {if (strcmp(sec_button[yes].b_name, "确定") == 0) {res = 2;sec_button[yes].b_down_flag = 0;Person p = get_person(&edit);address.work_search(p.p_name);if (intArray.m_cnt) {index = 0;strcpy_s(sec_button[yes].b_name, "修改");redraw = 3;}else {redraw = 2;}}else {res = 3;sec_button[cel].b_mouse_in_flag = 0;sec_button[yes].b_down_flag = 0;intArray.init();edit.destroy();dynflag = 0;initflag = 0;redraw = 0;}}break;case 2: {if (intArray.m_cnt) {redraw = 3;}else {redraw = 2;}}break;}if (intArray.m_cnt&&mouse.is_wheel()) {index += -mouse.wheel / 120;if (index < 0) index = 0;if (index >= intArray.m_cnt) index = intArray.m_cnt - 1;redraw = 3;}}if (redraw) {cleardevice();setbkcolor(EGEACOLOR(0X80, RGB(255, 255, 255)));switch (redraw) {case 1:for (int i = 0; i < 5; i++) {main_button[i].draw_button();}break;case 2:search.draw_button();setfont(search.b_h / 2, 0, "楷体");printx = (Windw - textwidth("请输入查找内容")) / 2;xyprintf(printx, search.b_y + search.b_h + 50, "请输入查找内容");edit.visible(true);sec_button[cel].draw_button();sec_button[yes].draw_button();break;case 3:search.draw_button();edit.visible(false);sec_button[cel].draw_button();sec_button[yes].draw_button();int w = 400;int x = (Windw - w) / 2;int h = Windh / 5;int y = h;person_show(index, x, y, w, h);if (index < intArray.m_cnt - 1) {y = y + h + 2;person_show(index + 1, x, y, w, h);}break;}}return res;
}
//修改界面
int rec_interface() {int res = 3;//定义一个主界面按钮,用来实现动态切换效果static Button main_button[5];//定义添加界面初始化完成标记static int initflag = 0;//动态切换效果开关static int dynflag = 0;//重画开关int redraw = 0;//提示开关、显示开关、联系人id、修改开关static int tip = 0, show = 0, id = -1, recflag = 0;//id输入框static sys_edit edit;//修改输入框static sys_edit editname, editage, edittel;//添加界面标题按钮int button_w = 200;int button_h = Windh / 11;int button_x = (Windw - button_w) / 2;int button_y = button_h;Button search(button_x, button_y, button_w, button_h, "修改联系人");enum { cel, yes };static Button sec_button[2];//输入框和主界面按钮数组初始化,一个添加界面只初始化1次if (initflag == 0) {init_mainbutton(main_button, 5);init_edit(edit, (Windw - 360) / 2, main_button[0].b_y + 150, 360, 30);edit.setfocus();sec_button[cel] = Button(search.b_x - 80, search.b_y + 380, 80, 40, "取消");sec_button[yes] = Button(search.b_x + search.b_w, sec_button[cel].b_y, 80, 40, "确定");tip = 0; show = 0; id = -1; dynflag = 0;initflag = 1;}//动态切换效果未完成时,重画界面动态界面if (dynflag < 5) {dynflag = dyn_interface(main_button, 5);redraw = 1;}//完成时,重画一次输入界面else if (dynflag == 5) {redraw = 2;dynflag = 6;}static clock_t start = 0;mouse_msg mouse;while (mousemsg()) {mouse = getmouse();int size = sizeof(sec_button) / sizeof(sec_button[0]);int checkflag = check_button(sec_button, size, mouse);switch (checkflag) {case 0: {res = 0;sec_button[cel].b_mouse_in_flag = 0;sec_button[cel].b_down_flag = 0;strcpy_s(sec_button[yes].b_name, "确定");intArray.init();edit.destroy();editname.destroy();editage.destroy();edittel.destroy();dynflag = 0;initflag = 0;id = -1;tip = 0;recflag = 0;}break;case 1: {sec_button[yes].b_down_flag = 0;if (strcmp(sec_button[yes].b_name, "确定") == 0) {id = get_id(edit);if (id > address.a_cnt || id < 1) {tip = 1;start = clock();redraw = 2;}else {show = 1;strcpy_s(sec_button[yes].b_name, "修改");intArray.m_array[0] = id - 1;redraw = 3;}}else if (strcmp(sec_button[yes].b_name, "修改") == 0) {redraw = 3;recflag = 1;int w = 400;int x = (Windw - w) / 2;int h = Windh / 5;int y = h;init_edit(editname, x + textwidth("姓名:"), y + h + 10, w - textwidth("姓名:"), 30);init_edit(editage, x + textwidth("姓名:"), y + h + 50, w - textwidth("姓名:"), 30);init_edit(edittel, x + textwidth("姓名:"), y + h + 90, w - textwidth("姓名:"), 30);editname.settext(address.a_array[id - 1].p_name);char buffer[MAXSIZENAME];sprintf_s(buffer, "%d", address.a_array[id - 1].p_age);editage.settext(buffer);sprintf_s(buffer, "%lld", address.a_array[id - 1].p_tel);edittel.settext(buffer);editname.setfocus();strcpy_s(sec_button[yes].b_name, "保存");}else if (strcmp(sec_button[yes].b_name, "保存") == 0) {redraw = 3;recflag = 0;Person p = get_person(&editname, &editage, &edittel);if (p.p_name[0] != '\0' || p.p_tel) {address.a_array[id - 1] = p;}strcpy_s(sec_button[yes].b_name, "修改");}}break;case 2: {if (show) {redraw = 3;}else {redraw = 2;}}break;}}clock_t end = clock();if (end - start >= 300 && tip == 1) {tip = 0;redraw = 2;}if (redraw) {cleardevice();setbkcolor(EGEACOLOR(0X80, RGB(255, 255, 255)));switch (redraw) {case 1:for (int i = 0; i < 5; i++) {main_button[i].draw_button();}break;case 2:search.draw_button();setfont(search.b_h / 2, 0, "楷体");xyprintf((Windw - textwidth("请输入要修改联系人的ID")) / 2, search.b_y + search.b_h + 50, "请输入要修改联系人的ID");edit.visible(true);sec_button[cel].draw_button();sec_button[yes].draw_button();if (tip) {xyprintf((Windw - textwidth("查无此人")) / 2, edit.gety() + edit.geth() + 10, "查无此人");}break;case 3:search.draw_button();edit.visible(false);sec_button[cel].draw_button();sec_button[yes].draw_button();int w = 400;int x = (Windw - w) / 2;int h = Windh / 5;int y = h;person_show(0, x, y, w, h);if (recflag) {xyprintf(x, editname.gety() + 4, "姓名:");xyprintf(x, editage.gety() + 4, "年龄:");xyprintf(x, edittel.gety() + 4, "电话:");editname.visible(true);editage.visible(true); edittel.visible(true);}else {editname.visible(false);editage.visible(false);edittel.visible(false);}break;}}return res;
}
//删除界面
int del_interface() {int res = 4;//定义一个主界面按钮,用来实现动态切换效果static Button main_button[5];//定义添加界面初始化完成标记static int initflag = 0, id = -1;//动态切换效果开关static int dynflag = 0;//重画开关int redraw = 0;//id输入框static sys_edit edit;//添加界面标题按钮int button_w = 200;int button_h = Windh / 11;int button_x = (Windw - button_w) / 2;int button_y = button_h;Button del(button_x, button_y, button_w, button_h, "删除联系人");enum { cel, yes };static Button sec_button[2];//输入框和主界面按钮数组初始化,一个添加界面只初始化1次if (initflag == 0) {init_mainbutton(main_button, 5);init_edit(edit, (Windw - 360) / 2, main_button[0].b_y + 150, 360, 30);edit.setfocus();sec_button[cel] = Button(del.b_x - 80, del.b_y + 380, 80, 40, "取消");sec_button[yes] = Button(del.b_x + del.b_w, sec_button[cel].b_y, 80, 40, "确定");initflag = 1;}//动态切换效果未完成时,重画界面动态界面if (dynflag < 5) {dynflag = dyn_interface(main_button, 5);redraw = 1;}//完成时,重画一次输入界面else if (dynflag == 5) {redraw = 2;dynflag = 6;}static clock_t start = 0;static int tip = 0, show = 0;mouse_msg mouse;while (mousemsg()) {mouse = getmouse();int size = sizeof(sec_button) / sizeof(sec_button[0]);int checkflag = check_button(sec_button, size, mouse);switch (checkflag) {case 0: {res = 0;sec_button[cel].b_mouse_in_flag = 0;sec_button[cel].b_down_flag = 0;strcpy_s(sec_button[yes].b_name, "确定");intArray.init();edit.destroy();dynflag = 0;initflag = 0;id = -1;show = 0;}break;case 1: {sec_button[yes].b_down_flag = 0;if (strcmp(sec_button[yes].b_name, "确定") == 0) {id = get_id(edit);if (id > address.a_cnt || id < 1) {tip = 1;start = clock();redraw = 2;}else {show = 1;strcpy_s(sec_button[yes].b_name, "删除");intArray.m_array[0] = id - 1;redraw = 3;}}else {address.work_del(id - 1);redraw = 2;show = 0;strcpy_s(sec_button[yes].b_name, "确定");edit.settext("");}}break;case 2: {if (show) {redraw = 3;}else {redraw = 2;}}}}clock_t end = clock();if (end - start >= 300 && tip) {tip = 0;redraw = 2;}if (redraw) {cleardevice();setbkcolor(EGEACOLOR(0X80, RGB(255, 255, 255)));switch (redraw) {case 1:for (int i = 0; i < 5; i++) {main_button[i].draw_button();}break;case 2:del.draw_button();setfont(del.b_h / 2, 0, "楷体");xyprintf((Windw - textwidth("请输入要删除联系人的ID")) / 2, del.b_y + del.b_h + 50, "请输入要删除联系人的ID");edit.visible(true);sec_button[cel].draw_button();sec_button[yes].draw_button();if (tip) {xyprintf((Windw - textwidth("查无此人")) / 2, edit.gety() + edit.geth() + 10, "查无此人");}break;case 3:del.draw_button();edit.visible(false);sec_button[cel].draw_button();sec_button[yes].draw_button();int w = 400;int x = (Windw - w) / 2;int h = Windh / 5;int y = h;person_show(0, x, y, w, h);break;}}return res;
}
//动态切换效果,动态效果完成后返回消失的按钮数量
int dyn_interface(Button main_button[], int n) {int res = 0;static int v = 1;for (int i = 0; i < n; i++) {if (i % 2 == 0) {main_button[i].b_x -= v;}else {main_button[i].b_x += v;}if (main_button[i].b_x <= -Windw || main_button[i].b_x >= Windw) {res++;}}v += 2;if (res == 5) v = 1;return res;
}
//初始化文本输入框
void init_edit(sys_edit& edit, int x, int y, int w, int h) {edit.create(false);edit.move(x, y);edit.size(w, h);edit.setmaxlen(24);edit.setfont(h - h / 3, 0, "楷体");edit.setbgcolor(EGEACOLOR(0x80, RGB(255, 255, 200)));edit.visible(false);edit.setfocus();
}
//检查按钮状态,返回被点击按钮下标
int check_button(Button* pbutton, int size, mouse_msg& mouse) {int res = -1;for (int i = 0; i < size; i++) {if (pbutton[i].b_mouse_in_flag != pbutton[i].mouse_in(mouse)) {res = size;if (pbutton[i].mouse_in(mouse)) {pbutton[i].b_mouse_in_flag = 1;}else {pbutton[i].b_mouse_in_flag = 0;}}if (pbutton[i].b_down_flag != pbutton[i].mouse_down(mouse)) {res = size;if (pbutton[i].mouse_down(mouse)) {pbutton[i].b_down_flag = 1;}else if (!pbutton[i].mouse_in(mouse)) {pbutton[i].b_down_flag = 0;}}if (pbutton[i].mouse_click(mouse)) {res = i;}}return res;
}
//获取联系人信息
Person get_person(sys_edit* editname, sys_edit* editage, sys_edit* edittel) {Person p;char buffer[MAXSIZENAME] = { 0 };editname->gettext(MAXSIZENAME, buffer);strcpy_s(p.p_name, buffer);if (editage) {editage->gettext(MAXSIZENAME, buffer);sscanf_s(buffer, "%d", &p.p_age);edittel->gettext(MAXSIZENAME, buffer);sscanf_s(buffer, "%lld", &p.p_tel);}return p;
}
//获取联系人ID
int get_id(sys_edit& edit) {int res = -1;char buffer[10];edit.gettext(10, buffer);if (buffer[0] != '\0') {sscanf_s(buffer, "%d", &res);}return res;
}
//显示联系人
void person_show(int index, int x, int y, int w, int h) {LOGFONTA tempfont;getfont(&tempfont);color_t tempcolor = getfillcolor();setfillcolor(EGEACOLOR(0x80, RGB(255, 255, 120)));ege_fillrect((float)x, (float)y, (float)w, (float)h);setfont(h / 5, 0, "楷体");int temp = textheight("zhangsan");xyprintf(x + 2, y + (h - 4 * temp) / 5, "ID:%d", intArray.m_array[index] + 1);xyprintf(x + 2, y + 2 * (h - 4 * temp) / 5 + temp, "姓名:%s", address.a_array[intArray.m_array[index]].p_name);xyprintf(x + 2, y + 3 * (h - 4 * temp) / 5 + 2 * temp, "年龄:%d", address.a_array[intArray.m_array[index]].p_age);xyprintf(x + 2, y + 4 * (h - 4 * temp) / 5 + 3 * temp, "电话:%lld", address.a_array[intArray.m_array[index]].p_tel);setfillcolor(tempcolor);setfont(&tempfont);
}
main.cpp
#include"interface.h"
int main(int argc, char const *argv[]) {//设置窗口类型和窗口位置,此版本setinitmode()不生效setinitmode(INIT_RENDERMANUAL, Windx, Windy);//设置窗口名字setcaption(Caption);initgraph(Windw, Windh, INIT_RENDERMANUAL);//开启高级渲染模式ege_enable_aa(true);//定义函数指针,指向主界面int(*pInterface)() = main_interface;int flag = 0;for (; is_run(); delay_fps(60)) {flag = draw_interface(pInterface);switch (flag) {case 0:pInterface = main_interface; break;case 1:pInterface = add_interface; break;case 2:pInterface = search_interface; break;case 3:pInterface = rec_interface; break;case 4:pInterface = del_interface; break;case 5:goto EXIT;}}
EXIT:closegraph();return 0;
}
三、通讯录内核代码
address.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
constexpr auto MAXSIZENAME = 24;
constexpr auto MAXSIZEADDR = 10;
//定义联系人类
class Person {
public:char p_name[MAXSIZENAME];int p_age = 0;long long p_tel = 0;Person() {}Person(const char* name, int age, long long tel);void operator=(const Person& p);
};
//定义通讯录类
class Address {
public:Person a_array[MAXSIZEADDR];int a_cnt = 0;int work_add(const Person& p);void work_del(int id);int work_search(char *str);Address();~Address();
};
extern Address address;
//定义数组类
class Array {
public:int m_array[MAXSIZEADDR];int m_cnt = 0;void init();
};
extern Array intArray;
address.cpp
#include"addresslist.h"
Address address;
Array intArray;
Person::Person(const char* name, int age, long long tel)
{strcpy_s(p_name, name);p_age = age;p_tel = tel;
}
void Person::operator=(const Person & p)
{strncpy_s(p_name, p.p_name, sizeof(p.p_name));p_age = p.p_age;p_tel = p.p_tel;
}
int Address::work_add(const Person & p)
{int res = -1;if (address.a_cnt < MAXSIZEADDR) {if (p.p_name[0] != '\n' || p.p_tel != 0) {address.a_array[address.a_cnt].p_age = p.p_age;address.a_array[address.a_cnt].p_tel = p.p_tel;strcpy_s(address.a_array[address.a_cnt].p_name, p.p_name);res = address.a_cnt++;}}return res;
}
void Address::work_del(int id)
{for (int i = id; i < a_cnt - 1; i++) {a_array[i] = a_array[i + 1];}a_cnt--;
}
int Address::work_search(char *str)
{intArray.init();if (str[0] != '\0') {cout << str << endl;for (int i = 0; i < a_cnt; i++) {if (strcmp(a_array[i].p_name, str) == 0) {intArray.m_array[intArray.m_cnt++] = i;}}}long long temptel = 0;sscanf_s(str, "%lld", &temptel);if (temptel != 0 && intArray.m_cnt == 0) {cout << intArray.m_cnt << endl;for (int i = 0; i < a_cnt; i++) {if (a_array[i].p_tel == temptel) {intArray.m_array[intArray.m_cnt++] = i;}}}return intArray.m_cnt;
}
Address::Address()
{ifstream ifs("address.txt", ios::binary | ios::in);if (ifs) {ifs.read((char*)this, sizeof(*this));ifs.close();cout << "通讯录打开成功" << endl;cout << "通讯录中共有 " << a_cnt << " 个联系人" << endl;}else {cout << "创建通讯录" << endl;}
}
Address::~Address()
{ofstream ofs("address.txt", ios::binary | ios::out);if (ofs.is_open()) {ofs.write((char*)this, sizeof(*this));ofs.close();cout << "通讯录保存成功" << endl;cout << "通讯录中共有 " << a_cnt << " 个联系人" << endl;}else {cout << "通讯录保存失败" << endl;}
}
void Array::init()
{m_cnt = 0;
}
这篇关于EGE——c++——通讯录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!