當前位置:首頁 » 編程語言 » c語言菜單怎麼做
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言菜單怎麼做

發布時間: 2022-01-28 20:04:28

c語言 菜單選擇的程序怎麼做 (用switch 語句)

#include "stdio.h"
void jc()
{
int n,i,s;
printf("輸入一個整數:");
scanf("%d",&n);
s=1;
for(i=2;i<=n;i++)
s*=i;
printf("%d",s);
}
void max()
{
float d[3],fmax;
int i;
printf("輸入三個數:");
for(i=0;i<3;i++)
{
scanf("%f",&d[i]);
if(i==0)
fmax=d[i];
else
{
if(d[i]>fmax)
fmax=d[i];
}
}
printf("max=%f",fmax);
}
void sxhs()
{
int d,g,s,b,t;
printf("輸入一個(100-999之間)整數:");
scanf("%d",&d);
if(d>=100 && d<=999)
{
g=d%10;
t=d/10;
s=t%10;
b=t/10;
if(g*g*g+s*s*s+b*b*b==d)
printf("%d是水仙花數\n",d);
else
printf("%d不是水仙花數\n",d);
}
else
{
printf("輸入錯誤!");
}
}
int main()
{
int num;
printf("*****************************************************\n");
printf("1、判斷一個數是否為水仙花數\n2、求三個數的最大值\n3、求一個數的階乘\n\n");
printf("*****************************************************\n\n");
printf("輸入序號:");
scanf("%d",&num);
switch(num)
{
case 1:
sxhs();
break;
case 2:
max();
break;
case 3:
jc();
break;
default:
printf("序號輸入錯誤");
break;
}
}

㈡ 用c語言製作一個簡單的菜單程序,一個主函數四個子函數

給你一個簡單的菜單程序吧。其中的子函數,填充成樓主所需即可。

#include<stdio.h>

/*子函數1*/
fun1(){
printf("子函數1 ");
}

/*子函數2*/
fun2(){
printf("子函數2 ");
}

/*子函數3*/
fun3(){
printf("子函數3 ");
}

/*子函數4*/
fun4(){
printf("子函數4 ");
}

intmain(void){
intkey;/*命令編號*/

do{
system("cls");/*清屏*/
/*列印菜單*/
printf("====================================================== ");
printf("# 功能詳情 ");
printf("------------------------------------------------------ ");
printf("1 功能1 ");
printf("2 功能2 ");
printf("3 功能3 ");
printf("4 功能4 ");
printf("5 退出 ");
printf("====================================================== ");
printf(" ");
printf("請輸入命令編號以開啟操作:");

/*輸入命令編號*/
scanf("%d",&key);
printf(" ");

/*switch函數實現輸入功能序號執行相應函數*/
switch(key){
case1:fun1();break;/*子函數1*/
case2:fun2();break;/*子函數2*/
case3:fun3();break;/*子函數3*/
case4:fun4();break;/*子函數4*/
case5:printf("程序結束!按任意鍵退出... ");break;
default:printf("輸入錯誤,請重新輸入! ");break;
}

/*屏幕暫留*/
if(key!=5){
printf(" ");
printf("按Enter鍵繼續... ");
printf(" ");
fflush(stdin);
getch();
}

}while(key!=5);

/*屏幕暫留*/
fflush(stdin);
getch();
return0;
}

運行結果

㈢ C語言中怎樣定義,才能製作一個以菜單方式運行的程序

