小言_互联网的博客

学生表维护(用顺序存储结构)。 帮女朋友完成系列。

329人阅读  评论(0)


源代码:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100

typedef int Status;

typedef struct
{
    char no[8];
    char name[20];
    int score;
}student;

typedef student ElemType;

typedef struct
{
    ElemType *elem;
    int length;
}SqList;

Status InitList(SqList *L)
{
    L->elem=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    if(!L->elem)  exit(OVERFLOW);
    L->length=0;
    return OK;
}

Status ListInsert(SqList &L,int i,ElemType e)
{
    if((i<1)||(i>L.length+1)) return ERROR;
    if(L.length==MAXSIZE)   return ERROR;
    for(int j=L.length;j>=i;j--)
    {
        L.elem[j+1]=L.elem[j];
    }
    L.elem[i]=e;
    ++L.length;
    return OK;
}

ElemType GetElem(SqList &L,int i)
{
    return L.elem[i];
}

Status ListDelete(SqList &L,int i)
{
    if((i<1)||(i>L.length))   return ERROR;
    for(int j=i;j<=L.length;j++)
    {
        L.elem[j]=L.elem[j+1];
    }
    --L.length;
    return OK;
}

void Input(ElemType *e)
{
    printf("姓名:");
	scanf("%s",e->name);
    printf("学号:");
	scanf("%s",e->no);
    printf("成绩:");
	scanf("%d",&e->score);
    printf("输入完成\n\n");
}

void Output(ElemType &e)
{
    printf("姓名:%s\n学号:%s\n成绩:%d\n\n",e.name,e.no,e.score);
}

int main()
{
    SqList L;
    ElemType a,b,c,d;
    printf("1. 构造顺序表\n");
    printf("2. 录入指定人数的学生信息\n");
    printf("3. 显示学生表中的所有信息\n");
    printf("4. 在指定位置插入学生信息\n");
    printf("5. 在指定位置删除学生信息\n");
    printf("6. 统计学生个数\n");
    printf("0. 退出\n");
    int x,choose;
    while(1)
    {
        printf("请输入你要选择的功能前的序号:");
        scanf("%d",&choose);
        if(choose==0)   break;
        switch(choose)
        {
            case 1:
                if(InitList(&L))
                    printf("成功建立顺序表\n");
                else
                    printf("顺序表建立失败\n");
                break;
            case 2:
                printf("请输入要录入学生的人数:");
                scanf("%d",&x);
                for(int i=1;i<=x;i++)
                {
                    printf("第%d个学生:\n",i);
                    Input(&L.elem[i]);
                }
                L.length=x;
                break;
            case 3:
                for(int i=1;i<=x;i++)
                {
                    a=GetElem(L,i);
                    Output(a);
                }
                break;


            case 4:
                printf ("请输入要插入的位置:");
                int id2;
                scanf("%d",&id2);
                printf("请输入学生信息:\n");
                Input(&c);
                if(ListInsert(L,id2,c))
                {
                    x++;
                    printf("插入成功\n");
                }
                else
                {
                    printf("插入失败\n");
                }
                break;
            case 5:
                printf("请输入要删除的位置:");
                int id3;
                scanf("%d",&id3);
                if(ListDelete(L,id3))
                {
                    x--;
                    printf("删除成功\n");
                }
                else
                {
                    printf("删除失败\n");
                }
                break;
            case 6:
                printf("已录入的学生个数为:%d\n\n",L.length);
                break;
        }
    }
    printf("\n\n请按任意键退出\n\n");
    return 0;
}

效果图:


———————————————————————————
更新:有些人复制我的代码之后,在code:block能跑起来。 但是在vc++的时候,会出现错误。

C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(116) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(109) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(117) : error C2374: 'i' : redefinition; multiple initialization
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(109) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(123) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(117) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(123) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(109) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(139) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(117) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(139) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(109) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(153) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(117) : see declaration of 'i'
C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(153) : error C2360: initialization of 'i' is skipped by 'case' label
        C:\Users\77359\Desktop\靓靓宝贝最漂亮\Cpp1.cpp(109) : see declaration of 'i'
执行 cl.exe 时出错.

这个是c++中的变量初始化问题,我查阅资料,进行详细说明。 稍后更新。


转载:https://blog.csdn.net/weixin_44696542/article/details/104502871
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场