原创 通讯录C语言代码

2008-11-14 21:24 3287 4 6 分类: 工程师职场
#include<stdio.h>  
struct student           /*声明一个结构体*/
{
       char name[8];
       int  num;
       char sex;
       stuct date birthday;
       char tel[15];
       char addr[20];
       char Email[10];
};                    /*定义了一个结构体来储存学生的通讯录:含姓名、性别、学号、出生年月、联系电话、Email*/  
struct date            /*声明一个结构体*/
{
       int month;
       int day;
       int year;
};                   /*定义结构体来放学生的出生年月*/
char filename[20];    /*学生的姓名*/
FILE *fp;            
void creat();
void output();
void find_by_name();
void find_by_tel();
void find_by_addr();
void find_by_Email();
void delete();
void append();
void modify();       /*定义了七个函数和一个指针变量完成相应的功能*/
main()
{
int m;
printf("            ***************************************************\n\n\n");
printf("            ********* Welcome to use wushenghua's     ***************\n\n\n");
printf("            **************     addressbook     **********************\n\n\n");
creat();  
while(1)
{     printf("           ******************************\n\n");
       printf("                 0 exit,     press 0\n\n");
       printf("                 1 append,press 1\n\n");
       printf("                 2 modify,press 2\n\n");
       printf("                 3 output,press 3\n\n");
       printf("                 4 delete,press 4\n\n");
       printf("                 5 find by name,press 5\n\n");
       printf("                 6 find by tel,press 6\n\n");
       printf("                 7 find by addr,press 7\n\n");
       printf("                 8 find by Email,press 8\n\n");
       printf("           ******************************\n");
       scanf("%d",&m);             /*输入0-9的可完成学生通讯录的相关查找以及具有增加,查询,修改,删除等功能*/
     if(m>=0&&m<=8)
     {
      switch(m)
         {  
           case 1: append();
                break;
           case 2: modify();
                break;
           case 3: output();
                break;
           case 4: delete();
                break;
           case 5: find_by_name();
                break;
           case 6: find_by_tel();
                break;
           case 7: find_by_addr();
                break;
           case 8: find_by_Email();
                break;
           case 0: exit();
         }                /*多分支结构用于查找以及具有增加,查询,修改,删除,程序等基本功能*/
      printf("\n\nHave finished,select again!\n");
}
     else
      printf("\n\nError,select again!\n");
}
}          /* 主函数主要是完成学生的个人信息的查找以及具有增加,查询,修改,删除,程序等基本功能*/

