现有一城市表,包含城市名,区号,城市说明,请用指针引用写一个顺序表来存储这些信息,并将其打印
时间: 2023-05-24 14:04:33 浏览: 120
顺序表实现城市信息记录
#include <iostream>
#include <string>
using namespace std;
struct City {
string name;
int areaCode;
string description;
};
const int MAX = 100; // 最大长度
class CityList {
private:
City cities[MAX]; // 存储城市信息的数组
int length; // 当前长度
public:
CityList() { // 构造函数
length = 0;
}
void addCity(City& c) { // 添加城市信息
if (length >= MAX) {
cout << "List is full!" << endl;
return;
}
cities[length++] = c;
}
void printCities() { // 打印所有城市信息
cout << "City\tArea Code\tDescription" << endl;
for (int i = 0; i < length; ++i) {
cout << cities[i].name << "\t" << cities[i].areaCode << "\t\t" << cities[i].description << endl;
}
}
};
int main() {
City beijing = {"Beijing", 10, "Capital of China"};
City shanghai = {"Shanghai", 21, "Largest city in China"};
City guangzhou = {"Guangzhou", 20, "South China's economic center"};
CityList cityList;
cityList.addCity(beijing);
cityList.addCity(shanghai);
cityList.addCity(guangzhou);
cityList.printCities();
return 0;
}
阅读全文