首页 > 试题广场 >

给定下面的输出: Please choose one

[问答题]
给定下面的输出:
Please choose one of the following:
1) copy files 2) move files
3) remove files 4) quit
Enter the number of your choice:
a. 用一个函数实现菜单的显示,且该菜单有4个用数字编号的选项并要求你选择其中之一(输出应该如题设中所示)。
b. 编写一个函数,该函数接受两个int类型的参数:一个下界和一个上界。在函数中,首先从输入终端读取一个整数,如果该整数不在上下界规定的范围内,则函数重新显示菜单(使用本题目a部分中的函数)以再次提醒用户输入新值。如果输入数值在规定范围内,那么函数应将该数值返回给调用函数。
c. 使用本题目a和b部分中的函数编写一个最小的程序。最小的意思是该程序不需实现菜单中所描述的功能;它只需要显示这些选项并能获取正确的响应即可。
推荐
下面是最简洁的程序,showmenu ( )和getchoice ( )函数是解决问题a和问题b的可能方案。
#include <stdio.h>
void showmenu (void);    /* 声明要用到的函数 */
int getchoice (int, int);
main ( )
{
int res;
showmenu ( );
while ((res = getchoice (1, 4)) != 4)
printf ("I like choice %d.\n", res);
printf ("Bye!\n");
return 0;
}
void showmenu (void)
{
printf ("Please choose one of the following: \n");
printf ("1) copy files 2) move files\n");
printf ("3) remove files 4) quit\n");
printf ("Enter the number of your choice: \n");
}
int getchoice (int low, int high)
{
int ans;
scanf ("%d", &ans);
while (ans < low l l ans > high)
{
printf ("%d is not a valid choice; try again\n", ans);
showmenu ( );
scanf ("%d", &ans);
}
return ans;
}

发表于 2018-03-18 22:13:48 回复(0)