diff --git a/课设2.cpp b/课设2.cpp new file mode 100644 index 0000000..cda0e9d --- /dev/null +++ b/课设2.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct WeatherData +{ + string cityName; + int temperature; + int humidity; + int windSpeed; +}; +bool readWeatherData(const string& filename, vector& data) +{ + ifstream File; + File.open(filename); + + + if(File.is_open()) + { + string line; + char ws; + while (getline(File, line)) + { + istringstream iss(line); + WeatherData weather; + + if (getline(iss, weather.cityName, ',') && + (iss >> weather.temperature >> ws >> weather.humidity >> ws >> weather.windSpeed)) + { + data.push_back(weather); + } + } + + } + else + { + cout << "ÎÞ·¨´ò¿ªÎļþ " << filename << endl; + return false; + } + File.close(); + return true; +} + +void findAndDisplayWeather(const string& cityName, const vector& data) +{ + bool found = false; + for (const auto& weather : data) + { + if (weather.cityName == cityName) + { + found = true; + cout << cityName << "µÄÌìÆøÊý¾ÝΪ£ºÎ¶È" << weather.temperature + << ", ʪ¶È" << weather.humidity + << ", ·çËÙ" << weather.windSpeed << endl; + return; + } + } + + if (!found) + { + cout << "δÕÒµ½³ÇÊÐ " << cityName << " µÄÌìÆøÊý¾Ý¡£" << endl; + } +} + +int main() +{ + string filename; + cout << "ÇëÊäÈëÎļþÃû£º"; + cin >> filename; + + string cityName; + cout << "ÇëÊäÈëÒª¼ìË÷µÄ³ÇÊÐÃû£º"; + cin >> cityName; + + vector weatherData; + if (!readWeatherData(filename, weatherData)) + { + return 1; + } + + findAndDisplayWeather(cityName, weatherData); + + return 0; +} +