這是我以前做過的一個,你可以參考一下。利用k
do
{
//Console.Write("輸入0結束,任意鍵繼續");
//k = Console.ReadKey().KeyChar;
Console.Clear();
Console.WriteLine("*******************主菜單*******************");
Console.WriteLine("**************選擇你要的服務:**************");
Console.WriteLine("****************1、輸入信息*****************");
Console.WriteLine("****************2、瀏覽信息*****************");
Console.WriteLine("****************3、排序*********************");
Console.WriteLine("****************4、查詢*********************");
Console.WriteLine("****************5、結束*********************");
int num = 0;
Console.Write("請輸入您的選擇:");
num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
{
Console.Clear();
for (int i = 1; i < m+1; i++)
{
Console.Write("請輸入學生{0}的學號(姓名):",i);
iArray[i, 0] = Convert.ToString(Console.ReadLine());
for (int j = 1; j < n+1; j++)
{
//Console.WriteLine();
Console.Write("{0}的成績:",iArray[0,j]);
iArray[i, j] = Convert.ToString(Console.ReadLine());
}
}
} break;

case 2:
{
Console.Clear();
for (int j = 0; j < n + 1; j++)
Console.Write("{0}\t",iArray[0,j]);
for (int i = 1; i < m + 1; i++)
{ Console.WriteLine();
for (int j = 0; j < n + 1; j++)
Console.Write("{0}\t", iArray[i, j]);
}
Console.WriteLine("\n按任意鍵回主菜單");
Console.ReadLine();
} break;

case 3:
{
Console.Clear();
int num3=0;
string temp,temp1;
int k3 = 1;
do
{
Console.Clear();
Console.WriteLine("選擇排序的科目:");
for (int j = 1; j < n + 1; j++)
{
Console.WriteLine("{0}、按{1}", j, iArray[0, j]);
}
Console.WriteLine("{0}、返回主菜單", n + 1);
if (!int.TryParse(Console.ReadLine(), out num3))
return;
for (int m1 = 1; m1 < n + 1; m1++)
if (num3 == m1)
{
for (int i = 1; i < m; i++)
for (int n1 = i + 1; n1 < m + 1; n1++)
if (string.Compare(iArray[i, num3], iArray[n1, num3]) < 0)
{
temp = iArray[i, num3];
temp1=iArray[i,0];
iArray[i, num3] = iArray[n1, num3];
iArray[i,0]=iArray[n1,0];
iArray[n1, num3] = temp;
iArray[n1, 0] = temp1;
}
Console.Clear();
Console.Write("{0}\t", iArray[0, 0]);
Console.Write("{0}\t", iArray[0, num3]);
for (int i = 1; i < m + 1; i++)
{
Console.WriteLine();
Console.Write("{0}\t", iArray[i, 0]);
Console.Write("{0}",iArray[i,num3]);
}
Console.WriteLine("\n{0}最高分為{1},最低分為{2}",iArray[0,num3],iArray[1,num3],iArray[m,num3]);
Console.ReadLine();
}
else if (num3 == n + 1)
k3 = 0;
} while (k3 != 0);
} break;
case 4:
Console.Clear();
Console.Write("請輸入要查找的學號(姓名):");
string x;
x = Console.ReadLine();
int pos = -1;
for (int i = 1; i < m+1; i++)
if (string.Equals(x,iArray[i, 0]))
{
pos = i;
Console.WriteLine("{0}在數組中位置為{1}", x, pos);
for (int j = 0; j < n + 1; j++)
Console.Write("{0}\t", iArray[0, j]);
Console.WriteLine();
for (int j = 0; j < n + 1; j++)
Console.Write("{0}\t", iArray[pos, j]);
}
if (pos == -1)
Console.WriteLine("{0}不存在於數組中", x);
Console.ReadLine();
break;

case 5:
k = 0;
break;
}
}
while (k!=0);

㈣ C語言中如何實現選項菜單的功能

#include<stdio.h>
#include<stdlib.h>

voidhello()
{
printf("helloworld ");
}

intmain()
{

intx;
while(1)
{
printf("-------------操作選項----------- ");
printf("1:返回菜單 ");
printf("2:退出程序 ");
printf("3:執行操作 ");
printf("-------------------------------- ");


printf("按數字鍵選擇要執行的操作:");
scanf("%d",&x);
printf(" ");

//輸入2跳出循環,退出程序
if(x==2)
break;

switch(x)
{
case1:break;//輸入1,跳出switch語句,進入下一次循環
case3:hello();break;
default://數字輸入錯誤,跳出siwtch語句,進入下一次循環
printf("輸入的數字不正確 ");
break;
}
}
return0;
}

說明:有3個選項,1是用返回菜單,2是用於退出程序,3是用於執行相關的操作,這里只是一個示例,所以將要執行的操作,編寫成了一個輸出hello world的函數。

