⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txl.c.txt

📁 用C做的一个通讯录
💻 TXT
字号:
#include <stdio.h> 
#include <conio.h> 

#define Back 8 
#define Enter 13 
#define Esc 27 

#define home 71 
#define end 79 
#define upkey 72 
#define dnkey 80 
#define pgup 73 
#define pgdn 81 

struct person{ 
 char name[14]; 
 char tel[15]; 
 char eml[23]; 
 char dpmnt[22]; 
}; 
int savstate=1; 

int barup(int y)  
 { 
 if(y>=8) 
 { 
 int i;struct{unsigned char ch;unsigned char attr;}t; 
 for(i=4;i<=30;i++) 
 { gettext(i,y,i,y,&t);t.attr=0x07;puttext(i,y,i,y,&t);} 
 y=y-2; 
 for(i=4;i<=30;i++) 
 { gettext(i,y,i,y,&t);t.attr=0x70;puttext(i,y,i,y,&t);} 
 } 
 return y; 
 } 
  
int bardn(int y)  
 { 
 if(y<=18) 
 { 
 int i;struct{unsigned char ch;unsigned char attr;}t; 
 for(i=4;i<=30;i++) 
 { gettext(i,y,i,y,&t);t.attr=0x07;puttext(i,y,i,y,&t);} 
 y=y+2; 
 for(i=4;i<=30;i++) 
 { gettext(i,y,i,y,&t);t.attr=0x70;puttext(i,y,i,y,&t);} 
 } 
 return y; 
 } 

int rtnstr(char ary[],int l,int m)  
 { 
 int i,x,y; char ch;  
 for(i=0;i<=m;i++) 
 { x=wherex();y=wherey(); 
 if(i<m) { ary[i]=getch();  
 if(ary[i]==0) {getch(); i=i-1; continue;} 
 } 
 clreol(); 
 if(i==m) do{ ch=getch(); if(ch==0)getch(); if(ch==Esc)return 0; 
 if(ch==Enter) return 1; if(ch==Back)break; 
 gotoxy(x+m-i,y) ;clreol(); cprintf(" # Can't be more than %d letters.",m); 
 gotoxy(x,y); 
 }while(1); 
 if(ary[i]==Esc)return 0; 
 if(ary[i]==Back&&i==0) {i=i-1;continue;} 
 if(ary[i]==Enter) 
 { 
 if(i>=l){ary[i]='\0';return 1;} 
 else{ gotoxy(x+m-i,y); clreol(); cprintf(" # Can't be less than %d letters.",l); 
 gotoxy(x,y); i=i-1; continue; 
 } 
 } 
 if(ary[i]==Back&&i>0||ch==Back) {gotoxy(x-1,y); clreol(); i=i-2; ch=0; continue;} 
 if(m==3||l==7&&m==15){if(ary[i]<48||ary[i]>57){i=i-1;continue;}} 
 putchar(ary[i]); 
 } 
 } 

int readrecord(struct person psn[],int n)  
 { int i,rcd=1; FILE *fp; 
 if((fp=fopen("C:\\zhyx.c","rb"))==NULL) 
 { rcd=0;if((fp=fopen("C:\\zhyx.c","wb"))==NULL) 
 { printf(" Cannot open file!"); 
return n; 
 } 
 } 
 if(rcd==0) for(i=0;i<100;i++)fwrite(&psn[i],sizeof(struct person),1,fp); 
 else for(i=0;i<100;i++) 
 {  
 fread(&psn[i],sizeof(struct person),1,fp);if(psn[i].name[0]=='\0')break;n++; 
 } 
 fclose(fp); printf(" There are %d records now.\n Records in C:\\zhyx.c",n); return n; 
 } 

int add(struct person psn[],int n)  
 { int i,ctn=1;int t=n; 
 while(n<100) 
 { clrscr(); 
 printf("\n\nAdded %d.\nPlease input the information,Esc back to menu:\n\n\n\n",n-t); 
 printf("name:"); 
 ctn=rtnstr(psn[n].name,2,14);if(ctn==0)break; 
 printf("\n\nphonenumber:"); 
 ctn=rtnstr(psn[n].tel,7,15);if(ctn==0)break; 
 printf("\n\nemail:"); 
 ctn=rtnstr(psn[n].eml,7,23);if(ctn==0)break; 
 printf("\n\ndepartment:"); 
 ctn=rtnstr(psn[n].dpmnt,2,22);if(ctn==0)break; 
 printf("\n\nRecord added successfully!"); 
 n++; 
 }if(n==100){ 
 printf("\n\nThe record number has reached 100,can't add any more!");getch(); 
 }  
 clrscr();if(ctn==0)psn[n]=psn[n+1]; 
 if(t!=n){savstate=0;if(n-t!=1) printf(" Added %d records.\n",n-t);else printf(" Added 1 record.\n");} 
 return n; 
 } 

