first commit

master
炎凉 6 years ago
commit 2693349810

@ -0,0 +1,221 @@
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(phone)
static int count=0;
FILE *ptr1;
struct txl
{
char name[10];
char hometown[10];
char number1[11];
char number2[11];
char e_mail[10];
}phone[100];
void tianjia()
{
static int i=0;
int j,t;
struct txl *p;
p=phone;
printf("可以开始添加\n");
ptr1=fopen("sto.txt","ab+");
if(ptr1==NULL)
{
ptr1=fopen("sto.txt","wb+");
if(ptr1==NULL)
{
printf("Cannot open file");
exit(0);
}
}
fseek(ptr1,0,SEEK_END);
do
{
printf("请输入姓名:");
scanf("%s",&p[i].name); printf("\n");
printf("请输入籍贯:");
scanf("%s",&p[i].hometown); printf("\n");
printf("请输入电话号码1:");
scanf("%s",&p[i].number1);printf("\n");
printf("请输入电话号码2:");
scanf("%s",&p[i].number2); printf("\n");
printf("请输入电子邮箱:");
scanf("%s",&p[i].e_mail); printf("\n");
fwrite(&p[i],LEN,1,ptr1);
printf("完成一个添加,是否继续添加\n ");
printf("1为继续 0为退出添加\n");
fflush(stdin);
scanf("%d",&j);
i++;count++;
}while(j!=0&&count!=100);
fclose(ptr1);
}
void chakan()//查看需查找的联系人
{
FILE *ptr1;
struct txl *p;
p=phone;
char name[10];
int j=0,i,m=0;
p=phone;
ptr1=fopen("sto.txt","rb");
// rewind(ptr1);
while(!feof(ptr1))
{
if(fread(&p[m],LEN,1,ptr1)==1)
m++;
}
fclose(ptr1);
printf("请输入你需要查看人的姓名:");
scanf("%s",&name);
printf(" 所有同名的人都在这:\n");
printf("姓名 籍贯 电话 电话 邮箱\n");
for(i=0;i<m;i++)
if(strcmp(p[i].name,name)==0)
{
printf("%-10s%-10s%-11s%-11s%-10s\n",p[i].name,p[i].hometown,p[i].number1,p[i].number2,p[i].e_mail);
j=1;
}
if(j==0)
{
printf("\n");
printf("没有此联系人!!\n");
printf("\n");
}
}
void daying()//输出所有的联系人列表
{
struct txl *p;
FILE *ptr1;
int i,m=0;
p=phone;
ptr1=fopen("sto.txt","rb");
// rewind(ptr1);
while(!feof(ptr1))
{
if(fread(&p[m],LEN,1,ptr1)==1)
m++;
}
fclose(ptr1);
printf("姓名 籍贯 电话 电话 邮箱\n");
for(i=0;i<m;i++)
{
printf("%-10s%-10s%-11s%-11s%-10s\n",p[i].name,p[i].hometown,p[i].number1,p[i].number2,p[i].e_mail);
}
}
void shanchu()
{
struct txl *p;
char name[20],mail[20];
p=phone;
int i,j=0,m=0;
FILE *ptr1;
ptr1=fopen("sto.txt","rb");
rewind(ptr1);
while(!feof(ptr1))
{
if(fread(&p[m],LEN,1,ptr1)==1)
m++;
}
fclose(ptr1);
printf("请输入被删除人的姓名和邮箱\n");
printf("姓名:");scanf("%s",&name);printf("\n");
printf("邮箱:");scanf("%s",&mail);printf("\n");
for(i=0;i<m;i++)
{
if(strcmp(p[i].name,name)==0&&strcmp(p[i].e_mail,mail)==0)
{
for(;i<m-1;i++)
p[i]=p[i+1];
j=1;
}
}
ptr1=fopen("sto.txt","w");
for(i=0;i<m-1;i++)
{
fwrite(&p[i],LEN,1,ptr1);
}
fclose(ptr1);
if(j==1)
printf("删除成功\n");
else
printf("没有此联系人\n");
}
void xiugai()
{
FILE *ptr1;
struct txl *p;
p=phone;
char name[10],mail[10];
int j,i,m=0;
p=phone;
ptr1=fopen("sto.txt","rb");
// rewind(ptr1);
while(!feof(ptr1))
{
if(fread(&p[m],LEN,1,ptr1)==1)
m++;
}
fclose(ptr1);
printf("请输入修改人的姓名和邮箱\n");
printf("姓名:");scanf("%s",&name);printf("\n");
printf("邮箱:");scanf("%s",&mail);printf("\n");
for(i=0;i<m;i++)
if(strcmp(p[i].name,name)==0&&strcmp(p[i].e_mail,mail)==0)
{
printf(" 选择修改的内容\n");
do
{
printf("___________________\n");
printf("1.修改姓名 ");printf("2.修改籍贯\n");
printf("3.修改电话号1 ");printf("4.修改电话号2\n");
printf("5.修改邮箱 ");printf("0.放弃修改/退出修改\n");
printf("___________________\n");
printf("请输入序号:\n");
scanf("%d",&j);
switch(j)
{
case 1:printf("请输入修改姓名: ");scanf("%s",&p[i].name);printf("修改成功\n");break;
case 2:printf("请输入修改籍贯: ");scanf("%s",&p[i].hometown);printf("修改成功\n");break;
case 3:printf("请输入修改电话号1: ");scanf("%s",&p[i].number1);printf("修改成功\n");break;
case 4:printf("请输入修改电话号2: ");scanf("%s",&p[i].number2);printf("修改成功\n");break;
case 5:printf("请输入修改邮箱: ");scanf("%s",&p[i].e_mail);printf("修改成功\n");break;
}
}while(j!=0);
}
ptr1=fopen("sto.txt","w");
for(i=0;i<m-1;i++)
{
fwrite(&p[i],LEN,1,ptr1);
}
fclose(ptr1);
}
int main()
{
int choice;
do
{
printf("===============菜单显示===============\n");
printf(" 1.添加联系人\n");
printf(" 2.查找联系人\n");
printf(" 3.删除联系人\n");
printf(" 4.修改电话簿\n");
printf(" 5.浏览所有联系人\n");
printf(" 0.退出电话簿\n");
// printf(" 7.清屏\n");
printf("======================================\n");
printf(" 请输入操作序号;");
scanf("%d",&choice);
switch(choice)
{
case 1:tianjia();break;
case 2:chakan();break;
case 3:shanchu();break;
case 4:xiugai();break;
case 5:daying();break;
}
}while(choice!=0);
return 0;
}
Loading…
Cancel
Save