1. 怎樣用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("感謝使用 ");
    }

2. C語言菜單怎麼弄

中國電力出版社書號為5123-5504-0的C語言程序設計書上有介紹方法:
現在,通過一個簡單的開發實例來理解上述概念。該應用軟體運行時,首先出現一個菜單,用戶選擇某一菜單項後進入相應子模塊處理具體問題,處理結束後回到主菜單。當在主菜單上選擇退出功能後,軟體運行結束。將各子模塊劃分成子函數後,進一步將菜單初始化和界面顯示、選擇功能劃分成子函數,最終設計出來的函數代碼框架如下:
#include <stdio.h>
//菜單初始化函數:設置菜單文字
void InitMenu( char *Menu[] )
{
Menu[0]="1. 菜單項1名稱 ";

}
// 顯示菜單、接受用戶選擇函數
// 初學者可以使用printf、getch函數實現基本交互
// 進階後可以引入圖形、字體、功能鍵、滑鼠等元素進行人機交互
int ChooseMenu( char *Menu[], int MaxItem )
{
return 1; // 選擇菜單1

return 0; // 選擇退出
}
//模塊一
void sub1(void)
{

}
void main( void )
{
char *Menu[10],MaxItem=10; //指針數組存儲菜單名稱:0-9項
intdone=0; //退出標志,為1時退出
InitMenu( Menu ); //調用菜單初始化模塊
do
{
// 每輪循環都重新顯示界面、接受用戶選擇、根據用戶選擇執行相應分支
switch( ChooseMenu(Menu, MaxItem ) )
{
case 1:
sub1();
break;
case 2:

case 0:
done=1;
}
}while(!done);
}
在結構那一章還有一個課程設計的菜單,太長了,感興趣找書看看吧

3. C語言函數菜單的問題

/**********************************
完整的代碼,只需編譯即可運行!
**********************************/
#define MAX 100
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"math.h"
#include"string.h"
int length(char* a )
{ return strlen(a);
}
void (char *a,char*b,int n,int m)
{ int i,j;
for(i=m,j=0;i<n;i++,j++)
b[j]=a[i];
b[j]='\0';
}
void Inv(int* a,int n)
{ int i,*b;
b=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
b[i]=a[n-i-1];
for(i=0;i<n;i++)
a[i]=b[i];
}
void clear()
{clrscr();
gotoxy(16,4);
textbackground(14);
textcolor(4);
}

void good_bye()
{clear();
printf("\n\n\n\n\n\t\t\t Program End,GOOD BYE!");
getchar();
}

void print_randnum(int a[],int n)
{int i;
clear();
printf("\n\n\t\t output randnum:\n\n\t\t");
for(i=1;i<=n;i++)
{printf("%6d",a);
if(i%6==0) printf("\n\t\t");
}
printf("\n");
getchar();
getchar();
}

void creat_randnum(int a[],int range,int n)
{int i;
for(i=1;i<=n;i++)
a[i]=random(range);
printf("\n\n\t\t output randnum:\n\n\t\t");
for(i=1;i<=n;i++)
{printf("%6d",a);
if(i%10==0)printf("\n\t\t");
}
printf("\n");
getchar();
getchar();
}

int menu_select()
{ char c;
int n;
clear();
printf(" Shuzu de paixu yunsuan:\n\n\n");
printf("\t\t\t1. Chansheng suiji shuzu\n");
printf("\t\t\t2. Shuchu suijishu shuzu\n");
printf("\t\t\t3. Shuchu zifu chang\n");
printf("\t\t\t4. fu zifu shuzu\n");
printf("\t\t\t5. Lixu shuchu shuzhu\n");
printf("\t\t\t6. Nothing\n");
printf("\t\t\t0. Exit\n");
do{printf("\n\t\t\t Enter 0-6 select:");
c=getchar();
n=c-48;
}while((n<0)||(n>6));
return(n);
}

