#include<stdlib.h>
#include<ctype.h>
#include<string.h>typedef struct /*定義結構體數組*/
{
char num[10]; /*學號*/
char name[20]; /*姓名*/
char sushe[20];/*宿舍*/
char tel[20];/*電話*/
char qq[20];/*QQ號碼*/
char email[20];/*電子郵箱*/
}Student;
Student stu[80]; /*結構體數組變量*/ int menu_select() /*菜單函數*/
{
char c;
do{
system("cls"); /*運行前清屏*/
printf("\t\t |☆☆☆☆☆☆通訊錄信息管理系統☆☆☆☆☆\n"); /*菜單選擇*/
printf("\t\t |****************************************|\n");
printf("\t\t |******** 1. 輸入學生信息 ***************|\n");
printf("\t\t |******** 2. 查看完整信息 ***************|\n");
printf("\t\t |******** 3. 按學號進行排列 *************|\n");
printf("\t\t |******** 4. 修改通訊錄信息 *************|\n");
printf("\t\t |******** 5. 刪除通訊錄信息 *************|\n");
printf("\t\t |******** 6. 按姓名檢索信息 *************|\n");
printf("\t\t |******** 7. 讀取文件 *******************|\n");
printf("\t\t |******** 8. 保存文件 *******************|\n");
printf("\t\t |******** 0. 退出系統 *******************|\n");
printf("\t\t |****************************************|\n");
printf("\t\t |***制作人:QIANG *****|\n");
printf("\t\t |****************************************|\n");
printf("\t\t (*^__^*) 您好,請選擇(*^__^*)→→→(0-8):");
c=getchar(); /*讀入選擇*/
}while(c<'0'||c>'9');
return(c-'0'); /*返回選擇*/
}
int Input(Student stud[],int n) /*輸入若幹條記錄*/
{
int i=0,p=0;
char sign,f,x[10]; /*x[10]為清除多余的數據所用*/
while(sign!='n'&&sign!='N') /*判斷*/
{
printf("\t\t\t請輸入學號:"); /*交互輸入*/
scanf("\t\t\t%s",stud[n+i].num);
for(p=0;p=n;p++)
{
if(strcmp(stud[p].num,stud[n+i].num)==0)
{
printf("\t\t\t學號已經存在!\n");
system("pause");
return(n);
}
}
printf("\t\t\t請輸入姓名:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\t請輸入住址:");
scanf("\t\t\t%s",stud[n+i].sushe);
printf("\t\t\t請輸入電話號碼:");
scanf("\t\t\t%s",stud[n+i].tel);
printf("\t\t\t請輸入QQ:");
scanf("\t\t\t%s",stud[n+i].qq);
printf("\t\t\t請輸入電子郵箱:");
scanf("\t\t\t%s",&stud[n+i].email);
gets(x); /*清除多余的輸入*/
printf("\t\t\tany more records?(Y/N)");
scanf("\t\t\t%c",&sign); /*輸入判斷*/
i++;
}
return(n+i);
} void Display(Student stud[],int n) /*顯示所有記錄*/
{
int i;
printf("--------------------------------------------------------------------------------\n"); /*格式頭*/
printf("學號 名字 住址 電話號碼 QQ號碼 電子郵箱\n");
printf("--------------------------------------------------------------------------------\n");
for(i=1;i<n+1;i++) /*循環輸入*/
{
printf("%-16s%-15s%-15s%-15s%-15s%-15s\n",stud[i-1].num,stud[i-1].name,stud[i-1].sushe,stud[i-1].tel,stud[i-1].qq,stud[i-
1].email);
if(i>1&&i%10==0) /*每十個暫停*/
{printf("--------------------------------------------------------------------------------\n"); /*格式*/ <br> printf("\t\t\t"); <br> system("pause"); <br> printf("--------------------------------------------------------------------------------\n"); <br> }
}
printf("\t\t\t");
system("pause");
} void Sort_by_num(Student stud[],int n) /*按學號排序*/
{ int i,j;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{
strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
strcpy(t,stud[j+1].sushe);
strcpy(stud[j+1].sushe,stud[j].sushe);
strcpy(stud[j].sushe,t);
strcpy(t,stud[j+1].tel);
strcpy(stud[j+1].tel,stud[j].tel);
strcpy(stud[j].tel,t);
strcpy(t,stud[j+1].qq);
strcpy(stud[j+1].qq,stud[j].qq);
strcpy(stud[j].qq,t);
strcpy(t,stud[j+1].email);
strcpy(stud[j+1].email,stud[j].email);
strcpy(stud[j].email,t);
}
} void Query_a_record(Student stud[],int n) /*查找並顯示壹個記錄*/
{
char s[20];
int i=0;
printf("\t\t\t請輸入要查找的人的名字:"); /*交互式輸入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t對不起,查無此人!\n"); /*輸入失敗信息*/
return;
}
printf("\t\t\t學號:%s\n",stud[i].num); /*輸出該學生信息*/
printf("\t\t\t住址:%s\n",stud[i].sushe);
printf("\t\t\t電話號碼:%s\n",stud[i].tel);
printf("\t\t\tQQ號碼:%s\n",stud[i].qq);
printf("\t\t\t電子郵箱:%s\n",stud[i].email);
}
int Delete_a_record(Student stud[],int n) /*按姓名查找,刪除壹條記錄*/
{
char s[20];
int i=0,j;
printf("\t\t\t請輸入要刪除的人的名字:"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t對不起,查無此人!\n"); /*返回失敗信息*/
return(n);
}
for(j=i;j<n-1;j++) /*刪除操作*/
{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].sushe,stud[j+1].sushe);
strcpy(stud[j].tel,stud[j+1].tel);
strcpy(stud[j].qq,stud[j+1].qq);
strcpy(stud[j].name,stud[j+1].name);
strcpy(stud[j].email,stud[j+1].email);
}
printf("\t\t\t刪除成功!\n"); /*返回成功信息*/
return(n-1);
}
int AddfromText(Student stud[],int n) /*從文件中讀入數據*/
{
int i=0,num;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\t請您輸入要讀取的文件名:");
scanf("\t\t\t%s",filename); /*輸入文件名*/
if((fp=fopen(filename,"rb"))==NULL) /*打開文件*/
{
printf("\t\t\t打開文件失敗!\n"); /*打開失敗信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*讀入總記錄量*/
while(i<num) /*循環讀入數據*/
{
fscanf(fp,"%s%s%s%s%s%s",stud[n+i].num,stud[n+i].name,stud[n+i].sushe,stud[n+i].tel,stud[n+i].qq,stud[n+i].email);
i++;
}
n+=num;
fclose(fp); /*關閉文件*/
printf("\t\t\t讀取成功!\n");
printf("\t\t\t");
system("pause");
return(n);
} void WritetoText(Student stud[],int n) /*將所有記錄寫入文件*/
{
int i=0;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\t保存文件\n"); /*輸入文件名*/
printf("\t\t\t請您為文件取個名字:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL) /*打開文件*/
{
printf("\t\t\t成功!\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循環寫入數據*/
while(i<n)
{
fprintf(fp,"%s %s %s %s %s %s \n",stud[i].num,stud[i].name,stud[i].sushe,stud[i].tel,stud[i].qq,stud[i].email);
i++;
}
fclose(fp); /*關閉文件*/
printf("成功!\n"); /*返回成功信息*/
} int Modify_a_record(Student stud[],int n) /*按姓名查找,修改壹條記錄*/
{
char s[20];
char newname[20];
char newnum[20];
char newsushe[20];
char newtel[20];
char newqq[20];
char newemail[20];
int i=0;
printf("\t\t\t輸入要修改人的名字:"); /*交互式問尋*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t找不到這個信息!\n"); /*返回失敗信息*/
return(n+1);
}
printf("請輸入新姓名:");
scanf("%s",newname);
strncpy(stud[i].name,newname,sizeof(stud[i].name));
stud[i].name[sizeof(stud[i].name)-1]='\0'; printf("請輸入新學號:");
scanf("%s",newnum);
strncpy(stud[i].num,newnum,sizeof(stud[i].num));
stud[i].num[sizeof(stud[i].num)-1]='\0'; printf("請輸入新地址:");
scanf("%s",newsushe);
strncpy(stud[i].sushe,newsushe,sizeof(stud[i].sushe));
stud[i].sushe[sizeof(stud[i].sushe)-1]='\0'; printf("請輸入新電話號碼:");
scanf("%s",newtel);
strncpy(stud[i].tel,newtel,sizeof(stud[i].tel));
stud[i].tel[sizeof(stud[i].tel)-1]='\0'; printf("請輸入新qq號碼:");
scanf("%s",newname);
strncpy(stud[i].qq,newqq,sizeof(stud[i].qq));
stud[i].qq[sizeof(stud[i].qq)-1]='\0'; printf("請輸入新電子郵箱:");
scanf("%s",newemail);
strncpy(stud[i].email,newemail,sizeof(stud[i].email));
stud[i].email[sizeof(stud[i].email)-1]='\0'; printf("\t\t\t修改信息成功咯\n"); /*返回成功信息*/
return (n);
}
void main() /*主函數*/
{
int n=0;
system("color 1A"); //調用dos改背景與前景色
for(;;)
{
switch(menu_select()) /*選擇判斷*/
{
case 1:
printf("\t\t\t輸入信息\n"); /*輸入若幹條記錄*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\t顯示信息\n"); /*顯示所有記錄*/
Display(stu,n);
break;
case 3:
printf("\t\t\t按學號排序\n");
Sort_by_num(stu,n); /*按學號排序*/
printf("\t\t\t排列成功咯!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\t修改學生信息\n");
n=Modify_a_record(stu,n); /*按姓名查找,修改壹條記錄*/
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\t刪除學生信息\n");
n=Delete_a_record(stu,n); /*按姓名查找,刪除壹條記錄*/
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\t查找壹個信息\n");
Query_a_record(stu,n); /*查找並顯示壹個記錄*/
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\t打開保存過的文件\n");
n=AddfromText(stu,n); /*新增功能,輸出統計信息*/
break;
case 8:
printf("\t\t\t保存數據到文件\n");
WritetoText(stu,n); /*循環寫入數據*/
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\t感謝使用,祝妳好運哦!\n"); /*結束程序*/
printf("\t\t\t");
system("pause");
exit(0);
}
}
}