|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一个学生管理系统
输入 1 张三 60 90 50
#include<stdio.h>
#include<stdlib.h>
typedef struct Student {
int num;
char name[10];
float chinese;
float math;
float english;
};
typedef struct node {
struct Student data;
struct node* next;
}StudentList;
StudentList* InitList() { // 初始化单链表
StudentList* L;
L = (StudentList*)malloc(sizeof(StudentList));
L->next = NULL;
}
int InsElem(StudentList* L, struct Student data, int i) { // 插入元素
int j = 0;
StudentList* p = L, * s;
while (p != NULL && j < i - 1) {
j++;
p = p->next;
}
if (p == NULL) {
return 0;
}
else {
s = (StudentList*)malloc(sizeof(StudentList));
s->data = data;
s->next = p->next;
p->next = s;
return 1;
}
}
void DispList(StudentList *L) { // 输出元素
StudentList* p = L->next;
while (p) {
float avg = (p->data.chinese + p->data.math + p->data.english) / 3;
printf("%d\t%s\t%f\n", p->data.num, p->data.name, avg);
p = p->next;
}
}
void main() {
StudentList* L;
L = InitList();
struct Student info;
printf("请输入学生的学号,姓名,语文成绩,数学成绩,英语成绩\n");
int i;
scanf("%d, %s, %f, %f, %f\n", &info.num, &info.name, &info.chinese, &info.math, &info.english);
InsElem(L, info, 1);
DispList(L);
}
#include<stdio.h>
#include<stdlib.h>
//typedef struct Student {
struct Student {
int num;
char name[10];
float chinese;
float math;
float english;
};
typedef struct node {
struct Student data;
struct node *next;
} StudentList;
StudentList* InitList() { // 初始化单链表
StudentList* L;
//L = (StudentList*)malloc(sizeof(StudentList));
L = malloc(sizeof(StudentList));
L->next = NULL;
return L;
}
int InsElem(StudentList* L, struct Student data, int i) { // 插入元素
int j = 0;
StudentList *p = L, *s;
while (p != NULL && j < i - 1) {
j++;
p = p->next;
}
if (p == NULL) {
return 0;
}
else {
//s = (StudentList*)malloc(sizeof(StudentList));
s = malloc(sizeof(StudentList));
s->data = data;
s->next = p->next;
p->next = s;
return 1;
}
}
void DispList(StudentList *L) { // 输出元素
StudentList* p = L->next;
while (p) {
float avg = (p->data.chinese + p->data.math + p->data.english) / 3;
printf("%d\t%s\t%f\n", p->data.num, p->data.name, avg);
p = p->next;
}
}
//void main() {
int main(void) {
StudentList* L;
L = InitList();
struct Student info;
printf("请输入学生的学号,姓名,语文成绩,数学成绩,英语成绩\n");
//int i;
//scanf("%d, %s, %f, %f, %f\n", &info.num, &info.name, &info.chinese, &info.math, &info.english);
scanf("%d%s%f%f%f", &info.num, info.name, &info.chinese, &info.math, &info.english);
InsElem(L, info, 1);
DispList(L);
return 0;
}
|
|