用C语言编写简单的通讯录(大一课程作业)
该系统概述:
因本人只学习了C语言的基础部分,所以编写该系统时只使用了一个C文件。存储通讯录信息以txt文件方式保存。
系统设计:
显示主菜单主函数:int main();
创建录入信息子函数:void book_create();
显示信息子函数1:void book_reada(int*count);
显示信息子函数2:void book_readb(int*count);
查询信息子函数 :void book_search();
添加信息子函数 :void book_insert(int count);
修改信息子函数:void books_change();
删除信息子函数:void book_delete();
功能测试:
1、启动系统:
2、创建通讯录:
输入1,回车
3、添加联系人信息:
输入4,回车,开始选择添加类型:
输入“是”,开始输入信息
添加一个联系人后,填入“是”可以继续添加
填入“否”则停止添加,并退出子函数,并且需选择6选项才能使信息保存至文件!!
保存成功!选择7选项可查看目前的联系人方式,注意,关闭程序重启时不能选择7选项!
另一种显示方式:重启程序后,选择8选项
再显示前需确认关闭前添加联系人的个数!例如添加两个联系人,输入“2”
显示出关闭程序前的联系人信息,另一种添加方式,填“fou”
填入联系人数量,例如“2”
回到显示菜单,选择8选项,查看联系人信息
4、查询联系人信息:
查询已保存的联系人信息,选择2选项,进行查找方式选择
选择(1)选项进行联系人查找
选择(2)选项进行学号查找
5、修改联系人信息:
选择(3)选项,输入联系人姓名,进入修改页面
选择修改类型,例如选择(3),进行地址修改
在主菜单页面选择(8)选项检查联系人信息
6、删除联系人信息:
在主菜单页面选择(5)选项,输入联系人姓名,例如“r”
在主菜单选项,选择(8)选项检查联系人信息
7、退出系统:
在主菜单选项选择(0)选项,退出系统
代码部分:
该代码仅为个人大一课程设计。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct BOOKS {
char id[20], tell[20], phone[20], name[20], add[20], email[30], wechat[20];
}books[20]; //创建结构体
//函数模块的声明
void book_create();
void book_show(int);
void book_search();
void books_change();
void book_insert(int *);
void book_delete();
void book_outside();
void book_save(int*);
void book_reada(int*);
void book_readb();
int main()
{
int count = 0;
int c;
do
{
printf("\n");
printf("\t\t\t\t*=================================*\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 欢迎使用通讯录! *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t*=================================*\n");
printf("\n");
printf("\n");
printf("\t\t\t\t|==================================|\n");
printf("\t\t\t\t| 1--创建通讯录 |\n");
printf("\t\t\t\t| 2--查询联系人信息 |\n");
printf("\t\t\t\t| 3--修改联系人信息 |\n");
printf("\t\t\t\t| 4--添加联系人信息 |\n");
printf("\t\t\t\t| 5--删除联系人信息 |\n");
printf("\t\t\t\t| 6--保存联系人信息 |\n");
printf("\t\t\t\t| 7--显示通讯录(未关闭程序) |\n");
printf("\t\t\t\t| 8--显示通讯录(再次运行程序)|\n");
printf("\t\t\t\t| 0--退出 |\n");
printf("\t\t\t\t|==================================|\n");
printf("\t\t\t\t 请输入你的选项:");
scanf("%d", &c);
switch (c)
{
case 0:book_outside(); //退出系统
break;
case 1:book_create(); //调用创建运算
break;
case 2:book_search(); //调用查询运算
break;
case 3:books_change(); //调用修改运算
break;
case 4:book_insert(&count); //调用插入运算
break;
case 5:book_delete(); //调用删除运算
break;
case 6:book_save(&count); //调用保存运算
break;
case 7:book_reada(&count);
break; //调用阅读运算
case 8:book_readb();
break;
default:printf("\n\t\t\t\t无此选项!请重新选择!");
}
} while (c != 0);
return 0;
}
void book_create() //创建模块
{
int ban = 1;
system("cls");
if (ban != 0)
{
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t\t成功创建通讯录!请选择添加(4)选项!\n");
ban = 1;
}
else
printf("通讯录已创建,请勿重复创建!");
}
void book_search() //查找模块
{
int i, number, flag = 1;
char nam[20], ID[12];
FILE*fp;
system("cls");
if ((fp = fopen("books.txt", "rb+")) == NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<20;i++)
fread(&books[i],sizeof(struct BOOKS),1,fp);
printf("\n\n\t\t\t\t按联系人查找(1)或按学号查找(2):");
scanf("%d", &number);
printf("\n");
if (number == 1)
{
printf("\n\t\t\t\t请输入联系人姓名:");
scanf("%s", &nam);
for (i = 0; i < 20 && flag; i++)
{
if ((strcmp(books[i].name, nam) == 0))
{
printf("\n\t\t\t\t姓名:%s\n", books[i].name);
printf("\t\t\t\t学号:%s\n", books[i].id);
printf("\t\t\t\t移动电话:%s\n", books[i].phone);
printf("\t\t\t\t固定电话:%s\n", books[i].tell);
printf("\t\t\t\t住址:%s\n", books[i].add);
printf("\t\t\t\t邮箱:%s\n", books[i].email);
printf("\t\t\t\t微信:%s\n", books[i].wechat);
flag = 0;
}
}
if(flag==1)
printf("没有查询到!");
}
else
{
printf("\n\t\t\t\t请输入学号:");
scanf("%s", &ID);
for (i = 0; i < 20 && flag; i++)
{
if ((strcmp(books[i].id, ID) == 0))
{
printf("\n\t\t\t\t学号:%s\n", books[i].id);
printf("\t\t\t\t姓名:%s\n", books[i].name);
printf("\t\t\t\t移动电话:%s\n", books[i].phone);
printf("\t\t\t\t固定电话:%s\n", books[i].tell);
printf("\t\t\t\t住址:%s\n", books[i].add);
printf("\t\t\t\t邮箱:%s\n", books[i].email);
printf("\t\t\t\t微信:%s\n", books[i].wechat);
flag = 0;
}
}
if(flag==1)
printf("没有查询到!");
}
fclose(fp);
}
void books_change() //修改模块
{
int i,c,b,flag = 1;
char nam[20];
FILE*fp;
FILE*fp2;
b=i-4;
system("cls");
if ((fp = fopen("books.txt", "rb+")) == NULL)
{
printf("\t\t\t\t不能打开文件!\n");
return;
}
if ((fp2 = fopen("xiugai.txt", "wb+")) == NULL)
{
printf("\t\t\t\t不能打开文件!\n");
return;
}
printf("\n\n\t\t\t\t请输入要修改的姓名:");
scanf("%s", &nam);
for (i = 0; i < 20; i++)
{
fread(&books[i],sizeof(struct BOOKS),1,fp);
if (strcmp(books[i].name, nam) == 0)
{
printf("\n\n\t\t\t\t|==================================|\n");
printf("\t\t\t\t| 1--固定电话 |\n");
printf("\t\t\t\t| 2--移动电话 |\n");
printf("\t\t\t\t| 3--地址 |\n");
printf("\t\t\t\t| 4--姓名 |\n");
printf("\t\t\t\t| 5--微信 |\n");
printf("\t\t\t\t| 6--邮箱 |\n");
printf("\t\t\t\t| 7--学号 |\n");
printf("\t\t\t\t| 0--返回 |\n");
printf("\t\t\t\t|==================================|\n");
printf("\t\t\t\t请输入选项:");
scanf("%d",&c);
switch(c)
{
case 0:break;
case 1:printf("\n\t\t\t\t请输入修改后的固定电话:");
scanf("%s", books[i].tell);
break;
case 2:printf("\n\t\t\t\t请输入修改后的移动电话:");
scanf("%s", books[i].phone);
break;
case 3:printf("\n\t\t\t\t请输入修改后的地址:");
scanf("%s", books[i].add);
break;
case 4:printf("\n\t\t\t\t请输入修改后的姓名:");
scanf("%s", books[i].name);
break;
case 5:printf("\n\t\t\t\t请输入修改后的微信:");
scanf("%s", books[i].wechat);
break;
case 6:printf("\n\t\t\t\t请输入修改后的邮箱:");
scanf("%s", books[i].email);
break;
case 7:printf("\n\t\t\t\t请输入修改后的学号:");
scanf("%s", books[i].id);
break;
default:printf("\n\t\t\t\t无此选项!请重新选择!");
}//switch函数结束括号
}//if函数结束括号
if(fwrite(&books[i], sizeof(struct BOOKS), 1, fp2) != 1)
{
printf("文件写入错误!");
}
} //for循环结束括号
printf("\n\n\t\t\t\t联系人信息修改成功!");
fclose(fp);
fclose(fp2);
remove("books.txt");
rename("xiugai.txt","books.txt");
}
void book_insert(int *count) //添加模块
{
int i,a,b;
FILE *fp;
char nam1[5] = "shi", nam2[5] = "fou", nam3[5],nam4[5];
system("cls");
printf("\t\t\t\t是否为初次添加联系人?(是或否):");
scanf("%s",nam4);
if((strcmp(nam1,nam4)==0))
{ printf("\n\n\n\t\t\t\t保存需选择选项(6)保存!");
for (i = *count; i < 20; i++)
{
printf("\n\n\t\t\t\t请输入姓名:");
scanf("%s", books[i].name);
printf("\t\t\t\t请输入学号:");
scanf("%s", books[i].id);
printf("\t\t\t\t请输入移动电话:");
scanf("%s", books[i].phone);
printf("\t\t\t\t请输入固定电话:");
scanf("%s", books[i].tell);
printf("\t\t\t\t请输入地址:");
scanf("%s", books[i].add);
printf("\t\t\t\t请输入邮箱:");
scanf("%s", books[i].email);
printf("\t\t\t\t请输入微信:");
scanf("%s", books[i].wechat);
(*count)++;
printf("\n\t\t\t\t是否继续添加?(是或否):");
scanf("%s", nam3);
if ((strcmp(nam2, nam3) == 0))
return;
}
}
else
{
printf("\t\t\t\t已有联系人个数:");
scanf("%d",&a);
b=a+1;
printf("\t\t\t\t请输入姓名:");
scanf("%s", books[b].name);
printf("\t\t\t\t请输入学号:");
scanf("%s", books[b].id);
printf("\t\t\t\t请输入移动电话:");
scanf("%s", books[b].phone);
printf("\t\t\t\t请输入固定电话:");
scanf("%s", books[b].tell);
printf("\t\t\t\t请输入地址:");
scanf("%s", books[b].add);
printf("\t\t\t\t请输入邮箱:");
scanf("%s", books[b].email);
printf("\t\t\t\t请输入微信:");
scanf("%s", books[b].wechat);
if ((fp = fopen("books.txt", "ab+")) == NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&books[b], sizeof(struct BOOKS), 1, fp) != 1)
printf("文件写入错误!");
fclose(fp);
printf("\n\n\t\t\t\t已自动保存!");
return;
}
}
void book_delete(void) //删除模块
{
int i;
char nam[20];
FILE*fp;
FILE*fp1;
system("cls");
printf("\t\t\t\t请输入要删除的联系人:");
scanf("%s", &nam);
if ((fp = fopen("books.txt", "rb+")) == NULL)
{
printf("\t\t\t\t不能打开文件!");
}
if((fp1 = fopen("linshi.txt", "wb+"))==NULL)
{
printf("\t\t\t\t不能打开文件!");
}
for(i=0;i<20;i++)
{
fread(&books[i], sizeof(struct BOOKS), 1, fp);
if (strcmp(books[i].name, nam) !=0)
{
fwrite(&books[i], sizeof(struct BOOKS), 1, fp1);
}
}
printf("\n\n\t\t\t\t联系人已删除!\n");
fclose(fp);
fclose(fp1);
remove("books.txt");
rename("linshi.txt","books.txt");
}
void book_outside() //退出模块
{
system("cls");
printf("\n");
printf("\t\t\t\t*==============================*\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 感谢使用 *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t*==============================*\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 广东海洋大学 *\n");
printf("\t\t\t\t* 物联网工程1191 黄伊权 *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t*==============================*\n");
printf("\n");
}
void book_save(int*count) //保存模块
{
system("cls");
FILE *fp;
int i;
if ((fp = fopen("books.txt", "wb")) == NULL)
{
printf("不能打开文件!\n");
return;
}
for (i = 0; i <*count; i++)
if (fwrite(&books[i], sizeof(struct BOOKS), 1, fp) != 1)
printf("文件写入错误!");
fclose(fp);
printf("\n\n\t\t\t\t\t已保存!\n");
}
void book_reada(int*count) //阅读模块1(初次启动程序)
{
system("cls");
FILE *fp;
int i;
if ((fp = fopen("books.txt", "rb")) == NULL)
{
printf("不能打开文件!");
exit(0);
}
fclose(fp);
for (i = 0; i <*count; i++)
{
fread(&books[i], sizeof(struct BOOKS), 1, fp);
printf("\t\t\t\t姓名:%s\n", books[i].name);
printf("\t\t\t\t学号:%s\n", books[i].id);
printf("\t\t\t\t移动电话:%s\n", books[i].phone);
printf("\t\t\t\t固定电话:%s\n", books[i].tell);
printf("\t\t\t\t住址:%s\n", books[i].add);
printf("\t\t\t\t邮箱:%s\n", books[i].email);
printf("\t\t\t\t微信:%s\n", books[i].wechat);
printf("\n");
}
}
void book_readb(void)
{ //阅读模块2(再次启动程序)
system("cls");
FILE *fp;
int i,a;
if((fp=fopen("books.txt","rb"))==NULL)
{
printf("不能打开文件!");
exit(0);
}
printf("\n\n\t\t\t\t关闭程序前联系人数量:");
scanf("%d",&a);
if(a==0)
{
printf("\n\t\t\t\t\t\t空!");
}
else
printf("\n\t\t\t\t联系人信息如下:\n\n");
for (i = 0; i <a; i++)
{
fread(&books[i], sizeof(struct BOOKS), 1, fp);
printf("\t\t\t\t姓名:%s\n", books[i].name);
printf("\t\t\t\t学号:%s\n", books[i].id);
printf("\t\t\t\t移动电话:%s\n", books[i].phone);
printf("\t\t\t\t固定电话:%s\n", books[i].tell);
printf("\t\t\t\t住址:%s\n", books[i].add);
printf("\t\t\t\t邮箱:%s\n", books[i].email);
printf("\t\t\t\t微信:%s\n", books[i].wechat);
printf("\n");
}
fclose(fp);
}