21xrx.com
2024-12-22 20:46:44 Sunday
登录
文章检索 我的文章 写文章
C++课程设计:城市信息管理题目和代码
2023-07-04 19:14:35 深夜i     --     --
C++ 课程设计 城市信息管理 题目 代码

在计算机程序设计中,C++是一门常用的编程语言,学习C++不仅对于计算机科学专业的学生至关重要,对于从事程序开发工作的人员也是必不可少的技能。在C++的课程设计中,城市信息管理题目是一道常见的考题。

城市信息管理题目要求开发一个管理城市信息的程序,功能包括添加城市、删除城市、修改城市信息、查询城市信息等基本操作。在程序中,每个城市都有一个唯一的编号和对应的信息,如城市名称、人口数量、地理位置等,这些信息需要被储存起来,以便随时查询。

以下是一份简易的城市信息管理程序代码:

#include

#include

#include

#include

#include

using namespace std;

const int MAX_SIZE = 100;

struct City {

  int cityID;

  char cityName[20];

  int cityPopulation;

  char cityLocation[20];

  char cityDescription[200];

} cityList[MAX_SIZE];

int cityCount = 0;

void addCity() {

  City c;

  cout<<"\nEnter City ID: ";

  cin>>c.cityID;

  cout<<"\nEnter City Name: ";

  cin>>c.cityName;

  cout<<"\nEnter City Population: ";

  cin>>c.cityPopulation;

  cout<<"\nEnter City Location: ";

  cin>>c.cityLocation;

  cout<<"\nEnter City Description: ";

  cin>>c.cityDescription;

  cityList[cityCount++] = c;

  cout<<"\nCity Added Successfully\n";

}

void displayCity(City c) {

  cout<<"\nCity ID: "< <

  cout<<"City Name: "< <

  cout<<"City Population: "< <

  cout<<"City Location: "< <

  cout<<"City Description: "< <

  cout<<"\n";

}

void searchCity() {

  int cityID;

  cout<<"\nEnter City ID to search: ";

  cin>>cityID;

  for(int i = 0; i < cityCount; i++) {

    if(cityList[i].cityID == cityID) {

      displayCity(cityList[i]);

      return;

    }

  }

  cout<<"\nCity not Found\n";

}

void deleteCity() {

  int cityID;

  cout<<"\nEnter City ID to delete: ";

  cin>>cityID;

  for(int i = 0; i < cityCount; i++) {

    if(cityList[i].cityID == cityID) {

      for(int j = i; j < cityCount-1; j++)

        cityList[j] = cityList[j+1];

      cityCount--;

      cout<<"\nCity Deleted Successfully\n";

      return;

    }

  }

  cout<<"\nCity not Found\n";

}

void updateCity() {

  int cityID, property;

  cout<<"\nEnter City ID to update: ";

  cin>>cityID;

  for(int i = 0; i < cityCount; i++) {

    if(cityList[i].cityID == cityID) {

      displayCity(cityList[i]);

      cout<<"\nWhich property you want to update?\n";

      cout<<"1. City Name\n2. City Population\n3. City Location\n4. City Description\n";

      cin>>property;

      switch(property) {

        case 1:

          cout<<"\nEnter City Name: ";

          cin>>cityList[i].cityName;

          break;

        case 2:

          cout<<"\nEnter City Population: ";

          cin>>cityList[i].cityPopulation;

          break;

        case 3:

          cout<<"\nEnter City Location: ";

          cin>>cityList[i].cityLocation;

          break;

        case 4:

          cout<<"\nEnter City Description: ";

          cin>>cityList[i].cityDescription;

          break;

        default:

          cout<<"\nInvalid Input\n";

          break;

      }

      cout<<"\nCity Updated Successfully\n";

      displayCity(cityList[i]);

      return;

    }

  }

  cout<<"\nCity not Found\n";

}

void options() {

  cout<<"\n1. Add a City\n";

  cout<<"2. Search a City\n";

  cout<<"3. Delete a City\n";

  cout<<"4. Update a City\n";

  cout<<"5. Exit Program\n";

  cout<<"\nChoose an Option: ";

}

int main() {

  int option;

  while(1) {

    options();

    cin>>option;

    switch(option) {

      case 1:

        addCity();

        break;

      case 2:

        searchCity();

        break;

      case 3:

        deleteCity();

        break;

      case 4:

        updateCity();

        break;

      case 5:

        exit(0);

        break;

      default:

        cout<<"\nInvalid Input\n";

        break;

    }

  }

  return 0;

}

通过阅读上述程序,我们可以了解到在C++中如何实现城市信息管理系统。程序通过数组储存城市信息,并提供了增、删、改、查等多种操作。程序代码清晰、简单易懂,可以用作C++初学者练手的绝佳案例。此外,对于需要实现城市信息管理系统的项目开发工程师来说,上述程序亦可为其提供实用的思路和借鉴。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复