标题:关于信息的修改与删除的子函数一直无效,求帮忙看看,谢谢了’
取消只看楼主
蔡伟超
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2016-11-13
结帖率:80%
已结贴  问题点数:15 回复次数:0 
关于信息的修改与删除的子函数一直无效,求帮忙看看,谢谢了’
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void zcd();            //主菜单子函数声明
void input();          //输入子函数声明
void output();         //显示子函数声明
void save(int n);      //保存子函数声明
void search();         //查询子函数声明
void search_number();  //按学号查询子函数声明
void search_name();    //按姓名查询子函数声明
void add();            //添加子函数声明
void revise();         //修改子函数声明
int N();               //统计总人数子函数声明
void dele();           //删除子函数声明


struct student         //学生信息结构体
{
    char number[13];
    char name[20];
    char sex[20];
    int age;
    char addr[50];
    char phone_number[11];
}stu[200];

void main()                  //主函数
{
    int x;
    int choose;
    for(;;)
    {
        x=N();
        zcd();
        printf("请输入您的选择:");
        flushall();          //神奇?!清空缓存区,不然没法输入= =、
        scanf("%d",&choose);
        switch(choose)
        {
        case 1:
            input();
            break;
        case 2:
            output();            
            break;
        case 3:
            search();
            break;
        case 4:
            add();
            break;
        case 5:
            revise();
            break;
        case 6:
            dele();
            break;
        case 7:
            printf("%d",x);
            break;
        default:
            printf("error!\n");
        }
    }
}

void zcd()    //主菜单子函数
{
    printf("\n");
    printf("\n");
    printf("\n");
    printf("******************************************\n");
    printf("▓          学生通讯录管理系统          ▓\n");
    printf("▓--------------------------------------▓\n");
    printf("▓         ●[1]创建通讯录信息          ▓\n");
    printf("▓         ●[2]显示通讯录信息          ▓\n");
    printf("▓         ●[3]查询通讯录信息          ▓\n");
    printf("▓         ●[4]添加通讯录信息          ▓\n");
    printf("▓         ●[5]修改通讯录信息          ▓\n");
    printf("▓         ●[6]删除通讯录信息          ▓\n");
    printf("▓         ●[7]统计总人数              ▓\n");
    printf("******************************************\n");
    printf("\n");
    printf("\n");
    printf("\n");
}

void input()   //创建输入子函数
{
    int i;   
    char ch;
    for(i=0;;i++)
    {
        printf("请输入第%d个学生的信息:\n",i+1);
        printf("学号:");
        scanf("%s",&stu[i].number);
        printf("姓名:");
        scanf("%s",&stu[i].name);
        printf("性别:");
        scanf("%s",&stu[i].sex);
        printf("年龄:");
        scanf("%d",&stu[i].age);
        printf("宿舍:");
        scanf("%s",&stu[i].addr);
        printf("电话号码:");
        scanf("%s",&stu[i].phone_number);  
        printf("是否继续输入(y/n)?\n");
        flushall();
        scanf("%c",&ch);//为什么ch=getchar()不行
        if(ch=='n')
        {
            break;
        }
    }
    save(i+1);
    printf("\n");
    printf("\n");
    printf("保存成功!\n");
}

void save(int n)      //保存子函数  
{
    int i;
    FILE *fp;
    if((fp=fopen("student.txt","wb+"))==NULL)   
    {
        printf("error!\n");   
        exit(0);                           
    }
    for(i=0;i<n;i++)
    {
        if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
            printf("error!\n");
    }
    fclose(fp);
}

void output()//显示子函数
{
    int i,a=N();
    printf("学号        | 姓名 |性别 |年龄|宿舍   | 手机号码   \n");
    for(i=0;i<a;i++)
    {
        printf("%s %s %s  %d   %s %s\n",stu[i].number,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].phone_number);
    }
}

void search()         //查询子函数
{
    int choose;
    printf("*****************************\n");
    printf("       (1)按学号查找         \n");
    printf("       (2)按姓名查找         \n");
    printf("*****************************\n");
    printf("please input(1/2):");
    scanf("%d",&choose);
    switch(choose)
    {
    case 1:
        search_number();
        break;
    case 2:
        search_name();
        break;
    }
}

int N()                   //统计总人数子函数
{
    FILE *fp;
    int i=0;
    if((fp=fopen("student.txt","rb+"))==NULL)
    {
        printf("error!\n");
        exit(0);
    }
    while(fread(&stu[i],sizeof(struct student),1,fp)!=0)
    {
        i++;
    }
    fclose(fp);
    return(i);
}

