Mirai997 2021-11-16 10:20 采纳率: 100%
浏览 63
已结题

链表实现新生信息系统

#include<malloc.h>
#include<string.h>
#include<stdlib.h>

struct student{
    char name[100];
    int num;
    char sex;
    int age;
    char major;
    int classno;
    
    struct student *next;
};


struct node{
    student stu;
    struct node *next;      
};
struct node *head=NULL;


void add();
void dele();
void menu();
void change();
void select();





//增操作(信息录入操作)
void add(){
    struct node* q=(struct node*)malloc(sizeof(struct node));
    q->next = NULL;
    struct node* p=head;
    while(p->next!=NULL&&head->next!=NULL){
      p=p->next;
    }
    if(head==NULL){
    head=q;
    }
    else p->next=q;

    printf("请输入学生的名字");
    scanf("%s",&q->stu.name);

    printf("请输入学生的学号");
    scanf("%d",&q->stu.num);

    printf("请输入学生的年龄");
    scanf("%d",&q->stu.age);

    printf("请输入学生的性别");
    scanf("%s",&q->stu.sex);

    printf("请输入学生的专业");
    scanf("%s",&q->stu.major);
    
    printf("请输入学生的班级");
    scanf("%d",&q->stu.classno);
    
    printf("Congratulations!信息录入成功");
    system("pause");
    system("cls");
}





//查操作(信息查询操作) 
void select(){
    struct node* p=head;
    printf("请输入需要查找的学号: ");
    int x;
    scanf("%d",&x);
    while (1){
    if(p->stu.num==x){
            printf("姓名\t学号\t性别\t年龄\t专业\t班级\n");
            printf("%s\t%d\t%s\t%d\t%s\t%d\t\n",p->stu.name,p->stu.num,p->stu.sex,p->stu.age,p->stu.major,p->stu.classno);
            system("pause");
            system("cls");
            break;
    }
    if(p->stu.num==NULL){
            printf("sorry,查无此人!");
            break;
    }
    
    }
    p=p->next;
    
    
    
    
}


//改操作(信息修改操作) 
void change(){
    struct node* p=head;
    printf("请输入需要改的学生学号:");
    int x;
    scanf("%d",&x);
    while(1){
        if(p->stu.num==x){
            printf("%s\t %d\t %s\t %d\t %s\t %d\t",p->stu.name,p->stu.num,p->stu.sex,p->stu.age,p->stu.major,p->stu.classno);
            printf("*1姓名 *2学号 *3性别 *4年龄 *5专业 *6班级");
            printf("请输入你想修改的数据:");
            int y;
            scanf("%d",&y);
            switch(y){
                case 1:scanf("%s",p->stu.name);
                case 2:scanf("%d",p->stu.num);
                case 3:scanf("%s",p->stu.sex);
                case 4:scanf("%d",p->stu.age);
                case 5:scanf("%s",p->stu.major);
                case 6:scanf("%d",p->stu.classno);
            }
            printf("Congratulations!修改成功!");
            system("pause");
            system("cls");
            
            break;
        }
        p=p->next;
    }
    
    system("cls");
    
}

