#include "ctype.h" #include "conio.h" #include "stdio.h" #include "string.h" #define M 50 typedef struct { char name[20]; /*姓名*/ char nuits[30]; /* 单位 */ char tele[20]; /* 电话*/ } Info; Info L[M]; /*定义结构体数组,保存联系人的的信息*/ int length=0; /*保存记录(联系人)的个数*/ int Find(char *name) {/*数组L存在,在L中查找与name名字相同的记录,找到返回该记录的位置i;否则也返回i的值*/ int i; for (i=0;i<=length ;i++ ) { if (strcmp(name,L[i].name)==0) { break; } } return i; } void ListInsert() {/*数组L存在,往数组中输入信息*/ int i,p; char yes; Info t={"maliang","hebtu","15831999946"}; printf("Plese input name nuits tele\n"); printf(" "); scanf("%s%s%s",&t.name,&t.nuits,&t.tele); printf("\nAre you want to Save this new record?\n"); yes=getche(); if(yes=='Y'||yes=='y') { i=Find(&t.name); if (i==length+1) { strcpy(L[length].name,t.name); strcpy(L[length].nuits,t.nuits); strcpy(L[length].tele,t.tele); length++; printf("\nInsert Successed"); } else { printf("Failed!Had a same name in record!\n"); } } } void ListDisplay() {/*数组L存在,显示数组L中的所有元素*/ int i; int page,p,start,end,pages=5; printf("\n\n\n--ID-- --------name-------- ------------nuits------------- --------tele--------\n"); page=((length-1)/pages)+1; if(page==0) { for (i=0;i<length ;i++ ) { printf("%-7d%-21s%-31s%-20s\n",i+1,L[i].name,L[i].nuits,L[i].tele); } } else { for(p=0;p<page;p++) { clrscr(); printf("\n\n\n--ID-- --------name-------- ------------nuits------------- --------tele--------\n"); start=p*pages; end=(p+1)*pages; for (i=start;i<end;i++) { printf("%-7d%-21s%-31s%-20s\n",i+1,L[i].name,L[i].nuits,L[i].tele); } printf("\nInput any key to continue!\n"); getche(); } } } void ListModify() {/*数组L存在,在数组中查找名字为e的元素,并对该元素进行修改*/ int i; char yes; char name[20]; Info t={"maliang","hebtu","15831999946"}; printf("\n\n\ninput name for modify:"); scanf("%s",name); i=Find(name); if (i==length+1) { printf("can't find\n"); } else { printf("input name nuits tele:\n"); scanf("%s%s%s",&t.name,&t.nuits,&t.tele); getche(); printf("Are you want to Save this time modify?"); yes=getche(); if(yes=='Y'||yes=='y') { strcpy(L[i].name,t.name); strcpy(L[i].nuits,t.nuits); strcpy(L[i].tele,t.tele); printf("\nModify Successed"); } } } void ListSearch() {/* 数组L存在,在数组L中查找某元素中成员的名字是否与name相等?若相等,输出该人的所有信息;若不相等,输出没有找到*/ int i; char name[20]; clrscr(); printf("\n\n\ninput name for search:"); scanf("%s",name); for (i=0;i<=length ;i++ ) { if (strcmp(name,L[i].name)==0) { printf("--ID-- --------name-------- ------------nuits------------- --------tele--------\n"); printf("%-6d %-20s %-30s %-20s\n",i+1,L[i].name,L[i].nuits,L[i].tele); break; } } if (i==length+1) { printf("can't find\n"); } } void ListDelet() {/*数组L存在,删除名字为name的记录*/ int i,k; char name[20]; char yes; Info t={"maliang","hebtu","15831999946"}; printf("\n\n\ninput name for delete:"); scanf("%s",name); i=Find(name); if (i==length+1) { printf("can't find\n"); } else { getche(); printf("\nAre you want to Delete \"%s\" record?\n",name); yes=getche(); if(yes=='Y'||yes=='y') { for (k=i;k<length ;k++ ) { strcpy(L[k].name,L[k+1].name); strcpy(L[k].nuits,L[k+1].nuits); strcpy(L[k].tele,L[k+1].tele); } strcpy(L[length].name,''); strcpy(L[length].nuits,''); strcpy(L[length].tele,''); length--; printf("\nDelete Successed"); } } } void ListSaveN() {/*数组L存在,把数组的长度length值保存到D:\number.txt文件中*/ FILE *f; f=fopen("D:\\number.txt","w"); /*if (f==0) { printf("can't save file!\n"); sleep(1); exit(0); }*/ fprintf(f,"%d",length); fclose(f); } void ListLoadN() {/*数组L存在,把D:\number.txt文件中元素的个数导入length中*/ FILE *f; f=fopen("D:\\number.txt","r"); /*if (f==0) { printf("can't open file!\n"); sleep(1); exit(0); }*/ fscanf(f,"%d",&length); fclose(f); } void ListSave() {/*数组L存在,把数组中的所有元素保存到D:\record.txt文件中*/ int i; FILE *f; f=fopen("D:\\record.txt","w"); /*if (f==0) { printf("can't save file!"); sleep(1); exit(0); }*/ for (i=0;i<length ;i++ ) { fprintf(f,"%s %s %s\n",&L[i].name,&L[i].nuits,&L[i].tele); } printf("!!!\n"); fclose(f); } void ListLoad() {/*数组L存在,把D:\record.txt文件中元素导入到数组L中*/ int i; FILE *f; f=fopen("D:\\record.txt","r"); /*if (f==0) { printf("can't open file!"); sleep(1); exit(0); }*/ for (i=0;i<length ;i++ ) { fscanf(f,"%s %s %s",&L[i].name,&L[i].nuits,&L[i].tele); } fclose(f); } void ListSort() {/*数组L存在,对数组L中的元素排序*/ int i,j; int sort=1; Info t; printf("\nChoice 1(Asc) or 2(Desc) to Sort the record:"); scanf("%d",&sort); if(sort==2) { for(j=0;j<length;j++) for(i=0;i<length-j-1;i++) if(strcmp(L[i].name,L[i+1].name)<0) { strcpy(t.name,L[i].name);strcpy(t.nuits,L[i].nuits);strcpy(t.tele,L[i].tele); strcpy(L[i].name,L[i+1].name);strcpy(L[i].nuits,L[i+1].nuits);strcpy(L[i].tele,L[i+1].tele); strcpy(L[i+1].name,t.name);strcpy(L[i+1].nuits,t.nuits);strcpy(L[i+1].tele,t.tele); } } else { for(j=0;j<length;j++) for(i=0;i<length-j-1;i++) if(strcmp(L[i].name,L[i+1].name)>0) { strcpy(t.name,L[i].name);strcpy(t.nuits,L[i].nuits);strcpy(t.tele,L[i].tele); strcpy(L[i].name,L[i+1].name);strcpy(L[i].nuits,L[i+1].nuits);strcpy(L[i].tele,L[i+1].tele); strcpy(L[i+1].name,t.name);strcpy(L[i+1].nuits,t.nuits);strcpy(L[i+1].tele,t.tele); } } } void About() {/*关于程序*/ printf(" , , :EBBBBBBSi \n"); printf(" BBBBBMBBBBBBBM \n"); printf(" , jBBBMBBBBBMBM8GBBM \n"); printf(" ,BBOBBBBEGBBBBBBOMBP , \n"); printf(" BBMBBr. ::,,FBB8BB , \n"); printf(" BBBB ,,.,,,, ,FBNB7 \n"); printf(" BBB7.;7ri,.,7UJr:BBB0 , \n"); printf(" FBM ;:;:..,i::;Y:7BB. \n"); printf(" , BB ..Jq,,:.,Br: 2BY , \n"); printf(" rB,,, ,.:;. . kBr \n"); printf(" .B.,,.,.:.ri..,,XB, \n"); printf(" , .Y..,,.i,ii..:rL \n"); printf(" , , ,:...:.::v7::Y \n"); printf(" , :..::r7LLi.7. \n"); printf(" , NMS,:, ,,,,i:BO , \n"); printf(" , FBBE :i:::irL;,BBBq \n"); printf(" BM;B2 ..:vk5L:: kBNBu \n"); printf(" kML:BU ,::, .::, EBULBF \n"); printf(" 7BGYUJ7BF ,:i:.:SBBBLUFBB, \n"); printf(" jBZ08ZkBBBBPFFBBBBBBOXBOMB. \n"); gotoxy(30,20); printf("RightCopy By mlboy!"); sleep(2); } void Quit() {/*退出整个程序*/ gotoxy(1,7); printf(" :::::::: :::::::: :::::::: ::::::::: ::::::::: ::: ::: :::::::::\n"); printf(":+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: \n"); printf("+:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ \n"); printf(":#: +#+ +:+ +#+ +:+ +#+ +:+ +#++:++#+ +#++: +#++:++# \n"); printf("+#+ +#+# +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ \n"); printf("#+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# \n"); printf(" ######## ######## ######## ######### ######### ### #########\n"); sleep(2); exit(); } void PrintMenu() {/*函数PrintMenu()把菜单的界面显示在显示器上,该函数没有返回值,没有参数*/ int i; clrscr(); textattr(0+YELLOW+(BLUE<<4)); /*shang bian kuang*/ printf(" ");printf(" ## ## ######## ## ## ## ##\n"); printf(" ");printf(" ### ### ## ### ## ## ##\n"); printf(" ");printf(" #### #### ## #### ## ## ##\n"); printf(" ");printf(" ## ### ## ###### ## ## ## ## ##\n"); printf(" ");printf(" ## ## ## ## #### ## ##\n"); printf(" ");printf(" ## ## ## ## ### ## ##\n"); printf(" ");printf(" ## ## ######## ## ## ####### \n"); printf(" ");putch(0xc9); for(i=0;i<28;i++) putch(0xcd); cprintf("MENU"); for(i=0;i<27;i++) putch(0xcd); putch(0xbb);printf("\n"); /*zhong jian*/ printf(" ");putch(0xba);cprintf(" 1.Insert record ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 2.Display record ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 3.Modify record ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 4.Search record on name ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 5.Delete a record from list ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 6.Sort record ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 7.About ");putch(0xba);printf("\n"); printf(" ");putch(0xba);cprintf(" 8.Quit ");putch(0xba);printf("\n"); /*xia bian kuang*/ printf(" ");putch(0xc8); for(i=0;i<59;i++) putch(0xcd); putch(0xbc); printf("\n");printf(" "); /*text*/ textattr(128+WHITE+(BLACK<<4)); cprintf("Enter you choice(1-8):"); textattr(0+WHITE+(BLACK<<4)); } void Rmenu() {/*是否返回菜单*/ printf("\ninput any key to ruturn to main menu!"); getchar(); getchar(); printf("\n"); } main() { /*window ( 10, 5, 70, 20);*/ int c; ListLoadN(); ListLoad(); while(1) { PrintMenu(); scanf("%d",&c); printf("\n"); switch (c) { case 1:{clrscr();ListInsert();ListSave();ListSaveN();Rmenu();}break; case 2:{clrscr();ListDisplay();Rmenu();}break; case 3:{ListModify();ListSave();Rmenu();}break; case 4:{ListSearch();Rmenu();}break; case 5:{ListDelet();ListSave();ListSaveN();Rmenu();}break; case 6:{ListSort();ListSave();Rmenu();}break; case 7:{clrscr();About();}break; case 8:{clrscr();Quit();}break; default:{ printf(" # # ## # # ## # # #### \n"); printf(" ## ## # # # # # # ## # # # \n"); printf(" # ## # # # # # # # # # # # \n"); printf(" # # ###### # # ###### # # # # ### \n"); printf(" # # # # # # # # # ## # # \n"); printf(" # # # # ###### # # # # # #### \n"); Rmenu(); }break; } } }