㈤ 怎樣用C語言編寫菜單

  1. 對於窗口組件菜單,需要根據不同平台,通過圖形編程介面,進行菜單的編制。

    常式:

    #include<stdio.h>
    #include<graphics.h>
    #include<conio.h>
    voidmain()
    {
    charstr;
    inti,k,choice=1;
    intgd=DETECT,gm;
    initgraph(&gd,&gm,"");
    setbkcolor(2);
    settextstyle(3,0,3);
    outtextxy(140,120,"A.TheMockClock.");
    outtextxy(140,150,"B.TheDigitalClock.");
    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)
    {case1:setlinestyle(0,0,3);
    rectangle(170,115,400,145);
    settextstyle(3,0,3);
    outtextxy(140,120,"A.TheMockClock.");
    settextstyle(3,0,3);
    outtextxy(140,150,"B.TheDigitalClock.");
    outtextxy(140,180,"C.Exit.");
    break;
    case2:setlinestyle(0,0,3);
    rectangle(170,145,400,175);
    settextstyle(3,0,3);
    outtextxy(140,120,"A.TheMockClock.");
    settextstyle(3,0,3);
    outtextxy(140,150,"B.TheDigitalClock.");
    settextstyle(3,0,3);
    outtextxy(140,180,"C.Exit.");
    break;
    case3:settextstyle(3,0,3);
    outtextxy(140,120,"A.TheMockClock.");
    outtextxy(140,150,"B.TheDigitalClock.");
    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)/*這里引用函數,實現所要的功能*/
    {
    case1:cleardevice();
    setbkcolor(4);
    settextstyle(3,0,4);
    outtextxy(160,120,"No.1havenotbuilt.");break;
    case2:cleardevice();
    setbkcolor(4);
    settextstyle(3,0,4);
    outtextxy(160,150,"No.2havenotbuilt.");
    break;
    case3:exit(0);
    }
    getch();
    closegraph();
    }
  2. 對於命令行菜單,直接通過不斷刷新輸出來模擬菜單行為。

    常式:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    intn,t,k;
    intm;
    chars1[20],s2[20],c;
    char**l;
    char*num[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
    voidmenu()
    {
    printf(" ******************************************************* ");
    printf(" **1.查找字元串S1中S2出現的次數** ");
    printf(" **2.統計字元串中大小寫字母,數字出現的次數** ");
    printf(" **3.將數字翻譯成英語** ");
    printf(" **4.結束** ");
    printf(" ******************************************************* ");
    printf(" 您的輸入:");
    fflush(stdin);
    scanf("%d",&n);
    }
    voidcheck()
    {

    chara[20],b[20];
    intj=0,k,m,l=0;
    intt=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]!='');/*查找完字元數組a結束*/
    }
    printf("子字元串出現次數: %d ",l);
    }
    voidcout()
    {
    intn=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);
    }
    voidnumber()
    {
    l=num;
    printf("請輸入一個數字:(0-10) ");
    fflush(stdin);
    scanf("%d",&m);
    printf("%d對應的英文是: %s ",m,*(l+m-1));
    }
    voidmain()
    {
    while(1)
    {
    system("cls");
    menu();
    switch(n)
    {
    case1:system("cls");check();system("pause");break;
    case2:system("cls");cout();system("pause");break;
    case3:system("cls");number();system("pause");break;
    case4:system("cls");break;
    default:system("cls");break;
    }
    if(n==4)break;
    }
    printf("感謝使用 ");
    }

㈥ 如何用C語言製作下拉菜單

你要看MFC方面的資料,傳統C不行的,沒這個功能

㈦ 怎麼用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("\n請按照以下列表選擇您想要查看的章節:");
printf("\n編號\t章節\n 1\t第一章\n 2\t第二章\n");
printf("請輸入編號進行選擇:");
choicemenue1();
}
void choicemenue1()
{
choice1=getch();
switch(choice1)
{
void menuechapter1();
void menuechapter2();
void menue1();
case '1':menuechapter1();break;
case '2':menuechapter2();break;
default:printf("\n輸入錯誤!請重新選擇!\n"),menue1();
}}
void menuechapter1()
{
char choiceex;
printf("\n您選擇的是第%c章",choice1);
printf("\n現在請選擇本章節的題目。");
printf("\n請按照以下列表選擇題目:");
printf("\n編號\t題目編號\n1\t第1題\n2\t第2題\n");
printf("請選擇:");
switch(choiceex=getch())
{
void c1_1();
void c1_2();
case '1':c1_1();break;
case '2':c1_2();break;
default:
printf("\n你個白痴!這也能選錯!不讓你重選了!");
printf("\n按任意鍵以退出!");
getch();
exit(0);
}
}
void menuechapter2()
{
char choiceex;
printf("\n您選擇的是第%c章",choice1);
printf("\n現在請選擇本章節的題目。");
printf("\n請按照以下列表選擇題目:");
printf("\n編號\t題目編號\n1\t第1題\n2\t第2題\n");
printf("\n請選擇:");
switch(choiceex=getch())
{
void c2_1();
void c2_2();
case '1':c2_1();break;
case '2':c2_2();break;
default:
printf("\n你個白痴!這也能選錯!不讓你重選了!");
printf("\n按任意鍵以退出!");
getch();
exit(0);
}
}
void c1_1()
{
char howtorun;
printf("現在請選擇考察方式:");
printf("\n編號\t考察方式\n 1\t運行程序\n 2\t查看源代碼\n 3\t返回主菜單\n 4\t退出程序\n");
printf("請選擇:");
switch(howtorun=getch())
{
void runc1_1();
void menue1();
case '1':runc1_1();break;
case '2':printf("\n"),system("type \\caidan\\chapter1\\c1_1.c"),printf("\n"),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("\nPlease input two integer numbers:");
printf("a=?,b=?\n");
scanf("%d,%d",&a,&b);
max=a>b?a:b;
printf("The max number is %d",max);
}
大概就是這樣做的。。

㈧ C語言 編寫菜單

#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("\n\n\t\t*******************************************************\n");
printf("\t\t** 1.查找字元串S1中S2出現的次數 **\n");
printf("\t\t** 2.統計字元串中大小寫字母,數字出現的次數 **\n");
printf("\t\t** 3.將數字翻譯成英語 **\n");
printf("\t\t** 4.結束 **\n");
printf("\t\t*******************************************************\n");
printf("\t\t 您的輸入:");
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("請輸入主字元串:\n");
scanf("%s",a);
k=strlen(a);
printf("請輸入子字元串:\n");
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("子字元串出現次數:\n%d\n",l);
}