int delete(struct person psn[],int n)  
{ 
int i,indx,num=0,cnt=1; char str[14]; 
 printf("\n\n\n\nDelete name:"); 
cnt=rtnstr(str,2,14); if(cnt==0) {clrscr();return n;} 
printf("\n"); 
for(i=0;i<n;i++)if(strcmp(psn[i].name,str)==0) 
 { num++; printf("\n%-2d %-14s %-15s %-23s %-22s",i,psn[i].name,psn[i].tel,psn[i].eml,psn[i].dpmnt); 
 if(num==1)cnt=i; 
 } 
 if(num==1)for(i=cnt;i<n;i++)psn[i]=psn[i+1]; 
if(num>1){ printf("\n\nFind %d recods.Input the number before a record to delete it:\n",num); 
 printf("If input error,you might delete other record,input %d or a larger number to cancel:",n); 
 cnt=rtnstr(str,1,3); if(cnt==0){clrscr();return n;} cnt=strlen(str); 
 if(cnt==1)indx=str[0]-48;if(cnt==2)indx=(str[0]-48)*10+str[1]-48;if(cnt==3) {clrscr();return n;} 
 if(indx<n) for(i=indx;i<n;i++)psn[i]=psn[i+1]; 
 else {clrscr(); printf("Delete error!"); return n;} 
 } 
 clrscr(); 
 if(num>0){ printf(" Deleted a record! name: %s",str);n--;} 
 else printf(" The name %s do not exist!",str); 
 return n; 
 } 

void order(struct person psn[],int n)  
 { char ch; 
 printf("\n\npress F to order by name,S by phonenumber.\n"); 
 do 
 { ch=getch(); 
 if(ch==Esc)break; 
 }while(ch!='f'&&ch!='F'&&ch!='s'&&ch!='S'); 
 if(ch=='f'||ch=='F') 
 { 
 int i,j,k; struct person temp; 
 for(i=0;i<n-1;i++) 
 { 
 k=i;for(j=i+1;j<n;j++)if(strcmp(psn[j].name,psn[k].name)<0)k=j; 
 if(k!=j){temp=psn[i];psn[i]=psn[k];psn[k]=temp;} 
 }clrscr(); 
 printf("\n Ordered by name."); 
 savstate=0;return; 
 } 
 if(ch=='s'||ch=='S') 
 { 
 int i,j,k; struct person temp; 
 for(i=0;i<n-1;i++) 
 { 
 k=i;for(j=i+1;j<n;j++)if(strcmp(psn[j].tel,psn[k].tel)<0)k=j; 
 if(k!=j){temp=psn[i];psn[i]=psn[k];psn[k]=temp;} 
 }clrscr(); 
 printf("\n Ordered by phonenumber."); 
 savstate=0;return; 
 } 
 clrscr(); 
 } 

