声明: 非原创
/*个人通讯录*/
#include
#include
#include
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
#include
#include
#include
#include
#include
#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<
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
{
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
}
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
}
RecordList::iterator RecordList::erase(iterator pos){
delete(*pos);
return list
}
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<
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<<"姓名"<
cout<<"一共找到"<
case '1':break;
case '2':{
if(!size()){
cout<<"记录为0,无法修改。"<
}
cout<<"要修改第几个?"<<"1~"<
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,无法修改。"<
}
cout<<"要删除第几个?"<<"1~"<
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)"<
case '1':addRecord();break;
case '2':break;
}
}
void AddressBook::queryRecord(){
cout<<"1"<
string patten;
switch(menuSelect()){
case '1':{
type=0;
cout<<"请输入姓名"<
}break;
case '2':{
type=1;
cout<<"请输入电话号码"<
}break;
}
Iterator it=begin();
list
cout<<"姓名"<
cout<<*it;
li.push_back(it);
it++;
}
cout<<"一共找到"<
case'1':break;
case'2':{
if(!li.size()){
cout<<"记录为0,无法修改。"<
}
cout<<"要修改第几个?"<<"1~"<
cin>>i;
Record *prec=new Record;
cout<<"姓名:"<<'\n'<<"电话:"<<'\n';
cin>>prec;
list
while(i!=1){
it++;
i--;
}
editRecord(prec,*it);
}break;
case'3':{
if(!li.size()){
cout<<"记录为0,无法修改。"<
}
cout<<"要删除第几个?"<<"1~"<
cin>>i;
list
while(i!=1){
it++;
i--;
}
removeRecord(*it);
}break;
}
}
void AddressBook::saveRecords(){
cout<<"1"<
case '1':{
ofstream file("default",ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '2':{
cout<<"请输入您定义的文件名:"<
cin>>fname;
ofstream file(fname.c_str(),ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::loadRecords(){
cout<<"1"<
case '1':{
cout<<"1"<
case '1':
AddressList::clear();break;
case '2':break;
}
ifstream file("default");
AddressList::loadRecords(file);
file.close();
}break;
case '2':{
cout<<"请输入文件名:"<
cin>>fname;
ifstream file(fname.c_str());
AddressList::loadRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::clear(){
cout<<"是否要清除内存中所有内容?(Y/N)"<
case '1':AddressList::clear();break;
case '2':break;
}
}
void AddressBook::quit(){
cout<<"是否要保存文件?(Y/N)"<
case'1':saveRecords();break;
case'2':break;
}
exit(0);
}
int main(int argc, char *argv[])
{
AddressBook a;
a.start();
system("PAUSE");
return EXIT_SUCCESS;
}
这个是我大一的时候做的
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
struct person
{
char name[10];
char tel[15];
char addr[50];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void del();
void menu()
{
printf("\n\t\t******************通讯录*****************");
printf("\n\t\t* *");
printf("\n\t\t* 添加,请按1 *");
printf("\n\t\t* 查找,请按2 *");
printf("\n\t\t* 修改,请按3 *");
printf("\n\t\t* 删除,请按4 *");
printf("\n\t\t* 输出,请按5 *");
printf("\n\t\t* 退出,请按0 *");
printf("\n\t\t*****************************************");
}
void main()
{
int m;
creat();
system("cls");
while(1)
{
menu();
printf("\n请选择:");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: del();
break;
case 5: output();
break;
case 0: exit(1);
}
printf("\n\n操作完毕,请在次选择!");
}
else
printf("\n\n选择错误,请在次选择!");
}
}
void creat()
{
system("cls");
struct person one;
printf("\n请输入通讯录名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\n不能建立通讯录!");
exit(1);
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
printf("\n请输入姓名、电话号码及住址(以0结束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
void output()
{
system("cls");
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能建立通讯录!");
exit(1);
}
printf("\n\n%20s\n","通 讯 录");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
void append()
{
system("cls");
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能建立通讯录!");
exit(1);
}
printf("\n请输入添加的姓名、电话号码及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
void search()
{
system("cls");
int k=0;
char namekey[10];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n对不起,通讯录中没有此人的记录.");
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
void modify()
{
system("cls");
int m=0,k=0;
long offset;
char namekey[10];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
printf("\n请输入新姓名、电话号码及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n对不起,通讯录中没有此人的记录.");
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
void del()
{
system("cls");
int m,k=0,flag;
long offset1,offset2;
char namekey[10],valid[4];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
printf("\n确实要删除,按1; 不删除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n"," "," "," ");
}
}
else
printf("\n对不起,通讯录中没有此人的记录.");
fclose(fp);
printf("按任意键继续......");
getch();
system("cls");
}
我们的结业设计就是这个
很麻烦
但是处理好的话不难~