C++汽车信息搜索程序开发求助:实现文件搜索功能
解决C++文本文件汽车信息搜索问题(无容器实现)
先优化派生类构造函数
你的派生类构造函数里重复给基类成员赋值是多余的,因为初始化列表已经调用基类构造完成了初始化,只需要初始化派生类自己的成员即可:
修改Sedans.cpp构造函数:
Sedans::Sedans(string make, string model, double price, int miles, bool hybrid) : Cars(make, model, price, miles), hybrid(hybrid) {}
修改Trucks.cpp构造函数:
Trucks::Trucks(string make, string model, double price, int miles, string engine) : Cars(make, model, price, miles), engine(engine) {}
实现文件搜索逻辑
因为不能用vector等容器,我们可以每次用户选择搜索选项时,重新逐行读取文件,解析成对应汽车对象后判断是否匹配搜索条件,匹配则输出信息。核心步骤:
- 每次搜索前重置文件指针到开头(避免之前读取后指针停在文件末尾)
- 逐行解析文件字段,创建
Sedans/Trucks对象 - 根据用户选择的选项,判断对象是否符合搜索条件
- 匹配则调用
displayInfo()输出
修改后的完整main.cpp代码
#include <iostream> #include <fstream> #include <string> #include <cmath> // 用于浮点数比较的abs函数 // 需要包含类的头文件 #include "Cars.h" #include "Sedans.h" #include "Trucks.h" using namespace std; int main() { ifstream sedansFile; sedansFile.open("sedans-1.txt"); ifstream trucksFile; trucksFile.open("trucks-1.txt"); // 检查文件是否成功打开 if (!sedansFile.is_open()) { cout << "Error: 无法打开sedans-1.txt文件" << endl; return 1; } if (!trucksFile.is_open()) { cout << "Error: 无法打开trucks-1.txt文件" << endl; return 1; } int option; string searchText; bool exit = false; cout << "**********SEARCH MENU***********" << endl; cout << "Search Make..............Press 1" << endl; cout << "Search Model.............Press 2" << endl; cout << "Search Price.............Press 3" << endl; cout << "Search Miles.............Press 4" << endl; cout << "Exit.....................Press 5" << endl; while (!exit) { cout << "\nSelect Option: "; cin >> option; if (option == 5) { exit = true; continue; } // 验证选项合法性 if (option < 1 || option > 4) { cout << "无效选项,请重新选择!" << endl; continue; } cout << "Enter Search Text: "; cin.ignore(); getline(cin, searchText); cout << endl; // 搜索Sedans文件 sedansFile.clear(); // 清除文件状态标志 sedansFile.seekg(0, ios::beg); // 重置文件指针到开头 string make, model, hybridStr; double price; int miles; while (sedansFile >> make >> model >> price >> miles >> hybridStr) { bool isHybrid = (hybridStr == "yes"); Sedans sedan(make, model, price, miles, isHybrid); bool match = false; switch(option) { case 1: match = (sedan.getMake() == searchText); break; case 2: match = (sedan.getModel() == searchText); break; case 3: // 字符串转浮点数,用微小误差判断相等(避免浮点数精度问题) double searchPrice = stod(searchText); match = (abs(sedan.getPrice() - searchPrice) < 0.001); break; case 4: // 字符串转整数 int searchMiles = stoi(searchText); match = (sedan.getMiles() == searchMiles); break; } if (match) { sedan.displayInfo(); cout << endl; } } // 搜索Trucks文件 trucksFile.clear(); trucksFile.seekg(0, ios::beg); string truckEngine; while (trucksFile >> make >> model >> price >> miles >> truckEngine) { Trucks truck(make, model, price, miles, truckEngine); bool match = false; switch(option) { case 1: match = (truck.getMake() == searchText); break; case 2: match = (truck.getModel() == searchText); break; case 3: double searchPrice = stod(searchText); match = (abs(truck.getPrice() - searchPrice) < 0.001); break; case 4: int searchMiles = stoi(searchText); match = (truck.getMiles() == searchMiles); break; } if (match) { truck.displayInfo(); cout << endl; } } } // 关闭文件 sedansFile.close(); trucksFile.close(); return 0; }
关键细节说明
- 文件指针重置:每次搜索前调用
clear()和seekg(0, ios::beg),确保每次都从文件开头读取,避免之前的读取导致指针停在末尾。 - 浮点数比较:用
abs(price - searchPrice) < 0.001判断价格相等,因为浮点数直接用==可能因精度问题出错。 - 输入合法性检查:添加了选项范围判断,避免用户输入无效选项导致逻辑错误。
- 文件关闭:程序结束前关闭打开的文件,养成良好的资源管理习惯。
内容的提问来源于stack exchange,提问作者user23636194
相关产品推荐
相关产品推荐

