倾城丶符华-kirito 2022-09-29 15:13 采纳率: 90%
浏览 33
已结题

关于顺序表管理学生信息管理的问题

我给一个动态数组用malloc分配储存空间,然后我给n赋值是2,但是我输入两个学生的信息后,还能继续插入,请问这是为什么,这个题目是用顺序表实现学生信息管理

用代码块功能插入代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//学生信息的定义 
typedef struct{
    char num[20];//10位学号
    char name[20];//姓名
    double score;//成绩 
}Student;
//顺序表的定义
typedef struct{
    Student *elem;//指向数据元素的基地址 
    int length;      //线性表的当前长度 
}Sqlist; 
void inputinformation(Sqlist &sq);
void outputinformation(Sqlist &sq);
void searchbyid(Sqlist &sq, char num[]);
void searchbyname(Sqlist &sq, char name[]);
void searchbylocation(Sqlist &sq, int n);
void insert(Sqlist &sq, int n);
void deletenode(Sqlist &sq, int n);
int sum(Sqlist &sq);
//根据指定学生个数,逐个输入学生信息
void inputinformation(Sqlist &sq){
    int n;//学生数量
    printf("please input the number of students\n");
    scanf("%d",&n);
    sq.elem=(Student *)malloc(n*sizeof(Student));
    sq.length=0;
    for(int i=0; i<n; i++){
        printf("please input the name and the num and the score of the student\n");
        scanf("%s%s%lf",&sq.elem[i].name,&sq.elem[i].num,&sq.elem[i].score);
        sq.length++;
    }
    /*while(sq.length>=0 && sq.length<n){
        printf("please input the num and the name and the score of the student\n");
        scanf("%c",&sq.elem[i].num);
        scanf("%c",&sq.elem[i].name);
        scanf("%lf",&sq.elem[i].score);
        ++sq.length;
        ++i;
    }*/
}
//逐个显示学生表中的所有学生的信息
void outputinformation(Sqlist &sq){
    if(sq.length<=0)printf("Error,there is no data in the sqlist");
    for(int i=0; i<sq.length; i++){
        printf("%s %s %.2f\n",sq.elem[i].name,sq.elem[i].num,sq.elem[i].score);
    }
} 
//根据ID寻找学生并且显示信息
void searchbyid(Sqlist &sq, char num[]){
    for(int i=0; i<=sq.length; i++){
        if(strcmp(sq.elem[i].num,num)==0){
            printf("%s %.2f\n",sq.elem[i].name,sq.elem[i].score);
        }
        else i++;
    }
} 
//根据姓名查找,返回此学生的学号和成绩
void searchbyname(Sqlist &sq, char name[]){
    for(int i=0; i<=sq.length; i++){
        if(strcmp(sq.elem[i].name,name)==0){
            printf("%s %.2f\n",sq.elem[i].num,sq.elem[i].score);
        }
        else i++;
    }
} 
//根据指定的位置可返回相应的学生信息(学号, 姓名, 成绩)
void searchbylocation(Sqlist &sq, int n){
    printf("%s %s %.2f\n",sq.elem[n].name,sq.elem[n].num,sq.elem[n].score);
} 
//给定一个学生信息,插入到表中指定的位置
void insert(Sqlist &sq, int n){
    if((n<0)||(n>=sq.length)) printf("Error,the number you input is bigger than the length of Sqlist");
    ++sq.length;
    for(int i=sq.length-1; i>=n; i--){
        sq.elem[i+1]=sq.elem[i];
    }
    printf("please input the information of the student(name num score)\n");
    scanf("%s%s%lf",&sq.elem[n].name,&sq.elem[n].num,&sq.elem[n].score);    
} 
//删除指定位置的学生记录
void deletenode(Sqlist &sq, int n){
    if((n<0)||(n>=sq.length)) printf("Error,the number you input is bigger than the length of Sqlist\n");
    for(int i=n; i<sq.length; i++){
        sq.elem[i]=sq.elem[i+1];
    }
    --sq.length;
} 
//计算学生数量
int sum(Sqlist &sq){
    return sq.length;
} 
int main(){
    printf("|---------------------------------------------|\n");
    printf("|             Experiment-1\n");
    printf("|---------------------------------------------|\n");
    printf("|    202103160033 zengshenger \n");
    printf("|---------------------------------------------|\n");
    printf("|    [1]Create Student List                   |\n");
    printf("|    [2]Show All the Students                 |\n");
    printf("|    [3]Search Student by ID                  |\n");
    printf("|    [4]Search Student by Name                |\n");
    printf("|    [5]Search Student by location            |\n");
    printf("|    [6]Add a New Student                     |\n");
    printf("|    [7]Delete a  Student                     |\n");
    printf("|    [8]Sum the Number of Students            |\n");
    printf("|    [9]Exit                                  |\n");
    printf("|---------------------------------------------|\n");
    printf("|Please make a choice by pressing number key  |\n");
    printf("|---------------------------------------------|\n");
    Sqlist sq;
    char name[20],num[20];
    int number,location;
    scanf("%d",&number);
    while(number!=9){
        switch(number){
            case 1:inputinformation(sq);
                   printf("创建成功\n");
                   break;
            case 2:outputinformation(sq);
                   break;
            case 3:printf("please input the student`s ID who you want to search for\n");
                   scanf("%s",&num);
                   searchbyid(sq,num);
                   break;
            case 4:printf("please input the student`s name who you want to search for\n");
                   scanf("%s",&name);
                   searchbyname(sq,name);
                   break;
            case 5:printf("please input the student`s location who you want to search for\n");
                   scanf("%d",&location);
                   searchbylocation(sq, location);
                   break;
            case 6:printf("please input the student`s location who you want to insert\n");
                   scanf("%d",&location);
                   insert(sq, location);
                   break;
            case 7:printf("please input the student`s location who you want to delete\n");
                   scanf("%d",&location);
                   deletenode(sq,location);
                   break;
            case 8:sum(sq);
                   break;
            case 9:break;
        }
        if(number==9){
            return 0;
        }
        else{
            printf("Please make a choice by pressing number key\n");
            scanf("%d",&number);
        }
    }
}

运行结果及报错内容

img

请问为什么我给n赋值2,然后用malloc分配内存空间,但是我输入完两组数据后,还能继续插入,况且都能输出。

  • 写回答

3条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 10月11日
      • 已采纳回答 10月3日
      • 创建了问题 9月29日

      悬赏问题

      • ¥15 用51单片机普中开发板实现数字时钟
      • ¥15 有没有大佬能写一个按键LED灯交替闪烁的keil5里的编程
      • ¥15 用python解决excel文件读写
      • ¥15 分别用到哪些协议?如何实现的通信过程?
      • ¥30 nginx代理第三方接口
      • ¥20 求求了有没有人帮我看看怎么判断稳定性
      • ¥50 求解R语言的数据可视化问题
      • ¥15 两块fpga数据传输时钟同步问题
      • ¥15 pycharm 导入paddle模块后找不到enable_static属性
      • ¥15 wear os 哪种模式是只显示时间的,接口在哪。