#include<iostream>
#include<cstdio>
using namespace std;
typedef struct student {
int num;
char name[10];
char sex[10];
int English;
int computer;
int math;
int sum;
double average;
}Student;
typedef struct NODE {
Student data;
struct NODE* next;
}Node;
void InputStu(Node*& stu, int n) {
Node* p;
for (int i = 0; i < n; i++) {
p = (Node*)malloc(sizeof(Node));
scanf("%d%s%s%d%d%d", &p->data.num, &p->data.name, &p->data.sex, &p->data.English, &p->data.computer, &p->data.math);
//cin >> p->data.num >> p->data.name >> p->data.sex >> p->data.English >> p->data.computer >> p->data.math;
p->data.sum = p->data.computer + p->data.English + p->data.math;
p->data.average = p->data.sum / 3.0;
p->next = stu;
stu = p;
}
return;
}
void OutputStu(Node*& stu) {
printf("序号\t学号\t姓名\t性别\t英语\t计算机\t高数\t总分\t平均分\n");
Node* p;
p = stu;
int i = 1;
while (p != NULL) {
printf("%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", i++, p->data.num, p->data.name, p->data.sex, p->data.English, p->data.computer, p->data.math, p->data.sum, p->data.average);
p = p->next;
}
return;
}
void SearchStu(Node*& stu) {
int num;
printf("请输入待查找的学号:");
scanf("%d", &num);
bool flag = true;
Node* p;
p = stu;
int i = 1;
while (p != NULL) {
if (p->data.num == num) {
printf("查找成功\n");
printf("%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", i, p->data.num, p->data.name, p->data.sex, p->data.English, p->data.computer, p->data.math, p->data.sum, p->data.average);
flag = false;
break;
}
i++;
p = p->next;
}
if (flag) printf("该学生的信息不存在,查找失败\n");
}
void UpdateStu(Node*& stu) {
printf("请输入待修改的学号:");
int num;
scanf("%d", &num);
bool flag = true;
Node* p;
p = stu;
int i = 1;
while (p != NULL) {
if (p->data.num == num) {
printf("%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", i, p->data.num, p->data.name, p->data.sex, p->data.English, p->data.computer, p->data.math, p->data.sum, p->data.average);
printf("\t1.姓名\n\t2.性别\n\t3.英语\n\t4.计算机\n\t5.高数\n");
int x;
printf("请选择需要修改的信息(1-5):");
scanf("%d", &x);
printf("请输入修改后的信息:");
if (x == 1) {
scanf("%s", &p->data.name);
}
else if (x == 2) {
scanf("%s", &p->data.sex);
}
else if (x == 3) {
scanf("%d", &p->data.English);
}
else if (x == 4) {
scanf("%d", &p->data.computer);
}
else {
scanf("%d", &p->data.math);
}
if (x == 3 || x == 4 || x == 5) {
p->data.sum = p->data.computer + p->data.English + p->data.math;
p->data.average = p->data.sum / 3.0;
}
flag = false;
printf("%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", i++, p->data.num, p->data.name, p->data.sex, p->data.English, p->data.computer, p->data.math, p->data.sum, p->data.average);
break;
}
i++;
p = p->next;
}
if (flag) printf("该学生的信息不存在,查找失败\n");
}
void AppendStu(Node*& stu) {
printf("请输入学生信息:");
Node* p;
Node* a;
a = (Node*)malloc(sizeof(Node));
scanf("%d%s%s%d%d%d", &a->data.num, &a->data.name, &a->data.sex, &a->data.English, &a->data.computer, &a->data.math);
a->data.sum = a->data.computer + a->data.English + a->data.math;
a->data.average = a->data.sum / 3.0;
a->next = NULL;
p = stu;
while (p != NULL) {
if (p->next == NULL) {
p->next = a;
break;
}
p = p->next;
}
printf("更新成功\n");
}
int main() {
Node* s = NULL;
int n;
printf("请输入学生记录个数n=");
scanf("%d", &n);
printf("请输入学生信息:\n");
InputStu(s, n);
OutputStu(s);
while (true) {
printf("%*s%s\n", 20, "", "学生信息管理系统");
printf("%*s%s\n", 10, "", "1.显示学生信息");
printf("%*s%s\n", 10, "", "2.查询学生信息");
printf("%*s%s\n", 10, "", "3.修改学生信息");
printf("%*s%s\n", 10, "", "4.添加学生信息");
printf("%*s%s", 10, "", "请选择(1-4):");
int x;
scanf("%d", &x);
if (x == 1) OutputStu(s);
else if (x == 2) SearchStu(s);
else if (x == 3) UpdateStu(s);
else if (x == 4)AppendStu(s);
else break;
}
free(s);
return 0;
}
第二次试验
最新推荐文章于 2022-04-17 12:01:29 发布