【项目 - 让吃货失望的菜单】
计算机中的应用程序,常常用“菜单”的形式,提供给用户便捷的操作。当然, 里面没有菜。
下面的程序段给出了一些提示,请在此基础上拓展,使程序的运行如图所示(不限于此,可以拓展)
int main()
{
char cChioce;
while(1)
{
cChioce = getChoice();
if (cChioce=='1')
eat();
else ...
}
return 0;
}
char getChoice()
{
char c;
printf("\n ********************\n");
printf(" * 1. 吃饭 *\n");
...
fflush(stdin);
scanf("%c", &c);
return c;
}
鼓励根据自己掌握的一些需求,例如银行存取款、学生档案管理、法律诉讼受理等,做出应用程序的“框架”,界面的表现形式也可以更美观一些。
[参考解答]
#include <stdio.h>
char getChoice();
void eat();
void sleep();
void hitdoudou();
void cry();
int main()
{
char cChioce;
while(1)
{
cChioce = getChoice();
if (cChioce=='1')
eat();
else if (cChioce=='2')
sleep();
else if (cChioce=='3')
hitdoudou();
else if (cChioce=='4')
cry();
else if (cChioce=='0')
break;
else
{
printf("\007选择错误!\n");
continue;
}
printf("恭喜你完成了一项工作!\n");
}
return 0;
}
char getChoice()
{
char c;
printf("\n ********************\n");
printf(" * 1. 吃饭 *\n");
printf(" * 2. 睡觉 *\n");
printf(" * 3. 打豆豆 *\n");
printf(" * 4. 找豆豆妈诉苦 *\n");
printf(" * 0. 退出 *\n");
printf(" ********************\n");
printf(" 请选择(0-4):");
fflush(stdin);
scanf("%c", &c);
return c;
}
void eat()
{
printf(" 我吃吃吃... ...\n");
}
void sleep()
{
printf(" 我睡觉觉... ...\n");
}
void hitdoudou()
{
printf(" 我打打打... ...\n");
}
void cry()
{
printf(" 哇! 你家豆豆骨头硬,害得我手疼... ...\n");
}