c++。以高校为基本单位进行信息存放,高校信息包括:编号、学校名称、地址、省份、建校时间等。 功能要求: 以文件形式保存相关信息,可以读取默认文件中的信息进行查询等操作。能够对高校信息进行维护:新建、修改、删除操作:.按照高校任意信息进行查询:
时间: 2023-11-28 08:50:14 浏览: 79
存储学校学生信息C++编写
4星 · 用户满意度95%
下面是一个简单的 C++ 程序,实现了上述要求:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 定义高校信息结构体
struct School {
int id; // 编号
string name; // 学校名称
string address; // 地址
string province;// 省份
int year; // 建校时间
// 构造函数
School(int i, string n, string a, string p, int y) {
id = i;
name = n;
address = a;
province = p;
year = y;
}
};
// 文件路径
const string filepath = "schools.txt";
// 读取高校信息
vector<School> readSchools() {
vector<School> schools;
ifstream fin(filepath);
if (fin) {
int id, year;
string name, address, province;
while (fin >> id >> name >> address >> province >> year) {
schools.push_back(School(id, name, address, province, year));
}
fin.close();
}
return schools;
}
// 保存高校信息
void saveSchools(vector<School> schools) {
ofstream fout(filepath);
if (fout) {
for (School school : schools) {
fout << school.id << " " << school.name << " " << school.address << " " << school.province << " " << school.year << endl;
}
fout.close();
}
}
// 新建高校信息
void addSchool(vector<School>& schools) {
int id, year;
string name, address, province;
cout << "请输入高校编号:";
cin >> id;
cout << "请输入高校名称:";
cin >> name;
cout << "请输入高校地址:";
cin >> address;
cout << "请输入高校所在省份:";
cin >> province;
cout << "请输入高校建校年份:";
cin >> year;
schools.push_back(School(id, name, address, province, year));
saveSchools(schools);
}
// 修改高校信息
void editSchool(vector<School>& schools) {
int id, year;
string name, address, province;
cout << "请输入需要修改的高校编号:";
cin >> id;
for (int i = 0; i < schools.size(); i++) {
if (schools[i].id == id) {
cout << "请输入新的高校名称(原值:" << schools[i].name << "):";
cin >> name;
cout << "请输入新的高校地址(原值:" << schools[i].address << "):";
cin >> address;
cout << "请输入新的高校所在省份(原值:" << schools[i].province << "):";
cin >> province;
cout << "请输入新的高校建校年份(原值:" << schools[i].year << "):";
cin >> year;
schools[i] = School(id, name, address, province, year);
saveSchools(schools);
return;
}
}
cout << "找不到编号为 " << id << " 的高校" << endl;
}
// 删除高校信息
void deleteSchool(vector<School>& schools) {
int id;
cout << "请输入需要删除的高校编号:";
cin >> id;
for (int i = 0; i < schools.size(); i++) {
if (schools[i].id == id) {
schools.erase(schools.begin() + i);
saveSchools(schools);
return;
}
}
cout << "找不到编号为 " << id << " 的高校" << endl;
}
// 按照编号查询高校信息
void queryById(vector<School>& schools) {
int id;
cout << "请输入需要查询的高校编号:";
cin >> id;
for (School school : schools) {
if (school.id == id) {
cout << "编号:" << school.id << endl;
cout << "名称:" << school.name << endl;
cout << "地址:" << school.address << endl;
cout << "省份:" << school.province << endl;
cout << "建校年份:" << school.year << endl;
return;
}
}
cout << "找不到编号为 " << id << " 的高校" << endl;
}
// 按照名称查询高校信息
void queryByName(vector<School>& schools) {
string name;
cout << "请输入需要查询的高校名称:";
cin >> name;
for (School school : schools) {
if (school.name == name) {
cout << "编号:" << school.id << endl;
cout << "名称:" << school.name << endl;
cout << "地址:" << school.address << endl;
cout << "省份:" << school.province << endl;
cout << "建校年份:" << school.year << endl;
}
}
}
// 按照省份查询高校信息
void queryByProvince(vector<School>& schools) {
string province;
cout << "请输入需要查询的高校省份:";
cin >> province;
for (School school : schools) {
if (school.province == province) {
cout << "编号:" << school.id << endl;
cout << "名称:" << school.name << endl;
cout << "地址:" << school.address << endl;
cout << "省份:" << school.province << endl;
cout << "建校年份:" << school.year << endl;
}
}
}
// 按照建校时间查询高校信息
void queryByYear(vector<School>& schools) {
int year;
cout << "请输入需要查询的高校建校年份:";
cin >> year;
for (School school : schools) {
if (school.year == year) {
cout << "编号:" << school.id << endl;
cout << "名称:" << school.name << endl;
cout << "地址:" << school.address << endl;
cout << "省份:" << school.province << endl;
cout << "建校年份:" << school.year << endl;
}
}
}
int main() {
vector<School> schools = readSchools();
while (true) {
int choice;
cout << "请选择操作:1.新建 2.修改 3.删除 4.按编号查询 5.按名称查询 6.按省份查询 7.按建校时间查询 0.退出:";
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
addSchool(schools);
break;
case 2:
editSchool(schools);
break;
case 3:
deleteSchool(schools);
break;
case 4:
queryById(schools);
break;
case 5:
queryByName(schools);
break;
case 6:
queryByProvince(schools);
break;
case 7:
queryByYear(schools);
break;
default:
cout << "无效操作" << endl;
break;
}
}
return 0;
}
```
这个程序使用了一个结构体 `School` 来表示高校信息,定义了一些操作函数来实现新建、修改、删除、查询等功能。在 `main` 函数中,程序通过循环来不断接受用户的输入,根据用户选择的操作调用相应的操作函数。同时,程序在启动时会读取文件中保存的高校信息,退出时会将修改后的高校信息保存到文件中。
阅读全文