void cout()
{
int n=0,t=0,k=0;
printf("請輸入一個字元串:\n");
fflush(stdin);/*清除緩沖*/
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z')
n++;
if(c>='A'&&c<='Z')
t++;
if(c>='0'&&c<='9')
k++;
}
printf("有大寫字母:\n%d\n",t);
printf("有小寫字母:\n%d\n",n);
printf("有數字:\n%d\n",k);
}

void number()
{
l=num;
printf("請輸入一個數字:(0-10)\n");
fflush(stdin);
scanf("%d",&m);
printf("%d對應的英文是:\n%s\n",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("感謝使用\n");
}

樓主,終於幫你寫完了,完美測試成功,第一功能因為學藝未精寫了兩個小時,艾,呵呵,不過我還是很開心,如果你有什麼不懂可以HI我,我會幫你解答,呵呵,真的好開心,終於寫出來了
哈哈。。。。

㈨ c語言中,怎樣實現子菜單向主菜單回退

1、如果只是想返回已經寫好的主菜單頁面的話 可以直接在當前函數中結束位置調用主菜單所在的函數,如果想輸入某值返回的話 就加個輸入提示 比如 在其他函數頁面加上「輸入0返回主菜單」就可以在任意時候 輸入0來返回 當然實現的時候也是輸入語句加判斷語句 加調用主菜單所在函數,調用前先調用清屏函數。
2、常式:

#include<stdio.h>int menu(){ int rt; char str[256]; printf("1 ****\n"); printf("2 ****\n"); printf("3 ****\n"); printf("4 ****\n"); printf("5 退出程序\n"); printf("請選擇,輸入選項前面的數字後回車: "); scanf("%s",str); if ( str[0]>='1' && str[0]<='5' ) rt=str[0]-'0'; else rt=0; return rt;}void f1() {}void f2() {}void f3() {}void f4() {}void main(){ int s,loop=1; while ( loop ) { s=menu(); switch ( s ) { case 1: f1(); break; case 2: f2(); break; case 3: f3(); break; case 4: f4(); break; case 5: loop=0; break; default: break; } }}

㈩ C語言編程做一個簡單的菜單

#include<stdio.h>
#include<stdlib.h>
voidmenu()//菜單繪制
{
printf("--------------------------------------- ");
printf(" 0.退出 ");
printf(" 1.錄入信息 ");
printf(" 2.查找信息 ");
printf(" 3.刪除信息 ");
printf(" 4.瀏覽信息 ");
printf(" 5.有其他疑問找群主")
printf(" 6.C++8群:491994603")
printf("--------------------------------------- ");
}

voidkeyDown()//按鍵處理
{
intchoice;
scanf("%d",&choice);
switch(choice)
{
case0:
printf("退出程序 ");
Sleep(2000);
return0;
case1:
//調用菜單1實現函數
break;
case2:
//調用菜單2實現函數
break;
case3:
//調用菜單3實現函數
break;
case4:
//調用菜單4實現函數
break;
default:
printf("輸入錯誤 ");
}
system("pause");
return0;
}
intmain()
{
while(1)
{
menu();
keyDown();
system("cls");
}
return0;
}