📄 vc++phonetext.txt
字号:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//================================================================================================
//结构与类声明
struct Person_Info
{
char name[20]; //姓 名
char sex[10]; //性 别
char tel[20]; //电话号码
char mo_tel[20]; //手 机
char born[10]; //出生年月
char email[40]; //电子邮箱
char qq[20]; //QQ 号 码
char addr[100]; //通信地址
char postid[10]; //邮政编码
char modtime[30]; //修改时间
Person_Info *next;
Person_Info *prv;
};
typedef char com[10];
class Tel_Book
{
public:
//构造函数
Tel_Book();
//数据保存函数
bool Add_To_Dat();
//接收窗口命令函数
bool Command_Get(com &str);
//命令窗口函数
void Command_Window();
//数据备份函数
bool Dat_Bak(bool bak);
//数据修改,删除函数
bool Dat_Mod_Del(unsigned int id,Person_Info &tempstruct,bool mod_or_del);
//数据排序函数
bool Dat_Order(char str[]);
//数据重写函数
bool Dat_Rewrite();
//头部介绍及帮助函数
void Head_Intro();
//电话本内容信息增加函数
void Info_Add_Mod(char ch,bool add_or_modify,Person_Info &tempstruct);
//信息备份函数
void Info_Bak();
//电话本内容信息检查函数
void Info_Check();
//信息列表函数
void Info_Detail(Person_Info &tempstruct);
//电话本内容信息显示函数
void Info_Display(char ch);
//信息检索函数
void Info_Find();
//电话本内容信息查看函数
void Info_List();
//电话本内容信息操作函数
bool Info_List_Op(char ch);
//电话本内容信息修改或删除函数
bool Info_Mod_Del(unsigned int id);
//电话本内容信息排序函数
void Info_Order();
//电话本内容信息导出函数
void Info_Out();
//电话本内容信息读出函数
void Info_Read();
//电话本内容信息重写函数
void Info_Rewrite();
//重名检查函数
bool Name_Check(char str[]);
//输入中多余字符去除函数
bool Rest_Eat();
//清屏函数
void Screen_Clr();
//结构元素清空函数
void Struct_Clr(Person_Info &tempstruct);
//系统时间函数
void System_Time(char (×tr)[30]);
protected:
char datcode;
char datstr[30];
char bakdatstr[30];
char outstr[30];
unsigned int structsize;
unsigned int count;
Person_Info telstruct;
Person_Info *head;
Person_Info *tail;
};
//===============================================================================================
//构造函数
Tel_Book::Tel_Book()
{
datcode = '\x9a';
strcpy(datstr,"info.dat");
strcpy(bakdatstr,"info.dat.bak");
strcpy(outstr,"Tel_Book.htm");
structsize = 280;
count = 0;
head = NULL;
tail = NULL;
}
//===============================================================================================
//头部介绍及帮助函数
void Tel_Book::Head_Intro()
{
cout<<endl<<setw(80)<<setfill('=')<<"=";
cout<<" 欢迎您使用多功能电话本系统,此系统全部采用c++语言编写,功能齐全,使用方便.此系统具有以下功能:"<<endl<<endl;
cout<<"1.可以方便地增加,删除,修改和查找电话本内容信息,您只需按要求输入相应的命令即可."<<endl;
cout<<"2.可以将电话本内容信息按照姓氏音序或电话号码的次序进行排序,操作简便,处理迅速."<<endl;
cout<<"3.可以将电话本内容信息进行备份,还原,有效防止数据丢失."<<endl;
cout<<"4.可以将电话本内容信息以表格的形式导出,方便您查阅,打印."<<endl<<endl;
cout<<"此电话本系统的主要命令有:"<<endl<<endl;
cout<<"ADD 增加电话本内容信息"<<endl;
cout<<"LIST 查看,修改,删除电话本内容信息"<<endl;
cout<<"RESIZE 对数据文件进行压缩"<<endl;
cout<<"ORDER 对电话本内容信息进行排序"<<endl;
cout<<"FIND 查找电话本内容信息"<<endl;
cout<<"BACKUP 对数据文件进行备份"<<endl;
cout<<"LOADOUT 将电话本内容信息导出为表格形式"<<endl;
cout<<"CLEAR 清屏"<<endl;
cout<<"HELP 查看帮助"<<endl;
cout<<"QUIT 退出系统"<<endl;
cout<<endl<<"其他的一些命令您只需按要求操作即可."<<endl<<endl;
cout<<setw(80)<<setfill(' ')<<setiosflags(ios::right)<<"制 作 人: 付腾桂 ";
cout<<setw(80)<<setfill(' ')<<setiosflags(ios::right)<<"电子邮箱: futenggui520@163.com ";
cout<<setw(80)<<setfill('=')<<"=";
}
//===============================================================================================
//命令窗口函数
void Tel_Book::Command_Window()
{
com commandstr;
while(1)
{
cout<<"TELBOOK::";
cin.getline(commandstr,sizeof(commandstr));
if(strlen(commandstr) < sizeof(commandstr)-1)
{
if(Command_Get(commandstr))
break;
else
continue;
}
else
Rest_Eat();
}
}
//================================================================================================
//接收窗口命令函数
bool Tel_Book::Command_Get(com &str)
{
if(stricmp(str,"add") == 0)
{
Info_Add_Mod(0,true,telstruct);
return false;
}
else if(stricmp(str,"list") == 0)
{
Info_List();
return false;
}
else if(stricmp(str,"resize") == 0)
{
Info_Rewrite();
return false;
}
else if(stricmp(str,"order") == 0)
{
Info_Order();
return false;
}
else if(stricmp(str,"find") == 0)
{
Info_Find();
return false;
}
else if(stricmp(str,"loadout") == 0)
{
Info_Out();
return false;
}
else if(stricmp(str,"help") == 0)
{
Head_Intro();
return false;
}
else if(stricmp(str,"backup") == 0)
{
Info_Bak();
return false;
}
else if(stricmp(str,"clear") == 0)
{
Screen_Clr();
return false;
}
else if(stricmp(str,"quit") == 0)
return true;
else
return false;
}
//===============================================================================================
//系统时间函数
void Tel_Book::System_Time(char (×tr)[30])
{
char timebuffer[10];
_strdate(timebuffer);
timestr[0]='2';
timestr[1]='0';
timestr[2]=timebuffer[6];
timestr[3]=timebuffer[7];
timestr[4]='\0';
strcat(timestr,"年");
timestr[6]=timebuffer[0];
timestr[7]=timebuffer[1];
timestr[8]='\0';
strcat(timestr,"月");
timestr[10]=timebuffer[3];
timestr[11]=timebuffer[4];
timestr[12]='\0';
strcat(timestr,"日");
strcat(timestr," ");
_strtime(timebuffer);
strcat(timestr,timebuffer);
return;
}
//===============================================================================================
//清屏函数
void Tel_Book::Screen_Clr()
{
system("cls");
cout<<endl;
return;
}
//===============================================================================================
//信息备份函数
void Tel_Book::Info_Bak()
{
fstream testfile;
char ch;
Person_Info *next_entry = head;
if(count == 0)
{
cout<<endl<<"您的电话本里没有任何记录."<<endl<<endl;
return;
}
testfile.open(bakdatstr,ios::in);
testfile.get();
if(testfile.eof())
{
testfile.close();
cout<<endl<<"您尚未备份,备份中......"<<endl;
if(Dat_Bak(1))
{
cout<<endl<<"数据备份成功."<<endl<<endl;
return;
}
else
{
cout<<endl;
return;
}
}
else
{
testfile.close();
while(1)
{
cout<<endl<<"友情提示:还原数据将覆盖您现有的记录,请谨慎操作"<<endl;
cout<<endl<<"备份数据请按B,还原数据请按R,退出请按Q:";
cin.get(ch);
if(ch == '\n')
continue;
if(Rest_Eat())
continue;
if(ch == 'B' || ch == 'b')
{
if(Dat_Bak(1))
{
cout<<endl<<"数据备份成功."<<endl<<endl;
return;
}
else
{
cout<<endl;
return;
}
}
else if(ch == 'R' || ch == 'r')
{
if(Dat_Bak(0))
{
while(next_entry != NULL)
{
head = next_entry->next;
delete next_entry;
next_entry = head;
}
Info_Read();
cout<<endl<<"数据还原成功."<<endl<<endl;
return;
}
else
{
cout<<endl;
return;
}
}
else if(ch == 'Q' || ch == 'q')
{
cout<<endl;
return;
}
else
continue;
}
}
}
//===============================================================================================
//数据备份函数
bool Tel_Book::Dat_Bak(bool bak)
{
fstream infile;
fstream outfile;
char tempchar;
infile.open(datstr,ios::in|ios::out|ios::binary);
outfile.open(bakdatstr,ios::out|ios::in|ios::binary);
if(!infile || !outfile)
{
cout<<endl<<"数据文件读取失败."<<endl;
return false;
}
if(bak)
{
while(infile.get(tempchar))
outfile.put(tempchar);
}
else
{
while(outfile.get(tempchar))
infile.put(tempchar);
}
infile.close();
outfile.close();
return true;
}
//===============================================================================================
//电话本内容信息导出函数
void Tel_Book::Info_Out()
{
fstream outfile;
Person_Info *next_entry = head;
unsigned int i = 0;
char htmlstr[]="<!DOCTYPE html PUBLIC \'-//W3C//DTD XHTML 1.0 Transitional//EN\' \'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\'>\n<html xmlns=\'http://www.w3.org/1999/xhtml\'>\n<head>\n<meta http-equiv=\'Content-Type\' content=\'text/html; charset=gb2312\' />\n<title>电话本清单列表</title>\n<style type=\'text/css\'>\n<!--\n.STYLE1 {font-size: 24px;font-weight: bold;}\n.STYLE2 {font-size: 12px;}\n.STYLE3 {font-size: 12px; font-weight: bold;}\n-->\n</style>\n</head>\n<body>\n<p align=\'center\' class=\'STYLE1\'>电 话 本 清 单 列 表</p>\n<table width=\'1000\' height=\'63\' border=\'1\' align=\'center\' cellpadding=\'0\' cellspacing=\'0\' bordercolor=\'#000000\' style=\'border-collapse:collapse\'>\n<tr class=\'STYLE3\'>\n<td width=\'36\' height=\'22\' align=\'center\'><span class=\'STYLE3 STYLE2\'>ID</span></td>\n<td width=\'84\' align=\'center\'><span class=\'STYLE3\'>姓 名</span></td>\n<td width=\'36\' align=\'center\'><span class=\'STYLE3\'>性别</span></td>\n<td width=\'102\' align=\'center\'><span class=\'STYLE3\'>电 话</span></td>\n<td width=\'118\' align=\'center\'><span class=\'STYLE3\'>出生年月</span></td>\n<td width=\'82\' align=\'center\'><span class=\'STYLE3\'>手 机</span></td>\n<td width=\'181\' align=\'center\'><span class=\'STYLE3\'>电子邮箱</span></td>\n<td width=\'65\' align=\'center\'><span class=\'STYLE3\'>QQ号码</span></td>\n<td width=\'107\' align=\'center\'><span class=\'STYLE3\'>通信地址</span></td>\n<td width=\'76\' align=\'center\'><span class=\'STYLE3\'>邮政编码</span></td>\n<td width=\'89\' align=\'center\'><span class=\'STYLE3\'>修改时间</span></td>\n</tr>\n";
if(count == 0)
{
cout<<endl<<"您的电话本里没有任何记录."<<endl<<endl;
return;
}
outfile.open(outstr,ios::out);
outfile<<htmlstr;
while(next_entry != NULL)
{
if(stricmp(next_entry->name,"") != 0)
{
i++;
outfile<<"<tr>\n<td width='36' height='22' align='center'><span class='STYLE2'>";
outfile<<i;
outfile<<"</span></td>\n<td width='84' align='center'><span class='STYLE2'>";
outfile<<next_entry->name;
outfile<<"</span></td>\n<td width='36' align='center'><span class='STYLE2'>";
outfile<<next_entry->sex;
outfile<<"</span></td>\n<td width='102' align='center'><span class='STYLE2'>";
outfile<<next_entry->tel;
outfile<<"</span></td>\n<td width='118' align='center'><span class='STYLE2'>";
if(stricmp(next_entry->born,"") != 0)
{
if(strlen(next_entry->born) == 6)
outfile<<next_entry->born[0]<<next_entry->born[1]<<next_entry->born[2]<<next_entry->born[3]<<"年"<<next_entry->born[4]<<next_entry->born[5]<<"月";
else
outfile<<next_entry->born[0]<<next_entry->born[1]<<next_entry->born[2]<<next_entry->born[3]<<"年"<<next_entry->born[4]<<next_entry->born[5]<<"月"<<next_entry->born[6]<<next_entry->born[7]<<"日";
}
outfile<<"</span></td>\n<td width='82' align='center'><span class='STYLE2'>";
outfile<<next_entry->mo_tel;
outfile<<"</span></td>\n<td width='181' align='center'><span class='STYLE2'>";
outfile<<next_entry->email;
outfile<<"</span></td>\n<td width='65' align='center'><span class='STYLE2'>";
outfile<<next_entry->qq;
outfile<<"</span></td>\n<td width='107' align='center'><span class='STYLE2'>";
outfile<<next_entry->addr;
outfile<<"</span></td>\n<td width='76' align='center'><span class='STYLE2'>";
outfile<<next_entry->postid;
outfile<<"</span></td>\n<td width='89' align='center'><span class='STYLE2'>";
outfile<<next_entry->modtime;
outfile<<"</span></td>\n</tr>\n";
}
next_entry = next_entry->next;
}
outfile<<"</table>\n</body>\n</html>\n";
outfile.close();
cout<<endl<<"导出成功,您现在可以使用浏览器将根目录下的"<<outstr<<"文件打开."<<endl<<endl;
}
//===============================================================================================
//电话本内容信息读出函数
void Tel_Book::Info_Read()
{
unsigned int i;
char tempchar;
fstream infile;
bool flag=true;
count = 0;
Person_Info *next_entry = NULL;
head = NULL;
tail = NULL;
infile.open(datstr,ios::in|ios::binary);
if(!infile)
{
cout<<endl<<"数据文件读取失败."<<endl;
return;
}
infile.get();
while(!infile.eof())
{
infile.seekp(infile.tellg() - 1);
if(count == 0 && flag)
{
next_entry = head = tail = new Person_Info;
next_entry->next = NULL;
next_entry->prv = NULL;
}
else
{
next_entry = new Person_Info;
next_entry->prv = tail;
tail->next = next_entry;
tail = next_entry;
next_entry->next = NULL;
}
for(i=0;i<structsize;i++)
{
infile.get(tempchar);
((char *)next_entry)[i] = tempchar ^ datcode;
}
count++;
if(stricmp(next_entry->name,"") == 0)
{
count--;
flag = false;
}
infile.get();
}
infile.close();
}
//===============================================================================================
//信息检索函数
void Tel_Book::Info_Find()
{
char ch;
char findstr[20];
char num[10];
unsigned int id;
unsigned int othercount;
unsigned int *ids = new unsigned int[count];
unsigned int i;
Person_Info *next_entry = NULL;
if(count == 0)
{
cout<<endl<<"您的电话本里没有任何记录."<<endl<<endl;
return;
}
while(1)
{
cout<<endl<<setw(80)<<setfill('=')<<"=";
cout<<"按 姓 名 查找请按N"<<endl;
cout<<"按 电话号码 查找请按T"<<endl;
cout<<"退出请按Q"<<endl;
cout<<setw(80)<<setfill('=')<<"=";
while(1)
{
cout<<endl<<"请输入您的查找方式:";
cin.get(ch);
if(ch == '\n')
continue;
if(Rest_Eat())
continue;
if(ch != 'N' && ch != 'n' && ch != 'T' && ch !='t' && ch != 'Q' && ch != 'q')
continue;
break;
}
if(ch == 'N' || ch == 'n')
{
while(1)
{
cout<<endl<<"请输入您要查找的姓名:";
next_entry = head;
id = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -