(1)为学生数据创建一个栈,学生数据包括学号,姓名,专业,英语成绩(2)班级有m名学生,请将他们的数据入栈(3)请找到第三位学生的数据,输出。不破坏栈中数据(4)第九位和第八位同学离开班级,请删除他们的数据(5)请按照英语成绩从高到低排序C语言代码实现
时间: 2023-04-11 16:01:10 浏览: 87
抱歉,我可以回答你的问题。以下是C语言代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
int id;
char name[20];
char major[20];
float english_score;
} Student;
typedef struct {
Student data[MAX_SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, Student stu) {
if (is_full(s)) {
printf("Stack is full.\n");
return;
}
s->top++;
s->data[s->top] = stu;
}
Student pop(Stack *s) {
if (is_empty(s)) {
printf("Stack is empty.\n");
exit(1);
}
Student stu = s->data[s->top];
s->top--;
return stu;
}
Student peek(Stack *s) {
if (is_empty(s)) {
printf("Stack is empty.\n");
exit(1);
}
return s->data[s->top];
}
int main() {
Stack s;
init(&s);
int m = 0;
printf("Please enter the number of students: ");
scanf("%d", &m);
for (int i = 0; i < m; i++) {
Student stu;
printf("Please enter the information of student %d:\n", i + 1);
printf("ID: ");
scanf("%d", &stu.id);
printf("Name: ");
scanf("%s", stu.name);
printf("Major: ");
scanf("%s", stu.major);
printf("English score: ");
scanf("%f", &stu.english_score);
push(&s, stu);
}
Student stu3 = peek(&s);
for (int i = 0; i < 2; i++) {
stu3 = pop(&s);
}
printf("The information of the third student is:\n");
printf("ID: %d\n", stu3.id);
printf("Name: %s\n", stu3.name);
printf("Major: %s\n", stu3.major);
printf("English score: %.2f\n", stu3.english_score);
push(&s, stu3);
Student stu8 = pop(&s);
Student stu9 = pop(&s);
printf("The information of the eighth and ninth students has been removed.\n");
int n = s.top + 1;
Student temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (s.data[i].english_score < s.data[j].english_score) {
temp = s.data[i];
s.data[i] = s.data[j];
s.data[j] = temp;
}
}
}
printf("The information of all students sorted by English score from high to low is:\n");
for (int i = 0; i < n; i++) {
printf("ID: %d\n", s.data[i].id);
printf("Name: %s\n", s.data[i].name);
printf("Major: %s\n", s.data[i].major);
printf("English score: %.2f\n", s.data[i].english_score);
}
return 0;
}