编程语言
首页 > 编程语言> > 《C语言及程序设计》实践参考——区号查询

《C语言及程序设计》实践参考——区号查询

作者:互联网

返回:贺老师课程教学链接

【项目3-区号查询】
在文件PostCode.txt中,提供了全国各省城市的长途区号和邮政编码。请以这个文件为数据来源设计程序,输入城市名,输出该城市的相关的信息。
[参考解答]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int readDate();
int Search(char*,int);
void Show(int);
struct Code
{
    char province[20];//省
    char abbreviation[4];//省的简称
    char city[24];//市
    int areacode;//区号
    int zipcode;//邮编
};
struct Code codes[5000];
int n;
int main()
{
    char s[22];
    int index = -1;
    n=readDate();
    printf("请输入要查询的城市:");
    scanf("%s", s);
    index =Search(s,n);   
    if (index>=0)
        Show(index);
    else
        printf("没有该城市,或者城市输入有误。\n");
    return 0;
}

int readDate()  //读取数据,返回人数
{
    FILE *infile=fopen("postcode.txt","r");     //以输入的方式打开文件
    if(!infile)                 //测试是否成功打开
    {
        printf("open error!\n");
        exit(1);
    }

    int i=0;
    fscanf(infile, "%s %s %s %d %d", codes[i].province, codes[i].abbreviation, codes[i].city, &codes[i].areacode, &codes[i].zipcode);
    while(!feof(infile))
    {
        ++i;
        fscanf(infile, "%s %s %s %d %d", codes[i].province, codes[i].abbreviation, codes[i].city, &codes[i].areacode, &codes[i].zipcode);
    }
    fclose(infile);
    return i;
}


int Search(char *s,int n)  //顺序查找,待改进
{
    int i;
    for (i=0; i<n; i++)
        if (strcmp(codes[i].city, s)==0)
            return i;
    return -1;

}

void Show(int i)
{
    printf("省份:%s\n", codes[i].province);
    printf("简称:%s\n", codes[i].abbreviation);
    printf("城市:%s\n",codes[i].city);
    printf("区号:0%d\n",codes[i].areacode);
    printf("邮政编码:%d\n",codes[i].zipcode);
}

标签:index,codes,int,zipcode,查询,char,程序设计,C语言,infile
来源: https://blog.51cto.com/sxhelijian/2814834