1.建立一个链表将实训12第2题定义的 学生数据存入链表,并读出显示出来。(10分) 数据为: { {"2020B200301","刘滨","男",18,{2020,9,20},70,85}, {"2020B200302","阚美娟","女",17,{2020,9,20},87,95}, {"2020B200303","胡大勇","男",17,{2020,9,20},69,80}, {"2020B200305","黎丽","女",18,{2020,9,20},68,88}, {"2020B200309","金鑫","男",19,{2019,9,1},90,85} }.打印链表中的成绩1和成绩2都大于等于70的记录,并计算各自的平均成绩后输出:
时间: 2023-07-19 15:08:21 浏览: 91
用链表实现学生成绩表
好的,我理解了你的要求。以下是实现链表存储学生数据并输出符合要求的记录的示例代码,你可以参考:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string id;
string name;
string gender;
int age;
int birthday[3];
int score1;
int score2;
Student *next;
};
// 求平均分函数
double calculateAverage(int score1, int score2) {
return (score1 + score2) / 2.0;
}
int main() {
// 存储学生数据
Student stuArr[] = {
{"2020B200301","刘滨","男",18,{2020,9,20},70,85,NULL},
{"2020B200302","阚美娟","女",17,{2020,9,20},87,95,NULL},
{"2020B200303","胡大勇","男",17,{2020,9,20},69,80,NULL},
{"2020B200305","黎丽","女",18,{2020,9,20},68,88,NULL},
{"2020B200309","金鑫","男",19,{2019,9,1},90,85,NULL}
};
// 建立链表
Student *head = new Student;
head->next = NULL;
Student *tail = head;
for(int i = 0; i < 5; i++) {
Student *newStu = new Student;
newStu->id = stuArr[i].id;
newStu->name = stuArr[i].name;
newStu->gender = stuArr[i].gender;
newStu->age = stuArr[i].age;
for(int j = 0; j < 3; j++) {
newStu->birthday[j] = stuArr[i].birthday[j];
}
newStu->score1 = stuArr[i].score1;
newStu->score2 = stuArr[i].score2;
newStu->next = NULL;
tail->next = newStu;
tail = newStu;
}
// 输出符合要求的记录并计算平均分
Student *p = head->next;
while(p != NULL) {
if(p->score1 >= 70 && p->score2 >= 70) {
double average = calculateAverage(p->score1, p->score2);
cout << p->id << " " << p->name << " " << p->score1 << " " << p->score2 << " " << average << endl;
}
p = p->next;
}
return 0;
}
```
输出结果为:
```
2020B200301 刘滨 70 85 77.5
2020B200302 阚美娟 87 95 91
2020B200309 金鑫 90 85 87.5
```
阅读全文