📄 c语言实现通讯录.txt
字号:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define LENGTH 20
typedef struct record{char num[20];
char name[8];
char sex[3];
char birth[8];
char addr[50];
char pcode[8];
char tel[15];
}PERSON;
char filename[10];int i,j;PERSON one[LENGTH];
FILE *fp;
void creat();/*创建并输入通讯录*/
void search(); /*根据姓名进行查找*/
void insert(); /*添加新记录*/
void modify(); /*修改记录*/
void del(); /*删除记录*/
void sequ();/*对记录进行排序*/
void display();/*显示所有记录*/
void textbackground();/*显示背景颜色*/
void textcolor();/*显示前景字符颜色*/
void show();/*设置前背景颜色*/
screen()/*设置通讯录界面*/
{
printf("\n ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
printf("\n + +");
printf("\n + Welcome to use this address book! +");
printf("\n + +");
printf("\n + If you want to +");
printf("\n + creat a record:press 1 +");
printf("\n + insert a record:press 2 +");
printf("\n + del a record:press 3 +");
printf("\n + modify a record:press 4 +");
printf("\n + search a record:press 5 +");
printf("\n + sequence a record:press 6 +");
printf("\n + exit the address book:press 0 +");
printf("\n + +");
printf("\n + made by Kehang +");
printf("\n + +");
printf("\n ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
main()
{int m;
clrscr();show();
screen();
printf ("\n Please make a choice:");
for(;;)
{
scanf("%d",&m);
if(m>=0&&m<=6)
{switch(m)
{case 1: creat(); break;
case 2: insert(); break;
case 3: del(); break;
case 4: modify(); break;
case 5: search(); break;
case 6: sequ();break;
case 0: exit(0);
}
}
}
}
void show()
{
textbackground(WHITE);
textcolor(LIGHTRED);
}
void creat () /*建立文本文件,方便用其他编辑器查看通讯簿内容*/
{char c;i=0;clrscr();show();
printf("please import the name of the address book!\n");
scanf("%s",filename);
loop:if(i<LENGTH) /*向空通讯录中输入记录*/
{fp=fopen("one_list.txt","ab");/*以追加方式打开文件*/
printf("Please import the num: ");
scanf("%s",one[i].num);
printf("\nPlease import the name:");
scanf("%s",one[i].name);
printf("\nPlease import the sex:");
scanf("%s",one[i].sex);
printf("\nPlease import the birthday:");
scanf("%s",one[i].birth);
printf("\nPlease import the address:");
scanf("%s",one[i].addr);
printf("\nPlease import the pcode:");
scanf("%s",one[i].pcode);
printf("\nPlease import the phone:");
scanf("%s",one[i].tel);
fp=fopen("one_list.txt","ab");/*以追加方式打开文件*/
fwrite(&one[i],sizeof(PERSON),1,fp); /*将一个数据块写入文件*/
fclose(fp); /*关闭文件*/
printf("Import successfully!Add again?Y/N ");
getchar();
c=getchar();
++i;
if (c=='Y'||c=='y') goto loop;
}
clrscr();
screen();
printf("\n please choose again!\n");
}
void search() /*按输入的姓名查找是否有相应的记录*/
{
clrscr();
show();
fp=fopen("one_list.txt","rb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fread(&one[j],sizeof(PERSON),1,fp); /*将一个数据块写入文件*/
fclose(fp); /*关闭文件*/
printf("Which record do you want to search:");
scanf("%d",&i);
printf("\nnumber:%s\n",one[i-1].num);
printf("name:%s\n",one[i-1].name);
printf("sex:%s\n",one[i-1].sex);
printf("birth:%s\n",one[i-1].birth);
printf("addr:%s\n",one[i-1].addr);
printf("pcode:%s\n",one[i-1].pcode);
printf("telephone:%s\n",one[i-1].tel);
getchar();getchar();
clrscr();
screen();
printf("\n Please choose again!\n");
}
void modify() /*修改指定序号对应的记录*/
{
clrscr();show();
fp=fopen("one_list.txt","rb");/*为输入打开文件*/
for(j=0;j<LENGTH;j++)
fread(&one[j],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
printf("Please import which record do you want to modify?");
scanf("%s",&i);
printf("Please import the num: ");
scanf("%s",one[i-1].num);
printf("Please import the name:");
scanf("%s",one[i-1].name);
printf("Please import the sex:");
scanf("%s",one[i-1].sex);
printf("Please import the birthday:");
scanf("%s",one[i-1].birth);
printf("Please import the address:");
scanf("%s",one[i-1].addr);
printf("Please import the pcode:");
scanf("%s",one[i-1].pcode);
printf("Please import the phone:");
scanf("%s",one[i-1].tel);
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(i=0;i<LENGTH;i++)
fwrite(&one[i],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
getchar();getchar();
clrscr();
screen();
printf("\n Please choose again!\n");
}
void del( ) /*删除指定姓名对应的记录*/
{
clrscr();
show();
printf("Which record do you want to delete?\n");
scanf("%d",&i);
fp=fopen("one_list.txt","rb");/*为输入打开文件*/
for(j=0;j<LENGTH;j++)
fread(&one[j],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=i-1;j<LENGTH;j++)
one[j]=one[j+1];
fp=fopen("one_list","wb");/*为输出打开文件*/
for(i=0;i<LENGTH;i++)
fwrite(&one[i],sizeof(PERSON),1,fp); /*将一个数据块写入文件*/
fclose(fp); /*关闭文件*/
clrscr();
screen();
printf("\n please choose again!\n");
}
void insert()/*在一特定序号处插入记录*/
{
clrscr();
show();
printf("Please import the number of the record you want to insert?\n");
scanf("%d",&i);
fp=fopen("one_list.txt","rb");/*为输入打开文件*/
for(j=0;j<LENGTH;j++)
fread(&one[j],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=i;j<=LENGTH;j++)
one[j]=one[j-1];
printf("Please import the num: ");
scanf("%s",one[i-1].num);
printf("Please import the name:");
scanf("%s",one[i-1].name);
printf("Please import the sex:");
scanf("%s",one[i-1].sex);
printf("Please import the birthday:");
scanf("%s",one[i-1].birth);
printf("Please import the address:");
scanf("%s",one[i-1].addr);
printf("Please import the pcode:");
scanf("%s",one[i-1].pcode);
printf("Please import the phone:");
scanf("%s",one[i-1].tel);
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(i=0;i<LENGTH;i++)
fwrite(&one[i],sizeof(PERSON),1,fp); /*将一个数据块写入文件*/
fclose(fp); /*关闭文件*/
printf("insert successfully!");getchar();
clrscr();
screen();
printf("\n please choose again!\n");
}
void display()/*显示所有记录*/
{i=0;
show();
printf("number\tname\tsex\tbirthday\taddress\tpcode\ttelephone");
while(i<LENGTH)
{fp=fopen("one_list.txt","rb");/*为输入打开文件*/
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
fclose(fp); /*关闭文件*/
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",one[i].num,one[i].name,one[i].sex,one[i].birth,one[i].addr,one[i].pcode,one[i].tel);
i++;}getchar();
printf("Any key to exit!");
getchar();
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_num()/*按学号排序*/
{
PERSON man;int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].num,one[k].num))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_name()/*按姓名排序*/
{
PERSON man;int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].name,one[k].name))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_birth()/*按出生年月排序*/
{
PERSON man;int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].birth,one[j].birth))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_addr()/*按地址排序*/
{
PERSON man;int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].addr,one[k].addr))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_pcode()/*按邮编排序*/
{
PERSON man;int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].pcode,one[k].pcode))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ_tel()/*按电话排序*/
{
PERSON man;
int k;
for(i=0;i<LENGTH;i++)
fread(&one[i],sizeof(PERSON),1,fp); /*从文件中读一个数据块*/
for(j=0;j<LENGTH;j++)
for(k=j+1;k<LENGTH;k++)
if((strcmp(one[j].tel,one[k].tel))>0) /*按顺序将数据从小到大排列*/
{
man=one[j];one[j]=one[k];one[k]=man;
}
fp=fopen("one_list.txt","wb");/*为输出打开文件*/
for(j=0;j<LENGTH;j++)
fwrite(&one[j],sizeof(LENGTH),1,fp); /*将一个数据块写入文件*/
fclose (fp); /*关闭文件*/
clrscr();
screen();
printf("\n Please choose again!\n");
}
void sequ()/*将所有排序汇总*/
{int m;
clrscr();
show();
printf("sequence records through the number:press 1\n");
printf("sequence records through the name:press 2\n");
printf("sequence records through the birthday:press 3\n");
printf("sequence records through the address:press 4\n");
printf("sequence records through the pcode:press 5\n");
printf("sequence records through the tel:press 6\n");
printf("make you choice please.\n");
scanf("%d",&m);
if(m>=1&&m<=6)
{switch(m)
{
case 1:sequ_num();display();break;
case 2:sequ_name(); display();break;
case 3:sequ_birth(); display();break;
case 4:sequ_addr(); display();break;
case 5:sequ_pcode(); display();break;
case 6:sequ_tel();display();
}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -