软题库 移动APP 扫码下载APP 随时随地移动学习 培训课程
当前位置:信管网 >> 在线考试中心 >> 信息系统项目管理师题库 >> 试题查看
试卷名称 2010年上半年程序员考试下午真题试题(案例分析)
考试中心《2010年上半年程序员考试下午真题试题(案例分析)》在线考试
试卷年份2010年上半年
试题题型【分析简答题】
试题内容

阅读以下说明和C 程序,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某旅游服务应用程序运行时,根据输入的两个城市名查找其间的距离。各城市间的距离如表4-1所示。表格中的第一行和第一列表示城市名,表中的每个元素是一个整数,代表该元素所在行和列对应的城市之间的距离(单位:km)。


程序中定义的函数FindCityInSortedArray和GetCity说明如下:
(1)函数 FindCityInSortedArray 的功能是用二分查找法在全局数组 cityTable 中查找城市名所对应的下标值。
(2)函数GetCity的功能是读入城市名,调用函数FindCityInSortedArray来获取城市所对应的下标值。如果该城市名不存在,则提示用户重新输入。
【C 程序】 
int main ( )   {
int city1,city2;
city1 = GetCity("输入第 1个城市名: ") ;
city2 = GetCity("输入第 2 个城市名: ");
printf(" %s 和%s之间的距离为:%d  km.\n" ,cityTable[city1] ,
cityTable[city2]  ,
kmTable[city1] [city2]);
return  0;
}
static int  GetCity(char *prompt)   {
char • cityName;
int index;
cityName   =   (char   *)malloc(20*sizeof(char));
while  (   TRUE   )    {
printf(" %s" ,prompt);
gets(cityName) ;                               /*获取输入字符串*/
index = FindCityInSortedArray(cityName);
if (      (1)     )    break;
printf(" 城市名不存在,请重新输入。 \n") ;
}
free(cityName);
return (2);
}
static  int  FindCityInSortedArray(char*key)    {
int lh ,rh ,mid ,cmp;
lh  =  0;
rh  = NCities  - 1;
while   (         (3)       )    {
mid  =  (lh  + rh)   /  2;
cmp  =  strcmp (     (4)     );     /*比较两个城市名是否相同*/
if   (cmp ==  0)  return  (5);             /*两个城市名相同*/
if (cmp  <  0)   {  rh  = mid  - 1;  }
else  {   lh = mid  +  1;  }
}
return  (-1);    /*城市名不存在时返回 -1 */
}


相关试题

推荐文章
合作网站内容