以高校为基本单位进行信息存放,高校信息包括:编号、学校名称、地址、省份、建校时间等。 功能要求: 1)能够对高校信息进行维护:新建、修改、删除操作: 2)按照高校任意信息进行查询: 3)以文件形式保存相关信息,可以读取默认文件中的信息进行查询等操作。c++
时间: 2023-11-28 20:48:40 浏览: 73
以下是一个简单的实现,主要使用了结构体和文件操作。具体代码如下:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// 定义高校信息结构体
struct College {
int id; // 编号
char name[50]; // 学校名称
char address[100]; // 地址
char province[20]; // 省份
int buildYear; // 建校时间
};
// 新建高校信息
void addCollege(College colleges[], int &size) {
College college;
cout << "请输入学校编号:";
cin >> college.id;
cout << "请输入学校名称:";
cin >> college.name;
cout << "请输入学校地址:";
cin >> college.address;
cout << "请输入学校所在省份:";
cin >> college.province;
cout << "请输入学校建校时间:";
cin >> college.buildYear;
// 将新建的高校信息添加到数组中
colleges[size++] = college;
cout << "添加成功!" << endl;
}
// 修改高校信息
void modifyCollege(College colleges[], int size) {
int id;
cout << "请输入要修改的学校编号:";
cin >> id;
// 查找要修改的高校信息
int index = -1;
for (int i = 0; i < size; i++) {
if (colleges[i].id == id) {
index = i;
break;
}
}
// 如果找到了要修改的高校信息
if (index != -1) {
College college;
cout << "请输入学校编号:";
cin >> college.id;
cout << "请输入学校名称:";
cin >> college.name;
cout << "请输入学校地址:";
cin >> college.address;
cout << "请输入学校所在省份:";
cin >> college.province;
cout << "请输入学校建校时间:";
cin >> college.buildYear;
// 修改高校信息
colleges[index] = college;
cout << "修改成功!" << endl;
} else {
cout << "未找到要修改的学校信息!" << endl;
}
}
// 删除高校信息
void deleteCollege(College colleges[], int &size) {
int id;
cout << "请输入要删除的学校编号:";
cin >> id;
// 查找要删除的高校信息
int index = -1;
for (int i = 0; i < size; i++) {
if (colleges[i].id == id) {
index = i;
break;
}
}
// 如果找到了要删除的高校信息
if (index != -1) {
// 将该高校信息从数组中删除
for (int i = index; i < size - 1; i++) {
colleges[i] = colleges[i + 1];
}
size--;
cout << "删除成功!" << endl;
} else {
cout << "未找到要删除的学校信息!" << endl;
}
}
// 按编号查询高校信息
void queryById(College colleges[], int size) {
int id;
cout << "请输入要查询的学校编号:";
cin >> id;
// 查找要查询的高校信息
int index = -1;
for (int i = 0; i < size; i++) {
if (colleges[i].id == id) {
index = i;
break;
}
}
// 输出查询结果
if (index != -1) {
cout << "编号\t名称\t地址\t省份\t建校时间" << endl;
cout << colleges[index].id << "\t" << colleges[index].name << "\t" << colleges[index].address << "\t" << colleges[index].province << "\t" << colleges[index].buildYear << endl;
} else {
cout << "未找到该学校信息!" << endl;
}
}
// 按名称查询高校信息
void queryByName(College colleges[], int size) {
char name[50];
cout << "请输入要查询的学校名称:";
cin >> name;
// 输出查询结果
cout << "编号\t名称\t地址\t省份\t建校时间" << endl;
for (int i = 0; i < size; i++) {
if (strcmp(colleges[i].name, name) == 0) {
cout << colleges[i].id << "\t" << colleges[i].name << "\t" << colleges[i].address << "\t" << colleges[i].province << "\t" << colleges[i].buildYear << endl;
}
}
}
// 按省份查询高校信息
void queryByProvince(College colleges[], int size) {
char province[20];
cout << "请输入要查询的省份:";
cin >> province;
// 输出查询结果
cout << "编号\t名称\t地址\t省份\t建校时间" << endl;
for (int i = 0; i < size; i++) {
if (strcmp(colleges[i].province, province) == 0) {
cout << colleges[i].id << "\t" << colleges[i].name << "\t" << colleges[i].address << "\t" << colleges[i].province << "\t" << colleges[i].buildYear << endl;
}
}
}
// 按建校时间查询高校信息
void queryByBuildYear(College colleges[], int size) {
int buildYear;
cout << "请输入要查询的建校时间:";
cin >> buildYear;
// 输出查询结果
cout << "编号\t名称\t地址\t省份\t建校时间" << endl;
for (int i = 0; i < size; i++) {
if (colleges[i].buildYear == buildYear) {
cout << colleges[i].id << "\t" << colleges[i].name << "\t" << colleges[i].address << "\t" << colleges[i].province << "\t" << colleges[i].buildYear << endl;
}
}
}
// 将高校信息保存到文件中
void saveToFile(College colleges[], int size) {
ofstream outFile("colleges.txt");
for (int i = 0; i < size; i++) {
outFile << colleges[i].id << "," << colleges[i].name << "," << colleges[i].address << "," << colleges[i].province << "," << colleges[i].buildYear << endl;
}
outFile.close();
}
// 从文件中读取高校信息
int readFromFile(College colleges[]) {
ifstream inFile("colleges.txt");
if (!inFile) {
return 0;
}
int size = 0;
while (!inFile.eof()) {
College college;
inFile >> college.id;
inFile.ignore(); // 忽略逗号
inFile.getline(college.name, 50, ',');
inFile.getline(college.address, 100, ',');
inFile.getline(college.province, 20, ',');
inFile >> college.buildYear;
colleges[size++] = college;
}
inFile.close();
return size;
}
int main() {
College colleges[100];
int size = readFromFile(colleges);
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 新建高校信息" << endl;
cout << "2. 修改高校信息" << endl;
cout << "3. 删除高校信息" << endl;
cout << "4. 按编号查询高校信息" << endl;
cout << "5. 按名称查询高校信息" << endl;
cout << "6. 按省份查询高校信息" << endl;
cout << "7. 按建校时间查询高校信息" << endl;
cout << "8. 保存高校信息到文件" << endl;
cout << "9. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addCollege(colleges, size);
break;
case 2:
modifyCollege(colleges, size);
break;
case 3:
deleteCollege(colleges, size);
break;
case 4:
queryById(colleges, size);
break;
case 5:
queryByName(colleges, size);
break;
case 6:
queryByProvince(colleges, size);
break;
case 7:
queryByBuildYear(colleges, size);
break;
case 8:
saveToFile(colleges, size);
cout << "保存成功!" << endl;
break;
case 9:
return 0;
default:
cout << "输入有误,请重新选择!" << endl;
break;
}
}
return 0;
}
```
需要注意的是,程序中将高校信息保存到了名为“colleges.txt”的文本文件中,每行记录使用逗号分隔不同的字段。在读取文件时,程序会将每行记录按照逗号进行分割,并将分割后的结果存储到对应的结构体字段中。
阅读全文