顺序表

/*coder:cyl   time: 2020.12.1*/
#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE  1000
#define ElemType int
#define Status   int
#define ERROR    -1
#define OK        1
int n;
typedef struct {
    ElemType *elem;
    int length;
} SqList;

Status InitList(SqList &L) {
    L.elem = new ElemType[MAXSIZE];
    if (! L.elem) {
        cout << "申请空间失败" << endl;
        exit(0);
    }
    L.length = 0;
    return OK;
}

Status ListIn(SqList &L, int i) {
    srand(time(0));
    for (int j = 0; j < i; j ++) {
        L.elem[j] = rand() % 100;
        ++ L.length;
    }
    return OK;
}

Status ListIn1(SqList &L, int i) {
    for (int j = 0; j < i; j ++) {
        L.elem[j] = rand() % 100;
        ++ L.length;
    }
    return OK;
}

Status ListOut(SqList L) {
    for(int k=0;k<L.length;k++){
        cout<<"["<<k<<"]"<<"\t";
    }
    cout<<endl;
    for (int i = 0; i < L.length; i ++) {
        cout << L.elem[i] << "\t";
    }
    cout<<endl;
    return OK;
}

Status ListInsert(SqList &L, int i, ElemType e) {
    if (i < 1 || i > L.length + 1) {
        cout << "不符合数据要求" << endl;
        return ERROR;
    }
    if (L.length == MAXSIZE) {
        cout << "达到最大量" << endl;
        return ERROR;
    }
    for (int k = L.length - 1; k >= i - 1; k --) {
        L.elem[k + 1] = L.elem[k];
    }
    L.elem[i - 1] = e;
    L.length ++;
    return OK;
}

bool ListEmpty(SqList L) {
    if (L.length == 0)return true;
    else return false;
}

Status ListLength(SqList L) {
    return L.length;
}

Status GetElem(SqList L, int i, ElemType &e) {
    if (i < 0 || i > L.length) {
        cout << "不符合数据要求" << endl;
        return ERROR;
    }
    e = L.elem[i - 1];
    return e;
}

Status compare1(ElemType a, ElemType b) {
    if(a==b)return 1;
    else return 0;
}
Status compare2(ElemType a, ElemType b) {
    if(a>b)return 1;
    else return 0;
}
Status compare3(ElemType a, ElemType b) {
    if(a<b)return 1;
    else return 0;
}

Status LocateElem(SqList L, ElemType e,Status (*compare)(ElemType,ElemType)) {
    int i=0;
    ElemType *p=L.elem;
    while(i<L.length&&!(compare(*p++, e))) {
        i++;
    }
    if (i<=L.length){
            cout<<"第一个满足关系的元素序号是"<<i+1<<endl;
            return OK;
        }
    else{
    cout << "表格中没有该数据" <<endl;
    }
    return 0;
}

Status PriorElem(SqList L, ElemType e, ElemType &pre_e) {
    int i;
    for (i = 1; i < L.length; i ++) {
        if (L.elem[i] == e) {
            pre_e = L.elem[i - 1];
            cout << "元素" << e << "的前驱是:" << pre_e << endl;
            return OK;
        }
    }
    cout << "不存在此元素" << endl;
    return ERROR;
}

Status NextElem(SqList L, ElemType e, ElemType &next_e) {
    int i;
    for (i = 0; i < L.length - 1; i ++) {
        if (L.elem[i] == e) {
            next_e = L.elem[i + 1];
            cout << "元素" << e << "的后驱是:" << next_e << endl;
            return OK;
        }
    }
    cout << "不存在此元素" << endl;
    return ERROR;
}

Status DeleteElem(SqList &L, int i, ElemType &e) {
    if (i < 1 || i > L.length) {
        cout << "不符合数据要求" << endl;
        return ERROR;
    }
    e = L.elem[i - 1];
    for (int j = i; j < L.length; j ++) {
        L.elem[j - 1] = L.elem[j];
    }
    L.length --;
    cout << "删除的元素是" << e << endl;
    return OK;
}

Status ClearList(SqList &L) {
    L.length = 0;
    return OK;
}

