You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pm72q4gsh/课程设计源代码_通讯录管理系统_计算机类1812_18...

327 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#define LEN sizeof(struct addritem)
#define FORMAT "%-15s%-20s%-10s%-15s%-30s%\n"//规定输出格式
#define DATA addrinfo[i].name,addrinfo[i].occu,addrinfo[i].tel,addrinfo[i].email,addrinfo[i].address//结构体数组元素中所有成员,方便引用
struct addritem
{
char name[10];
char occu[10];
char address[35];
char tel[15];
char email[25];
};
struct addritem addrinfo[100];
void input();
void search();
void update();
void del();
void display();
void sort();
void menu();
void menu()
{
system("cls");
printf("\n\n\n\n\n");
printf("\t\t|----------contact--------|\n");
printf("\t\t|0.exit |\n");
printf("\t\t|1.input record |\n");
printf("\t\t|2.search record |\n");
printf("\t\t|3.update record |\n");
printf("\t\t|4.delete record |\n");
printf("\t\t|5.sort |\n");
printf("\t\t|6.display |\n");
printf("\t\t|-------------------------|\n\n");
printf("\t\t\tchoose(0-6):");
}
int main()
{
int n;
menu();
scanf("%d",&n);
while(n)
{
switch(n)
{ case 1: input(); break;
case 2: search(); break;
case 3: update(); break;
case 4: del(); break;
case 5: sort(); break;
case 6: display(); break;
default:break;
}
getch();
menu();
scanf("%d",&n);
}
}
void input()
{
int i,count=0;//count用来记录通讯录中联系人的个数
char ch[2];//用来记录用户的输入
FILE *fp;
if((fp=fopen("data.txt","a+"))==NULL)//创建一个文件,如果文件打开失败则返回空值
{
printf("can not open\n");
return;
}
while(!feof(fp))//判断文件是否结束如果没有调用fread函数统计记录条数
{
if(fread(&addrinfo[count],LEN,1,fp)==1)//LEN统计结构体类型addritem中变量所有成员的长度即所占的字节数
count++;
}
fclose(fp);
if(count==0)
printf("No contact record!\n");
else
{
system("cls");
display();
}
if((fp=fopen("data.txt","wb"))==NULL)
{
printf("can not open!\n");
return;
}
for(i=0;i<count;i++)
fwrite(&addrinfo[i],LEN,1,fp);//将addrinfo的结构体数组写入磁盘文件
printf("please input(y/n)");
scanf("%s",ch);
while(strcmp(ch,"y")==0||(ch,"Y")==0)//字符串的比较函数
{ printf("name:");
scanf("%s",&addrinfo[count].name);//count变量记录结构体数组当前 可以输入的位置
for(i=0;i<count;i++)
if(strcmp(addrinfo[i].name,addrinfo[count].name)==0)
{ printf("the name already exists,press any key to continue.");
getch();
fclose(fp);
return;
} //若通讯录找不到联系人就输入其他信息
printf("occupation:");
scanf("%s",&addrinfo[count].occu);
printf("telephone:");
scanf("%s",&addrinfo[count].tel);
printf("address:");
scanf("%s",&addrinfo[count].address);
printf("email:");
scanf("%s",&addrinfo[count].email);
if(fwrite(&addrinfo[count],LEN,1,fp)!=1)//新录入的联系人信息写入磁盘文件
{printf("can not save thr record!");
getch();
}
else
{printf("%s saved!\n",addrinfo[count].name);
count++;
}
printf("continue?(y/n):");
scanf("%s",ch);
}
fclose(fp);
printf("ok\n");
}
void search()
{
FILE *fp;
int i,count=0;
char ch[2],name[15];
if((fp=fopen("data.txt","r"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))//判断是否到达文件尾部如果文件没有结束使用if语句读取
{
if(fread(&addrinfo[count],LEN,1,fp)==1)
count++;
}
fclose(fp);
if(count==0)
{
printf("no record\n");
return;
}
printf("please input the name:");
scanf("%s",name);
for(i=0;i<count;i++)
if(strcmp(name,addrinfo[i].name)==0)
{
printf("find the contact,display?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("name occupation telephone email addresse\t\n");
printf(FORMAT,DATA);
}break;
}
}
void update()
{
FILE *fp;
int i,j,count=0;
char name[15];
if((fp=fopen("data.txt","r+"))==NULL)
{
printf("can not open");
return;
}
while(!feof(fp)){
printf("%d",count);
if(fread(&addrinfo[count],LEN,1,fp)==1)
count++;
}
if(count==0)
{
printf("no record\n");
fclose(fp);
return;
}
display();
printf("please input the name of the contact which you want to update\n");
printf("update name:");
scanf("%s",&name);
for(i=0;i<count;i++)
{ if(strcmp(name,addrinfo[i].name)==0)
{printf("find the contact!you can update!\n");
printf("name:");
scanf("%s",&addrinfo[i].name);
printf("occuption:");
scanf("%s",&addrinfo[i].occu);
printf("telephone");
scanf("%s",&addrinfo[i].tel);
printf("email:");
scanf("%s",&addrinfo[i].email);
printf("address:");
scanf("%s",&addrinfo[i].address);
printf("update successful");//信息在数组中进行修改,需将信息写回磁盘中
if((fp=fopen("data.txt","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(j=0;j<count;j++)
if(fwrite(&addrinfo[j],LEN,1,fp)!=1)
{
printf("can not save!");
getch();
}
fclose(fp);
return;
}
}printf("not find contact");//若未找到联系人信息
}
void del()
{
FILE *fp;
int i,j,count=0;
char ch[2];
char name[15];
if((fp=fopen("data.txt","r+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&addrinfo[count],LEN,1,fp)==1)
count++;
fclose(fp);
if(count==0)
{
printf("no record!\n");
return;
}
display();
printf("please input the name");
scanf("%s",&name);
for(i=0;i<count;i++)
{
if(strcmp(name,addrinfo[i].name)==0)
{
printf("find the contact,del?(y\n)");
scanf("%s",ch);
if(strcmp(ch,"Y")||strcmp(ch,"y")==0)
for(j=i;j<count;j++)//i为所需删的联系人需将i后的联系人都前移一个位置
addrinfo[j]=addrinfo[j+1];
count--;
if((fp=fopen("data.txt","w"))==NULL)
{
printf("can not open");
return;
}for(j=0;j<count;j++)
if(fwrite(&addrinfo[j],LEN,1,fp)!=1)
{
printf("can not save!\n");
getch();
}
fclose(fp);
printf("del successfully!\n");
return;
}
}
printf("not find the contact");
}
void sort()
{
FILE *fp;
struct addritem t;
int i,j,count=0;
if((fp=fopen("data.txt","r+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&addrinfo[count],LEN,1,fp)==1)
count++;
fclose(fp);
if(count==0)
{
printf("no record!\n");
return;
}
for(i=0;i<count-1;i++)
for(j=i+1;j<count;j++)
if(strcmp(addrinfo[i].name,addrinfo[j].name)>0)
{
t=addrinfo[i];
addrinfo[i]=addrinfo[j];
addrinfo[j]=t;
}
if((fp=fopen("data.txt","w"))==NULL)
{
printf("can not open\n");
return;
}
for(i=0;i<count;i++)
if(fwrite(&addrinfo[i],LEN,1,fp)!=1)
{
printf("can not save!\n");
getch();
}
fclose(fp);
printf("successfully!\n");
}
void display()
{
FILE *fp;
int i,count=0;
fp=fopen("data.txt","r");
while(!feof(fp))
{
if(fread(&addrinfo[count],LEN,1,fp)==1)
count++;
}
fclose(fp);
printf("name occupation telephone email address\t\n");
for(i=0;i<count;i++)
printf(FORMAT,DATA);
}