c语言电话簿管理系统
㈠ 求用C语言编写一个 通讯录管理系统 。设计要求如下
这个主要用到链表来做就可以的啦,链表的基本操作(创建、插入、删除等)要掌握好。之前我自己写有一个,和你的功能差不多,但是变量有所不同,给你参考参考吧,可以仿照。不懂的再问吧。代码如下:
#include"stdafx.h"
using namespace std;
//结构体
struct contaxt
{
char name[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
struct contaxt *next;
};
//函数声明
typedef struct contaxt CONT;
CONT *InsertCon(CONT *head);
CONT *AddContaxt(int n);
CONT *FindCon(CONT *head);
CONT *AmendMessage(CONT *head);
CONT *DeleteMessage(CONT *head);
void showmessage(CONT *head);
int menu(CONT *head);
CONT *saveFileCONT(CONT *head );
CONT *readFileCONT(CONT *head);
int choose;
//入口
void main()
{
//system("color F4");
CONT *head;
head = NULL;
menu(head);
}
//系统主菜单
int menu(CONT *head)
{
cout << "请选择操作菜单!!!" << endl ;
do
{
cout << "---------------------------欢迎进入通讯录管理系统-------------------------------"
<< "---------------------------------操作菜单---------------------------------------";
cout << "--------------------------------------------------------------------------------"
<< " 1 添加联系人信息 "
<< " 2 插入联系人信息 " << endl
<< " 3 查找联系人信息 "
<< " 4 修改联系人信息 " << endl
<< " 5 删除联系人信息 "
<< " 6 显示联系人信息 " << endl
<< " 7 退出本系统 " << endl
<< " 8 保存文件 " << endl
<< " 9 读取文件 " << endl
<< "--------------------------------------------------------------------------------" << endl;
cout << "请选择操作菜单!!!" << endl ;
int choose;
//choose = getchar();
fflush(stdin);
//return choose;
cin >> choose;
if(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7&&choose != 8)
{
cout << "输入错误:"<<endl;
}
else
{
switch(choose)
{
case 1:
int n;
do
{
fflush(stdin);
cout << "请确定要添加几个联系人:";
cin >> n;
}while(n <= 0);
head = AddContaxt(n);
break;
case 2:
system("CLS");
head = InsertCon(head);
break;
case 3:
head = FindCon(head);
break;
case 6:
system("CLS");
showmessage(head);
break;
case 7:
exit(0);
break;
case 4:
system("CLS");
AmendMessage(head);
break;
case 5:
system("CLS");
DeleteMessage(head);
break;
case 8:
system("CLS");
head = saveFileCONT(head );
break;
case 9:
// head = readFileCONT(head);
default:
system("CLS");
cout << "你输入错误,请重新选择!!!" << endl;
break;
}
}
}while(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7);
return choose;
}
//添加联系人
CONT *AddContaxt(int n)
{
CONT *p,*q,*head;
int i;
system("CLS");
p = (CONT *)malloc(sizeof(CONT));
cout << "开始添加联系人信息:"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入姓别:";
cin >> p->sex ;
cout << "输入班级:";
cin >> p->Class ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入qq号:";
cin >> p->qq ;
head = p;
for(i = 2;i <= n;i++)
{
q = (CONT *)malloc(sizeof(CONT));
p->next = q ;
p = q;
system("CLS");
cout<< "总共需要输入"<< n << "个联系人的信息,"<<"现在你已输入" << i-1<< "个联系人信息."<< endl<< "请继续输入第"<<i << "个人的信息!!"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入姓别:";
cin >> p->sex ;
cout << "输入班级:";
cin >> p->Class ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入qq号:";
cin >> p->qq ;
}
p->next = NULL;
return head;
}
//插入联系人信息
CONT *InsertCon(CONT *head)
{
CONT *pc,*p,*q;
int choose ;
char name[10];
if(head ==NULL)
{
cout << "还没有添加任何联系人,请先添加联系人!!"<< endl;
return head;
}
pc = (CONT *)malloc(sizeof(CONT));
cout << "请依次输入被插入人的信息:姓名 /性别/班级/住址/手机号/qq号"<< endl;
cin >> pc->name >> pc->sex >> pc->Class >> pc->Adrre >> pc->Mobi >> pc->qq ;
pc ->next = NULL;
cout << "请选择自己喜欢的插入方式: "<< endl<< "1 插入在自己喜欢的人之前"<< endl << "2 插入在最后" << endl << "3 插入在开头" << endl;
do {
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入自己喜欢的人的名字:";
cin >> name ;
if(strcmp(head->name ,name)==0)
{
pc->next = head;
head = pc;
return head;
}
else
{
q = head;
p = head->next ;
while(strcmp(p->name,name)!=0 && p->next !=NULL)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
pc->next = p ;
q = pc;
return head;
}
else
{
p->next = pc;
return head;
}
}
break;
case 2:
//cout << "请输入自己喜欢的人的名字:";
//cin >> name ;
p = head;
while(p->next != NULL)
p = p->next ;
p->next = pc;
return head;
break;
case 3:
//cout << "请输入自己喜欢的人的名字:";
//cin >> name ;
pc->next = head;
head = pc;
return head;
break;
default :
cout<<"error"<< endl;
break;
}
}while(choose!=1 &&choose != 2 && choose != 3);
}
//查找联系人信息
CONT *FindCon(CONT *head)
{
system("CLS");
char na[20];
CONT *p,*q;
if(head == NULL)
{
cout << "没有任何信息可查找,请先添加信息!!"<< endl;
return head;
}
cout << "请您输入要查找人的姓名:"<< endl;
cin >> na;
p = head;
while(p!= NULL)
{
if(strcmp(p->name ,na)==0)
{
q = p;
cout << "找到联系人,信息如下!!"<< endl;
cout << "姓名:" << q->name <<" 性别: " << q->sex << q->Class <<" 住址: " <<q->Adrre <<" 手机号: " <<q->Mobi <<" qq号: "
<< q->qq << endl;
break;
}
else
p = p->next ;
}
return head;
}
//修改联系人信息
CONT *AmendMessage(CONT *head)
{
CONT *p;
char nam[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
if(head == NULL)
{
cout<< "没有任何信息,请返回添加信息!!!"<< endl;
return head;
}
else
p = head;
cout << "请你输入要修改人的名字:";
cin >> nam;
while(p != NULL && p->name != nam)
{
if(strcmp(p->name ,nam)==0)
{
strcpy(p->name ,nam);
int choose;
do
{
cout << "请选择要修改哪一项的信息!!!"<< endl;
cout << "1 修改性别 2 修改班别 3 修改地址 4 修改手机号码 5 修改qq号 " << endl;
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入要修改后的性别:";
cin >> sex;
strcpy(p->sex , sex);
cout << "修改成功!!!"<< endl;
system("CLS");
return head;
break;
case 2:
cout << "请输入修改后的班别:";
cin >> Class;
strcpy(p->Class ,Class);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 3:
cout << " 请输入修改后的地址:";
cin >> Adrre;
strcpy(p->Adrre ,Adrre);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 4:
cout << "请输入修改后的手机号码:";
cin >> Mobi;
strcpy(p->Mobi ,Mobi);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 5:
cout << "请输入修改后的qq号:";
cin >> qq;
strcpy(p->qq ,qq);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
default:
cout << "输入错误,重新选择!!" << endl;
break;
}
}while(choose != 0);
}
else if(p->next != NULL && p->name != nam)
{
p = p->next ;
}
else
{
cout << "输入名字错误,请重新选择4修改!!!" << endl;
return head;
}
}
//return head;
}
//删除联系人
CONT *DeleteMessage(CONT *head)
{
CONT *p,*q,*t;
char name[20];
char elem[10];
cout << "请输入要删除联系人的姓名:";
cin >> name;
if(head == NULL)
{
cout << "没有任何信息可删除,请添加信息!!"<< endl;
return head;
}
else if(strcmp(head->name,name)==0 )
{
t = head;
head = head->next ;
return head;
}
else
{
p = head->next ;
q = head;
}
while(p !=NULL && p->name != name)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
q->next = p->next ;
free(p);
//t = p;
}
else
{
cout<< "表中无该元素!" << endl;
return head;
}
if(t)
{
// elem = t->name ;
free(t);
}
return head;
}
//保存文件
CONT *saveFileCONT(CONT *head )
{
char *fileName;
FILE *fp;
CONT *p;
p = (CONT *)malloc(sizeof(CONT));
if(p=NULL)
{
cout <<"分配内存失败"<< endl;
// exit(0);
}
if(head ==NULL)
{
cout <<"没有任何信息,请先添加信息!!"<< endl;
return head;
}
else
{
p = head;
fileName = (char *)malloc(sizeof(char));
cout << "请输入文件名:" << endl;
cin >> fileName;
fileName = strcat(fileName,".txt");
if( (fp = fopen(fileName,"a")) == NULL)
cout << "can't open file" << endl;
do
{
//fwrite(head,sizeof(CONT),1,fp);
//fprintf(fp,p->name,p->sex,p->Class,p->Adrre,p->Mobi,p->qq );
fprintf(fp," 姓名:");
fprintf(fp, p->name );
fprintf(fp," 性别:");
fprintf(fp, p->sex);
fprintf(fp," 班别:");
fprintf(fp, p->Class);
fprintf(fp," 地址:");
fprintf(fp, p->Adrre);
fprintf(fp," 手机号:");
fprintf(fp, p->Mobi);
fprintf(fp," qq号:");
fprintf(fp, p->qq);
fprintf(fp,"\n");
cout << endl;
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p=p->next ;
}while(p!=NULL);
}
fclose(fp);
return head;
}
//读取文件
CONT *readFileCONT(CONT *head)
{
char *fileName;
CONT *p;
FILE *fp;
fileName = (char *)malloc(sizeof(char));
cout << "请输入要打开的文件名:";
cin >> fileName;
fileName = strcat(fileName,".txt");
fp = fopen(fileName,"r");
if(fp == NULL)
{
cout << "文件没有任何信息,请先输入信息!!"<< endl;
}
else
{
while(!feof(fp))
{
p = (CONT *)malloc(sizeof(CONT));
if(p == NULL)
{
cout << "分配内存失败!!"<< endl;
return head;
}
fscanf(fp,p->name );
fscanf(fp,p->sex );
fscanf(fp,p->Class );
fscanf(fp,p->Adrre );
fscanf(fp,p->Mobi );
fscanf(fp,p->qq );
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p->next = head;
head = p;
p = p->next ;
}
fclose(fp);
cout<< "读取信息成功!!!"<< endl;
}
fclose(fp);
return head;
}
void showmessage(CONT *head)
{
CONT *p;
int i=0;
if(head == NULL )
{
cout << "没有任何信息可输出,请添加信息!!!"<< endl;
}
else
{
p = head;
cout << "*******************************联系表如下***************************************";
do
{
i++;
cout << "--------------------------------------------------------------------------------";
cout << i << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p = p->next ;
}while(p!=NULL);
}
cout << "*********************************联系表***************************************";
cout << endl;
}
㈡ C语言程序设计 通讯录系统管理
// 头文件部分
//============================
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
// 初始化数据部分
int Menu();
void Start();
void Write();
void Read();
void Search();
void Detele();
void beauty();
void Rework();
void Lock();
int Judge();
//============================
struct Student{
char name[20];
char Tel[20];
char QQ[20];
int age;
}xy[100];
char wenjian[10000];
char Number[10];
char number=0;
int GOTO;
//
int main()
{
Start();
for(;;)
{
GOTO=Menu();
if(GOTO==1)
{
for(;;)
{
Write();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==2)
{
for(;;)
{
Read();
system("pause");
break;
}
}
if(GOTO==3)
{
for(;;)
{
Search();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==4)
{
for(;;)
{
Rework();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==5)
{
Detele();
break;
}
if(GOTO==6)
{
beauty();
break;
}
if(GOTO==7)
{
}
if(GOTO==8)
{
printf("");
break;
}
if(GOTO==9)
{
printf("\t\t\t退出系统\n\n");
return 0;
}
if(GOTO==0)
{
printf("");
break;
}
}
// beauty();
//
// Write();
// Read();
// Search();
// Rework();
// Detele();
return 0;
}
void Write()
{
printf("\t\t\t请输入好友名字\n");
scanf("%s",&xy[number].name);
printf("\t\t\t请输入好友电话号码\n");
scanf("%s",&xy[number].Tel);
printf("\t\t\t请输入好友QQ号码\n");
scanf("%s",&xy[number].QQ);
printf("\t\t\t请输入好友年龄\n");
scanf("%d",&xy[number].age);
FILE *file;
if((file=fopen("Friend.txt","rt"))==NULL)
{
file=fopen("Friend.txt","wt");
fprintf(file,"%-20s%-20s%-20s%-20s","姓名","电话","QQ","年龄");
}
file=fopen("Friend.txt","at");
fprintf(file,"%-20s%-20s%-20s%-20d\n",xy[number].name,xy[number].Tel,xy[number].QQ,xy[number].age);
fclose(file);
number++;
Number[0]=number;
FILE * Sta;
if((Sta=fopen("number.txt","r"))=NULL)
{
Sta=fopen("number.txt","wt");
exit(1);
}
Sta=fopen("number.txt","wt");
fprintf(Sta,"%d",Number[0]);
fclose(Sta);
}
void Read()
{
printf("\t\t\t您通讯录中一共有%d位好友\n",Number[0]);
FILE* read;
if((read=fopen("Friend.txt","r"))=NULL)
{
printf("\t\t\t您通讯中当前没有好友,请返回添加\n");
}
read=fopen("Friend.txt","rt");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
for(int i=0;i<number;i++)
{
fscanf(read,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
/* fread(wenjian,10000,1,read);
for(int i=0;i<number*80+80;i++)
{
printf("%c",wenjian[i]);
}
printf("\n");
*/
fclose(read);
}
void beauty()
{
int d;
do{
printf("choose");
scanf("%d",&d);
}while(d<1&&d>5);
if(d==1)
{
system("color 27");
printf("===1===");
}
if(d==2)
{
system("color 37");
printf("===2===");
}
if(d==3)
{
system("color 47");
printf("===3===");
}
if(d==4)
{
system("color 57");
printf("===4===");
}
if(d==5)
{
system("color 67");
printf("===5===");
}
}
void Start()
{
FILE* Start;
if((Start=fopen("number.txt","r"))==NULL)
{
Start=fopen("number.txt","w");
number=0;
Number[0]=number;
fscanf(Start,"%d",&Number[0]);
printf("\t\t\t您的通讯录中还没有任何好友,赶快添加吧.此次操作需要重新运行程序。\n");
exit(1);
}
Start=fopen("number.txt","r");
fscanf(Start,"%d",&Number[0]);
number=Number[0];
fclose(Start);
}
void Search()
{
int change=0;
char Searchname[20];
printf("\t\t\t输入要查找的好友的名字\n\n");
scanf("%s",&Searchname);
fflush(stdin);
printf("\t\t\t您要查找的好友是 %s \n\n",Searchname);
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Searchname)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
}
fclose(Search);
}
if(change==0)
{
printf("\t\t\t您要查找的好友不存在,请返回菜单添加。\n");
}
}
void Rework()
{
char Rework[20];
printf("\t\t\t输入要修改的好友的名字\n\n");
scanf("%s",&Rework);
printf("\t\t\t您要修改 %s 的信息\n\n",Rework);
int Change=0;
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Rework)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
Change=1;
int Choose;
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
//---------------------------------------------------------------------
do
{
printf("\t\t\tchoose\t\t\t1-----3");
scanf("%d",&Choose);
}while(Choose>3||Choose<1);
if(Choose==1){
printf("\t\t\t请输入好友电话号码\n");
scanf("%s",&xy[i].Tel);
}
if(Choose==2)
{
printf("\t\t\t请输入好友QQ号码\n");
scanf("%s",&xy[i].QQ);
}
if(Choose==3)
{
printf("\t\t\t请输入好友年龄\n");
scanf("%d",&xy[i].age);
}
Search=fopen("Friend.txt","wt");
for(int sta=0;sta<i;sta++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[sta].name,xy[sta].Tel,xy[sta].QQ,xy[sta].age);
}
if(sta==i)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
i++;
}
for(i;i<=number;i++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
printf("\t\t\t修改后的信息为:\n\n");
Read();
}
fclose(Search);
}
if(Change==0)
{
printf("\t\t\t您要查找的好友不存在,请返回菜单添加。\n");
}
}
void Detele()
{
int change=0;
char delete_friend[20];
printf("\t\t\t输入要删除的好友的名字\n\n");
scanf("%s",&delete_friend);
fflush(stdin);
printf("\t\t\t您要删除的好友是 %s \n\n",delete_friend);
FILE *DeleteF;
DeleteF=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(DeleteF,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,delete_friend)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
DeleteF=fopen("Friend.txt","wt");
for(int st=0;st<i;st++)
{
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[st].name,xy[st].Tel,xy[st].QQ,xy[st].age);
}
i++;
for(i;i<=number;i++)
{
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
}
//--------------------------------------------------------------------------------
if(change==1)
{
printf("\t\t\t删除后的通讯录信息为:\n\n");
number--;
Number[0]=number;
FILE * St;
if((St=fopen("number.txt","r"))=NULL)
{
St=fopen("number.txt","wt");
exit(1);
}
St=fopen("number.txt","wt");
fprintf(St,"%d",Number[0]);
fclose(St);
fclose(DeleteF);
Start();
Read();
}
}
if(change==0)
{
printf("\t\t\t您要删除的好友不存在。\n");
}
}
void Lock()
{
}
int Menu()
{
printf("\t\t*****欢迎进入通讯管理界面*****\n\n");
printf("\t\t\t1.添加我的好友\n");
printf("\t\t\t2.显示所有好友\n");
printf("\t\t\t3.查找我的好友\n");
printf("\t\t\t4.修改好友信息\n");
printf("\t\t\t5.删除我的好友\n");
printf("\t\t\t6.设置背景颜色\n");
printf("\t\t\t7.设置管理密码\n");
printf("\t\t\t8.使用注意事项\n");
printf("\t\t\t9.退出通讯系统\n");
printf("\t\t\t0.作者想说的话\n");
printf("\t\t******************************\n\n");
do{
printf("\t\t\t请输入你的选择:\n\n");
scanf("%d",&GOTO);
}while(GOTO>9||GOTO<1);
return GOTO;
}
int Judge()
{
char JUDGE;
do
{
printf("\t\t\t是否继续当前操作\n\n Y(yes) / N(no)\n");
printf("\t\t\t输入你的选择\n");
JUDGE=getch();
if(JUDGE=='y'||JUDGE=='Y')
{
return 1;
}
if(JUDGE=='n'||JUDGE=='N')
{
return 2;
}
}while(JUDGE!='y'||JUDGE!='n');
}
㈢ 用C语言或C++做个通讯录管理系统
//简单的学生通讯录管理系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
structrecord
{
charname[20];//姓名
charphone[12];//电话
charadress[50];//地址
charpostcode[8];//邮政编码
chare_mail[20];//电子邮件。
}student[100];//假设最大数为100.
//定义全局变量num,表示已经输入的人数。
intnum;//这里使用数组解决通讯录的问题,实际上使用链表更好。
intmenu_select()
{
chars[80];
inta;/*定义整形变量*/
system("cls");
printf(" ***********欢迎进入通讯管理界面******** ");
printf(" 0.输入记录 ");
printf(" 1.显示记录 ");
printf(" 2.按姓名查找 ");
printf(" 3.按电话号码查找 ");
printf(" 4.插入记录 ");
printf(" 5.按姓名排序 ");
printf(" 6.删除记录 ");
printf(" 7.Quit ");
printf(" *********************************************** ");
do{
printf("Enteryouchoice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while(a<0||a>7);
returna;
}
intadser()
{
printf(" ****************请输入用户信息**************** ");
printf(" 输入姓名: ");
scanf("%s",student[num].name);
printf(" 输入电话号码: ");
scanf("%s",student[num].phone);
printf(" 输入地址: ");
scanf("%s",student[num].adress);
printf(" 输入邮编: ");
scanf("%s",student[num].postcode);
printf(" 输入e-mail: ");
scanf("%s",student[num].e_mail);
num++;
printf(" 是否继续添加?(Y/N): ");
if(getch()=='y'||getch()=='Y')
adser();
return(0);
}
voidlist()
{
inti;
system("cls");
if(num!=0)
{
printf(" ***************以下为通讯录所有信息************ ");
for(i=0;i<num;i++)
{
printf(" 姓名:%s ",student[i].name);
printf(" 电话:%s ",student[i].phone);
printf(" 地址:%s ",student[i].adress);
printf(" 邮编:%s ",student[i].postcode);
printf(" e-mail:%s ",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf(" ************************************************ ");
}
else
printf(" 通讯录中无任何纪录 ");
printf(" 按任意键返回主菜单: ");
getch();//这里是无回显的输入字符,你输入的字符不会显示在屏幕上。
return;
}
intsearchbyname()
{
intmark=0;
inti;
printf(" *****************按姓名查找******************* ");
charname[20];
printf(" 请输入姓名: ");
scanf("%s",name);
for(i=0;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" *************以下是您查找的用户信息*********** ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
mark++;
if((i+1)<num)
{
printf(" 是否继续查找相同名字的用户信息:(y/n) ");
if(getch()=='y'||getch()=='Y')
{
continue;
}
else
return(0);
}
else
{
printf(" 按任意键返回主菜单");
getch();
return(0);
}
}
}
if(mark==0)
{
printf(" 没有相同姓名的用户纪录 ");
printf(" 按任意键返回主菜单 ");
getch();
return(0);
}
return0;
}
intsearchbyphone()
{
intmark=0;
inti;
printf(" ******************按电话查找****************** ");
charphone[10];
printf(" 请输入电话号码: ");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" **************以下是您查找的用户信息********** ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
printf(" 按任意键返回主菜单 ");
mark++;
getch();
return(0);
}
}
if(mark==0)
{
printf(" 没有改用户的信息 ");
printf(" 按任意键返回主菜单 ");
getch();
return(0);
}
return(0);
}
voiddeletebyphone()
{
inti,j;
intdeletemark=0;
charphone[20];
printf(" 请输入要删除用户电话号码: ");
scanf("%s",phone);
if(num==0)
{
printf(" 对不起,文件中无任何纪录 ");
printf(" 按任意键返回主菜单 ");
getch();
return;
}
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" 以下是您要删除的用户纪录: ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" 是否删除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 删除成功");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
else
return;
}
continue;
}
if(deletemark==0)
{
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
return;
}
voiddeletebyname()
{
inta=0;
intfindmark=0;
intj;
intdeletemark=0;
inti;
charname[20];
printf(" 请输入要删除用户姓名: ");
scanf("%s",name);
for(i=a;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" 以下是您要删除的用户纪录:");
findmark++;
printf(" ________________________________");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ________________________________");
printf(" 是否删除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 删除成功");
if((i+1)<num)
{
printf(" 是否继续删除相同姓名的用户信息?(y/n)");
if(getch()=='y')
{
a=i;
continue;
}
}
printf(" 是否继续删除?(y/n)");
if(getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf(" 是否继续删除相同姓名的用户信息?(y/n)");
if(getch()=='y'||getch()=='Y')
{
a=i;
continue;
}
}
}
else
continue;
}
if((deletemark==0)&&(findmark==0))
{
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
elseif(findmark!=0)
{
printf(" 没有重名信息");
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
}
intdele()
{
charchoic;
printf(" 1-按电话号码删除2-按姓名删除");
printf(" 请选择:");
choic=getch();
switch(choic)
{
case'1':deletebyphone();break;
case'2':deletebyname();break;
}
return(0);
}
intsortbyname()//按姓名进行排序
{
inti,j;
structrecordtmp;
for(i=1;i<num;i++)
{
if(strcmp(student[i].name,student[i-1].name)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while((strcmp(tmp.name,student[j].name)<0&&j>=0));
student[j+1]=tmp;
}
}
printf(" 排序成功,是否显示?(y/n)");
if(getch()=='y')
list();
return(0);
}
intmain()
{
printf(" ************************************************ ");
printf(" ********welcometoTONGXUNLU******************* ");
printf(" ###########codebyXXXXX################### ");
printf(" ************************************************* ");
printf("按任意键进入主菜单 ");
getch();
intselectnum;
while(1)
{
selectnum=menu_select();
switch(selectnum)
{
case0:
{
adser();
break;
}
case1:
{
list();
break;
}
case2:
{
searchbyname();
break;
}
case3:
{
searchbyphone();
break;
}
case4:
{
adser();//这里插入,应该能指定位置,不过意义不大,所以和添加记录一样了。
break;
}
case5:
{
sortbyname();
break;
}
case6:
{
dele();
break;
}
case7:
{
printf("BYEBYE! ");
system("pause");
getchar();
exit(0);
}
}
}
getchar();
return0;
}
㈣ C语言程序设计,通讯录管理系统
/*【用C语言编写具有数据插入、修改、删除、显示和查询功能的电话薄管理程序】
1、数据包括:人名、工作单位、电话号码和E-mail地址。
2、可对记录中的姓名和电话号码进行修改。
3、可增加或删除记录。
4、可显示所有保存的记录。
5、可按人名或电话号码进行查询。
另:采用结构体数组和文件系统实现。结构体包括人名、工作单位、电话号码和E-mail
绝对原创,2010年7月12日晚刚写完测试通过的,纯C语言! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define szSTR 64
#define fileDB "c:\\contact.txt"
struct CONTACT {
char name[szSTR];/*姓名*/
char unit[szSTR];/*工作单位*/
char tele[szSTR];/*电话*/
char mail[szSTR];/*mail*/
};
typedef struct CONTACT CON;
#define BNAME 1
#define BUNIT 2
#define BTELE 4
#define BMAIL 8
#define BFULL 0xFF
int OPER[5] = {BFULL, BNAME, BUNIT, BTELE, BMAIL};
void input(CON * s, int mask)
{
if(mask & BNAME) { printf("请输入学生姓名:"); scanf("%s", s->name); }
if(mask & BUNIT) { printf("请输入工作单位:"); scanf("%s", s->unit); }
if(mask & BTELE) { printf("请输入联系电话:"); scanf("%s", s->tele); }
if(mask & BMAIL) { printf("请输入电子邮件:"); scanf("%s", s->mail); }
}/*end input*/
void show (int id, CON * s)
{
if(!s) return ;
if(id)
printf("%d\t%s\t%s\t%s\t%s\n",
id, s->name , s->unit , s->tele , s->mail );
else
printf("【姓名】%s【工作单位】%s【电话】%s【电邮】%s\n",
s->name , s->unit , s->tele , s->mail );
/*end if*/
}/*end show*/
void regist(void)
{
CON s; FILE * f = 0;
input(&s, BFULL);
f = fopen(fileDB, "ab");
fwrite(&s,sizeof(s),1,f);
fclose(f);
printf("资料已保存!\n");
}/*end regist*/
int browse(char * key, fpos_t * p, CON * s)
{
FILE * f =0; int i = 0, r = 0; CON t;
char * a = 0, * b = 0; fpos_t q;
if(!s) return 0;
f = fopen(fileDB, "rb");
if(!f) {
printf("数据库暂无记录\n");
return 0;
}/*end if*/
printf("编号\t姓名\t单位\t电话\t电邮\n");
printf("------------------------------------------------\n");
while(!feof(f)) {
fgetpos(f, &q);
r = fread(&t, sizeof(t), 1, f);
if(r<1)break;
if(t.name[0]==0) continue;
if(key) {
a = strstr(t.name , key);
b = strstr(t.tele , key);
if(a == 0 && b == 0) continue;
}/*end if*/
if(p) *p = q;
if(s) memcpy(s, &t, sizeof(t));
i++;
show(i, &t);
}/*end while*/
fclose(f);
printf("------------------------------------------------\n");
return i;
}/*end browse*/
fpos_t query(CON * s)
{
char key[szSTR]=""; fpos_t p = 0; int r = 0;
printf("请输入姓名或者电话号码:");
scanf("%s", key);
r = browse(key, &p, s);
return r?p:-1;
}/*end query*/
void clear(void)
{
FILE * f = 0; CON s;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
memset(&s, 0, sizeof(s));
fwrite(&s, sizeof(s),1, f);
fclose(f);
printf("记录已删除!\n");
}/*end clear*/
void modify(void)
{
FILE * f = 0; CON s; int cmd = 0;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
for(;;) {
show(0, &s);
printf("请选择:1-姓名;2-工作单位;3-电话;4-电邮;5-保存并退出;6-放弃修改并退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 1:
case 2:
case 3:
case 4:
input(&s, OPER[cmd]);
break;
case 5:
fsetpos(f, &p);
fwrite(&s, sizeof(s), 1, f);
case 6:
fclose(f);
return;
default:
printf("错误的操作,请重新输入。\n");
}/*end case*/
}/*mext*/
}/*end clear*/
int main(void)
{
int cmd = 0; CON s;
for(;;) {
printf("欢迎使用通信簿!请选择:1-添加;2-查询;3-删除;4-修改;5-全部输出;0-退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 0: return 0;
case 1: regist(); break;
case 2: query(&s); break;
case 3: clear(); break;
case 4: modify(); break;
case 5: browse(0, 0, &s); break;
}//end case
}
return 0;
}