QT实现学生信息管理,修改学生信息,C++实现
时间: 2023-07-30 13:08:01 浏览: 153
下面是一个简单的学生信息管理系统,你可以在QT中使用C++代码进行实现。
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
vector<Student> students;
void addStudent() {
Student student;
cout << "Enter name: ";
cin >> student.name;
cout << "Enter age: ";
cin >> student.age;
cout << "Enter GPA: ";
cin >> student.gpa;
students.push_back(student);
cout << "Student added!" << endl;
}
void displayStudents() {
if (students.empty()) {
cout << "No students to display" << endl;
return;
}
for (int i = 0; i < students.size(); i++) {
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "GPA: " << students[i].gpa << endl;
cout << "---------------------" << endl;
}
}
void updateStudent() {
if (students.empty()) {
cout << "No students to update" << endl;
return;
}
cout << "Enter student name to update: ";
string name;
cin >> name;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
cout << "Enter new name: ";
cin >> students[i].name;
cout << "Enter new age: ";
cin >> students[i].age;
cout << "Enter new GPA: ";
cin >> students[i].gpa;
cout << "Student updated!" << endl;
return;
}
}
cout << "Student not found" << endl;
}
int main() {
int choice;
do {
cout << "1. Add student" << endl;
cout << "2. Display students" << endl;
cout << "3. Update student" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
updateStudent();
break;
case 4:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 4);
return 0;
}
```
这是一个基本的控制台应用程序,它可以添加、显示和更新学生信息。你可以将其用作基础,使用QT中的UI组件以更友好和易于使用的方式实现相同的功能。
阅读全文