C++控制台界面库_实例6: 电话簿

这是一个C++控制台应用程序,实现了电话簿的基本功能,包括添加、删除、查找和修改联系人信息。代码中使用了自定义的Console库,并且通过COORD结构体处理鼠标交互。程序在启动时显示主界面,并提供了相应的子界面来执行各项操作。
摘要由CSDN通过智能技术生成

可以添加,删除,查找,修改。

代码如下:

// PhoneBook.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include “console.h”
#pragma comment(lib,“console.lib”)
#include <io.h>

int circulation = 1;
int interface_num = 0;

void MainInterface(); // 0
void Add(); // 1
void Delete(); // 2
void Find(); // 3
void Modify(); // 4

char phone_book[] = “dianhuabu.txt”;

struct Dhb
{
char name[10], telephone[20];
};

extern COORD mouse;

int main()
{
Screen screen;
screen.ShowOrHideCursor(0);

Event event;
event.AddMouseMode();

while (circulation)
{
	screen.Clear_screen();

	switch (interface_num)
	{
	case 0:
		MainInterface();
		break;
	case 1:
		Add();
		break;
	case 2:
		Delete();
		break;
	case 3:
		Find();
		break;
	case 4:
		Modify();
		break;
	}
}

event.RestoreMode();
//screen.Pause();
screen.Close_handle();
return 0;

}

