定义一个描述学生基本信息的结构体类型,包括姓名,学号,C语言课程成绩。并定义一个结构体数组。输入若干学生的基本信息;编写函数print(),输出全体学生记录信息;编写函数search(), 按学号检索指定学生信息并返回, 由主函数打印到屏幕上;编写函数modify(), 修改指定学号的学生的成绩;编写函数sort(), 按照学生学号或者成绩对结构体数组排序;编写函数insert(), 插入一条新的学生信息;编写函数delete(), 删除指定学号的学生信息;再改为从文件加载记录数据,用户修改(增加、删除、修改)后数据保存到文件
时间: 2024-02-25 18:56:35 浏览: 89
好的,以下是定义一个描述学生基本信息的结构体类型,包括姓名,学号,C语言课程成绩的代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student{
string name;
string id;
int score;
};
const int MAX = 100;
Student stuArray[MAX];
int count = 0;
void print(){
for(int i = 0; i < count; i++){
cout << "姓名:" << stuArray[i].name << endl;
cout << "学号:" << stuArray[i].id << endl;
cout << "成绩:" << stuArray[i].score << endl;
cout << endl;
}
}
Student* search(string id){
for(int i = 0; i < count; i++){
if(stuArray[i].id == id){
return &stuArray[i];
}
}
return NULL;
}
void modify(string id, int score){
Student* pStu = search(id);
if(pStu){
pStu->score = score;
}
}
void sort(string key){
for(int i = 0; i < count - 1; i++){
for(int j = 0; j < count - i - 1; j++){
if(key == "id"){
if(stuArray[j].id > stuArray[j + 1].id){
swap(stuArray[j], stuArray[j + 1]);
}
}else if(key == "score"){
if(stuArray[j].score > stuArray[j + 1].score){
swap(stuArray[j], stuArray[j + 1]);
}
}
}
}
}
void insert(Student stu){
if(count < MAX){
stuArray[count++] = stu;
}
}
void deleteStu(string id){
int pos = -1;
for(int i = 0; i < count; i++){
if(stuArray[i].id == id){
pos = i;
break;
}
}
if(pos != -1){
for(int i = pos; i < count - 1; i++){
stuArray[i] = stuArray[i + 1];
}
count--;
}
}
void loadData(){
ifstream fin("data.txt");
if(!fin){
cout << "打开文件失败!" << endl;
return;
}
string name, id;
int score;
while(fin >> name >> id >> score){
Student stu = {name, id, score};
insert(stu);
}
fin.close();
}
void saveData(){
ofstream fout("data.txt");
if(!fout){
cout << "打开文件失败!" << endl;
return;
}
for(int i = 0; i < count; i++){
fout << stuArray[i].name << " " << stuArray[i].id << " " << stuArray[i].score << endl;
}
fout.close();
}
int main(){
loadData();
print();
Student stu1 = {"Tom", "1001", 90};
insert(stu1);
print();
Student* pStu = search("1002");
if(pStu){
cout << "找到了学生:" << endl;
cout << "姓名:" << pStu->name << endl;
cout << "学号:" << pStu->id << endl;
cout << "成绩:" << pStu->score << endl;
}else{
cout << "没有找到学生!" << endl;
}
modify("1001", 95);
print();
sort("score");
print();
deleteStu("1003");
print();
saveData();
return 0;
}
```
以上代码实现了基本的数据操作,包括:输出全体学生记录信息、按学号检索指定学生信息、修改指定学号的学生的成绩、按照学生学号或者成绩对结构体数组排序、插入一条新的学生信息、删除指定学号的学生信息。同时,还实现了从文件加载记录数据、用户修改(增加、删除、修改)后数据保存到文件的功能。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)