void modify(struct person psn[],int n)  
 { 
 int i,indx,num=0,cnt=1; char str[14]; struct person temp; 
 printf("\nPlease input the friend's name:"); 
 cnt=rtnstr(str,2,14);if(cnt==0) {clrscr();return;} 
 printf("\n"); 
 for(i=0;i<n;i++)if(strcmp(psn[i].name,str)==0) 
 { num++;  
 printf("\n%-2d %-14s %-15s %-23s %-22s",i,psn[i].name,psn[i].tel,psn[i].eml,psn[i].dpmnt); 
 if(num==1)indx=i; 
 } 
 if(num==1) 
 { 
 temp=psn[indx]; 
 printf("\n\nPlease input new information,Esc back to menu:\n\n"); 
 printf("name:"); 
 cnt=rtnstr(psn[indx].name,2,14);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\nphonenumber:"); 
 cnt=rtnstr(psn[indx].tel,7,15);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\nemail:"); 
 cnt=rtnstr(psn[indx].eml,7,23);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\ndepartment:"); 
 cnt=rtnstr(psn[indx].dpmnt,2,22);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 } 
 if(num>1){ printf("\n\nFind %d recods.Input the number before a record to modify it:\n",num); 
 printf("If input error,you might modify other record,input %d or a larger number to cancel:",n); 
 cnt=rtnstr(str,1,3); if(cnt==0){clrscr();return;} cnt=strlen(str); 
 if(cnt==1)indx=str[0]-48;if(cnt==2)indx=(str[0]-48)*10+str[1]-48;if(cnt==3) {clrscr();return;} 
 if(indx<n) 
 { 
 temp=psn[indx]; 
 printf("\n\nPlease input new information,Esc back to menu:\n\n"); 
printf("name:"); 
 cnt=rtnstr(psn[indx].name,2,14);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\nphonenumber:"); 
 cnt=rtnstr(psn[indx].tel,7,15);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\nemail:"); 
 cnt=rtnstr(psn[indx].eml,7,23);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 printf("\n\ndepartment:"); 
 cnt=rtnstr(psn[indx].dpmnt,2,22);if(cnt==0){psn[indx]=temp;clrscr();return;} 
 } 
 else {clrscr(); printf("Modify error!"); return;} 
 } 
 clrscr(); 
 if(num>0){printf(" Modified a record!");n--;} 
 else printf(" The name %s do not exist!",str); 
 } 

void print(struct person psn[],int n)  
 { 
 int i,k=0,l=0,j=n/10+(n%10?1:0); char ch; 
 do 
 { 
 printf("\n name:: phonenumber:: email:: department::\n\n"); 
 for(i=k*10+l;i<(k+1)*10+l;i++) 
 { if(i==n)break; 
 printf(" %-14s %-15s %-23s %-22s\n\n",psn[i].name,psn[i].tel,psn[i].eml,psn[i].dpmnt); 
 } 
 if(l%10==0) {gotoxy(1,24);cprintf(" Press Esc back to menu. page %d / %d ",k+l/10+1,j);} 
 else {gotoxy(1,24);cprintf(" Press Esc back to menu. (%d pages)",j);}  
 do 
 { ch=getch(); 
 if(ch==0) 
 { ch=getch(); 
 if(ch==pgdn&&((k+1)*10+l<n))k++; 
 if(ch==pgup&&(k*10+l>=10))k--;else if(ch==pgup)ch=home; 
 if(ch==home){k=0;l=0;} 
 if(ch==end){k=j-1;l=0;} 
 if(ch==upkey&&(k*10+l>0))l--; 
 if(ch==dnkey&&(k*10+l<n-1))l++; 
 break; 
 } 
 }while(ch!=Esc); 
 if(ch==Esc) break;clrscr(); 
 }while(1);clrscr(); 
 } 

void find(struct person psn[],int n)  
 { 
 int i,num=0,cnt=1; char str[15]; 
 printf("\n\n\n Please input the name or phonenumber you want to find:\n\n"); 
 printf(" name or phone:"); 
 cnt=rtnstr(str,2,15); if(cnt==0)return; 
 for(i=0;str[i]!='\0';i++) 
 if(str[i]<48||str[i]>57) 
 { 
 for(i=0;i<n;i++) 
 if(strcmp(psn[i].name,str)==0) 
 { num++;if(num==1) 
 printf("\n\n name:: phonenumber:: email:: department::\n\n"); 
 printf(" %-14s %-15s %-23s %-22s\n\n",psn[i].name,psn[i].tel,psn[i].eml,psn[i].dpmnt); 
 } 
 if(num==0) {clrscr();printf("Can't find any record in accordance with %s!",str);return;} 
 gotoxy(2,24); cprintf("Press any key to cotinue."); 
 getch(); clrscr(); return; 
 } 
 for(i=0;i<n;i++) 
 if(strcmp(psn[i].tel,str)==0) 
 { num++;if(num==1) 
 printf("\n\n name:: phonenumber:: email:: department::\n\n"); 
 printf(" %-14s %-15s %-23s %-22s\n\n",psn[i].name,psn[i].tel,psn[i].eml,psn[i].dpmnt); 
 } 
 if(num==0) {clrscr();printf("Can't find any record in accordance with %s!",str);return;} 
 gotoxy(2,24); cprintf("Press any key to cotinue."); 
 getch(); clrscr(); return; 
 } 

