zoukankan      html  css  js  c++  java
  • 利用链表实现通讯录的建立

    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct lnode                              /*定义单链表*/                      
    {
      char num[10];
      char name[10];
      char sex[10];
      char age[8];
      struct lnode *next;
    } student;
    
    
    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请您选择(0-8):"); 
            c=getchar();
            }
            while(c<'0'&&c>'8');
            return(c-'0'); 
        } 
    
    
    void initlist(student *&l)                               /*初始化链表*/
    {
    l=(student *)malloc(sizeof(student));
    l->next=NULL;
    }
    
    
    
    void createlistf(student *&l)                             /*创建通讯录*/
    { 
     student *r,*s;char sign;
     l=(student *)malloc(sizeof(student));
     r=l;
    while(sign!='n'&&sign!='N')
    {
    s=(student *)malloc(sizeof(student));
    printf("学号:");
    scanf("%s",&(s->num));
    printf("姓名:");
    scanf("%s",&(s->name));
    printf("性别:");
    scanf("%s",&(s->sex));
    printf("年龄:");
    scanf("%s",&(s->age));
    r->next=s;
    r=s;
    r->next=NULL;
    printf("是否继续添加记录(Y/N)\n");
    scanf("%s",&sign);
    
    }
    //r->next=NULL;
    }
    
    
    
    
    void displist(student *l)                                   /*显示通讯录*/
    {student *p=l->next;int i;i=1;
    while (p!=NULL)
    {
        printf("学号:%10s\n",p->num);
        printf("姓名:%10s\n",p->name);
        printf("性别:%10s\n",p->sex);
        printf("年龄:%10s\n",p->age);
        printf("------第%d条记录-----\n",i);
        p=p->next;
        i++;
    }
    printf("\n");
    }
    
    
    
    void listdelete(student *&l,char *elem)                      /*删除某个信息从通讯录中*/
    {
    
    student *p=l;
    student *q=p->next;
    while(strcmp(elem,q->name)!=0)
    {
    p=q;
    q=q->next;
    if(q==NULL)
    printf("没有此人信息,无法删除");
    break;
    }
    if(q!=NULL)
    p->next=q->next;
    //else
    //printf("没有此人信息,无法删除!");
    }
    
    
    void listinsert(student *&l,char *elem)                     /*插入信息到通讯录中*/ 
    {
    student *p=l->next;student *s=l;student *q;q=p;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
    
    }
    if(p==NULL)
    {
    s=(student *)malloc(sizeof(student));
    printf("学号:");
    scanf("%s",&(s->num));
    printf("姓名:");
    scanf("%s",&(s->name));
    printf("性别:");
    scanf("%s",&(s->sex));
    printf("年龄:");
    scanf("%s",&(s->age));
    //s->next=q->next;
    q->next=s;
    s->next=NULL;
    }
    else
    printf("插入失常!");
    }
    
    
    
    
    
    
    void listlocate(student *&l,char *elem)                        /*按学生姓名查找某个记录*/
    {
    student *p=l->next; //student *s=l;
    
    while(p!=NULL && strcmp(p->name,elem)!=0)
    {
    p=p->next;
    }
    if(p==NULL)
    printf("没有此人记录");
    else
    printf("学号=%s,姓名=%s,性别=%s,年龄=%s",p->num,p->name,p->age,p->sex);
    printf("\n");
    }
    
    
    void listchange(student *&l,char *elem)                        /*按学生学号修改记录*/
    {
    student *p=l->next; student *s;
    while(p!=NULL && strcmp(p->num,elem)!=0)
    p=p->next;
    if(p==NULL)
    printf("没有此学号学生的记录");
    else
    {
    s=(student *)malloc(sizeof(student));
    printf("学号:");
    scanf("%s",&(s->num));
    printf("姓名:");
    scanf("%s",&(s->name));
    printf("性别:");
    scanf("%s",&(s->sex));
    printf("年龄:");
    scanf("%s",&(s->age));
    strcpy(p->num,s->num);strcpy(p->name,s->name);strcpy(p->sex,s->sex);strcpy(p->age,s->age);
    printf("新纪录修改为:\n");
    printf("学号=%s,姓名=%s,性别=%s,年龄=%s",p->num,p->name,p->age,p->sex);
    }
    }
    
    
    
    void writetotext(student *&l)                                       /*写入文本*/
    {
    student *p=l->next; 
    FILE *fp;
    char filename[20];
    printf("\t保存到文件\n");
    printf("\t请输入所保存的文件名:");
    scanf("\t%s",filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
       printf("\t无法打开文件\n");
       system("pause");
       return;
    }
    fprintf(fp,"***********通讯录************\n");
    fprintf(fp,"学号   姓名    性别     年龄 \n");
    while(p!=NULL)
    {
    fprintf(fp,"%s\t%s\t%s\t%s\t\n",p->num,p->name,p->sex,p->age);
    p=p->next;
    }
    fclose(fp);
    printf("保存成功!\n");
    }
    
    
    
    
    
    
    int readfromtext(student *&l)                                      /*从文本中读取*/
    {
    FILE *fp;
    char ch;
    char filename[20];
    printf("\t打开的文件\n");
    scanf("\t%s",filename);
    fp=fopen(filename,"rb");
    if(fp==NULL)
    {
    printf("open file error\n");
    return 0;
    }
    while(!feof(fp))
    {
    ch=fgetc(fp);
    printf("%c",ch);
    }
    
    fclose(fp);
    }
    
    
    
    
    int main()                                    /*主函数*/
    {  
        
    char e[80]="yangbing";
    student *s;
    initlist(s);
    for(;;)
    {
    switch(menu_select())
    {
     
    case 1:
    createlistf(s);
    system("pause");
    break;
    
    case 2:
    displist(s);
    system("pause");
    break;
    
    case 3:
    printf("输入要删除的姓名:\n");
    printf("姓名:");
    scanf("%s",e);
    listdelete(s,e);
    break;
    
    
    case 4:
    printf("输入要插入的信息:\n");
    listinsert(s,e);
    break;
    
    case 5:
    printf("输入要查询人的姓名:\n");
    scanf("%s",e); 
    listlocate(s,e);
    system("pause");
    break;
    
    case 6:
    printf("输入要修改人的学号:\n");
    scanf("%s",e);
    listchange(s,e);
    system("pause");
    break;
    
    case 7:
    writetotext(s);
    system("pause");
    break;
    
    case 8:
    readfromtext(s);
    system("pause");
    break;
    
    case 0:                                                    /*退出程序*/
    exit(0);
    break;
    }
    }
    return 1;}
  • 相关阅读:
    TYVJ 2002 扑克牌 题解
    TYVJ P1933 绿豆蛙的归宿 题解(未完成)
    TYVJ-P1864 守卫者的挑战 题解
    HDU 4901 The Romantic Hero 题解——S.B.S.
    OpenJudge 8782 乘积最大——S.B.S
    COGS 08-备用交换机 题解——S.B.S.
    poj2186 Popular Cows 题解——S.B.S.
    高级c++头文件bits/stdc++.h
    #include &lt;NOIP2010 Junior&gt; 三国游戏 ——using namespace wxl;
    NOIP 2008提高组第三题题解by rLq
  • 原文地址:https://www.cnblogs.com/sherlockhomles/p/3089135.html
Copyright © 2011-2022 走看看