#include
#include
#include
#include
using namespace std;
struct Child {
string name;
int age;
string gender;
string address;
float height;
float weight;
string parentName;
string parentPhone;
string remark;
};
void readFromFile(vector& children) {
ifstream inFile("children.txt");
if (!inFile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
string line;
while (getline(inFile, line)) {
Child child;
child.name = line;
inFile >> child.age >> child.gender >> child.address >> child.height >> child.weight >> child.parentName >> child.parentPhone;
getline(inFile, child.remark);
children.push_back(child);
}
inFile.close();
}
void saveToFile(const vector& children) {
ofstream outFile("children.txt");
if (!outFile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
for (const auto& child : children) {
outFile << child.name << endl;
outFile << child.age << " " << child.gender << " " << child.address << " " << child.height << " " << child.weight << " " << child.parentName << " " << child.parentPhone << " " << child.remark << endl;
}
outFile.close();
}
void addChild(vector& children) {
Child child;
cout << "请输入小朋友信息:" << endl;
cout << "姓名:";
cin >> child.name;
cout << "出生年月(岁数):";
cin >> child.age;
cout << "性别:";
cin >> child.gender;
cout << "住址:";
cin >> child.address;
cout << "身高(米):";
cin >> child.height;
cout << "体重(千克):";
cin >> child.weight;
cout << "父母联系人姓名:";
cin >> child.parentName;
cout << "联系电话:";
cin >> child.parentPhone;
cout << "备注:";
getline(cin, child.remark);
children.push_back(child);
cout << "添加成功!" << endl;
}
void deleteChild(vector& children) {
string name;
cout << "请输入要删除小朋友的姓名:";
cin >> name;
auto it = find_if(children.begin(), children.end(), [name](const Child& child){return child.name == name;});
if (it != children.end()) {
children.erase(it);
cout << "删除成功!" << endl;
} else {
cout << "未找到该小朋友!" << endl;
}
}
void queryParentInfo(const vector& children) {
string name;
cout << "请输入要查询小朋友的姓名:";
cin >> name;
auto it = find_if(children.begin(), children.end(), [name](const Child& child){return child.name == name;});
if (it != children.end()) {
cout << "父母联系人姓名:" << it->parentName << endl;
cout << "联系电话:" << it->parentPhone << endl;
} else {
cout << "未找到该小朋友!" << endl;
}
}
void queryAddress(const vector& children) {
string name;
cout << "请输入要查询小朋友的姓名:";
cin >> name;
auto it = find_if(children.begin(), children.end(), [name](const Child& child){return child.name == name;});
if (it != children.end()) {
cout << "住址:" << it->address << endl;
} else {
cout << "未找到该小朋友!" << endl;
}
}
void showChildren(const vector& children) {
if (children.empty()) {
cout << "班级还没有小朋友!" << endl;
return;
}
cout << "班级小朋友信息如下:" << endl;
for (const auto& child : children) {
cout << child.name << " " << child.age << " " << child.gender << " " << child.address << " " << child.height << " " << child.weight << " " << child.parentName << " " << child.parentPhone << " " << child.remark << endl;
}
}
void sortByHeight(vector& children) {
sort(children.begin(), children.end(), [](const Child& a, const Child& b){return a.height > b.height;});
cout << "按身高排序结果如下:" << endl;
showChildren(children);
}
void sortByWeight(vector& children) {
sort(children.begin(), children.end(), [](const Child& a, const Child& b){return a.weight > b.weight;});
cout << "按体重排序结果如下:" << endl;
showChildren(children);
}
void countByGender(const vector& children) {
int maleCount = count_if(children.begin(), children.end(), [](const Child& child){return child.gender == "男";});
int femaleCount = children.size() - maleCount;
cout << "班级中男生人数为:" << maleCount << ",女生人数为:" << femaleCount << ",男女比例为:" << maleCount << ":" << femaleCount << endl;
}
int main() {
vector children;
readFromFile(children);
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:
addChild(children);
saveToFile(children);
break;
case 2:
deleteChild(children);
saveToFile(children);
break;
case 3:
queryParentInfo(children);
break;
case 4:
queryAddress(children);
break;
case 5:
showChildren(children);
break;
case 6:
sortByHeight(children);
break;
case 7:
sortByWeight(children);
break;
case 8:
countByGender(children);
break;
case 9:
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
return 0;