用C++编写一个学生通讯录管理系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FORMAT "%s\t\t%s\t\t%s\t%d\t%s\n"
int count=0;
struct Address
{
char Name[21];
int Age;
char Sex[6];
char ClassNum[11];
char PhonNum[12];
};
int Enterface();
int Add(Address book[]);
int Modify(Address book[]);
int Delent(Address book[]);
int Insert(Address book[]);
int Find(Address book[]);
int Show(Address book[]);
int FindName(Address book[]);
int FindClassNum(Address book[]);
int DelName(Address book[]);
int DelClassNum(Address book[]);
int FindPhonNum(Address book[]);
int Readtxt(Address book[]);
int Writetxt(Address book[]);
int Search(Address book[],char str1[],int n);
int FSearch(Address book[],char str2[],int n);
int Taxis(Address book[]);
int Exit();
////////////////////////////////////////////////////////////////////
void main()
{
Address book[100];
int choice;
char or='m';
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n");
printf("\n");
printf(" !!!欢迎使用班级通讯录系统!!!\n");
printf("\n");
printf(" !!!按任意键进入系统!!!\n");
printf("\n");
printf(" !!!管理员:******!!!\n");
printf("\n");
printf("///////////////////////////////////////////////////////\n");
system("pause");
system("cls");
Readtxt(book);
while(or=='m')
{
choice=Enterface();
switch(choice)
{
case 1: Add(book);
break;
case 2: Modify(book);
break;
case 3: Delent(book);
break;
case 4: Insert(book);
break;
case 5: Find(book);
break;
case 6: Show(book);
break;
case 7: Exit();
break;
default : printf("!!!输入错误!!!\n");
Exit();
break;
}
}
}
int Enterface()
{
int choice=0;
printf("*******************\n");
printf(" 1.添加\n");
printf(" 2.修改\n");
printf(" 3.删除\n");
printf(" 4.插入\n");
printf(" 5.查找\n");
printf(" 6.显示\n");
printf(" 7.退出\n");
printf("*******************\n");
printf("请输入您的选择:");
scanf("%d",&choice);
return choice;
}
int Add(Address book[])
{
char or='Y';
while('Y'==or)
{
printf("请输入姓名:");
scanf("%s",&book[count].Name);
flag1:
printf("请输入电话号码:");
scanf("%s",&book[count].PhonNum);
for(int i=0;i<count;i++)
{
if(strcmp(book[count].PhonNum,book[i].PhonNum)==0)
{
printf("!!!号码已存在!!!\n");
goto flag1;
}
}
flag2:
printf("请输入学号:");
scanf("%s",&book[count].ClassNum);
for(i=0;i<count;i++)
{
if(strcmp(book[count].ClassNum,book[i].ClassNum)==0)
{
printf("!!!学号已存在!!!\n");
goto flag2;
}
}
printf("请输入年龄:");
scanf("%d",&book[count].Age);
printf("请输入性别:");
scanf("%s",&book[count].Sex);
getchar();
count++;
printf("是否继续添加('Y'添加,其他键返回主界面):");
scanf("%c",&or);
if('Y'!=or)
{
system("cls");
break;
}
}
Taxis(book);
Writetxt(book);
// printf("sdfsdfsdf\n");
return 0;
}
int Modify(Address book[])
{
char str[11];
int flag=0;
int ksd=0;
printf("去年个输入学号:");
scanf("%s",&str);
for(int i=0;i<count;i++)
{
if(strcmp(str,book[i].ClassNum)==0)
{
if(ksd==0)
{
printf(" 姓名\t 电话\t\t学号\t性别\t年龄\n");
ksd=1;
}
printf("%8s\t%8s\t%s\t%s\t%d\n",book[i].Name,book[i].PhonNum,
book[i].ClassNum,book[i].Sex,book[i].Age);
// str[flag_num++]=i;
flag=1;
// i--;
break;
}
}
if(flag==0)
{
printf("!!!无此记录!!!\n");
return 0;
}
printf("请输入姓名:");
scanf("%s",&book[i].Name);
flag0:
printf("请输入电话号码:");
scanf("%s",&book[i].PhonNum);
for(int j=0;j<count;j++)
{
if(j==i)
continue;
if(strcmp(book[i].PhonNum,book[j].PhonNum)==0)
{
printf("!!!号码已存在!!!\n");
goto flag0;
}
}
flag3:
printf("请输入学号:");
scanf("%s",&book[i].ClassNum);
for(j=0;j<count;j++)
{
if(j==i)
continue;
if(strcmp(book[i].ClassNum,book[j].ClassNum)==0)
{
printf("!!!学号已存在!!!\n");
goto flag3;
}
}
printf("请输入年龄:");
scanf("%d",&book[i].Age);
printf("请输入性别:");
scanf("%s",&book[i].Sex);
getchar();
printf("修改完毕!\n");
Writetxt(book);
system("pause");
// printf("modify\n");
return 0;
}
int Delent(Address book[])
{
// printf("delent\n");
int choice=0;
printf("================\n");
printf(" 1.按姓名删除\n");
printf(" 2.按学号删除\n");
printf("================\n");
scanf("%d",&choice);
switch(choice)
{
case 1: DelName(book);
break;
case 2: DelClassNum(book);
break;
default :
break;
}
return 0;
}
int Insert(Address book[])
{
Add(book);
// printf("insert\n");
return 0;
}
int Find(Address book[])
{
int choice=0;
printf("================\n");
printf(" 1.按姓名查找\n");
printf(" 2.按学号查找\n");
printf(" 3.按电话查找\n");
printf("================\n");
scanf("%d",&choice);
switch(choice)
{
case 1: FindName(book);
break;
case 2: FindClassNum(book);
break;
case 3: FindPhonNum(book);
break;
default :
break;
}
// printf("find\n");
return 0;
}
int FindName(Address book[])
{
char aname[30];
printf("请输入要查找的名字:");
scanf("%s",&aname);
FSearch(book,aname,3);
return 0;
}
int FindClassNum(Address book[])
{
char anum[20];
printf("请输入要查找的学号:");
scanf("%s",&anum);
FSearch(book,anum,2);
return 0;
}
int FindPhonNum(Address book[])
{
char aphonnum[20];
printf("请输入要查找的学号:");
scanf("%s",&aphonnum);
FSearch(book,aphonnum,1);
return 0;
}
int Show(Address book[])
{
int or=0;
if(count==0)
{
printf("!!!无记录!!!\n");
system("pause");
system("cls");
}
else
{
printf(" 姓名\t 电话\t\t学号\t性别\t年龄\n");
for(int i=0;i<count;i++)
printf("%8s\t%8s\t%s\t%s\t%d\n",book[i].Name,book[i].PhonNum,
book[i].ClassNum,book[i].Sex,book[i].Age);
system("pause");
}
printf("是否排序?\n-=1.排序=- -=2.返回主界面=-\n");
scanf("%d",&or);
if(1==or)
Taxis(book);
return 0;
}
int DelName(Address book[])
{
char aname[20];
printf("请输入要删除的姓名:");
scanf("%s",&aname);
Search(book,aname,2);
return 0;
}
int DelClassNum(Address book[])
{
char anum[20];
printf("请输入要删除的学号:");
scanf("%s",&anum);
Search(book,anum,1);
return 0;
}
int Readtxt(Address book[])
{
FILE *fp1;
fp1=fopen("photonumbers.txt","r");
if(fp1 == NULL)
{
printf("wrong!\n");
system("pause");
}
while(!feof(fp1))
{
if(!fscanf(fp1,"%s\t\t%s\t\t%s\t%d\t%s",book[count].Name,book[count].PhonNum,
book[count].ClassNum,&book[count].Age,book[count].Sex))
printf("读入出错!");
count++;
}
count--;
fclose(fp1);
return 0;
}
int Writetxt(Address book[])
{
FILE *fp;
fp=fopen("photonumbers.txt","w");
for(int i=0;i<count;i++)
{
fprintf(fp,FORMAT,book[i].Name,book[i].PhonNum,
book[i].ClassNum,book[i].Age,book[i].Sex);
}
fclose(fp);
return 0;
}
int Search(Address book[],char str1[],int n)
{
int flag=0;
int str[5];
int flag_num=0;
int j;
int gf;
int ksd=0;
for(int i=0;i<count;i++)
{
if(n==1)
{
if(strcmp(str1,book[i].ClassNum)==0)
flag=2;
}
else if(n==2)
{
if(strcmp(str1,book[i].Name)==0)
flag=2;
}
if(flag==2)
{
if(ksd==0)
{
printf(" 姓名\t 电话\t\t学号\t性别\t年龄\n");
ksd=1;
}
printf("%8s\t%8s\t%s\t%s\t%d\n",book[i].Name,book[i].PhonNum,
book[i].ClassNum,book[i].Sex,book[i].Age);
str[flag_num++]=i;
flag=1;
}
}
if(flag==0)
{
printf("!!!无记录!!!\n");
system("pause");
system("cls");
}
if(flag==1)
{
printf("-=1.确认删除=-\t-=2.取消=-\n");
scanf("%d",&j);
if(1==j)
{
gf=1;
for(int m=str[0];m<count-flag_num;m++)
{
for(int j=gf-1;j<flag_num;j++)
{
if((m+gf)==str[j])
gf++;
}
strcpy(book[m].Name,book[m+gf].Name);
book[m].Age=book[m+gf].Age;
strcpy(book[m].ClassNum,book[m+gf].ClassNum);
strcpy(book[m].PhonNum,book[m+gf].PhonNum);
strcpy(book[m].Sex,book[m+gf].Sex);
}
count=count-flag_num;
Writetxt(book);
}
else if(2==j)
{
printf("!!!操作已取消!!!\n");
}
system("pause");
}
return 0;
}
int FSearch(Address book[],char str2[],int n)
{
int flag=0;
int ksd=0;
for(int i=0;i<count;i++)
{
if(n==1)
{
if(strcmp(str2,book[i].PhonNum)==0)
flag=2;
}
else if(3==n)
{
if(strcmp(str2,book[i].Name)==0)
flag=2;
}
else if(2==n)
{
if(strcmp(str2,book[i].ClassNum)==0)
flag=2;
}
if(flag==2)
{
if(0==ksd)
{
printf(" 姓名\t 电话\t\t学号\t性别\t年龄\n");
ksd=1;
}
printf("%8s\t%8s\t%s\t%s\t%d\n",book[i].Name,book[i].PhonNum,
book[i].ClassNum,book[i].Sex,book[i].Age);
flag=1;
}
}
if(1==flag)
system("pause");
else if(flag==0)
{
printf("!!!无此记录!!!\n");
system("pause");
system("cls");
}
return 0;
}
int Taxis(Address book[])
{
/* for(int i=0;i<count;i++)
{
for(int j=0;j<count-1-i;j++)
{
if(strcmp(book[j].ClassNum,book[j+1].ClassNum)>0)
{
Address bk;
strcpy(bk.Name,book[j].Name);
bk.Age=book[j].Age;
strcpy(bk.ClassNum,book[j].ClassNum);
strcpy(bk.PhonNum,book[j].PhonNum);
strcpy(bk.Sex,book[j].Sex);
strcpy(book[j].Name,book[j+1].Name);
book[j].Age=book[j+1].Age;
strcpy(book[j].ClassNum,book[j+1].ClassNum);
strcpy(book[j].PhonNum,book[j+1].PhonNum);
strcpy(book[j].Sex,book[j+1].Sex);
strcpy(book[j+1].Name,bk.Name);
book[j+1].Age=bk.Age;
strcpy(book[j+1].ClassNum,bk.ClassNum);
strcpy(book[j+1].PhonNum,bk.PhonNum);
strcpy(book[j+1].Sex,bk.Sex);
}
}
}
printf("!!!操作成功!!!\n");
system("pause");
system("cls");
Writetxt(book);*/
return 0;
}
int Exit()
{
exit(1);
return 0;
}
这个是我以前写的程序,本来有个已经写好的文档,里面存了一些虚拟的联系方式,如果需要,我把整个工程也发给你,留个邮箱....O(∩_∩)O~