怎样用C语言编写菜单

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-07-04
怎么用C语言编写菜单?

#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include "windows.h"
char choice1;
void main()
{
void menue1();
printf("欢迎进入Ben的作业查询系统!");
menue1();
}
void menue1()
{
void choicemenue1();
printf("
请按照以下列表选择您想要查看的章节:");
printf("
编号章节
1第一章
2第二章
");
printf("请输入编号进行选择:");
choicemenue1();
}
void choicemenue1()
{
choice1=getch();
switch(choice1)
{
void menuechapter1();
void menuechapter2();
void menue1();
case '1':menuechapter1();break;
case '2':menuechapter2();break;
default:printf("
输入错误!请重新选择!
"),menue1();
}}
void menuechapter1()
{
char choiceex;
printf("
您选择的是第%c章",choice1);
printf("
现在请选择本章节的题目。");
printf("
请按照以下列表选择题目:");
printf("
编号题目编号
1第1题
2第2题
");
printf("请选择:");
switch(choiceex=getch())
{
void c1_1();
void c1_2();
case '1':c1_1();break;
case '2':c1_2();break;
default:
printf("
你个白痴!这也能选错!不让你重选了!");
printf("
按任意键以退出!");
getch();
exit(0);
}
}
void menuechapter2()
{
char choiceex;
printf("
您选择的是第%c章",choice1);
printf("
现在请选择本章节的题目。");
printf("
请按照以下列表选择题目:");
printf("
编号题目编号
1第1题
2第2题
");
printf("
请选择:");
switch(choiceex=getch())
{
void c2_1();
void c2_2();
case '1':c2_1();break;
case '2':c2_2();break;
default:
printf("
你个白痴!这也能选错!不让你重选了!");
printf("
按任意键以退出!");
getch();
exit(0);
}
}
void c1_1()
{
char howtorun;
printf("现在请选择考察方式:");
printf("
编号考察方式
1运行程序
2查看源代码
3返回主菜单
4退出程序
");
printf("请选择:");
switch(howtorun=getch())
{
void runc1_1();
void menue1();
case '1':runc1_1();break;
case '2':printf("
"),system("type \\caidan\\chapter1\\c1_1.c"),printf("
"),getch();break;
case '3':menue1();break;
case '4':exit(0);break;
default:
printf("程序即将结束!哈哈哈……");
printf("按任意键以结束程序!");
getch();
exit(0);
}}
void c1_2()
{
printf("c1_2 is OK!");
}void c2_1()
{
printf("c2_1 is OK!");
}void c2_2()
{
printf("c2_2 is OK!");
}
void runc1_1()
{
int a,b,max;
printf("
Please input two integer numbers:");
printf("a=?,b=?
");
scanf("%d,%d",&a,&b);
max=a>b?a:b;
printf("The max number is %d",max);
}
大概就是这样做的。。

#include#include void hello(){printf("hello world
");}int main(){ int x; while (1) { printf("-------------操作选项-----------
"); printf("1:返回菜单
"); printf("2:退出程序
"); printf("3:执行操作
"); printf("--------------------------------
"); printf("按数字键选择要执行的操作: "); scanf("%d",&x); printf("
"); //输入2跳出循环,退出程序 if(x==2) break; switch(x) { case 1: break; //输入1,跳出switch语句,进入下一次循环 case 3: hello();break; default: //数字输入错误,跳出siwtch语句,进入下一次循环 printf("输入的数字不正确
"); break; } } return 0;}
说明:有3个选项,1是用返回菜单,2是用于退出程序,3是用于执行相关的操作,这里只是一个示例,所以将要执行的操作,编写成了一个输出hello world的函数。

  1. 对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。

    例程:

    #include<stdio.h> 
    #include<graphics.h> 
    #include<conio.h> 
    void main() 

    char str; 
    int i,k,choice=1; 
    int gd=DETECT,gm; 
    initgraph(&gd,&gm," "); 
    setbkcolor(2); 
    settextstyle(3,0,3); 
    outtextxy(140,120,"A. The Mock Clock."); 
    outtextxy(140,150,"B. The Digital Clock."); 
    outtextxy(140,180,"C. Exit."); 
    setlinestyle(0,0,3); 
    rectangle(170,115,370,145); 
    /*按上下键选择所需选项*/ 
    for(i=1;i<=100;i++) 

    str=getch(); 
    if(str==72) 

    --choice; 
    if(choice==0)choice=3; 

    if(str==80) 

    ++choice; 
    if(choice==4)choice=1; 

    if(str==13)break; /*按回车键确认*/ 
    /*画图做菜单*/ 
    cleardevice(); 
    switch(choice) 
    { case 1: setlinestyle(0,0,3); 
    rectangle(170,115,400,145); 
    settextstyle(3,0,3); 
    outtextxy(140,120,"A. The Mock Clock."); 
    settextstyle(3,0,3); 
    outtextxy(140,150,"B. The Digital Clock."); 
    outtextxy(140,180,"C. Exit."); 
    break; 
    case 2: setlinestyle(0,0,3); 
    rectangle(170,145,400,175); 
    settextstyle(3,0,3); 
    outtextxy(140,120,"A. The Mock Clock."); 
    settextstyle(3,0,3); 
    outtextxy(140,150,"B. The Digital Clock."); 
    settextstyle(3,0,3); 
    outtextxy(140,180,"C. Exit."); 
    break; 
    case 3: settextstyle(3,0,3); 
    outtextxy(140,120,"A. The Mock Clock."); 
    outtextxy(140,150,"B. The Digital Clock."); 
    settextstyle(3,0,3); 
    outtextxy(140,180,"C. Exit."); 
    setlinestyle(0,0,3); 
    rectangle(170,175,400,205); 
    break; 


    if(i>=100)exit(0);/*如果按键超过100次退出*/ 
    switch(choice)/*这里引用函数,实现所要的功能*/ 

    case 1: cleardevice(); 
    setbkcolor(4); 
    settextstyle(3,0,4); 
    outtextxy(160,120,"No.1 have not built."); break; 
    case 2: cleardevice(); 
    setbkcolor(4); 
    settextstyle(3,0,4); 
    outtextxy(160,150,"No.2 have not built."); 
    break; 
    case 3: exit(0); 

    getch(); 
    closegraph(); 
    }
  2. 对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。

    例程:

    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    int n,t,k;
    int m;
    char s1[20],s2[20],c;
    char **l;
    char *num[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
    void menu()
    {
     printf("

    *******************************************************
    ");
     printf("**   1.查找字符串S1中S2出现的次数                    **
    ");
     printf("**   2.统计字符串中大小写字母,数字出现的次数        **
    ");
     printf("**   3.将数字翻译成英语                              **
    ");
     printf("**   4.结束                                          **
    ");
     printf("*******************************************************
    ");
     printf("     您的输入:");
        fflush(stdin);
     scanf("%d",&n);
    }
    void check()
    {
     
     char a[20],b[20];
     int j=0,k,m,l=0;
     int t=0,n=0;
     printf("请输入主字符串:
    ");
     scanf("%s",a);
     k=strlen(a);
     printf("请输入子字符串:
    ");
     scanf("%s",b);
     m=strlen(b);
     for(n=0;n<k;n++)
      if(a[n]==b[0])
      {
       j++; /*记录相同的字符数*/
       do
       {    
        if(a[++n]==b[++t])
        { 
         j++;
         if(j==m) 
         {
          l++;/*子字符串相同数*/
          j=0;/*判断后相同字符数归零*/
          t=-1;/*判断中if中++t;t将会归零*/
         }
        }
        else
        {
         j=0;
         t=0;
         break;/*如果不同跳出while循环让for使n+1继续判断*/
        }
       }while(a[n]!='\0');/*查找完字符数组a结束*/
      }
      printf("子字符串出现次数:
    %d
    ",l);
    }                                  
    void cout()
    {
     int n=0,t=0,k=0;
     printf("请输入一个字符串:
    ");
        fflush(stdin);/*清除缓冲*/
     while((c=getchar())!='
    ')
     {
      if(c>='a'&&c<='z')
       n++;
      if(c>='A'&&c<='Z')
       t++;
      if(c>='0'&&c<='9')
       k++;
     }
     printf("有大写字母:
    %d
    ",t);
     printf("有小写字母:
    %d
    ",n);
     printf("有数字:
    %d
    ",k);
    }
    void number()
    {
     l=num;
     printf("请输入一个数字:(0-10)
    ");
        fflush(stdin);
     scanf("%d",&m);
     printf("%d对应的英文是:
    %s
    ",m,*(l+m-1));
    }
    void main()
    {
     while(1)
     {   
      system("cls");
      menu();
      switch(n)
      {
      case 1:system("cls");check();system("pause");break;
      case 2:system("cls");cout();system("pause");break;
      case 3:system("cls");number();system("pause");break;
      case 4:system("cls");break;
      default:system("cls");break; 
      }
      if(n==4) break;
     }
     printf("感谢使用
    ");
    }



在C语言中,通常菜单有系统菜单、弹出菜单、下拉菜单等,不知道你要做哪一种。

菜单的制作,通常需要一下几个步骤:
1、定义菜单项目数据结构;
2、定义你要做的弹出菜单或下拉菜单或...的数据结构;
3、建立菜单内容;
4、加载菜单内容;
5、在某个位置上显示(包含保存原有内容以备关闭菜单后恢复覆盖的屏幕,测试菜单显示区域根据实际情况调整显示区域);
6、光标移动,菜单项目颜色变化;
7、执行选中菜单项程序;
8、关闭菜单。

如果纯粹用C语言写,写一个通用的,估计至少要上千行程序代码,以前做过,后来不再使用C语言了,放在机器上,就找不到了。要不就放上去了。

  • c语言写菜单的示例?
    答:sub(7, a, &b);// 把7和a的值分别富值给x,y 把b的地址传个*z在sub中对z指向的b地址富值 sub(a, b, &c);// 把a的值和吧的值分别富值给x,y 把c的地址传个*z在sub中对z指向的c地址富值 printf("a=%d\nb=%d\nc=%d\n", a, b, c);// 输出被赋值后的a b c}void main4(){ i...
  • 怎样用C语言编写菜单?
    答:include<stdio.h> include<string.h> intMENU_Print(ints32QuerNum){ switch(s32QuerNum){ case1:printf(\"您选择的题目如下:\");printf(\"1、以下可以正确表示标识符的是:()\");printf(\"A、Data,Double\");printf(\"B、case,Big\");printf(\"C、2011A,hi_ok\");printf(\"D、_2...
  • 怎么用C语言编写菜单?
    答:printf("现在请选择考察方式:");printf("\n编号\t考察方式\n 1\t运行程序\n 2\t查看源代码\n 3\t返回主菜单\n 4\t退出程序\n");printf("请选择:");switch(howtorun=getch())
  • 用c语言写一个简单的菜单程序,一个主函数至少四个子函数急求!!就像这样...
    答:分析:问题库(QAS)用外部的配置文件保存,每次运行前先载入库中已经建立的问题。然后再进入操作选择。完整代码如下:/*1.暂时不考虑汉字;2.可以用链表优化;3.将每一种操作封装成一个函数;*/#include#include#include#include///字符串最大长度#defineMAX_STR_LEN2048//题库上限#defineMAX_QAS_LEN...
  • 怎么写简单的C语言菜单?
    答:1、使用输出函数即可。2、例程:include <iostream>#include <stdlib.h>#include <conio.h>#include <ctype.h>using namespace std; void xuanzepaixu() //选择排序函数 { printf("待添加,请按任意键继续..."); getchar(); } void erfenpaixu() //二分查找函数 { printf("待...
  • c语言二叉树选择菜单怎么制作
    答:c语言二叉树选择菜单怎么制作?选择一个菜单项,选择文件,按alt键复制或选择菜单单位,在菜单的中心点打开,选择菜单命令,输入0数字等等,单击下方的复制按钮,选择选中后位置默认,单击ok即可使用,如下图。将复制好的选择按钮右击,选择选择命令,打开回到文件,在弹出的界面,找到复制内容的选择框(点击...
  • 如何用“C语言”实现“选项有功能的文本菜单”?
    答:1、直接用输出即可实现、2、例程:include<stdio.h>#include <stdlib.h>void hello(){ printf("hello world\n");}int main(){ int x; while (1) { printf("---操作选项---\n"); printf("1:返回菜单 \n"); printf("2:退出程序 \n"); printf("3:执行操作 \n"); ...
  • 如何用C语言做一个菜单,里面放程序
    答:1、首先新建源文件c.c,用文本编辑器打开。2、输入图片里面的代码,编辑确认无误之后保存。3、编写好控制台批处理文件,设置好环境变量,主要是PATH,INCLUDE和LIB三个环境变量!编辑好之后保存为bat后者cmd后缀文件。4、双击打开刚才设置好的批处理文件,在里面输入cl **.c,自动编译链接,生成exe文件...
  • C语言,编写一个菜单选项0-5,菜单1-5已经写好,分别命名为X1、X2、X3...
    答:int main(){ int loop=1;char ss;while ( loop ){ ShowMenu(); //函数显示菜单本身 //如:printf("请输入你的选择:\n"); printf(" 0:退出\n 1:计算\n 2:...\n");printf("\n请选择:“); ss=getch();switch ( ss ){ case '0': printf("\n确定退出吗?(y\n)")...
  • 设计一通过 c语言 个简单实用的菜单,通过菜单选择不同的菜单项并实现...
    答:定义为1后 后面的成员也会自动变换(排序)searchn,printn,quitn};void main(){ enum mmenu input; while(1){ printf("菜单\n1.增加 2.查询 3.打印 4.退出\n"); scanf("%d",&input); Sleep(500); system("cls"); switch(input){//case内的情况 只能是整型或字符型的常量 ...