声明: 非原创
/*个人通讯录*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct dataWab{
char name[8];
char telephone[16];
char mobile[16];
char address[16];
char unit[16];
struct dataWab *nextPtr;
}; /*自引用结构,用作通讯录的数据结构*/
typedef struct dataWab DATAWAB;
typedef DATAWAB *DATAWABPTR;
void insertWab(DATAWABPTR*,char[],char[],char[],char[],char[]); /*插入函数*/
void menuWab(void); /*菜单函数*/
void printWab(DATAWABPTR); /*打印所有记录函数*/
void seekWab(DATAWABPTR,char[]); /*查找函数*/
void deleteWab(DATAWABPTR,char[]); /*删除函数*/
void modifyWab(DATAWABPTR,char[]); /*修改函数*/
int test(DATAWABPTR); /*测试函数,测试通讯录内是否有记录*/
void printone(DATAWABPTR); /*打印单条记录函数*/
int controlWab(void); /*程序分支控制函数*/
int main()
{
DATAWABPTR startPtr=NULL;
DATAWABPTR tempPtr;
FILE *fileWab;
char menu; /*菜单操作控制变量*/
char insert; /*插入确认控制变量*/
char modifyName[8]; /*查找时的临时姓名存储*/
char deleteName[8]; /*删除时的临时姓名存储*/
char tempName[8]; /*临时姓名存储*/
char tempTelephone[16]; /*临时电话存储*/
char tempMobile[16]; /*临时手机存储*/
char tempAddress[16]; /*临时地址存储*/
char tempUnit[16]; /*临时单位存储*/
char seekName[8]; /*查找时的临时姓名存储*/
if(!(fileWab=fopen("wab.dat","rb")))
return 0;
tempPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
while(fread(tempPtr,sizeof(DATAWAB),1,fileWab)==1){
tempPtr->nextPtr=startPtr;
startPtr=tempPtr;
tempPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
}
fclose(fileWab);
puts("\n\n\t\t欢迎使用个人通讯录1.0\n");
menuWab();
menu=getch();
while(menu!='6'){
switch(menu){
case '1':
printWab(startPtr);
break;
case '2':
while(controlWab()){
printf("\n查找姓名:\n#>");
scanf("%s",seekName);
seekWab(startPtr,seekName);
}
break;
case '3':
while(controlWab()){
puts("\n录入个人信息\n");
printf("姓名:");
scanf("%s",tempName);
printf("\n电话:");
scanf("%s",tempTelephone);
printf("\n手机:");
scanf("%s",tempMobile);
printf("\n地址:");
scanf("%s",tempAddress);
printf("\n单位:");
scanf("%s",tempUnit);
printf("录入正确请按 1 \n#>");
insert=getch();
if(insert=='1')
insertWab(&startPtr,tempName,tempTelephone,tempMobile,
tempAddress,tempUnit);
}
break;
case '4':
while(controlWab()){
printf("\n删除姓名\n#>");
scanf("%s",deleteName);
deleteWab(startPtr,deleteName);
}
break;
case '5':
while(controlWab()){
printf("\n修改姓名\n#>");
scanf("%s",modifyName);
modifyWab(startPtr,modifyName);
}
break;
default:
puts("\n\n输入选项错误,请重新输入\n");
break;
}
menuWab();
menu=getch();
}
if((fileWab=fopen("wab.dat","wb"))==NULL)
return 0;
while(startPtr!=NULL){
fwrite(startPtr,sizeof(DATAWAB),1,fileWab);
startPtr=startPtr->nextPtr;
}
puts("\n\n\t\t谢谢使用个人通讯录1.0,再见\n");
}
void menuWab(void) /*菜单函数*/
{
puts("\t\t\t主菜单");
puts("**********************************************************");
puts("**********************************************************");
puts("**\t<<-1->>所有记录\t\t<<-2->>查找记录\t\t**");
puts("**\t<<-3->>增加记录\t\t<<-4->>删除记录\t\t**");
puts("**\t<<-5->>修改记录\t\t<<-6->>退出程序\t\t**");
puts("**********************************************************");
puts("**********************************************************");
puts("\n请选择 1-6");
printf("#>");
}
void insertWab(DATAWABPTR *tempPtr,char temp2Name[],char temp2Telephone[],
char temp2Mobile[],char temp2Address[],char temp2Unit[])
/*插入函数*/
{
DATAWABPTR newPtr;
newPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
strcpy(newPtr->name,temp2Name);
strcpy(newPtr->telephone,temp2Telephone);
strcpy(newPtr->mobile,temp2Mobile);
strcpy(newPtr->address,temp2Address);
strcpy(newPtr->unit,temp2Unit);
newPtr->nextPtr=*tempPtr;
*tempPtr=newPtr;
}
void printWab(DATAWABPTR printPtr) /*打印函数*/
{
if(!(test(printPtr))){
printf("\n%8s%16s%16s%16s%16s\n","姓名","电话","手机","住址","单位");
while(printPtr!=NULL){
printf("%8s%16s%16s%16s%16s\n",printPtr->name,printPtr->telephone,
printPtr->mobile,printPtr->address,printPtr->unit);
printPtr=printPtr->nextPtr;
}
}
}
void seekWab(DATAWABPTR seekPtr,char tempName[])
/*查找函数*/
{
if(!(test(seekPtr))){
while(seekPtr!=NULL){
if(!(strcmp(tempName,seekPtr->name))){
printone(seekPtr);
seekPtr=seekPtr->nextPtr;
}
else
seekPtr=seekPtr->nextPtr;
}
}
}
void deleteWab(DATAWABPTR deletePtr,char deleteName[])
/*删除函数*/
{
DATAWABPTR tempPtr,forePtr;
forePtr=deletePtr;
tempPtr=deletePtr->nextPtr;
while(tempPtr!=NULL){
if(!(strcmp(tempPtr->name,deleteName))){
forePtr->nextPtr=tempPtr->nextPtr;
free(tempPtr);
break;
}
else{
forePtr=tempPtr;
tempPtr=tempPtr->nextPtr;
}
}
}
void modifyWab(DATAWABPTR modifyPtr,char modifyName[])
/*修改函数*/
{
if(!(test(modifyPtr))){
while(modifyPtr!=NULL){
if(!(strcmp(modifyName,modifyPtr->name))){
printf("请输入新姓名:");
scanf("%s",modifyPtr->name);
printf("\n请输入新电话:");
scanf("%s",modifyPtr->telephone);
printf("\n请输入新手机:");
scanf("%s",modifyPtr->mobile);
printf("\n请输入新地址:");
scanf("%s",modifyPtr->address);
printf("\n请输入新单位:");
scanf("%s",modifyPtr->unit);
break;
}
else
modifyPtr=modifyPtr->nextPtr;
}
}
}
int test(DATAWABPTR testPtr)
{
if(testPtr==NULL)
puts("通讯录内没有任何记录");
else
return 0;
return 1;
}
void printone(DATAWABPTR printone)
{
printf("\n%8s%16s%16s%16s%16s\n","姓名","电话","手机","住址","单位");
printf("%8s",printone->name);
printf("%16s",printone->telephone);
printf("%16s",printone->mobile);
printf("%16s",printone->address);
printf("%16s\n",printone->unit);
}
int controlWab(void)
{
puts("继续执行请按 1");
if(getch()=='1')
return 1;
return 0;
}
回答者: 虑而后能得 - 助理 二级 2-19 11:36
我来评论>>
提问者对于答案的评价:感激不尽~~评价已经被关闭 目前有 1 个人评价
好
100% (1) 不好
0% (0)
相关内容
• 救命啊! 哪位高手能帮我用C语言编写一个通讯录管理的...
• 3110c 通讯录的问题
• 索爱510c的电话本备份?
• 6120C的电话本能不能通过什么方式转到6500C来
• 关于诺基亚7310c通讯录显示姓名的问题~~~
更多关于c语言编写通讯录管理程序的问题>>
查看同主题问题:高手 语言 通讯录 救命 管理 程序
其他回答 共 4 条
分少啊,没动力,谁会写啊
回答者: 229273121 - 见习魔法师 三级 2-19 10:16
本人的C++通讯录程序代码
#include<iostream>
#include<string>
#include<iomanip>
#include<list>
#include<fstream>
#include<algorithm>
#define SPACE "\t\t\t"
using namespace std;
struct Record{
string name;
string tel;
public:
friend ostream& operator<<(ostream& os,const Record *rec){
return os<<rec->name<<SPACE<<rec->tel<<endl;
};
friend istream& operator>>(istream& is,Record *rec){
is>>rec->name;
is>>rec->tel;
return is;
}
static bool comp(string patten,int type,Record *rec){
switch(type){
case 0: {return rec->name==patten;}break;
case 1: {return rec->tel==patten;}break;
}
return false;
}
static bool isvan(Record *rec){
return (rec->name==""&&rec->tel=="");
}
};
class RecordList:public list<Record*>
{
public:
friend ostream& operator<<(ostream& os,const RecordList &c_rl){
RecordList::const_iterator it=c_rl.begin();
while(it!=c_rl.end()){
os<<*it;
it++;
}
return os;
}
friend istream& operator>>(istream& is,RecordList &rl){
RecordList::const_iterator it;
while(!is.eof()){
Record *prec=new Record;
is>>prec;
if(Record::isvan(prec)) continue;
rl.list<Record*>::push_back(prec);
}
return is;
}
void clear();
iterator erase(iterator pos);
~RecordList(){
this->clear();
}
};
void RecordList::clear(){
RecordList::const_iterator it=this->begin();
while(it!=this->end()){
delete(*it);
it++;
}
this->list<Record*>::clear();
}
RecordList::iterator RecordList::erase(iterator pos){
delete(*pos);
return list<Record*>::erase(pos);
}
class AddressList{
protected:
RecordList recList;
public:
typedef RecordList::iterator Iterator;
//从通信录中寻找一个记录
virtual Iterator findRecord(string patten,int type,Iterator from);
virtual void addRecord(Record* rec);
virtual void removeRecord(Iterator it);
virtual void editRecord(Record* rec,Iterator it);
virtual void saveRecords(ostream& os);
virtual void loadRecords(istream& is);
virtual void addRecords(istream& is);
virtual ~AddressList();
const int size() const{return (int)recList.size();}
const void clear(){recList.clear();}
Iterator begin(){return recList.begin();}
Iterator end() {return recList.end();}
};
AddressList::Iterator AddressList::findRecord(string patten,int type,Iterator from){
Iterator it;
it=from;
while(it!=recList.end()){
if(Record::comp(patten,type,*it)){
return it;
}
it++;
}
return it;
}
void AddressList::addRecord(Record* rec){
recList.push_back(rec);
}
void AddressList::removeRecord(Iterator it){
recList.erase(it);
}
void AddressList::editRecord(Record* rec,Iterator it){
delete(*it);
*it=rec;
}
void AddressList::saveRecords(ostream& os){
os<<recList;
}
void AddressList::loadRecords(istream& is){
is>>recList;
}
void AddressList::addRecords(istream& is){
}
AddressList::~AddressList(){
recList.clear();
}
class AddressBook : public AddressList{
protected:
bool isModified;//通信录是否已被修改还未保存
public:
AddressBook();
virtual ~AddressBook();
//开始通信录的用户界面操作
virtual void start();
//菜单处理函数管理程序的用户界面操作
virtual void handleMenu();
//显示记录
virtual void displayRecords();
//查询记录
virtual void queryRecord();
//添加记录
virtual void addRecord();
//保存记录
virtual void saveRecords();
//读取记录
virtual void loadRecords();
//结束程序
virtual void quit();
//清空当前记录信息
virtual void clear();
protected:
//菜单选择函数
char menuSelect();
//显示表头
void dispTable();
};
AddressBook::AddressBook(){
}
AddressBook::~AddressBook(){;}
char AddressBook::menuSelect(){
char a=0;
cin>>a;
if(a=='y'||a=='Y'){a='1';}
if(a=='n'||a=='N'){a='2';}
return a;
}
void AddressBook::start(){
cout << "\n\t***** 欢迎使用通讯录管理程序 *****";
handleMenu();
}
void AddressBook::handleMenu(){
cout << "\n\n\t\t\t-- 菜单管理 --\n\n";
cout << "\tA -- 添加人员纪录 (Add Record)\n"
<< "\tF -- 查询纪录 (Find Record)\n"
<< "\tD -- 显示纪录 (Display Records)\n"
<< "\tL -- 从文本或标准输入流中加载纪录 (Load Records)\n"
<< "\tC -- 清空当前记录信息\n"
<< "\tS -- 保存记录 (Save Records)\n"
<< "\tQ -- 结束程序 (QUIT)\n";
switch(menuSelect()){
case 'A':
case 'a':addRecord();break;
case 'F':
case 'f':queryRecord();break;
case 'D':
case 'd':displayRecords();break;
case 'L':
case 'l':loadRecords();break;
case 'C':
case 'c':clear();break;
case 'S':
case 's':saveRecords();break;
case 'Q':
case 'q':quit();break;
}
handleMenu();
}
void AddressBook::displayRecords(){
cout<<"姓名"<<SPACE<<"电话"<<endl;
AddressList::saveRecords(cout);
cout<<"一共找到"<<size()<<"个对象。"<<endl;
cout<<"1"<<SPACE<<"返回"<<endl;
cout<<"2"<<SPACE<<"修改"<<endl;
cout<<"3"<<SPACE<<"删除"<<endl;
switch(menuSelect()){
case '1':break;
case '2':{
if(!size()){
cout<<"记录为0,无法修改。"<<endl;
break;
}
cout<<"要修改第几个?"<<"1~"<<size()<<endl;
int i;
cin>>i;
if(i<1||i>size()){
break;
}
Record *prec=new Record;
cout<<"姓名:"<<'\n'<<"电话:"<<'\n';
cin>>prec;
Iterator it=recList.begin();
while(i!=1){
it++;
i--;
}
editRecord(prec,it);
}break;
case'3':{
if(!size()){
cout<<"记录为0,无法修改。"<<endl;
break;
}
cout<<"要删除第几个?"<<"1~"<<size()<<endl;
int i;
cin>>i;
if(i<1||i>size()){
break;
}
Iterator it=recList.begin();
while(i!=1){
it++;
i--;
}
removeRecord(it);
}break;
}
}
void AddressBook::addRecord(){
string name,tel;
cout << "\n输入名字: ";
cin >> name;
cout << "输入电话: ";
cin >> tel;
Record* temp=new Record;
temp->name=name;
temp->tel=tel;
AddressList::addRecord(temp);
cout<<"是否继续(Y/N)"<<endl;
switch(menuSelect()){
case '1':addRecord();break;
case '2':break;
}
}
void AddressBook::queryRecord(){
cout<<"1"<<SPACE<<"按姓名查找"<<endl;
cout<<"2"<<SPACE<<"按电话查找"<<endl;
int type=-1;
string patten;
switch(menuSelect()){
case '1':{
type=0;
cout<<"请输入姓名"<<endl;
cin>>patten;
}break;
case '2':{
type=1;
cout<<"请输入电话号码"<<endl;
cin>>patten;
}break;
}
Iterator it=begin();
list<Iterator> li;
cout<<"姓名"<<SPACE<<"电话"<<endl;
while((it=findRecord(patten,type,it))!=end()){
cout<<*it;
li.push_back(it);
it++;
}
cout<<"一共找到"<<li.size()<<"个对象。"<<endl;
cout<<"1"<<SPACE<<"返回"<<endl;
cout<<"2"<<SPACE<<"修改"<<endl;
cout<<"3"<<SPACE<<"删除"<<endl;
switch(menuSelect()){
case'1':break;
case'2':{
if(!li.size()){
cout<<"记录为0,无法修改。"<<endl;
break;
}
cout<<"要修改第几个?"<<"1~"<<li.size()<<endl;
int i;
cin>>i;
Record *prec=new Record;
cout<<"姓名:"<<'\n'<<"电话:"<<'\n';
cin>>prec;
list<Iterator>::const_iterator it=li.begin();
while(i!=1){
it++;
i--;
}
editRecord(prec,*it);
}break;
case'3':{
if(!li.size()){
cout<<"记录为0,无法修改。"<<endl;
break;
}
cout<<"要删除第几个?"<<"1~"<<li.size()<<endl;
int i;
cin>>i;
list<Iterator>::const_iterator it=li.begin();
while(i!=1){
it++;
i--;
}
removeRecord(*it);
}break;
}
}
void AddressBook::saveRecords(){
cout<<"1"<<SPACE<<"使用默认文件名。"<<endl;
cout<<"2"<<SPACE<<"使用自定义文件名。"<<endl;
cout<<"3"<<SPACE<<"返回"<<endl;
switch(menuSelect()){
case '1':{
ofstream file("default",ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '2':{
cout<<"请输入您定义的文件名:"<<endl;
string fname;
cin>>fname;
ofstream file(fname.c_str(),ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::loadRecords(){
cout<<"1"<<SPACE<<"使用默认文件。"<<endl;
cout<<"2"<<SPACE<<"使用自定义文件。"<<endl;
cout<<"3"<<SPACE<<"返回"<<endl;
switch(menuSelect()){
case '1':{
cout<<"1"<<SPACE<<"覆盖当前内容"<<endl;
cout<<"2"<<SPACE<<"追加到当前文件"<<endl;
switch(menuSelect()){
case '1':
AddressList::clear();break;
case '2':break;
}
ifstream file("default");
AddressList::loadRecords(file);
file.close();
}break;
case '2':{
cout<<"请输入文件名:"<<endl;
string fname;
cin>>fname;
ifstream file(fname.c_str());
AddressList::loadRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::clear(){
cout<<"是否要清除内存中所有内容?(Y/N)"<<endl;
switch(menuSelect()){
case '1':AddressList::clear();break;
case '2':break;
}
}
void AddressBook::quit(){
cout<<"是否要保存文件?(Y/N)"<<endl;
switch(menuSelect()){
case'1':saveRecords();break;
case'2':break;
}
exit(0);
}
int main(int argc, char *argv[])
{
AddressBook a;
a.start();
system("PAUSE");
return EXIT_SUCCESS;
}
温馨提示:内容为网友见解,仅供参考