编写一个能管理城市区号信息的系统程序,系统能够读取、查找、输出城市区号信息。其中,区号查询内容显示效果为:

![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.尽量用简单基础一点的代码,大一初学者学过能看得懂的,不要太晦涩复杂,别用没学过的头文件

定义了一个结构体 PostCode 来存储省份等信息。打开 txt 文件使用 fgets 函数逐行读取文件内容,使用 sscanf 函数将每行内容存储到结构体数组 postcodes 中,利用 for 循环便利数组和打印结果:

#include <stdio.h>
#include <stdlib.h>

#define MAX_PROVINCE_LENGTH 20
#define MAX_CITY_LENGTH 20
#define MAX_POSTCODE_LENGTH 10

// 定义结构体 PostCode
typedef struct {
    char province[MAX_PROVINCE_LENGTH];
    char city[MAX_CITY_LENGTH];
    char postcode[MAX_POSTCODE_LENGTH];
} PostCode;

int main() {
    // 打开文件 PostCode.txt
    FILE *fp = fopen("/data/workspace/myshixun/PostCode.txt", "r");
    if (fp == NULL) {
        printf("Error opening file!\\\\n");
        exit(1);
    }

    // 读取文件
    PostCode postcodes[1000];
    int num_postcodes = 0;
    char line[100];
    while (fgets(line, 100, fp) != NULL) {
        sscanf(line, "%s %s %s", postcodes[num_postcodes].province, postcodes[num_postcodes].city, postcodes[num_postcodes].postcode);
        num_postcodes++;
    }

    // 关闭文件
    fclose(fp);

    // 打印结果
    for (int i = 0; i < num_postcodes; i++) {
        printf("%s %s %s\\\\n", postcodes[i].province, postcodes[i].city, postcodes[i].postcode);
    }

    return 0;
}


你好,不知道你没有找到满意答案,我尝试跑了一下代码,看看下面的这个代码复合你的要求么。假设你的PostCode.txt的文件格式是如下的格式:

img

那么你运行下面的代码,将会有这样的效果:

img

img

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct
{
    char placename[100];
    char alias[50];
    char city[50];
    char num[50];
    char code[50];
}Info;
 
int ReadInfoFromTxt(Info *p);
void SearchInfo(int number,Info *p,char *name);
 
int main()
{
    int number = 0;
    Info city[3000] = {0};
    Info *p = city;
    char name[100] = {0};
 
    //从文件中读取城市的信息
    number = ReadInfoFromTxt(p);
    if(number == 0)
    {
        printf("read info from txt error\n");
    }
 
    printf("请输入要查找的城市名:");
    scanf("%s",name);
    //查找信息中是否有该城市
    SearchInfo(number,p,name);
 
    return 0;
}

// 从txt文件中获取信息,返回有效的信息个数
int ReadInfoFromTxt(Info *p)
{
    int number = 0;  // 纪录有效信息
    FILE *fp = NULL;
 
    if((fp = fopen("PostCode.txt","r")) == NULL)
    {
        printf("open PostCode.txt error!\n");
        exit(1);
    }
 
    while(!feof(fp))
    {
         //使用fscanf函数从文件中读取五个字符串,分别对应Info结构体中的placename, alias, city, num, code字段,并存储到p指向的数组中
        fscanf(fp,"%s %s %s %s %s",(p+number)->placename,(p+number)->alias,(p+number)->city,(p+number)->num,(p+number)->code);
        number++;  // 有效数据自增
    }
 
    fclose(fp);
    return number;
}

// 从txt文件寻找有效的名字
void SearchInfo(int number,Info *p,char *name)
{
    int i = 0;
    int flag = 0;
 

    for(i = 0;i < number;i++)
    {
        if(!strcmp((p+i)->city,name))  // 比较字段相等执行
        {
            printf("%s %s %s %s %s",(p+i)->placename,(p+i)->alias,(p+i)->city,(p+i)->num,(p+i)->code);
            flag = 1;
            break;
        }
    }
    printf("\n");
    if(flag == 0)
    {
        printf("对不起,找不到该城市!\n");
    }
 
    return;
}
该回答引用ChatGPT
题目描述:

使用C语言编写一个能管理城市区号信息的系统程序,系统能够读取、查找、输出城市区号信息。其中,区号查询内容显示效果为:![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/539273945386161.jpg)

要求:

1. 数据来源:“区号信息文件(PostCode.txt)”--文本文件形式,文件包含每个省的简称以及每个省中若干个城市的名称、区号以及邮政编码等信息。(在EduCoder平台上默认存放路径为:"/data/workspace/myshixun/PostCode.txt")
2. 程序编写要独立完成,雷同视为作废。
3. 代码要有适当的注释,使程序容易阅读。

实现思路:

该程序主要分为以下几个模块:

1. 读取城市区号信息文件:从文件中读取城市区号信息,构建区号信息链表。
2. 查找城市区号信息:根据输入的城市名称或区号查询对应的城市区号信息。
3. 输出城市区号信息:按照一定的格式输出城市区号信息。
4. 释放内存:程序结束时释放申请的内存空间。

实现细节:

1. 采用链表存储城市区号信息。
2. 读取城市区号信息文件时,顺序读取每行信息并解析,将解析后的信息构建成链表。
3. 查找城市区号信息时,根据输入的城市名称或区号,在链表中查询对应信息。
4. 输出城市区号信息时,按照一定的格式输出城市区号信息,若查找不到对应信息则输出相应提示。
5. 程序结束时需要释放链表占用的内存空间。

参考代码如下:(注:由于无法在线编译,以下代码可能存在一些语法错误)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//定义城市区号信息结构体
typedef struct {
char province[20]; //省份简称
char city[20]; //城市名称
char code[10]; //区号
char postalcode[10]; //邮政编码
} CityInfo;

//定义城市区号信息节点结构体
typedef struct Node {
CityInfo data; //城市区号信息
struct Node* next; //指向下一个节点的指针
} ListNode;

//读取城市区号信息文件
//返回链表头指针
ListNode* readCityInfoFile(const char* filename) {
//打开文件
FILE* fp;
fp = fopen(filename, "r");
if(fp == NULL) {
printf("Failed to open file: %s
", filename);
return NULL;
}

//定义链表头指针和当前节点指针
ListNode *head, *current;
head = current = NULL;

//读取文件内容并构建链表
char line[100];
char* token;
while(fgets(line, 100, fp) != NULL) {
CityInfo info;
token = strtok(line, " ");
strcpy(info.province, token);
token = strtok(NULL, " ");
strcpy(info.city, token);
token = strtok(NULL, " ");
strcpy(info.code, token);
token = strtok(NULL, "
");
strcpy(info.postalcode, token);
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node -> data = info;
node -> next = NULL;
if(current == NULL) {
head = node;
current = node;
} else {
current -> next = node;
current = node;
}
}

//关闭文件并返回链表头指针
fclose(fp);
return head;
}

//查找城市区号信息
void searchCityInfo(ListNode* head, const char* keyword) {
//遍历链表并查找
ListNode* current = head;
int found = 0;
while(current != NULL) {
if(strcmp(current->data.province, keyword) == 0 || strcmp(current->data.city, keyword) == 0 || strcmp(current->data.code, keyword) == 0) {
found = 1;
printf("Province: %s
City: %s
Code: %s
PostalCode: %s
", current->data.province, current->data.city, current->data.code, current->data.postalcode);
}
current = current->next;
}

//输出查找结果或提示未找到信息
if(!found) {
printf("No matching city or code found.
");
}
}

//输出城市区号信息
void printCityInfo(ListNode* head) {
//遍历链表并输出信息
ListNode* current = head;
while(current != NULL) {
printf("Province: %s
City: %s
Code: %s
PostalCode: %s
------------------------
", current->data.province, current->data.city, current->data.code, current->data.postalcode);
current = current->next;
}
}

//释放内存空间
void freeMemory(ListNode* head) {
//遍历链表并释放内存
ListNode* current = head;
while(current != NULL) {
ListNode* temp = current;
current = current->next;
free(temp);
}
}

int main() {
//读取城市区号信息文件并构建链表
ListNode* head = readCityInfoFile("/data/workspace/myshixun/PostCode.txt");
if(head == NULL) {
return 0;
}

//显示查询输入框
printf("Please enter a city name or code to search (q to quit):
");

//循环输入查询关键字并执行查询操作
char keyword[20];
while(1) {
scanf("%s", keyword);
if(strcmp(keyword, "q") == 0) {
break;
}
searchCityInfo(head, keyword);
}

//输出城市区号信息
printf("City info list:
------------------------
");
printCityInfo(head);

//释放内存空间
freeMemory(head);

return 0;
}

我看你的要求和发布时间,我感觉你是校友