标签:
不得不吐槽一下CSDN的审核速度,前天发表的文章到现在还没有审核完。好吧,寒假理解。
下午无聊想查一下某城市的区号,还是找到度娘,后来索性任性了一会,用C++写了一个区号查询。
程序和先前的写作风格差不多,利用多函数,文件查找,结构比较简单。
上源码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:邵帅
*完成时间:2015年1月30日
*版本号:v1.0
*/
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
void Homepage(void);
int Opendate(void);
int Search(string,int);
void Show(int);
struct Code
{
string province;//省
string abbreviation;//省的简称
string city;//市
int areacode;//区号
int zipcode;//邮编
};
int Allnum;
Code code[5000];
int main()
{
int back;
string Input;
Allnum=Opendate();
Homepage();
cin>>Input;
back=Search(Input,Allnum);
if (back>=0)
Show(back);
else
cout<<"没有该城市,或者城市输入有误。"<<endl;
}
int Opendate(void)
{
int sum=0;
ifstream infile("CodeDate.dat",ios::in);
if (!infile)
{
cout<<"打开文件失败!";
exit(1);
}
while (infile>>code[sum].province)
{
infile>>code[sum].abbreviation;
infile>>code[sum].city;
infile>>code[sum].areacode;
infile>>code[sum].zipcode;
sum++;
}
infile.close();
//文件读取结束
return sum;
}
void Homepage(void)
{
cout<<"+----------------------+"<<endl;
cout<<" 区号查询"<<endl;
cout<<" Mayuko"<<endl;
cout<<"+----------------------+"<<endl;
cout<<"请输入要查询的城市:";
}
int Search(string put,int sum)
{
int i;
for (i=0; i<sum; i++)
if (code[i].city==put)
return i;
return -1;
}
void Show(int i)
{
cout<<"省份:"<<code[i].province<<endl;
cout<<"简称:"<<code[i].abbreviation<<endl;
cout<<"城市:"<<code[i].city<<endl;
cout<<"区号:0"<<code[i].areacode<<endl;
cout<<"邮政编码:"<<code[i].zipcode<<endl;
}
当然,最重要的还是数据库,度娘文库里找到了一份。
好吧,密密麻麻的不看了。
运行结果:
由于没有使用二分查找,而是采用了顺序查找。观察了内存使用情况,可以接受。
下一步,将编写VB版的区号查询。
另外,微信公众平台上线了,搜索Mayu_OS,选择麻鱼即可关注!
@ Mayuko
标签:
原文地址:http://blog.csdn.net/mayuko2012/article/details/43309655