附
录 #include<stdio.h>
#include<stdlib.h>typedef struct {double* data;int length;
}Sqlist;void initSqlist(Sqlist* L, int n) {L->data = (double*)malloc(n * sizeof(double)); if (L->data == NULL){printf("内存分配失败\n");exit(1);}printf("请输入%d个非递减的有序元素值:\n", n);for (int i = 0; i < n; i++) {scanf("%lf", &(L->data[i]));}L->length = n;
}void printfSqlist(Sqlist* L)
{printf("有序线性表各个元素的值为:\n");for (int i = 0;i < L->length;i++){printf("%.2lf ", L->data[i]);}printf("\n");
}void insert(Sqlist* L,int n, double x) {int i = L->length - 1;while (i >= 0 && L->data[i] > x) {L->data[i + 1] = L->data[i];i--;}L->data[i + 1] = x;L->length++;
}void delet(Sqlist* L)
{int i, j;for (i = 0, j = 1; j < L->length; j++) {if (L->data[i] != L->data[j]) {L->data[++i] = L->data[j];}}L->length = i + 1;
}int main()
{int n = 0;printf("请输入线性表的长度n\n");scanf("%d", &n);Sqlist L;initSqlist(&L, n);printfSqlist(&L);double x = 0;printf("请输入要插入的元素x的值:\n");scanf("%lf", &x);insert(&L,n,x);printf("插入x后的线性表的值为:\n");printfSqlist(&L);delet(&L);printf("删除有序线性表中重复的元素后的值为:\n");printfSqlist(&L);return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define MAXSIZE 100typedef struct {long long id;char name[MAXSIZE];char sex[MAXSIZE];int math;int english;int nums;
} Students;void instudent(Students* student, int n) {printf("请按学号,姓名,性别,数学成绩,英语成绩依次输入学生信息:\n");for (int i = 0; i < n; i++) {scanf("%lld %s %s %d %d", &student[i].id, student[i].name, student[i].sex, &student[i].math, &student[i].english);}student->nums = n;
}void printstudent(Students* student) {for (int i = 0; i < student->nums; i++) {printf("学号:%lld 姓名:%s 性别:%s 数学成绩:%d 英语成绩:%d\n", student[i].id, student[i].name, student[i].sex, student[i].math, student[i].english);}
}void sercharstudent(Students* student, long long* x) {int found = 0; // 用于记录是否找到学生for (int i = 0; i < student->nums; i++) {if (student[i].id == *x) {printf("学号:%lld 姓名:%s 性别:%s 数学成绩:%d 英语成绩:%d\n", student[i].id, student[i].name, student[i].sex, student[i].math, student[i].english);found = 1;break;}}if (found == 1)printf("查找成功,学生信息如上\n");elseprintf("查找失败\n");
}void correct(Students* student, long long z) {int found = 0;for (int i = 0; i < student->nums; i++) {if (student[i].id == z) {found = 1;int math1, english2;printf("请修改数学:\n");scanf("%d", &math1);student[i].math = math1;printf("请修改英语:\n");scanf("%d", &english2);student[i].english = english2;break; // 找到匹配学生后终止循环}}if (!found) {printf("未找到学号为 %lld 的学生\n", z);}
}void insert(Students* student)
{int i = student->nums;scanf("%lld %s %s %d %d", &student[student->nums].id, student[student->nums].name, student[student->nums].sex, &student[student->nums].math, &student[student->nums].english);student->nums++;
}void delet(Students* student, long long k) {int found = 0;for (int i = 0; i < student->nums; i++) {if (student[i].id == k) {found = 1;for (int j = i; j < student->nums - 1; j++) {student[j] = student[j + 1];}break;}}if (found) {student->nums--;printf("学生 %lld 已删除\n", k);}else {printf("未找到学号为 %lld 的学生\n", k);}
}int main() {//输入学生人数int n = 0;printf("请输入学生人数\n");scanf("%d", &n);//输入学生数据Students student[MAXSIZE];instudent(student, n);//打印学生数据printstudent(student);//按学号查找学生printf("请输入要查找学生的学号\n");long long x = 0;scanf("%d", &x);sercharstudent(student, &x);//按学号对学生信息进行修改printf("请输入要修改学生学号:\n");long long z = 0;scanf("%d", &z);correct(student, z);//打印修改后的数据printf("修改后的信息为:\n");printstudent(student);//插入学生信息printf("请输入插入学生的信息:\n");insert(student);printf("插入后学生信息为:\n");printstudent(student);//删除学生信息long long k = 0;printf("请输入要删除学生的学号:\n");scanf("%lld", &k);delet(student, k);printf("删除后学生信息为:\n");printstudent(student);return 0;
}