21xrx.com
2025-03-28 18:59:31 Friday
文章检索 我的文章 写文章
C++城市信息管理课程设计题目和代码
2023-07-13 13:44:29 深夜i     9     0
C++ 城市信息管理 课程设计 题目 代码

在大学计算机科学专业的教育中,C++编程语言是必学的一门课程。而在C++的学习过程中,课程设计成为了一个非常重要的环节。在这个环节中,学生需要通过自己的努力设计和编写一个较为完整的C++程序,来实现特定的功能。

在城市信息管理课程设计中,我们的目标是设计一个可以管理城市信息的C++程序。程序应该具备以下基本功能:

1. 添加城市信息:程序应该允许用户添加新的城市信息,包括城市名称、人口数量、GDP、面积等等必要的信息。

2. 删除城市信息:程序应该允许用户删除现有的城市信息,同时需要保证数据的完整性和正确性。

3. 修改城市信息:程序应该允许用户修改现有的城市信息。

4. 查询城市信息:程序应该允许用户查询已有的城市信息,包括人口数量、GDP、面积等等必要的信息。

在上述基本功能的基础上,可以加入其他更具体的功能。例如,可以支持按照城市名称进行排序、统计城市人口、GDP等各项数据总和等等。基于这些功能要求,我们可以设计出以下的C++代码:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class City
{
public:
  string name;
  int peopleNum;
  double gdp;
  int area;
  City() {};
  City(string _name, int _peopleNum, double _gdp, int _area)
  
    name = _name;
    peopleNum = _peopleNum;
    gdp = _gdp;
    area = _area;
  
};
class CityList
{
public:
  City cityList[100];
  int cityNum;
  CityList()
  
    cityNum = 0;
  
