编写一个能管理城市区号信息的系统程序,系统能够读取、查找、输出城市区号信息。 c语言 ,其中,区号查询内容显示效果为:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/539273945386161.jpg
回顾与复习课程相关知识点,确定该软件的功能与实现目标,分析软件需求,确定组成程序的函数模块;绘制程序流程图,完成软件代码设计;提交本实验设计报告。
注:数据来源:“区号信息文件(PostCode.txt)”–文本文件形式,文件包含每个省的简称以及每个省中若干个城市的名称、区号以及邮政编码等信息。(在EduCoder平台上默认存放路径为:“/data/workspace/myshixun/PostCode.txt”)
2. 只能使用C语言,源程序要有适当的注释,使程序容易阅读;
3. 程序编写要独立完成,雷同视为作废。
4.分享实验思路
5.尽量用简单基础一点的代码
接下来,我们需要编写一个函数模块来读取区号信息文件,并将数据存储到程序中。这个函数的主要功能是打开文件、读取文件中的数据,并将数据存储到数组中。
void readPostCode(char *fileName, PostCode *postCode) {
FILE *fp;
char line[100];
int i = 0, j = 0;
fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Error opening file\n");
return;
}
while (fgets(line, sizeof(line), fp)) {
if (line[0] != ' ') {
i++;
j = 0;
strcpy(postCode[i].province, line);
} else {
sscanf(line, "%s %s %s", postCode[i].city[j].name, postCode[i].city[j].code, postCode[i].city[j].zipCode);
j++;
}
}
fclose(fp);
}
在这个函数中,我们首先打开文件,然后使用 fgets 函数逐行读取文件内容。如果读取到的是省份名称,则将省份名称存储到数组中;如果读取到的是城市信息,则使用 sscanf 函数将城市名称、区号和邮政编码分别存储到结构体中。
接下来,我们需要编写一个函数模块来查询城市的区号信息。这个函数的主要功能是从数组中查找城市信息,并将结果输出到屏幕上。
void searchPostCode(PostCode *postCode, int numProvinces) {
char city[20];
int i, j, found = 0;
printf("Enter the name of a city: ");
scanf("%s", city);
for (i = 1; i <= numProvinces; i++) {
for (j = 0; j < postCode[i].numCities; j++) {
if (strcmp(city, postCode[i].city[j].name) == 0) {
printf("The postcode of %s is %s.\n", postCode[i].city[j].name, postCode[i].city[j].code);
found = 1;
}
}
}
if (!found) {
printf("City not found.\n");
}
}
在这个函数中,我们首先提示用户输入要查询的城市名称。然后,我们使用嵌套的 for 循环遍历整个数组,并使用 strcmp 函数比较用户输入的城市名称和数组中的城市名称是否相等。如果相等,则将城市的区号信息输出到屏幕上;如果遍历完整个数组还没有找到匹配的城市,则输出 “City not found.”。
最后,我们需要编写主函数来调用上述两个函数。在主函数中,我们首先读取区号信息文件,然后提示用户输入要查询的城市名称,最后调用 searchPostCode 函数查询城市的区号信息。
接下来是完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROVINCE_NAME_LEN 20
#define MAX_CITY_NAME_LEN 20
#define MAX_AREA_CODE_LEN 6
#define MAX_POST_CODE_LEN 6
#define MAX_LINE_LEN 100
typedef struct {
char province[MAX_PROVINCE_NAME_LEN + 1];
char city[MAX_CITY_NAME_LEN + 1];
char area_code[MAX_AREA_CODE_LEN + 1];
char post_code[MAX_POST_CODE_LEN + 1];
} PostCodeInfo;
PostCodeInfo* read_post_codes(int* size) {
FILE* fp = fopen("/data/workspace/myshixun/PostCode.txt", "r");
if (!fp) {
printf("打开文件失败!\n");
return NULL;
}
PostCodeInfo* infos = NULL;
char line[MAX_LINE_LEN];
int cnt = 0;
while (fgets(line, MAX_LINE_LEN, fp)) {
if (strlen(line) == 0 || line[0] == '#') {
continue; // 跳过注释行和空行
}
cnt++;
PostCodeInfo* tmp = (PostCodeInfo*)realloc(infos, sizeof(PostCodeInfo) * cnt);
if (!tmp) {
printf("内存分配失败!\n");
free(infos);
fclose(fp);
return NULL;
}
infos = tmp;
char* p = strtok(line, " ");
strcpy(infos[cnt - 1].province, p);
p = strtok(NULL, " ");
strcpy(infos[cnt - 1].city, p);
p = strtok(NULL, " ");
strcpy(infos[cnt - 1].area_code, p);
p = strtok(NULL, " ");
strcpy(infos[cnt - 1].post_code, p);
}
*size = cnt;
fclose(fp);
return infos;
}
void search_by_city(PostCodeInfo* infos, int size) {
char city[MAX_CITY_NAME_LEN + 1];
printf("请输入城市名称:");
scanf("%s", city);
int found = 0;
for (int i = 0; i < size; i++) {
if (strcmp(infos[i].city, city) == 0) {
found = 1;
printf("%-5s %s %s %s\n", infos[i].area_code, infos[i].province, infos[i].city, infos[i].post_code);
}
}
if (!found) {
printf("未找到与%s相关的信息。\n", city);
}
}
int main() {
int size = 0;
PostCodeInfo* infos = read_post_codes(&size);
if (!infos) {
return 1;
}
while (1) {
printf("\n");
printf("请选择操作:\n");
printf("1. 根据城市名称查询\n");
printf("2. 退出程序\n");
printf(">>> ");
int choice;
scanf("%d", &choice);
if (choice == 1) {
search_by_city(infos, size);
} else if (choice == 2) {
break;
} else {
printf("无效的选择!\n");
}
}
free(infos);
return 0;
}