void creat()
{
       struct student one;        /*对结构体进行声明*/
       printf("\nInput the name of addressbook:");
       gets(filename);                /*输入学生的姓名的文件*/
       if((fp=fopen(filename,"w"))==NULL)
       {
         printf("\nCann't creat addressbook!");
          exit();
       }
       fprintf(fp,"%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n","name","num","sex","birthday.month","birthday.day","birthday.year","tel","addr","Email");
       printf("\nInput name,num,sex,birthday,telephone,address,Email(end press 0)\n");
       scanf("%s",&one.name);
       while(strcmp(one.name,"0"))
       {
        scanf("%s%d%c%d%d%d%s%s",&one.tel,&one.num,&one.sex,&birthday.month,&birthday.day,&birthday.year,&one.addr,&one.Email);/*输入学生的通        讯录信息*/
        fprintf(fp,"%-10s%d%s%d%-20s%-20s%-9s\n","name","num","sex","birthday","tel","addr","Email");
        scanf("%s",&one.name);
       }
       fclose(fp);
}                          /*函数void creat()创建学生的个人信息*/
void find_by_name()
{  
       int k="0";
       char namekey[8];
       struct student one;
       printf("\nInput name:");
       scanf("%s",&namekey);    /*输入学生的姓名*/
       if((fp=fopen(filename,"rb"))==NULL)
       {  
         printf("\nCann't open this addressbook!!!");
         exit();
       }
       while(!feof(fp))
       {  
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(namekey,one.name))
         {
           printf("\n\nHave finded,as follow:\n");
           printf("  name   num   sex    tel     birthday       addr         Email   ");
           printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
           k="1";
         }
       }
      if(!k)  
      printf("\n\nSorry,there is no this student!!");
       fclose(fp);
}                   /*通过学生的姓名查找学生的通讯录*/
void find_by_tel()
{  
       int k="0";
       char telkey[8];
       struct student one;
       printf("\nInput tel:");
       scanf("%s",&telkey);    /*输入学生的联系电话*/
       if((fp=fopen(filename,"rb"))==NULL)
       {  
         printf("\nCann't open this addressbook!!!");
         exit();
       }
       while(!feof(fp))
       {  
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(telkey,one.tel))
         {
          printf("\n\nHave finded,as follow:\n");
          printf("  name   num   sex    tel     birthday       addr         Email  ");
          printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);/*输入学生的通        讯录信息*/
          k=1;
         }
       }
       if(!k)  
       printf("\n\nSorry,there is no this student!!");
       fclose(fp);
}                   /*通过学生的联系电话查找学生的通讯录*/        
void find_by_addr()
{  
       int k="0";
       char addrkey[8];
       struct student one;
       printf("\nInput addr:");
       scanf("%s",&addrkey);    /*输入学生的地址*/
       if((fp=fopen(filename,"rb"))==NULL)
       {  
        printf("\nCann't open this addressbook!!!");
        exit();
       }
       while(!feof(fp))
       {  
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(addrkey,one.addr))
         {
           printf("\n\nHave finded,as follow:\n");
           printf("  name   num   sex    tel     birthday       addr         Email  ");
           printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);/*输入学生的通        讯录信息*/
           k="1";
         }
        }
       if(!k)  
       printf("\n\nSorry,there is no this student!!");
       fclose(fp);
}             /*通过学生的地址查找学生的通讯录*/
void find_by_Email()
{  
       int k="0";
       char Emaikey[8];
       struct student one;
       printf("\nInput Email:");
       scanf("%s",& Emaikey);      /*输入学生的Email*/
       if((fp=fopen(filename,"rb"))==NULL)
       {  
     printf("\nCann't open this addressbook!!!");
     exit();
       }
       while(!feof(fp))
       {  
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(Emailkey,one.Email))
         {
           printf("\n\nHave finded,as follow:\n");
           printf("  name   num   sex    tel     birthday       addr         Email  ");
           printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
           k="1";
         }
       }
       if(!k)  
       printf("\n\nSorry,there is no this student!!");
       fclose(fp);
}              /*通过学生的Email查找学生的通讯录*/      

void output()         /*函数void output()用于输出学生的通讯录*/
{
       struct student one;
       if((fp=fopen(filename,"r"))==NULL)
       {
         printf("\nCann't open this addressbook!!!");
         exit();
       }
       printf("\n\n%20s\n"," \nCann't open this addressbook\n");
       while(!feof(fp))
       {
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);/*输入学生的通        讯录信息*/
       }
      fclose(fp);
}
void append()
{
       struct student one;
       if((fp=fopen(filename,"a"))==NULL)
       {
         printf("\nCann't open this addressbook!!");
         exit();
       }
       printf("\nInput new name,num,sex,birthday,telephone,address,Email\n");
       scanf("%s%d%c%d%d%d%s%s",&one.tel,&one.num,&one.sex,&birthday.month,&birthday.day,&birthday.year,&one.addr,&one.Email);/*输入学生的通讯录信息*/);
       fprintf(fp,"%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n","name","num","sex","birthday.month","birthday.day","birthday.year","tel","addr","Email");
       fclose(fp);
}