  void addCity(City c)
  {
    cityList[cityNum++] = c;
  }
  void deleteCity(string name)
  {
    int index;
    for (index = 0; index < cityNum; index++)
    {
      if (cityList[index].name == name)
      {
        for (int i = index + 1; i < cityNum; i++)
        {
          cityList[i - 1] = cityList[i];
        }
        cityNum--;
        break;
      }
    }
  }
  void modifyCity(string name, int peopleNum, double gdp, int area)
  {
    for (int i = 0; i < cityNum; i++)
    {
      if (cityList[i].name == name)
      {
        cityList[i].peopleNum = peopleNum;
        cityList[i].gdp = gdp;
        cityList[i].area = area;
        break;
      }
    }
  }
  void queryCity(string name)
  {
    for (int i = 0; i < cityNum; i++)
    {
      if (cityList[i].name == name)
      {
        cout << left << setw(20) << "City Name:" << setw(15) << "People Num:" << setw(12) << "GDP:" << setw(10) << "Area:" << endl;
        cout << left << setw(20) << cityList[i].name << setw(15) << cityList[i].peopleNum << setw(12) << cityList[i].gdp << setw(10) << cityList[i].area << endl;
      }
    }
  }
  void printList()
  {
    cout << left << setw(20) << "City Name:" << setw(15) << "People Num:" << setw(12) << "GDP:" << setw(10) << "Area:" << endl;
    for (int i = 0; i < cityNum; i++)
    {
      cout << left << setw(20) << cityList[i].name << setw(15) << cityList[i].peopleNum << setw(12) << cityList[i].gdp << setw(10) << cityList[i].area << endl;
    }
  }
  void sortByName()
  {
    for (int i = 0; i < cityNum - 1; i++)
    {
      for (int j = 0; j < cityNum - i - 1; j++)
      {
        if (cityList[j].name > cityList[j + 1].name)
        {
          City temp = cityList[j];
          cityList[j] = cityList[j + 1];
          cityList[j + 1] = temp;
        }
      }
    }
  }
  void sortByGdp()
  {
    for (int i = 0; i < cityNum - 1; i++)
    {
      for (int j = 0; j < cityNum - i - 1; j++)
      {
        if (cityList[j].gdp > cityList[j + 1].gdp)
        {
          City temp = cityList[j];
          cityList[j] = cityList[j + 1];
          cityList[j + 1] = temp;
        }
      }
    }
  }
  void sortByArea()
  {
    for (int i = 0; i < cityNum - 1; i++)
    {
      for (int j = 0; j < cityNum - i - 1; j++)
      {
        if (cityList[j].area > cityList[j + 1].area)
        {
          City temp = cityList[j];
          cityList[j] = cityList[j + 1];
          cityList[j + 1] = temp;
        }
      }
    }
  }
  int sumPeople()
  {
    int sum = 0;
    for (int i = 0; i < cityNum; i++)
    {
      sum += cityList[i].peopleNum;
    }
    return sum;
  }
  double sumGdp()
  {
    double sum = 0.0;
    for (int i = 0; i < cityNum; i++)
    {
      sum += cityList[i].gdp;
    }
    return sum;
  }
  int sumArea()
  {
    int sum = 0;
    for (int i = 0; i < cityNum; i++)
    {
      sum += cityList[i].area;
    }
    return sum;
  }
};
int main()
{
  CityList cityList;
  while (true)
  {
    cout << " 1.Add City Information" << endl;
    cout << " 2.Delete City Information" << endl;
    cout << " 3.Modify City Information" << endl;
    cout << " 4.Query City Information" << endl;
    cout << " 5.Print City Information List" << endl;
    cout << " 6.Sort City List By Name" << endl;
    cout << " 7.Sort City List By GDP" << endl;
    cout << " 8.Sort City List By Area" << endl;
    cout << " 9.Calculate City Population" << endl;
    cout << "10.Calculate City GDP" << endl;
    cout << "11.Calculate City Area" << endl;
    cout << "12.Quit" << endl;
    cout << "Please Enter the Number of Your Choice:";
    string choice;
    cin >> choice;
    if (choice == "1")
    {
      string name;
      int peopleNum;
      double gdp;
      int area;
      cout << "Enter City Name:";
      cin >> name;
      cout << "Enter City People Number:";
      cin >> peopleNum;
      cout << "Enter City GDP:";
      cin >> gdp;
      cout << "Enter City Area:";
      cin >> area;
      City c(name, peopleNum, gdp, area);
      cityList.addCity(c);
      cout << endl;
    }
    else if (choice == "2")
    {
      string name;
      cout << "Enter City Name:";
      cin >> name;
      cityList.deleteCity(name);
      cout << endl;
    }
    else if (choice == "3")
    {
      string name;
      int peopleNum;
      double gdp;
      int area;
      cout << "Enter City Name:";
      cin >> name;
      cout << "Enter City People Number:";
      cin >> peopleNum;
      cout << "Enter City GDP:";
      cin >> gdp;
      cout << "Enter City Area:";
      cin >> area;
      cityList.modifyCity(name, peopleNum, gdp, area);
      cout << endl;
    }
    else if (choice == "4")
    {
      string name;
      cout << "Enter City Name:";
      cin >> name;
      cityList.queryCity(name);
      cout << endl;
    }
    else if (choice == "5")
    {
      cityList.printList();
      cout << endl;
    }
    else if (choice == "6")
    {
      cityList.sortByName();
      cityList.printList();
      cout << endl;
    }
    else if (choice == "7")
    {
      cityList.sortByGdp();
      cityList.printList();
      cout << endl;
    }
    else if (choice == "8")
    {
      cityList.sortByArea();
      cityList.printList();
      cout << endl;
    }
    else if (choice == "9")
    {
      cout << "Total People Num:" << cityList.sumPeople() << endl;
      cout << endl;
    }
    else if (choice == "10")
    {
      cout << "Total GDP:" << cityList.sumGdp() << endl;
      cout << endl;
    }
    else if (choice == "11")
    {
      cout << "Total Area:" << cityList.sumArea() << endl;
      cout << endl;
    }
    else if (choice == "12")
    
      break;
    
    else
    
      cout << "Invalid Input!" << endl;
    
  }
  return 0;
}

上述代码实现了上述基本需求,并且还包括了一些附加功能。在这个代码的基础上,学生可以继续拓展和完善,学习和掌握更多的C++编程技巧。

  
  

评论区

请求出错了