void create()
{int b[MAX],r[MAX];
void creat_randnum();
int range1,i,k=MAX;
clear();
printf("Jianli suijishu hanshu(press any key start):\n\n\n\t\t");
printf("\n\t\t shuru suijishu de fanwei range1=\n\t");
scanf("%4d",&range1);
creat_randnum(b,range1,k);
for(i=1;i<=MAX;i++)
r[i]=b[i];
}
void creat_randnum1(a)
int a[MAX];
{int n,range;
int i;
clear();
printf("chansheng suiji zhengshu chengxu\n\n");
printf("\n\t\t Shuru suijishu de geshu(n<=200):");
scanf("%d",&n);
printf("\n\t\t Shuru suijishu de fanwei range=");
scanf("%d",&range);
for(i=1;i<=n;i++)
a[i]=random(range)+1;
printf("\n\n\t\t Shuchu suiji zhengshu:\n\n\t\t");
for(i=1;i<=n;i++)
{printf("%6d",a);
if(i%5==0)printf("\n\t\t");
}
printf("\n");
getchar();
getchar();
}
void fun1(char*ch)

{ printf("Input a string:\n");
scanf("%s",ch);
printf("%d\n",length(ch));
}
void fun2(char a[],char b[])
{
int n, m;
printf("Input string a:\n");
scanf("%s",a);
n=strlen(a);
printf("Input the insert position:\n");
scanf("%d",&m);
(a,b,n,m);
printf("Now b string is:\n%s\n",b);
}
void fun3()
{ int N[1000];
int n,i;
printf("Input the length of the array\n");
scanf("%d",&n);
printf("Input the the array\n");
for(i=0;i<n;i++)
scanf("%d",&N[i]);
printf("After inverse,the array is:\n");
Inv(N,n);
for(i=0;i<n;i++)
printf("%d ",N[i]);
printf("\n");

}
main()
{int a[MAX],b[MAX],kk;
int range=999,n=MAX;
char ch[1000],ac[1000],bc[1000];
do
{kk=menu_select();
switch(kk)
{case 1:{creat_randnum(a,range,n);break;}
case 2:{print_randnum(a,n);break;}
case 3:{fun1(ch);break;}
case 4:{fun2(ac,bc);break;}
case 5:{fun3();break;}
case 6:{break;}
case 0:{good_bye();exit(0);}
}
getchar();
getchar();
}while(kk!=0);
}

4. 在c語言中,菜單函數中如何寫才好返回上一個菜單啊

菜單函數用一個while循環套一個switch.裡面再用一個while套switch就行了.
再簡單一點的話直接用goto

5. C語言編寫菜單

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//值傳遞
voidfun1(chara)
{
a='b';
}
//地址傳遞
charfun2(char*a)
{
*a='a';
return*a;
}
//動態分配數組
voidfun3(char*&a)
{
a=(char*)malloc(sizeof(char));
}
charcaidan()
{
charchoose[5];
printf(" *************************************** ");
printf("┌──────┐ ");
printf("│菜單│ ");
printf("__________________________________ ");
printf("│1,按值傳遞| ");
printf("│2,按地址傳遞| ");
printf("│3,動態數組演示| ");
printf("│4,退出| ");
printf("└────────────────┙ ");
printf("************************************** ");
do
{
printf("請輸入您的選擇(1-4):");
scanf("%s",choose);
}while(strcmp(choose,"1")&&strcmp(choose,"2")&&strcmp(choose,"3")&&strcmp(choose,"4"));
//返回選擇的菜單,注意如果輸入太長這里會異常,因為choose[5],只申請了5個位元組。
returnchoose[0];
}
voidmain()
{
//執行菜單的選擇
while(1)
{
switch(caidan())
{
case'1':
{
chara='a';
fun1(a);
//按值傳遞內容不變
break;
}
case'2':
{
charb='b';
chara=fun2(&b);
//按地址傳遞內容會變
break;
}
case'3':
{
char*p=NULL;
fun3(p);
//動態數組分配成功
break;
}
case'4':exit(0);
}
}

}

6. 求一個C語言菜單函數的程序

我寫了一個完整程序給你參考,你可以在這個基礎上增加你的功能。
源代碼如下(vc++6.0下編譯通過):
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>

void func1()
{
printf("func1 ... \n");getch();
//coding here ...
}

void func2()
{
printf("func2 ... \n");getch();
//coding here ...
}

//any other func coding here ..