void MergeList(SqList La, SqList Lb, SqList &Lc) {
    int i, j, k, ai, bj;
    int La_Len = ListLength(La);
    int Lb_Len = ListLength(Lb);
    i = j = 1;
    k = 0;
    while ((i <= La_Len) && (j <= Lb_Len)) {
        GetElem(La, i, ai);
        GetElem(Lb, j, bj);
        if (ai <= bj) {
            ListInsert(Lc, ++ k, ai);
            ++ i;
        } else {
            ListInsert(Lc, ++ k, bj);
            ++ j;
        }
    }
    while (i <= La_Len) {
        GetElem(La, i ++, ai);
        ListInsert(Lc, ++ k, ai);
    }
    while (j <= Lb_Len) {
        GetElem(Lb, j ++, bj);
        ListInsert(Lc, ++ k, bj);
    }
    Lc.length = k;
}

Status Menus() {
    cout << "1.在第i个元素之前插入一个元素" << endl;
    cout << "2.判断顺序表是否为空" << endl;
    cout << "3.求顺序表中元素的个数" << endl;
    cout << "4.取第i个元素" << endl;
    cout << "5.查找第1个与某元素满足compare()关系的元素的序号" << endl;
    cout << "6.返回某元素的前驱" << endl;
    cout << "7.返回某元素的后驱" << endl;
    cout << "8.删除第i个元素" << endl;
    cout << "9.把一个顺序表赋值给另一个顺序表" << endl;
    cout << "10.顺序表置空" << endl;
    cout << "11.合并两个顺序表" << endl;
    cout<<"12.随机生成顺序表"<<endl;
    cout<<"13.学生管理系统"<<endl;
    cout << "0.结束操作" << endl << endl;
    cout << "请输入你要执行操作的序号:" << endl;
    cin >> n;
    return n;
}

int main() {
    int num, problem;
    SqList L;
    InitList(L);
    srand(time(0));
    num=rand()%10+5;
    ListIn(L, num);
    ListOut(L);
    cout << endl;
    problem = Menus();
    while (problem != 0) {
        if (problem == 1) {
            int i, e;
            cout << "请输入插入元素的位置:i" << endl;
            cin >> i;
            cout << "请输入插入元素的值:e" << endl;
            cin >> e;
            ListInsert(L, i, e);
            ListOut(L);
        } else if (problem == 2) {
            if (ListEmpty(L))cout << "线性表为空" << endl;
            else cout << "线性表不为空" << endl << endl;
        } else if (problem == 3) {
            cout << "顺序表中元素的个数为:" << ListLength(L) << "个" << endl << endl;
        } else if (problem == 4) {
            int i, e;
            cout << "请输入取第元素的序号i" << endl;
            cin >> i;
            cout << "第"<<i<<"个元素为:" << GetElem(L, i, e) << endl << endl;
        } else if (problem == 5) {
            int e,k;
            cout << "请输入你要查找的元素e" << endl;
            cin >> e;
            cout<<"请选择你想要的关系:1(相等) 2(小于你输入的元素) 3(大于你输入的元素)"<<endl;
            cin>>k;
            if(k==1) {
                LocateElem(L, e, compare1);
                cout << endl;
            }
            else if(k==2){
                LocateElem(L, e, compare2);
                cout<<endl;
            }
            else {
                LocateElem(L, e, compare3);
                cout<<endl;
            }
        } else if (problem == 6) {
            cout << "请输入元素e" << endl;
            int e, pre_e;
            cin >> e;
            PriorElem(L, e, pre_e);
            cout << endl;
        } else if (problem == 7) {
            cout << "请输入元素e" << endl;
            int e, next_e;
            cin >> e;
            NextElem(L, e, next_e);
            cout << endl;
        } else if (problem == 8) {
            cout << "请输入要删除元素的序号:i" << endl;
            int i, e;
            cin >> i;
            DeleteElem(L, i, e);
            ListOut(L);
            cout << endl;
        } else if (problem == 9) {
            SqList M;
            InitList(M);
            for (int i = 0; i < L.length; i ++) {
                M.elem[i] = L.elem[i];
                M.length ++;
            }
            cout << "已经将L线性表赋值给M线性表,下面打印M线性表" << endl;
            ListOut(M);
            cout << endl;
        } else if (problem == 10) {
            ClearList(L);
            cout << "已经清空,下面打印空表" << endl;
            ListOut(L);
            cout << endl;
        } else if (problem == 11) {
            cout << "首先生成两个随机线性表,请分别输入它们的元素个数" << endl;
            int ian, ibn;
            srand(time(0));
            ian=rand()%10+5;
            ibn=rand()%10+5;
            SqList ia, ib, ic;
            InitList(ia);
            InitList(ib);
            InitList(ic);
            ListIn1(ia, ian);
            cout << "打印第一个表" << endl;
            sort(ia.elem,ia.elem+ia.length);
            ListOut(ia);
            ListIn1(ib, ibn);
            sort(ib.elem,ib.elem+ib.length);
            cout << "打印第二个表" << endl;
            ListOut(ib);
            MergeList(ia, ib, ic);
            cout << "合并完成,打印合并的表,按非递增顺序排序" << endl;
            ListOut(ic);
            cout << endl;
        }
        else if(problem==12){
            srand(time(0));
            num=rand()%10+5;
            ClearList(L);
            ListIn(L, num);
            ListOut(L);
            cout << endl;
        }
        else if(problem==13){
            system("C:\\Users\\Woftc\\CLionProjects\\untitled11\\cmake-build-debug\\untitled11.exe");
        }
        problem = Menus();
    }
    return 0;
}

