Fatsnake2 2022-09-29 15:13 采纳率: 90%
浏览 41
已结题

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

我给一个动态数组用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条回答 默认 最新

  • 炼丹小白师 2022-09-29 18:22
    关注

    你这个问题,就不是理论问题了,我以前遇到过一个类似问题,就是数组越界,这里的顺序队列你可以看作一个数组,很明显数组越界,如果你用DevCpp调试你会发现,溢出的元素插入到了另一片存储空间,这是集成环境对于编码者机器的保护措施。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测