首页

2010年5月29日星期六

C/C++编写通讯程序

#include
#include

#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[13];
char eml[22];
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)                       /*对按键逐个测试,排除部分非法输入,按后退键修改,Enter键确认*/
{
int i,x,y; char ch;
for(i=0;i<=m;i++)
   { x=wherex();y=wherey();
     if(i
               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){ary[m]='\0'; 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==12)){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)             /*增加记录。任意位置按Esc键退出,返回记录总数,并于主菜单显示增加的个数*/
{ int i,cnt=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:");
       cnt=rtnstr(psn[n].name,2,13);if(cnt==0)break;
       printf("\n\nphonenumber:");
       cnt=rtnstr(psn[n].tel,7,12);if(cnt==0)break;
       printf("\n\nemail:");
       cnt=rtnstr(psn[n].eml,7,21);if(cnt==0)break;
       printf("\n\ndepartment:");
       cnt=rtnstr(psn[n].dpmnt,2,21);if(cnt==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(cnt==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 ch; char str[14];
   printf("\n\n\n\nDelete name:");
cnt=rtnstr(str,2,13);str[13]='\0'; if(cnt==0) {clrscr();return n;}
        printf("\n");
for(i=0;i
     { 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){ printf("\n\nFind 1 recod. Delete it(Y/N)?:");
                    do{ ch=getch();
                        if(ch=='Y'||ch=='y'){ clrscr();printf(" Deleted a record! name: %s",str);
                                              for(i=indx;i
                                            }
                        else if(ch=='N'||ch=='n'){clrscr();return n;}
                       }while(1);
                  }
if(num>1){ printf("\n\nFind %d recods.Input the number before a record to delete it:\n\n",num);
           cnt=rtnstr(str,1,3);if(cnt==0){clrscr();return n;}
                   cnt=strlen(str);if(cnt==3) {clrscr();printf(" Index error!");return n;}         /*借用cnt(continue)*/
           if(cnt==1)cnt=str[0]-48;                                                        /*续借,indx还要用到*/
                   else if(cnt==2)cnt=(str[0]-48)*10+str[1]-48;
              if(cnt
                   { clrscr();printf(" Deleted a record! name: %s",psn[indx].name);
                     for(i=cnt;i
                   }
            else {clrscr(); printf(" Index error!"); return n;}
           }
    clrscr();
    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
         {
          k=i;for(j=i+1;j<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
         {
          k=i;for(j=i+1;j<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,13);str[13]='\0'; if(cnt==0) {clrscr();return;}
   printf("\n");
   for(i=0;i
     { 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,13);if(cnt==0){psn[indx]=temp;clrscr();return;}
           printf("\n\nphonenumber:");
           cnt=rtnstr(psn[indx].tel,7,12);if(cnt==0){psn[indx]=temp;clrscr();return;}
           printf("\n\nemail:");
           cnt=rtnstr(psn[indx].eml,7,21);if(cnt==0){psn[indx]=temp;clrscr();return;}
           printf("\n\ndepartment:");
           cnt=rtnstr(psn[indx].dpmnt,2,21);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\n",num);
              cnt=rtnstr(str,1,3);if(cnt==0){clrscr();return;}
                   cnt=strlen(str);if(cnt==3) {clrscr();printf(" Index error!");return;}                     /*借用cnt*/
           temp=psn[indx]; if(cnt==1)indx=str[0]-48;
                                   if(cnt==2)indx=(str[0]-48)*10+str[1]-48;
              if(indx
                 {
        temp=psn[indx];
                  printf("\n\nPlease input new information,Esc back to menu:\n\n");
                  printf("name:");
                  cnt=rtnstr(psn[indx].name,2,13);if(cnt==0){psn[indx]=temp;clrscr();return;}
                  printf("\n\nphonenumber:");
                  cnt=rtnstr(psn[indx].tel,7,12);if(cnt==0){psn[indx]=temp;clrscr();return;}
                  printf("\n\nemail:");
                  cnt=rtnstr(psn[indx].eml,7,21);if(cnt==0){psn[indx]=temp;clrscr();return;}
                  printf("\n\ndepartment:");


                   cnt=rtnstr(psn[indx].dpmnt,2,21);if(cnt==0){psn[indx]=temp;clrscr();return;}
                 }
               else {clrscr(); printf(" Index error!"); return;}
             }
    clrscr();
    if(num>0){printf(" Modified a record! name:%s->%s",temp.name,psn[indx].name);savstate=0;n--;}
    else printf(" The name %s do not exist!",str);
}

void print(struct person psn[],int n)                              /*显示所有记录。由pgup、pgdn、home、end及上下键控制*/
{
   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
            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
            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,14);str[14]='\0'; if(cnt==0)return;
     for(i=0;str[i]!='\0';i++)
       if(str[i]<48||str[i]>57)
    {
      for(i=0;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
      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 certain records.    #");
    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)                                                   /*若按部分非字符键,可能是移动光条,y值跟踪光条改变*/
{
ch=getch();
switch(ch)
            {
     case upkey:
     y=barup(y);ch=0;break;
     case dnkey:
     y=bardn(y);ch=0;
            }
          }
       if(ch==Enter)
           {
            switch(y)                                                                       /*光条对应y值,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:                                             /*退出时的处理。按Y保存后退出,N直接退出,Esc返回程序*/
    { clrscr();ex=1;
      if(savstate==0)
{ gotoxy(5,5); cprintf("Records not saved! save(y/n)? Cancel: Esc");
do{ ch=getch();
      if(ch=='n'||ch=='N')savstate=1;
      if(ch=='y'||ch=='Y')save(psn,n);
                      if(ch==Esc){ex=0;break;}
    }while(savstate!=1);
}
      clrscr(); ch=Enter; break;
    }
}
     }while(ch!=Enter);
    if(ex==1)break;
   }while(1);
clrscr();
}                

没有评论:

发表评论