void save(struct person psn[],int n)  
 { FILE *fp; int i; 
 if(savstate==1)return; 
 if((fp=fopen("C:\\zhyx.c","wb"))==NULL) 
 { 
 printf(" Cannot open file!");return; 
 } 
 for(i=0;i<=n;i++) if(fwrite(&psn[i],sizeof(struct person),1,fp)!=1)  
 {printf(" File error!");fclose(fp);return;} 
 savstate=1;printf(" Records saved!"); 
 fclose(fp); 
 } 
main()  
{ 
 int i,n=0,ex=0,y=8; char ch; 
 struct person psn[100]; 
 for(i=0;i<100;i++) 
 { strcpy(psn[i].name,"\0"); 
 strcpy(psn[i].tel,"\0"); 
 strcpy(psn[i].eml,"\0"); 
 strcpy(psn[i].dpmnt,"\0"); 
 } 
 n=readrecord(psn,n); 
do 
 { 
 gotoxy(2,4); cprintf("#################################"); 
 gotoxy(2,5); cprintf("# #"); 
 gotoxy(2,6); cprintf("# A: add new records. #"); 
 gotoxy(2,7); cprintf("# #"); 
 gotoxy(2,8); cprintf("# D: delete a record. #"); 
 gotoxy(2,9); cprintf("# #"); 
 gotoxy(2,10);cprintf("# O: order them. #"); 
 gotoxy(2,11);cprintf("# #"); 
 gotoxy(2,12);cprintf("# M: modify a record. #"); 
 gotoxy(2,13);cprintf("# #"); 
 gotoxy(2,14);cprintf("# P: print all. #"); 
 gotoxy(2,15);cprintf("# #"); 
 gotoxy(2,16);cprintf("# F: find certaen records. #"); 
 gotoxy(2,17);cprintf("# #"); 
 gotoxy(2,18);cprintf("# S: save. #"); 
 gotoxy(2,19);cprintf("# #"); 
 gotoxy(2,20);cprintf("# Esc: exit. #"); 
 gotoxy(2,21);cprintf("# #"); 
 gotoxy(2,22);cprintf("#################################"); 
 y=8;y=barup(y); 
 gotoxy(2,24);cprintf("Please choose a menu form the table above."); 
 do 
 { ch=getch(); 
 if(ch==0)  
 { 
 ch=getch(); 
 switch(ch) 
 { 
 case upkey: 
 y=barup(y);ch=0;break; 
 case dnkey: 
 y=bardn(y);ch=0; 
 } 
 } 
 if(ch==Enter) 
 { 
 switch(y)  
 { 
 case 6: ch='a';break; 
 case 8: ch='d';break; 
 case 10: ch='o';break; 
 case 12: ch='m';break; 
 case 14: ch='p';break; 
 case 16: ch='f';break; 
 case 18: ch='s';break; 
 case 20: ch=Esc;break; 
 } 
 } 
 switch(ch)  
 { case 'a': 
 case 'A': 
 { clrscr(); n=add(psn,n); 
 ch=Enter; break; 
 } 
 case 'd': 
 case 'D': 
 { clrscr(); n=delete(psn,n); 
 ch=Enter; break; 
 } 
 case 'o': 
 case 'O': 
 { clrscr(); order(psn,n); 
 ch=Enter; break; 
 } 
 case 'm': 
 case 'M': 
 { clrscr(); modify(psn,n); 
 ch=Enter; break; 
 } 
 case 'p': 
 case 'P': 
 { clrscr(); print(psn,n); 
 ch=Enter; break; 
 } 
 case 'f': 
 case 'F': 
 { clrscr(); find(psn,n); 
 ch=Enter; break; 
 } 
 case 's': 
 case 'S': 
 { clrscr(); save(psn,n); 
 ch=Enter; break; 
 } 
 case Esc:  
 { clrscr(); 
 if(savstate==0) 
{ gotoxy(5,5); cprintf("Records not saved! save(y/n)? Cancel button: c"); 
 do{ ch=getch(); 
 if(ch=='n'||ch=='N')savstate=1; 
 if(ch=='y'||ch=='Y')save(psn,n); 
 if(ch=='c'||ch=='C')break; 
 }while(savstate!=1); 
} 
 if(ch!='c'&&ch!='C')ex=1; 
 clrscr(); ch=Enter; break; 
 } 
 } 
 }while(ch!=Enter); 
 if(ex==1)break; 
 }while(1); 
 clrscr(); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -