现在的位置: 首页 > 编程语言 > 正文

函数模块化程序设计

2019年11月21日 编程语言 ⁄ 共 1729字 ⁄ 字号 评论关闭
函数模块化程序设计的方式。。。
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int iChioce;  
  5.     do  
  6.     {  
  7.         printf("*  1. 吃饭\n");  
  8.         printf("*  2. 睡觉\n");  
  9.         printf("*  3. 打豆豆\n");  
  10.         printf("*  0. 退出\n");  
  11.         printf("*  请选择(0-3):");  
  12.         scanf("%d", &iChioce);  
  13.         //选择后的处理  
  14.         if (iChioce==1)  
  15.             printf("我吃吃吃...\n");  
  16.         else if (iChioce==2)  
  17.             printf("我睡觉觉...\n");  
  18.         else if (iChioce==3)  
  19.             printf("我打打打...\n");  
  20.         else if (iChioce==0)  
  21.             break;  
  22.         else  
  23.         {  
  24.             printf("\007选择错误!\n");  
  25.             continue;  
  26.         }  
  27.         printf("恭喜你完成了一项工作!\n");  
  28.     }  
  29.     while(1);  
  30.     return 0;  
  31. }  



这样写如何?

  1. #include <stdio.h>  
  2. int choose()  
  3.     void eat();  
  4.     void sleep();  
  5.     void hitdoudou();  
  6. int main()  
  7. {  
  8.     int iChioce;  
  9.     do  
  10.     {  
  11.         iChioce=choose();  
  12.         if (iChioce==1)  
  13.             eat();  
  14.         else if (iChioce==2)  
  15.             sleep();  
  16.         else if (iChioce==3)  
  17.             hitdoudou();  
  18.         else if (iChioce==0)  
  19.             break;  
  20.         else  
  21.         {  
  22.             printf("\007选择错误!\n");  
  23.             continue;  
  24.         }  
  25.         printf("恭喜你完成了一项工作!\n");  
  26.     }  
  27.     while(1);  
  28.     return 0;  
  29. }  
  30.   
  31.   
  32. int choose()  
  33. {  
  34.     int i;  
  35.     printf("\n*  1. 吃饭\n");  
  36.     printf("*  2. 睡觉\n");  
  37.     printf("*  3. 打豆豆\n");  
  38.     printf("*  0. 退出\n");  
  39.     printf("*  请选择(0-3):");  
  40.     scanf("%d", &i);  
  41.     return i;  
  42. }  
  43.   
  44.   
  45. void eat()  
  46. {  
  47.     printf("我吃吃吃... ...\n");  
  48. }  
  49.   
  50.   
  51. void sleep()  
  52. {  
  53.     printf("我睡觉觉... ...\n");  
  54. }  
  55.   
  56.   
  57. void hitdoudou()  
  58. {  
  59.     printf("我打打打... ...\n");  
  60. }  

抱歉!评论已关闭.