//删操作(信息删除操作)
void dele(){    
    struct node*p=head->next;
    struct node*q;
    printf("请输入你想删除的学生学号");
    int z;
    scanf("%d",&z);
    q=p;
    while(1){
     if(p->stu.num==z){
        q->next=p->next;
        free(p);
        break;
      }
      else{
          p=p->next;
      }
    
     if(p->stu.num==NULL){
    printf("Very Sorry!你输入的学号不存在!"); 
    break;
    }
    system("cls");
}



 
void menu(){
    printf("\t=============================\n");
     printf("\t*Welcome to新生信息管理系统*\n" );         
    printf("\t=============================\n");
    printf("\t   *1)信息录入              \n");        
    printf("\t   *2)信息修改              \n");        
    printf("\t   *3)信息删除              \n");        
    printf("\t   *4)信息查询              \n");    
    printf("\t=============================\n");
}
 
 
int main(){
    while(1){
    
    menu();
    printf("请输入你的选择:");
    int x;
    scanf("%d",&x);
    switch(x){
        case 1:
        add();
        break;//信息录入
        case 2:
        change();    
        break;//信息修改
        case 3:
        dele();
        break;//信息删除
        case 4:
        select();
        break;//信息查询 
        default:
        printf("你输入的数字有误error!!\n");
        }
        system("cls";)
    }
}


报错

img

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-11-16 17:10
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    struct student {
        char name[100];
        int num;
        char sex;
        int age;
        char major[32];
        int classno;
        struct student* next;
    };
    
    struct node {
        student stu;
        struct node* next;
    };
    struct node* head = NULL;
    
    void add();
    void Dele();
    void menu();
    void change();
    void select();
    
    
    //增操作(信息录入操作)
    void add()
    {
        struct node* q = (struct node*)malloc(sizeof(struct node));
        q->next = NULL;
        struct node* p = head;
        if (head == NULL) {
            head  = q;
        }
        else {
            while (p->next != NULL) { //while (p->next != NULL && head->next != NULL) {
                p = p->next;
            }
            p->next = q;
        }
        printf("请输入学生的名字:");
        getchar();
        scanf("%s", q->stu.name);  //scanf("%s", &q->stu.name);
        printf("请输入学生的学号:");
        scanf("%d", &q->stu.num);
        printf("请输入学生的年龄:");
        scanf("%d", &q->stu.age);
        printf("请输入学生的性别(男:F,女:M):");
        getchar();
        scanf("%c", &q->stu.sex);  //scanf("%s", &q->stu.sex); 
        printf("请输入学生的专业:");
        getchar();
        scanf("%s", q->stu.major); //scanf("%s", &q->stu.major);
        printf("请输入学生的班级:");
        scanf("%d", &q->stu.classno);
        printf("Congratulations!信息录入成功");
        system("pause");
        system("cls");
    }
    
    void Dele()
    {
        struct node* p = head;
        struct node* q = NULL, * t;
        printf("请输入你想删除的学生学号");
        int z;
        scanf("%d", &z);
        while (p) {
            if (p->stu.num == z) {
                t = p;
                if (t == head) {
                    if (t->next != NULL)
                        head = t->next;
                    else
                        head = NULL;
                }
                else {
                    if (t->next == NULL)
                        q->next = NULL;
                    else
                        q->next = t->next;
                }
                free(t);
                break;
            }
            q = p;
            p = p->next;
        }
        if (p == NULL) {
            printf("Very Sorry!你输入的学号不存在!");
        }
        system("pause");
        system("cls");
    }
    //查操作(信息查询操作) 
    void select()
    {
        struct node* p = head;
        printf("请输入需要查找的学号: ");
        int x;
        scanf("%d", &x);
        while (p) {
            if (p->stu.num == x) {
                printf("姓名\t学号\t性别\t年龄\t专业\t班级\n");
                printf("%s\t%d\t%c\t%d\t%s\t%d\t\n", p->stu.name, p->stu.num, p->stu.sex,
                                                 p->stu.age, p->stu.major, p->stu.classno);
                break;
            }
            p = p->next;
        }
        if (p == NULL)
            printf("sorry,查无此人!");
        system("pause");
        system("cls");
    }
    
    //改操作(信息修改操作) 
    void change()
    {
        struct node* p = head;
        printf("请输入需要改的学生学号:");
        int x;
        scanf("%d", &x);
        while (p) {
            if (p->stu.num == x) {
                printf("%s\t%d\t%c\t%d\t%s\t%d\n", p->stu.name, p->stu.num, p->stu.sex,
                                                     p->stu.age, p->stu.major, p->stu.classno);
                printf("*1姓名 *2学号 *3性别 *4年龄 *5专业 *6班级\n");
                printf("请输入你想修改的数据:");
                int y;
                scanf("%d", &y);
                switch (y) {
                      case 1:scanf("%s", p->stu.name); break;
                      case 2:scanf("%d", &p->stu.num); break;
                      case 3:getchar(); scanf("%c", &p->stu.sex); break;
                      case 4:scanf("%d", &p->stu.age); break;
                      case 5:scanf("%s", p->stu.major); break;
                      case 6:scanf("%d", &p->stu.classno); break;
                     default:y = 0; break;
                }
                if (y != 0)
                    printf("Congratulations!修改成功!");
                break;
            }
            p = p->next;
        }
        if (p == NULL)
            printf("sorry,查无此人!");
        system("pause");
        system("cls");
    }
    //删操作(信息删除操作)
    
    void menu()
    {
        printf("\t=============================\n");
        printf("\t*Welcome to新生信息管理系统*\n");
        printf("\t=============================\n");
        printf("\t   *1)信息录入              \n");
        printf("\t   *2)信息修改              \n");
        printf("\t   *3)信息删除              \n");
        printf("\t   *4)信息查询              \n");
        printf("\t=============================\n");
    }
    void print()
    {
        struct node* p = head;
        while (p)
        {
            printf("%s\t%d\t%c\t%d\t%s\t%d\n", p->stu.name, p->stu.num, p->stu.sex,
                                                p->stu.age, p->stu.major, p->stu.classno);
            p = p->next;
        }
        system("pause");
    }
    int main()
    {
        while (1) {
            menu();
            printf("请输入你的选择:");
            int x;
            scanf("%d", &x);
            switch (x) {
            case 1:
                add();
                break;//信息录入
            case 2:
                change();
                break;//信息修改
            case 3:
                Dele();
                break;//信息删除
            case 4:
                select();
                break;//信息查询
            default:
                printf("你输入的数字有误error!!\n");
            }
            system("cls");
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 11月16日

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错