void modify()      /*函数vvoid modify()用于输出学生的通讯录*/
{  
       int m,k=0;
       long offset;
       char namekey[8];
       struct student one;
       printf("\nInput name:");
       scanf("%s",&namekey);
       if((fp=fopen(filename,"r+"))==NULL)
       {  
         printf("\nCann't open this addressbook!!!");
         exit();
       }
       while(!feof(fp))
       {  
         offset="ftell"(fp);
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(namekey,one.name))
         {
           k="1";
           break;  
         }
      }
       if(k)
       {  
           printf("\nHave finded,as follow:");
           printf("\n%-10s%-20s%-20s%-9s\n",one.name,one.tel,one.addr,one.Email);
           printf("\nInput new name,num,sex,telephone,address,Email:");   /*修改学生的通讯录信息*/
           scanf("%s%d%c%d%d%d%s%s",&one.tel,&one.num,&one.sex,&birthday.month,&birthday.day,&birthday.year,&one.addr,&one.Email);
           fseek(fp,offset,SEEK_SET);
           printf("%ld",ftell(fp));
           fprintf(fp,"%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n","name","num","sex","birthday.month","birthday.day","birthday.year","tel","addr","Email");
      }
       else
         printf("\nSorry,there is no this student!!");
         fclose(fp);
}
void delete()              /*函数void delete() 用于输出学生的通讯录*/
{  
       int m,k=0,flag;
       long offset1;
       char namekey[8];
       struct student one;
       printf("\nInput name:");
       scanf("%s",&namekey);
       if((fp=fopen(filename,"r+"))==NULL)
       {  
         printf("\nCann't open this addressbook!!");
         exit();
       }
       while(!feof(fp))
       {  
         offset1=ftell(fp);
         fscanf(fp,"%s%d%c%d%d%d%s%s%s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
         if(!strcmp(namekey,one.name))
         {  
           k="1";
           break;
         }
       }
       if(k)
       {
           printf("\nHave finded,as follow:");
           printf("\n%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n",one.name,one.num,one.sex,birthday.month,birthday.day,birthday.year,one.tel,one.addr,one.Email);
           printf("\n      delete,press1:\n not delete,press0:");
           scanf("%d",&m);                          /*删除学生的通讯录信息*/
           if(m)
           {  
             fseek(fp,offset1,SEEK_SET);
             fprintf(fp,"%-10s%-5d%-5c%d%d%-5d%-20s%-20s%-9s\n","name","num","sex","birthday.month","birthday.day","birthday.year","tel","addr","Email");
           }
       }
       else
       printf("\nSorry,there is no this student!!");
       fclose(fp);
}
PARTNER CONTENT

文章评论2条评论)

登录后参与讨论

用户367860 2011-7-4 10:14

这个程序怎么运行时会出错呢?

用户165980 2008-12-6 16:51

每一步后都有一个脚印!!!!!!
相关推荐阅读
用户165980 2009-05-02 11:20
因为我是机务
  我从不穿洁白的衬衫 也无缘于笔挺的西装、黑亮的皮鞋 更不要怪我连最简单的领带打法都学不会 我只是夏天穿着短裤背心拖鞋 冬天穿着宽松舒适的运动服旅游鞋 来到单位换上一年四季都是同样的沾满油迹、略带燃...
用户165980 2009-03-10 19:28
海南航空
           海南航空公司企业文化<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&...
用户165980 2008-11-02 17:24
单片机的中断响应
   在CPU响应中断的条件得到满足后,CPU对中断源中断请求的回答。此时CPU的动作:保护断点、寻找中断源、转向中断服务程序的入口地址<?xml:namespace prefix = o ns...
用户165980 2008-09-05 10:38
mc-51单片机RAM
内部RAM(可扩展到64K)大小为128B,可以分为三部分:       第一部分:地址从00H~1FH,共4个字节,每个字节的位各代表一个寄存器从D0到D7位分别是寄存器R0~R7,也就有4组R0~...
我要评论
2
4
关闭 站长推荐上一条 /3 下一条