void menu() //主菜單
{
system("cls"); //清屏
printf("\n\t\t\t 菜單\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t# 1.xxxx #\n");
printf("\t\t\t# 2.xxxxxxx #\n");
printf("\t\t\t# //any other.... #\n");
printf("\t\t\t# 9.退出 #\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t 請選擇(1-9): ");
}
void main()
{
char select;
while(1)
{
menu();
system("COLOR 9f");
fflush(stdin);
scanf("%c",&select);

getchar(); //讀入回車符
if(!isdigit(select)) //如果不是數字字元
{
printf("\n\7Your select may be wrong, must enter the digit!\n");
getch();
}
else
{
switch (select)
{
case '1':
func1();
break;
case '2':
func2();
break;

//any other coding here ...

case '9':
exit(-1);//直接退出
default:
printf("\n\7\7Your selected digit may be wrong, select again!\n");
getch();
break;
}
}
}
}

希望對你有幫助。

7. c語言函數調用自己定義的函數裡面加菜單使用自己的函數!

voidFunctionChoice()
{
intindex=0,flag=0;
intx,y,z;

while(flag!=1)
{
system("cls");
printf("------Menu------ ");
printf("0.Exit ");
printf("1.jiafa ");
printf("2.jiafa ");
//morefunction...
printf(" Enterxandy:");
scanf("%d%d",&x,&y);
printf(" Choosefunction:");
scanf("%d",&index);

switch(index)
{
case0:
flag=1;
break;
case1:
z=jia(x,y);
break;
case2:
z=jian(x,y);
break;
//more...
}
printf(" z=%d ",z);
getchar();
}
}

8. 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的函數。

9. 用c語言寫一個簡單的菜單程序,一個主函數至少四個子函數急求!!就像這樣的。

分析:問題庫(QAS)用外部的配置文件保存,每次運行前先載入庫中已經建立的問題。然後再進入操作選擇。完整代碼如下:/*1.暫時不考慮漢字;2.可以用鏈表優化;3.將每一種操作封裝成一個函數;*/#include#include#include#include////////////////////////////////////////////////////////////////////////////字元串最大長度#defineMAX_STR_LEN2048//題庫上限#defineMAX_QAS_LEN1024//配置文件路徑#defineQAS_FILE_PATH"d:\\qasconfig.txt"////////////////////////////////////////////////////////////////////////////問題結點typedefstructtagQASNode{charQuestion[MAX_STR_LEN];charAnswer[MAX_STR_LEN];doubleScore;boolIsAnswered;}QASNode;//問題庫QASNode*g_QASLib[MAX_QAS_LEN];intg_QASIndex=0;////////////////////////////////////////////////////////////////////////////添加boolInsertToLib(char*pQuestion,char*pAnswer,doublescore){if(pQuestion==NULL||strcmp(pQuestion,"")==0||pAnswer==NULL)returnfalse;QASNode*pNewNode=(QASNode*)malloc(sizeof(QASNode));memset(pNewNode,0L,sizeof(QASNode));strcpy_s(pNewNode->Question,pQuestion);strcpy_s(pNewNode->Answer,pAnswer);pNewNode->Score=score;g_QASLib[g_QASIndex++]=pNewNode;returntrue;}//隨機一個不重復的序號intGetQuestionIndex(){intretVal=-1;inttryCnt=0;while(tryCnt++IsAnswered){retVal=randVal;break;}}returnretVal;}//////////////////////////////////////////////////////////////////////////intmain(){//打開配置文件FILE*pStream=NULL;if(fopen_s(&pStream,QAS_FILE_PATH,"a+")!=0){printf_s("Opentheconfigfilefailed.\n");return-1;}//載入已有的QASfseek(pStream,0L,SEEK_END);intfileSize=ftell(pStream);fseek(pStream,0L,SEEK_SET);while(!feof(pStream)&&fileSize){if(g_QASIndexQuestion);printf_s("Inputanswer:");chartempA[MAX_STR_LEN]={0};fflush(stdin);gets_s(tempA,MAX_STR_LEN);if(strcmp(tempA,g_QASLib[currentIndex]->Answer)==0){printf("Right!\n");totalSocore+=g_QASLib[currentIndex]->Score;g_QASLib[currentIndex]->IsAnswered=true;}else{printf("Wrong!\n");}}else{printf("你已經答對了所有的問題!\n");}}break;case2:{if(currentIndex!=-1){printf("Quest:%s\nScore=%.2f\n",g_QASLib[currentIndex]->Question,g_QASLib[currentIndex]->Score);}else{printf("請先選擇\"回答問題\"!\n");}}break;case3:{//一次只添加一個問題if(g_QASIndexQuestion,g_QASLib[i]->Answer,g_QASLib[i]->Score);}}break;default:break;}}printf("\n");return0;}運行起來後先建立問題庫!