设计一个完整的C++程序
设计一个完整的C++程序需要明确目的、需求和设计思路。以下是一个设计和实现一个简单的学生信息管理系统的完整示例。该程序可以执行添加学生、显示所有学生、查找学生和退出程序等功能。
需求分析
- 添加学生信息:包括学号、姓名和年龄。
- 显示所有学生信息。
- 根据学号查找学生信息。
- 退出程序。
设计思路
- 使用一个
Student
类来封装学生的信息。 - 使用一个
vector
来存储多个学生信息。 - 提供一个菜单驱动的用户界面,用户可以选择不同的操作。
实现步骤
步骤一:定义Student
类
cpp#include <iostream>
#include <vector>
#include <string>
class Student {
private:
int id;
std::string name;
int age;
public:
Student(int id, std::string name, int age) : id(id), name(name), age(age) {}
int getId() const {
return id;
}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
void display() const {
std::cout << "ID: " << id << ", Name: " << name << ", Age: " << age << std::endl;
}
};
步骤二:定义管理学生信息的函数
cppvoid addStudent(std::vector<Student>& students) {
int id;
std::string name;
int age;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter student name: ";
std::cin.ignore(); // 清除输入缓冲区
std::getline(std::cin, name);
std::cout << "Enter student age: ";
std::cin >> age;
Student newStudent(id, name, age);
students.push_back(newStudent);
std::cout << "Student added successfully!" << std::endl;
}
void displayAllStudents(const std::vector<Student>& students) {
if (students.empty()) {
std::cout << "No students found!" << std::endl;
} else {
for (const auto& student : students) {
student.display();
}
}
}
void findStudentById(const std::vector<Student>& students, int id) {
for (const auto& student : students) {
if (student.getId() == id) {
student.display();
return;
}
}
std::cout << "Student with ID " << id << " not found." << std::endl;
}
步骤三:实现主函数及菜单驱动界面
cppint main() {
std::vector<Student> students;
int choice;
while (true) {
std::cout << "\nStudent Management System\n";
std::cout << "1. Add Student\n";
std::cout << "2. Display All Students\n";
std::cout << "3. Find Student by ID\n";
std::cout << "4. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
displayAllStudents(students);
break;
case 3: {
int id;
std::cout << "Enter student ID: ";
std::cin >> id;
findStudentById(students, id);
break;
}
case 4:
std::cout << "Exiting the program." << std::endl;
return 0;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
}
完整代码
cpp#include <iostream>
#include <vector>
#include <string>
class Student {
private:
int id;
std::string name;
int age;
public:
Student(int id, std::string name, int age) : id(id), name(name), age(age) {}
int getId() const {
return id;
}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
void display() const {
std::cout << "ID: " << id << ", Name: " << name << ", Age: " << age << std::endl;
}
};
void addStudent(std::vector<Student>& students) {
int id;
std::string name;
int age;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter student name: ";
std::cin.ignore(); // 清除输入缓冲区
std::getline(std::cin, name);
std::cout << "Enter student age: ";
std::cin >> age;
Student newStudent(id, name, age);
students.push_back(newStudent);
std::cout << "Student added successfully!" << std::endl;
}
void displayAllStudents(const std::vector<Student>& students) {
if (students.empty()) {
std::cout << "No students found!" << std::endl;
} else {
for (const auto& student : students) {
student.display();
}
}
}
void findStudentById(const std::vector<Student>& students, int id) {
for (const auto& student : students) {
if (student.getId() == id) {
student.display();
return;
}
}
std::cout << "Student with ID " << id << " not found." << std::endl;
}
int main() {
std::vector<Student> students;
int choice;
while (true) {
std::cout << "\nStudent Management System\n";
std::cout << "1. Add Student\n";
std::cout << "2. Display All Students\n";
std::cout << "3. Find Student by ID\n";
std::cout << "4. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
displayAllStudents(students);
break;
case 3: {
int id;
std::cout << "Enter student ID: ";
std::cin >> id;
findStudentById(students, id);
break;
}
case 4:
std::cout << "Exiting the program." << std::endl;
return 0;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
}
总结
这个学生信息管理系统使用C++编写,展示了如何创建类、使用向量存储数据,以及实现一个简单的菜单驱动界面来添加、显示和查找学生信息。
关键字
C++, 学生信息管理系统, Student
类, vector
, 菜单驱动界面, 添加学生, 显示学生, 查找学生, 输入输出