学生管理系统

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include <random>
#include<ctime>
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
std::default_random_engine e(time(0));
typedef int Status; // 定义函数返回值类型
typedef struct
{
    int  num; // 学号
    char name[10]; // 姓名
    double grade_C; // 成绩
    double grade_E;
    double grade_M;
    double grade_aver;
    double grade_sum;
}student;

typedef student ElemType;

typedef struct
{
    ElemType *elem; // 存储空间的基地址
    int length; // 当前长度
    int size;
}SqList;

Status InitList(SqList *L) // 构造空的顺序表 L
{
    L->elem=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    if(!L->elem)  exit(OVERFLOW);
    L->length=0;
    return OK;
}

ElemType GetElem(SqList &L,int i) // 访问顺序表,找到 i位置,返回给 e
{
    return L.elem[i];
}

int Search1(SqList &L,const char str[] ) // 根据名字查找,返回该同学在顺序表中的编号
{
    for(int i=2;i<=L.length;i++)
    {
        if(strcmp((L.elem[i].name), str) == 0)
            return i-1;
    }
    return 0;
}

int Search2(SqList &L,char str[]) // 根据名字查找,返回该同学在顺序表中的编号
{
    for(int i=1;i<L.length;i++)
    {
        if(strcmp((L.elem[i].name), str) == 0)
            return i+1;
    }
    return 0;
}

Status ListInsert(SqList &L,int i,ElemType e) // 在 i位置插入某个学生的信息
{
    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;
}

Status ListDelete(SqList &L,int i) // 在顺序表中删除 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("%d",e->num);
    printf("语文成绩:");    scanf("%lf",&e->grade_C);
    printf("英语成绩:");    scanf("%lf",&e->grade_E);
    printf("数学成绩:");    scanf("%lf",&e->grade_M);
    printf("输入完成\n\n");
    e->grade_aver=(e->grade_M+e->grade_E+e->grade_C)/3;
    e->grade_sum=e->grade_M+e->grade_E+e->grade_C;
}

Status RandomData(SqList &l,int j)
{ //15-10.返回一个随机生成的学生表
    l.elem = new student[j];
    l.length = j;
    l.size = j;
    char name1[50][20] = {"蔡易霖", "王星饭",  "欧阳", "黄", "詹", "彭", "吕","孙","李","罗","大涨","小王","李子","哈哈"};
    static uniform_int_distribution<int> u(0, 100); //时间种子,防止伪随机
    for (int i= 1; i <= j; i++)
    {
        strcpy(l.elem[i].name,name1[u(e) % 10]);
        l.elem[i].num = u(e);
        l.elem[i].grade_M= u(e);
        l.elem[i].grade_C = u(e);
        l.elem[i].grade_E= u(e);
        l.elem[i].grade_sum = l.elem[i].grade_M + l.elem[i].grade_C + l.elem[i].grade_E;
        l.elem[i].grade_aver = l.elem[i].grade_sum/ 3;
    }
    return OK;
}
void Output(ElemType *e)
{
    cout<<e->num<<"\t"<<e->name<<"\t"<<e->grade_C<<"\t\t"<<e->grade_E<<"\t\t"<<e->grade_M<<"\t\t"<<e->grade_aver<<"\t\t"<<e->grade_sum<<endl;
}