void MainInterface()
{
Screen screen;
screen.SetSize(70, 29);
screen.SetTitle(“电话簿”);

if (_access(phone_book, 0) == -1)
{
	FILE* dhb;
	fopen_s(&dhb, phone_book, "a");
	fclose(dhb);
}

screen.text_color_num = 6;
screen.DisplayText(30, 2, "电话簿管理");

Listbox listbox = Listbox(24, 5, 20, 8);
listbox.Attribute(3, 13, 13, true, false, 0);
char item[][100] = { "        添加","        删除","        查找","        修改" };
int item_num = 4;
listbox.AddArrayto_SinglePageListbox(item, item_num);

Form form;
form.Attribute(listbox.listbox_textcolor_num, listbox.listbox_textBgcolor_num);
form.DrawSeparatorBar_H(11, listbox.cx0 - 2, listbox.cx0 + listbox.wide + 1);

Button EXIT_button = Button(listbox.cx0, 12, "      退出程序      ");
EXIT_button.button_color_num = 3;
EXIT_button.button_Bgcolor_num = 13;
EXIT_button.Display_button();

Event event;

int index = 0;

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (EXIT_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			circulation = 0;
			return;
		}

		if (EXIT_button.MousePosition_At_button())
		{
			EXIT_button.PaintText_button(2, 13);
		}
		else
		{
			EXIT_button.PaintText_button(3, 13);
		}

		if (listbox.MousePosition_At_listbox())
		{
			listbox.index_textbox.PaintText_textbox(3, 13);
			event.Get_mouse_position();
			listbox.index_textbox.cy0 = mouse.Y;
			listbox.index_textbox.PaintText_textbox(1, 6);

			if (event.Left_Button_Pressed())
			{
				index = listbox.SinglePageItemIndex();
				interface_num = index + 1;
				return;
			}
		}
		else	listbox.index_textbox.PaintText_textbox(3, 13);
	}

	if (event.Key_Event() 
适用于C++初学者 /************************************************* Copyright (C), 2008- , mekinglong of cumtcs File name:面向对象8(面向对象8.cpp) Author: 计07-2 秦杰 Version: 1.00 Date: 08.11.12 Description:编写程序实现一个简单的电话记录簿,要求记录的个人信息包括: 姓名,单位,家庭电话,移动电话。具体功能如下: 1.创建信息链表并以磁盘文件保存。 2.读取磁盘文件并显示输出所有人的移动电话。 3.按姓名或单位查询家庭电话。 4.通过姓名和单位确定个人,修改其电话信息并存盘. Others: .... Function List: // 主要函数列表,每条记录应包括函数名及功能简要说明 1.main() 完成各种提示与主操作输入输出 History: <author> <time> <version > <desc> *****************************************************/ #include <iostream> #include <assert.h> #include <string> #include <cstring> #include <fstream> #include <iomanip> using namespace std; class telist; class Node { private: char name[30]; char workplace[50]; char hometel[50]; char mobiletel[50]; Node *next; public: friend class telist; friend void load(telist&); }; class telist { private: Node *head; Node *tail; public: telist() { head=tail=NULL; } telist(Node* h,Node *t):tail(t),head(h){} void add(); void del(); void show(); Node* search(); void change(); void store(); Node* find1(); Node* find2(); friend void load(telist&); }; void operate(string ,telist &); void load(telist &); int main() { system("cls"); cout<<"欢迎使用电话簿管理系统:\n"; telist tele; load(tele); string strord; do { cout<<"输入你的操作:\n"; cout<<"1.加入新的电话记录。\n"; cout<<"2.显示输出所有人的移动电话\n"; cout<<"3.按姓名或单位查询家庭电话\n"; cout<<"4.通过姓名和单位确定个人,修改其电话信息并存盘.\n"; cout<<"5.删除一个人信息并存盘。\n"; cout<<"0.退出程序。\n"; cin>>strord; operate(strord,tele); system("cls"); }while(strord!="0") ; system("pause"); system("cls"); return 0; } void operate(string str,telist &t) { if(str=="1") t.add(); else if(str=="2") t.show(); else if(str=="3") t.search(); else if(str=="4") t.change(); else if(str=="5") t.del(); else if(str=="0") t.store(); else cout<<"输入错误!\n"; system("pause"); } void load(telist &t) { ifstream infile("电话簿.dat",ios::in|ios::binary); if(infile == NULL) { cerr<<"打开电话簿失败!!\n"; t.head=t.tail=NULL; return; } if(!infile.eof()) { cout<<"没有数据\n"; t.head=t.tail=NULL; return ; } Node *temp=new Node; t.head=temp; Node *temp2; infile.read ((char*)temp,sizeof(Node)); while(!infile.eof()||temp->next==NULL) // { temp->next=new Node; temp2=temp; temp=temp->next; infile.read ((char*)temp,sizeof(*temp)); t.tail=temp; } temp->next=NULL; delete(temp); t.tail=temp2; t.tail->next=NULL; // delete temp; assert(t.tail->next==NULL); infile.close(); // delete infile; } void telist::add() { if(head==tail&&tail==NULL) { Node * newnode; newnode=head=tail=new Node; if(!newnode) { cout<<"警告:内存分配失败!\n"; return; }//if cout<<"姓名:\n"; cin>>newnode->name; cout<<"工作单位:\n"; cin>>newnode->workplace; cout<<"家庭电话:\n"; cin>>newnode->hometel; cout<<"移动电话:\n"; cin>>newnode->mobiletel; newnode->next=NULL; }//if else{ Node *temp; temp=new Node; tail->next = temp; if(!temp) { cout<<"警告:内存分配失败!\n"; return; } cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; temp->next=NULL; tail=temp; }//else } void telist::del() { cout<<"先确定你要删除的人:\n"; Node *temp=search(); Node *p=head; if(temp==NULL) { cout<<"没找到要找的人!\n"; return ; } if(p==temp) { cout<<"确定删除?(y或n)"; char ord1; cin>>ord1; if(ord1=='N'||ord1=='n') return; if(ord1=='Y'||ord1=='y') { head=head->next; if(p==tail) tail=NULL;//wenti zaizheli } } else { for(;(p->next)!=temp;) { p=p->next; } cout<<"确定删除?(y或n)"; char ord; cin>>ord; if(ord=='N'||ord=='n')return; if(ord=='Y'||ord=='y')//if1 { if(temp==tail)//if2 { delete temp; tail=p; }//if2 else { p->next=temp->next; delete temp; }//else }//if1 }//else store(); }//del void telist::show() { Node *temp=head; for(;temp!=NULL;) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; temp=temp->next; } return; } Node* telist::search() { // char sname[30]; // char sworkplace[50]; int ord; cout<<"按照什么查询?\n"; cout<<"1.名字。\n"; cout<<"2.工作单位。\n"; cin>>ord; for(;!cin;) { cout<<"error!"; cin>>ord; } switch(ord) { case 1:return(find1());break; case 2:return(find2());break; default:cout<<"警告:操作错误!\n";return 0; } } Node* telist::find1() { char sn[30]; cout<<"请输入姓名:\n"; cin>>sn; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sn,temp->name)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } Node* telist::find2() { char sw[50]; cout<<"请输入工作单位:\n"; cin>>sw; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sw,temp->workplace)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; break; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } void telist::store() { Node * temp=head; ofstream outfile("电话簿.dat",ios::out|ios::binary); if(!outfile) { cout<<"打开文件失败!\n"; return ; } for(;temp;) { outfile.write ((char *)temp,sizeof(*temp)); temp=temp->next ; } outfile.close(); } void telist::change() { Node *temp=search(); cout<<"请输入新的记录:\n"; cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; store(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值