c语言程序设计——编一个通讯录管理程序:序号 姓名 电话 地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言程序设计——编一个通讯录管理程序:序号 姓名 电话 地址相关的知识,希望对你有一定的参考价值。
c语言程序设计——编一个通讯录管理程序:序号 姓名 电话 地址
可显示已录入的通讯录
实现新纪录的录入
可以删除、修改任何一条记录
可以根据姓名、电话、或地址查询相关信息
系统的退出
嗯,邮箱,962625910@qq.com
#include <string.h>
#include<iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
#define LEN sizeof(struct friends)
#define CODE_SIZE 10
struct friends
char num[20];//编号
char name[100];//用户姓名
char phone[20];//电话号码
char home[100];//通讯地址
char email[100]; //email
char occupation[100];//工作
char birthday[20];//出生日期
struct friends *next;
;
int password()
FILE *fp;
char s1[CODE_SIZE], s2[CODE_SIZE], s3[CODE_SIZE], fun;
while (1)
fp = fopen("password.txt", "r");
if (fp == NULL)
printf("第一次运行,请输入初始密码(最多8位):\n");
scanf("%s", s1);
printf("请再次输入初始密码:\n");
scanf("%s", s2);
if (strcmp(s1, s2) == 0)
fp = fopen("password.txt", "w+");
if (fp == NULL)
printf("创建文件失败退出\n");
getch();
exit(1);
else
//对s1加密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
s1[i] = s1[i] + i;
fputs(s1, fp);
printf("初始密码创建完成.\n");
else
printf("两次输入的密码不一致!\n");
fclose(fp);
else
fgets(s1, CODE_SIZE, fp);
fclose(fp);
printf("输入密码:\n");
scanf("%s", s2);
//对s1解密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
s1[i] = s1[i] - i;
loop:
if (strcmp(s1, s2) == 0)
printf("-----密码正确-----\n");
printf("-----请选择功能-----\n");
printf("-----1:修改密码-----\n");
printf("-----2:进入通讯录-----\n");
scanf("%d", &fun);
switch (fun)
case 1: printf("请输入新密码\n");
scanf("%s", s1);
printf("请再次输入新密码\n");
scanf("%s", s2);
if (strcmp(s1, s2) == 0)
fp = fopen("password.txt", "w+");
if (fp == NULL)
printf("文件错误!\n");
else
//对s1加密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
s1[i] = s1[i] + i;
fputs(s1, fp);
fclose(fp);
printf("密码修改成功\n");
else
printf("两次输入的密码不一致,修改失败\n");
break;
case 2: return 1;
default: printf("无效指令\n");
else
printf("密码错误\n请重新输入\n");
scanf("%s", s2);
goto loop;
printf("------------------\n\n\n\n");
void face(void)//功能选择面板
cout << "********************************************************************" << endl;
cout << "\t\t\t☆★欢迎使用***通讯录☆★" << endl;
cout << "\n\n\t☆★选择你需要操作的功能:★☆" << endl;
cout << "\t\t\t1.增加通讯录信息" << endl;
cout << "\t\t\t2.显示通讯录中所有记录" << endl;
cout << "\t\t\t3.删除需要删除的信息" << endl;
cout << "\t\t\t4.以名字查询所需的信息" << endl;
cout << "\t\t\t5.保存并退出" << endl;
cout << "\t\t\t6.退出不保存!!" << endl;
cout << "********************************************************************" << endl;
void print(struct friends *head)
struct friends *p;
p = head;
system("CLS");
cout << "*************************************************************" << endl;
cout << "==================== → 用户信息记录表 ← ===================" << endl;
cout << "*************************************************************" << endl;
if (head != NULL)
do
cout << "联系人编号:" << p->num << endl;
cout << "联系人姓名:" << p->name << endl;
cout << "联系人电话号码:" << p->phone << endl;
cout << "联系人地址:" << p->home << endl;
cout << "联系人电子邮箱:" << p->email << endl;
cout << "联系人职业" << p->occupation << endl;
cout << "联系人出生日期:" << p->birthday << endl;
cout << "********************************************************" << endl;
p = p->next;
while (p != NULL);
else
cout << "对不起!!没有任何联系人记录!!" << endl;
cout << "=============================================================\n" << endl;
//增添电子通讯录中的内容,即创建连表过程
struct friends *append(struct friends *head)
struct friends *p0 = NULL, *p1, *p2;
p1 = head;
p2 = head;
system("CLS");
cout << "********************************************************************" << endl;
cout << "\t\t你能在此目录下创建并添加联系人信息" << endl;
cout << "********************************************************************" << endl;
p0 = (struct friends *)malloc(LEN);
cout << "请输入联系人编号:" << endl;
gets(p0->num);
cout << "请输入联系人姓名:" << endl;
gets(p0->name);
cout << "请输入联系人电话号码:" << endl;
gets(p0->phone);
cout << "请输入联系人地址:" << endl;
gets(p0->home);
cout << "请输入联系人Email:" << endl;
gets(p0->email);
cout << "请输入联系人职位:" << endl;
gets(p0->occupation);
cout << "请输入联系人出生日期:" << endl;
gets(p0->birthday);
//对插入的节点排序,按姓名的拼音顺序
if (head == NULL)
head = p0; p0->next = NULL;
else
while ((strcmp(p0->name, p1->name)>0) && (p1->next != NULL))
p2 = p1; p1 = p1->next;
if ((strcmp(p0->name, p1->name)) <= 0)
if (head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
else
p1->next = p0; p0->next = NULL;
cout << "恭喜你!!成功添加了联系人信息!!" << endl;
cout << "********************************************************************" << endl;
return(head);
//电子通讯录的维护(删除),通过输入联系人ID号码删除联系人数据
struct friends *del(struct friends *head)
struct friends *p1, *p2;
char num[12];
system("CLS");
cout << "********************************************************************" << endl;
cout << "================= → 用户信息记录删除功能 ← ===============" << endl;
cout << "********************************************************************" << endl;
printf("输入要删除的联系人编号:");
gets(num);
p1 = head;
if (head == NULL)
cout << "很抱歉!!没有任何联系人纪录!!" << endl;
cout << "*******************************************************" << endl;
return(head);
while (p1 != NULL)
if (strcmp(p1->num, num) == 0)
if (p1 == head)
head = p1->next;
else p2->next = p1->next;
free(p1);
cout << "删除记录成功!!" << endl;
return(head);
p2 = p1;
p1 = p1->next;
cout << "对不起!!没有要删除的联系人纪录!!\n" << endl;
return(head);
//电子通讯录的查找,关键字为用户姓名;
void search(struct friends *head)
struct friends *p1, *p2;
char name[20];
p1 = head;
p2 = p1;
system("CLS");
cout << "***************************************************************" << endl;
cout << "================ → 用户信息记录查询功能 ← ==================\n" << endl;
cout << "***************************************************************" << endl;
cout << "输入要查找联系人的姓名:" << endl;
gets(name);
while (p1 != NULL)
if (strcmp(p1->name, name) == 0)
cout << "联系人编号:" << endl;
puts(p1->num);
cout << "联系人姓名:" << endl;
puts(p1->name);
cout << "联系人电话号码:" << endl;
puts(p1->phone);
cout << "联系人地址:" << endl;
puts(p1->home);
cout << "联系人电子邮箱:" << p1->email << endl;
puts(p1->email);
cout << "联系人职业" << p1->occupation << endl;
puts(p1 ->occupation);
cout << "联系人出生日期:" << endl;
puts(p1->birthday);
cout << "=============================================================" << endl;
break;
p2 = p1;
p1 = p1->next;
if (p1 == NULL)
cout << "对不起!!没有该联系人的纪录!!" << endl;
void save(struct friends *head)
FILE *fp, *fp1;
struct friends *p;
p = head;
fp = fopen("record.txt", "w");
fp1 = fopen("record.txt", "w");
fprintf(fp1, "=============== → 用户信息记录表 ← =================\n");
while (p != NULL)
fprintf(fp1, "====================================================\n");
fprintf(fp1, "联系人编号:%s\n", p->num);
fprintf(fp1, "联系人姓名:%s\n", p->name);
fprintf(fp1, "联系人电话:%s\n", p->phone);
fprintf(fp1, "联系人家庭地址:%s\n", p->home);
fprintf(fp1, "联系人电子邮箱:%s\n", p->email);
fprintf(fp1, "联系人职业:%s\n", p->occupation);
fprintf(fp1, "联系人出生日期:%s\n", p->birthday);
p = p->next;
fprintf(fp1, "*************************************************************\n");
fclose(fp1);
fclose(fp);
cout << "成功储存,你能在record.txt找到相应纪录" << endl;
cout << "**************************************************************" << endl;
cout << "PRESS ANY KEY TO EXIT." << endl;
getchar();
exit(1);
//电子通讯录的记录读盘操作,使用文件指针;
struct friends *load(void)
FILE *fp;
struct friends *head = NULL, *p1 = NULL, *p2 = NULL;
char c;
int i;
fp = fopen("record.txt", "r");
for (i = 1; (c = fgetc(fp)) != -1; i++)
p1 = (struct friends *)malloc(LEN);
//fscanf(fp,"%s %s %s %s %s %s",p1->num,p1->name,p1->phone,p1->email,p1->home,p1->birthday);
if (i == 1)
head = p1; p2 = p1;
else
p2->next = p1; p2 = p1;
if (p1 == NULL)
fclose(fp); return(head);
p2->next = NULL;
fclose(fp);
return(head);
int main()
FILE *fp1, *fp2;
int a;//功能选择需要的号码
system("cls");
system("cls");
struct friends *head = NULL;
if ((fp1 = fopen("record.txt", "r")) == NULL)
fp2 = fopen("record.txt", "w");//如果不存在record.txt就创建一个
fclose(fp2);
head = load();
password();
system("cls");
while (1)
face();
printf("选择你需要操作的功能号码:");
cin >> a;
getchar();
switch (a)
case 1:head = append(head); break;
case 2:print(head); break;
case 3:head = del(head); break;
case 4:search(head); break;
case 5:save(head); break;
case 6:exit(0); break;
default:cout << "Enter error!!" << endl;
cout << "◇◆请按ENTER返回功能操作菜单◇◆" << endl;
getchar();
system("cls");
参考技术A #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define SIZE 30
struct inform
char num[20];
char name[20];
char adrr[30];
student[SIZE];
void inter_face()
printf (" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<信息管理系统1.0.0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
printf (" 1---------------------显示所有数据\n");
printf (" 2---------------------添加一个新的信息\n");
printf (" 3---------------------删除一个旧的信息\n");
printf (" 4---------------------查找\n");
printf (" 0---------------------退出系统\n");
printf (" 请输入选项并确定:");
void put_in() //向文件中读入数据一次
FILE *fp;
int c;
if ((fp=fopen("e:\\123.txt","a"))==NULL)
printf ("open file error!!");
printf ("学号:\n");
c=getchar();
while (c!='\n')
fputc(c,fp);
c=getchar();
fputc(' ',fp);
printf("姓名:\n");
c=getchar();
while (c!='\n')
fputc(c,fp);
c=getchar();
fputc('\n',fp);
fclose(fp);
int load(inform student[]) //加载存储文件数据
FILE *fp;
int t=0,i;
if ((fp=fopen("e:\\123.txt","r"))==NULL)
printf ("打开文件失败!");
while(!feof(fp))
for (i=0;i<20;i++)
student[t].num[i]=fgetc(fp);
if (student[t].num[i]==' ')
break;
for (i=0;i<20;i++)
student[t].name[i]=fgetc(fp);
if (student[t].name[i]=='\n')
break;
t++;
fclose(fp);
return t;
void display(int t1) //显示存储文件中所有数据
int i,j;
for (j=0;j<t1-1;j++)
printf("学号:");
for (i=0;i<20;i++)
printf ("%c",student[j].num[i]);
printf("\n");
printf ("姓名:");
for (i=0;i<20;i++)
printf ("%c",student[j].name[i]);
printf ("\r------------------\n");
printf("-------------------------------->数据总数:%d\n",t1-1);
void search(int t) //搜索指定号码并显示搜索结果
int i,j,l=0,k[SIZE]=0,h[SIZE]=0,max,hx=0,hy=-1;
char a[20],c;
printf ("\n\n\n 1------------按学号查找\n");
printf (" \n 2------------按姓名查找\n");
printf ("选择《1》或《2》:");
c=getchar();
getchar();
if(c=='1')
printf ("\n\n请输入要搜索的学号:");
for (i=0;i<20;i++)
if ((a[i]=getchar())=='\n')
break;
else l++;
for (i=0;i<t-1;i++)
for (j=0;j<=l;j++)
if(a[j]==student[i].num[j])
k[i]++;
max=k[0];
for (i=0;i<t-1;i++)
if (k[i]>max)
max=k[i];
for (i=0;i<t-1;i++)
if (k[i]==max)
h[hx++]=i;
if (max!=0)
printf ("系统共搜索%d个结果:\n",hx);
for (j=0;j<hx;j++)
printf ("姓名:");
for(i=0;i<20;i++)
printf ("%c",student[h[j]].name[i]);
printf ("\n学号:");
for (i=0;i<20;i++)
printf ("%c",student[h[j]].num[i]);
printf ("\n\n");
else printf ("共搜索到%d个结果!\n",max);
else if (c=='2')
printf ("请输入要查询的姓名:");
for (i=0;i<20;i++)
if ((a[i]=getchar())=='\n')
break;
else l++;
for (i=0;i<t-1;i++)
for (j=0;j<l;j++)
if (a[j]!=student[i].name[j])
hx++;
if (hx==0)
hy=i;
break;
hx=0;
if (hy>0)
printf ("\n\r搜索到的结果:\n");
printf ("姓名:");
for(i=0;i<20;i++)
printf ("%c",student[hy].name[i]);
printf ("\n学号:");
for (i=0;i<20;i++)
printf ("%c",student[hy].num[i]);
printf ("\n\n");
else printf ("搜索到0个结果!\n");
else
printf ("---------->不正确的选项!\n");
int delet_n(int t)//删除一组数据
int i,j,k=0,m=0,x;
char c;
FILE *fp;
char n[20];
if ((fp=fopen("e:\\123.txt","w"))==NULL)
printf ("创建文件失败!!\n");
printf ("\n1<----------------按学号删除\n");
printf ("2<--------------------------------按名称删除\n");
printf ("请选择:");
c=getchar();
getchar();
if (c=='1')
printf ("输入要删除的学号:");
for (i=0;i<20;i++) //输入要删除的学号
if ((n[i]=getchar())=='\n')
break;
else k++;
for (i=0;i<t-1;i++)//选出内存中的数据编码
for (j=0;j<=k;j++)
if (n[j]==student[i].num[j])
m++;
if (m==k)
x=i;
break;
m=0;
else if (c=='2')
printf ("请输入名称:");
for (i=0;i<20;i++) //输入要删除的姓名
if ((n[i]=getchar())=='\n')
break;
else k++;
for (i=0;i<t-1;i++) //选出内存中的数据编码
for (j=0;j<k;j++)
if (n[j]==student[i].name[j])
m++;
if (m==k)
x=i;
break;
m=0;
//向文件输出更改后的数据
if(i<t-1)
for (i=0;i<t-1;i++)
if (i!=x)
j=0;
while(student[i].num[j]!=' ')
fputc(student[i].num[j++],fp);
fputc(' ',fp);
j=0;
while(student[i].name[j]!='\n')
fputc(student[i].name[j++],fp);
fputc('\n',fp);
printf ("删除成功!\n");
else
printf ("删除失败!\n");//删除失败恢复原有数据
for (i=0;i<t-1;i++)
j=0;
while(student[i].num[j]!=' ')
fputc(student[i].num[j++],fp);
fputc(' ',fp);
j=0;
while(student[i].name[j]!='\n')
fputc(student[i].name[j++],fp);
fputc('\n',fp);
fclose(fp);
return 0;
void encrypt() //信息加密
int main() //主函数
int t1,t;
char choice;
inter_face();
choice=getchar();
while (1)
getchar();
system("cls");
system("cls");
switch(choice)
case '2':put_in();break;
case '3': t1=load(student);delet_n(t1); break;
case '1': t1=load(student);display(t1);break;
case '4':t1=load(student);search(t1);break;
case '0': t=1;break;
default: break;
inter_face();
if (t==1)
break;
choice=getchar();
return 0;
这是以前做的.用VC++写的;你自己看一下吧 该一点就可以完全实现你搜要的功能了;
不要忘记给分啊本回答被提问者采纳 参考技术B 简单通讯录管理程序,代码如下:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student *ST;
struct student
char stN[20];
char stVal[128];
char stNo[10];
char stTel[12];
ST next;
;
ST head = NULL;
int ShowNode()
char no[20] = 0;
ST t;
printf("输入姓名:");
scanf("%s", no);
for (t = head; t; t = t->next)
if (!memcmp(t->stN, no, strlen(no)))
printf("姓名:%s, 地址:%s 邮编:%s 电话:%s\n", t->stN, t->stVal, t->stNo, t->stTel);
return 0;
printf("无此人信息\n");
return 0;
int ChangeNode()
char no[20];
ST t;
printf("输入姓名:");
scanf("%s", no);
for (t = head; t; t = t->next)
if (!memcmp(t->stN, no, strlen(no)))
printf("输入新的记录:\n");
printf("地址:");
scanf("%s", t->stVal);
printf("邮编:");
scanf("%s", t->stNo);
printf("英语成绩:");
scanf("%s", t->stTel);
return 0;
printf("无此人信息\n");
return 0;
int InsertNode()
ST t;
t = (ST)malloc(sizeof *t);
printf("输入姓名:");
scanf("%s", t->stN);
printf("输入地址:");
scanf("%s", t->stVal);
printf("输入邮编:");
scanf("%s", t->stNo);
printf("输入电话:");
scanf("%s", t->stTel);
t->next = head;
head = t;
return 0;
int DelNode()
ST x, y;
char no[20] = 0;
printf("输入姓名:");
scanf("%s", no);
for (x = y = head; x; y = x, x = x->next)
if (!memcmp(x->stN, no, strlen(no)))
if (x ==y)
x = head = head->next;
y->next = NULL;
free(y);
return 0;
else
y->next = x->next;
x->next = NULL;
free(x);
return 0;
printf("无此学生\n");
return 0;
int main(void)
int i;
while(1)
printf("查询记录:1\n"
"更改记录:2\n"
"插入记录:3\n"
"删除记录:4\n"
"退出:5\n");
scanf("%d", &i);
if (1==i)
ShowNode();
else if(2==i)
ChangeNode();
else if(3==i)
InsertNode();
else if(4==i)
DelNode();
else if(5==i)
break;
else
printf("输入错误!请重新输入!\n");
continue;
return 0;
参考技术C 你可以自己看哈 C++ 参考技术D 代码有点长,最好留个邮箱号
C语言课程设计
参考技术A 自己编了一个不是太符合要求,算法也很笨,但是勉强可以做出来楼主可以看看= =注释我也写了
#include<stdio.h>
void main()
int year,month;//定义年月
int run,num,x,y,i;//辅助变量
int cun[4];//辅助数组,用来存储输入月份及其相邻月份相距1980.1的天数
char c;
printf("please input a year number and month number:");
scanf("%d%c%d",&year,&c,&month);//输入请用题目上相同的输入方式
run=(year-1980)/4+1;//计算闰年个数
num=run*366+(year-1980-run)*365;//计算天数
if((year%4==0||year%400==0)&&year!=1980)//考虑闰年,因为发现1980在这里会多计算一次闰年,所以排除了
switch(month)
case 1:cun[0]=num-31;cun[1]=num;cun[2]=num+31;cun[3]=num+60;break;
case 2:cun[0]=num;cun[1]=num+31;cun[2]=num+60;cun[3]=num+91;break;
case 3:cun[0]=num+31;cun[1]=num+60;cun[2]=num+91;cun[3]=num+121;break;
case 4:cun[0]=num+60;cun[1]=num+91;cun[2]=num+121;cun[3]=num+152;break;
case 5:cun[0]=num+91;cun[1]=num+121;cun[2]=num+152;cun[3]=num+182;break;
case 6:cun[0]=num+121;cun[1]=num+152;cun[2]=num+182;cun[3]=num+213;break;
case 7:cun[0]=num+152;cun[1]=num+182;cun[2]=num+213;cun[3]=num+244;break;
case 8:cun[0]=num+182;cun[1]=num+213;cun[2]=num+244;cun[3]=num+274;break;
case 9:cun[0]=num+213;cun[1]=num+244;cun[2]=num+274;cun[3]=num+305;break;
case 10:cun[0]=num+244;cun[1]=num+274;cun[2]=num+305;cun[3]=num+335;break;
case 11:cun[0]=num+274;cun[1]=num+305;cun[2]=num+335;cun[3]=num+366;break;
case 12:cun[0]=num+305;cun[1]=num+335;cun[2]=num+366;cun[3]=num+397;
else switch(month)
case 1:cun[0]=num-31;cun[1]=num;cun[2]=num+31;cun[3]=num+59;break;
case 2:cun[0]=num;cun[1]=num+31;cun[2]=num+59;cun[3]=num+90;break;
case 3:cun[0]=num+31;cun[1]=num+59;cun[2]=num+90;cun[3]=num+120;break;
case 4:cun[0]=num+59;cun[1]=num+90;cun[2]=num+120;cun[3]=num+151;break;
case 5:cun[0]=num+90;cun[1]=num+120;cun[2]=num+151;cun[3]=num+181;break;
case 6:cun[0]=num+120;cun[1]=num+151;cun[2]=num+181;cun[3]=num+212;break;
case 7:cun[0]=num+151;cun[1]=num+181;cun[2]=num+212;cun[3]=num+243;break;
case 8:cun[0]=num+181;cun[1]=num+212;cun[2]=num+243;cun[3]=num+273;break;
case 9:cun[0]=num+212;cun[1]=num+243;cun[2]=num+273;cun[3]=num+304;break;
case 10:cun[0]=num+243;cun[1]=num+273;cun[2]=num+304;cun[3]=num+334;break;
case 11:cun[0]=num+273;cun[1]=num+304;cun[2]=num+334;cun[3]=num+365;break;
case 12:cun[0]=num+304;cun[1]=num+334;cun[2]=num+365;cun[3]=num+396;
//输出前一个月
x=month-1;
y=year;
if(x==0)y=year-1;x=12;
printf("%d.%d\n",y,x);//年月
printf("SU\tMO\tTU\tWE\tTH\tFR\tSA\n");
x=cun[1]-cun[0];
y=(cun[0]-5)%7;
for(i=0;i<y;i++)printf(" \t");
for(i=1;i<=x;i++)
printf("%d\t",i);
if((i==7-y)||((i-(7-y))%7==0))printf("\n");
printf("\n==================================\n");
//输出当月
x=month;
y=year;
printf("%d.%d\n",y,x);//年月
printf("SU\tMO\tTU\tWE\tTH\tFR\tSA\n");
x=cun[2]-cun[1];
y=(cun[1]-5)%7;
for(i=0;i<y;i++)printf(" \t");//考虑第一行的空格
for(i=1;i<=x;i++)
printf("%d\t",i);
if((i==7-y)||((i-(7-y))%7==0))printf("\n");//星期换行
//输出之后的日期
printf("\n==================================\n");
//输出后一个月
x=month+1;
y=year;
if(x==13)y=year+1;x=1;
printf("%d.%d\n",y,x);//年月
printf("SU\tMO\tTU\tWE\tTH\tFR\tSA\n");
x=cun[3]-cun[2];
y=(cun[2]-5)%7;
for(i=0;i<y;i++)printf(" \t");
for(i=1;i<=x;i++)
printf("%d\t",i);
if((i==7-y)||((i-(7-y))%7==0))printf("\n");
printf("\n==================================\n");
本回答被提问者和网友采纳
以上是关于c语言程序设计——编一个通讯录管理程序:序号 姓名 电话 地址的主要内容,如果未能解决你的问题,请参考以下文章