void Menus(){
    printf("\n********************************\n\n");
    puts("1. 随机生成学生信息");
    puts("2. 显示学生信息");
    puts("3. 输入姓名,查找该学生前驱");
    puts("4. 输入姓名,查找该学生后驱");
    puts("5. 显示某位置该学生信息");
    puts("6. 在指定位置插入学生信息");
    puts("7. 在指定位置删除学生信息");
    puts("8. 统计学生个数");
    puts("9.判断是否为空");
    puts("10.学生表置空");
    puts("11.把一个学生表赋值给另一个学生表");
    puts("0. 退出");
    printf("\n********************************\n\n");
}

int main()
{
    SqList L,M;
    ElemType a,b,c,d;
    Menus();
    int x,choose;
    InitList(&L);
    /*srand(time(0));
    x = rand() % 10 + 1;*/
    x=6;
    RandomData(L, x);
    L.length = x;
    puts("");
    cout<<"学号\t"<<"姓名\t"<<"语文成绩\t"<<"英语成绩\t"<<"数学成绩\t"<<"平均成绩\t"<<"总成绩"<<endl;
    for (int i = 1; i <= L.length; i++) {
        Output(&L.elem[i]);
    }
    cout<<endl;
    while(1)
    {
        puts("请选择:");
        scanf("%d",&choose);
        if(choose==0)    break;
        switch(choose) {
            case 1:
                srand(time(0));
                x = rand() % 10 + 1;
                RandomData(L, x);
                L.length = x;
                puts("");
                cout<<"学号\t"<<"姓名\t"<<"语文成绩\t"<<"英语成绩\t"<<"数学成绩\t"<<"平均成绩\t"<<"总成绩"<<endl;
                for (int i = 1; i <= L.length; i++) {
                    Output(&L.elem[i]);
                    }
                cout<<endl;
                break;
            case 2:
                printf("当前学生总人数为:%d\n",L.length);
                printf("当前顺序表为:\n");
                for(int i=1;i<=L.length;i++)
                    Output(&L.elem[i]);
                printf("\n");
                break;
            case 3:
                char s[20];
                printf("请输入学生姓名,返回前驱:");
                scanf("%s",s);
                if(Search1(L,s))
                    Output(&L.elem[Search1(L,s)]);
                else
                    puts("对不起,查无此人");
                puts("");
                break;
            case 4:
                char ss[20];
                printf("请输入学生姓名,返回后驱:");
                scanf("%s",ss);
                if(Search2(L,ss))
                    Output(&L.elem[Search2(L,ss)]);
                else
                    puts("对不起,查无此人");
                puts("");
                break;
            case 5:
                printf("请输入要查询的位置:");
                int id1;
                scanf("%d",&id1);
                b=GetElem(L,id1);
                Output(&b);
                break;
            case 6:
                printf ("请输入要插入的位置:");
                int id2;
                scanf("%d",&id2);
                printf("请输入学生信息:\n");
                Input(&c);
                if(ListInsert(L,id2,c))
                {
                    x++;
                    puts("插入成功");
                    puts("");
                }
                else
                {
                    puts("插入失败");
                    puts("");
                }
                break;
            case 7:
                printf("请输入要删除的位置:");
                int id3;
                scanf("%d",&id3);
                if(ListDelete(L,id3))
                {
                    x--;
                    puts("删除成功");
                    puts("");
                }
                else
                {
                    puts("删除失败");
                    puts("");
                }
                break;
            case 8:
                printf("已录入的学生个数为:%d\n\n",L.length);
                break;
            case 9:
                if(L.length==0)
                    printf("学生表为空");
                else
                    printf("学生表不为空");
                break;
            case 10:
                L.length=0;
                printf("学生表已经置空");
                break;
            case 11:
                InitList(&M);
                for(int cnt=1;cnt<=L.length;cnt++){
                    M.elem[cnt]=L.elem[cnt];
                    M.length++;
                }
                printf("赋值成功,已将L表赋值给M表,下面打印M表\n");
                for(int cnt=1;cnt<=M.length;cnt++){
                    Output(&M.elem[cnt]);
                }
                break;
        }
        Menus();
    }
    printf("\n\n谢谢您的使用,请按任意键退出\n\n\n");
    system("pause");
    return 0;
}