void search_number()          //按学号查询子函数
{
    FILE *fp;
    int i=0;
    int a=N();
    char number[12];
    printf("请输入您要查找学生的学号:\n");
    scanf("%s",number);
    if((fp=fopen("student.txt","rb+"))==NULL)
    {
        printf("error!\n");
        exit(0);
    }
    while(fread(&stu[i],sizeof(struct student),1,fp)!=0)
    {
        if(strcmp(number,stu[i].number)==0)
        {
            printf("该学生信息为:\n");
            printf("学号        |    姓名      |性别 |年龄|宿舍   | 手机号码   \n");
            printf("%s %s %s   %d   %s %s\n",stu[i].number,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].phone_number);
        }
        else if(i==a)
        {
            printf("没有此学生\n");
        }
        i++;
    }
    fclose(fp);

}

void search_name()        //按姓名查询子函数
{
    FILE *fp;
    int i=0;
    int a=N();
    char name[20];
    printf("请输入您要查找学生的名字:\n");
    scanf("%s",name);
    if((fp=fopen("student.txt","rb+"))==NULL)
    {
        printf("error!\n");
        exit(0);
    }
    while(fread(&stu[i],sizeof(struct student),1,fp)!=0)
    {
        if(strcmp(name,stu[i].name)==0)
        {
            printf("该学生信息为:\n");
            printf("学号        |    姓名   |性别 |年龄|宿舍   | 手机号码   \n");
            printf("%s %s %s  %d   %s %s\n",stu[i].number,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].phone_number);
        }
        else if(i==a)
        {
            printf("没有此学生\n");
        }
        i++;
    }
    fclose(fp);
}

void add()             //添加子函数
{
    int i,m;
    int a=N();
    printf("请输入要添加的人数:");
    scanf("%d",&m);
    for(i=a;i<m+a;i++)
    {
        printf("学号:");
        scanf("%s",stu[i].number);
        printf("姓名:");
        scanf("%s",stu[i].name);
        printf("性别:");
        scanf("%s",stu[i].sex);
        printf("年龄:");
        scanf("%d",&stu[i].age);
        printf("宿舍:");
        scanf("%s",stu[i].addr);
        printf("电话号码:");
        scanf("%s",stu[i].phone_number);
        printf("创建成功!\n");
    }
    save(m+a);
}

void revise()
{
    int n=N();
    char number[13];
    char name[20];
    char sex[20];
    int age;
    char addr[50];
    char phone_number[11];
    int ch,i=0;
    printf("请输入要修改学生的姓名:");
    scanf("%s",name);
    for(;i<n;i++)
    {   
        if(strcmp(stu[i].name,name)==0)   
        {        
            printf("该学生信息为:\n");
            printf("学号        |    姓名   |性别 |年龄|宿舍   | 手机号码   \n");
            printf("%s %s %s  %d   %s %s\n",stu[i].number,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].phone_number);   
            printf("1学号,2姓名,3性别,4年龄,5宿舍地址,6手机号码\n");   
            printf("请输入您要修改的部分(1-6):\n");
            scanf("%d",&ch);
            switch(ch)   
            {   
            case 1:   
                printf("将学号改为:");   
                scanf("%s",number);   
                strcpy(stu[i].number,number);
                break;
            case 2:
                printf("将姓名改为:");
                strcpy(stu[i].name,name);
                break;
            case 3:
                printf("将性别改为:");
                scanf("%s",sex);
                strcpy(stu[i].sex,sex);
                break;
             case 4:
                printf("将年龄改为:");
                scanf("%d",&age);
                stu[i].age=age;
                break;
              case 5:
                printf("将宿舍改为:");
                scanf("%s",addr);
                strcpy(stu[i].addr,addr);
                break;
            case 6:
                printf("将手机号码改为:");
                scanf("%s",phone_number);
                strcpy(stu[i].phone_number,phone_number);
                break;
            }            
            printf("修改成功!\n");
            break;
        }
    }
    if(i==n)
        {
            printf("无该学生信息!\n");
            printf("请重新输入!\n");
        }
}

void dele()        //删除子函数
{
    int i,n=N(),ch;
    char name[20];
    printf("请输入要删除学生的姓名:");
    scanf("%s",name);
    for(i=0;i<n;i++)
    {
        if(strcmp(stu[i].name,name)==0)
        {
            printf("是否删除(1为确认)?\n");
            scanf("%d",&ch);
            if(ch==1)
            {
                stu[i]=stu[i+1];
            }
            else
            {
                dele();
            }

        }
    }
    if(i==n)
        {
            printf("无该学生信息!\n");
            printf("请重新输入!\n");
        }
}
搜索更多相关主题的帖子: 学号查询 include search 信息 统计 
2016-12-02 11:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-471896-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.120995 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved