C++编写一个菜单显示程序

C++编写一个菜单显示程序,第1张

#include <cstdlib>

#include <iostream>

#include <stringh> using namespace std;void showMenu()

{

cout<<"主 菜 单"<<endl;

cout<<"1添加记录 2显示记录"<<endl;

cout<<"3读取记录 4保存记录"<<endl;

}

void input()

{

int select;

cin>>select;

switch(select)

{

case 0:

exit(1); //程序正常退出

case 1:

cout<<"你选择了1"<<endl;

break;

case 2:

cout<<"你选择了2"<<endl;;

break;

case 3:

cout<<"你选择了3"<<endl;

break;

case 4:

cout<<"你选择了4"<<endl;

break;

default:

cout<<"选择错误,请重新输入!"<<endl;

input();//重新选择

}

}

int main(int argc, char argv[])

{

//函数声明

void showMenu();

void input();

//调用

showMenu();

input();

system("PAUSE");

return EXIT_SUCCESS;

}

#include

<stdioh>

#include

<mathh>

void

armstrong(int

a)

{

int

i;

int

n

=

1;

int

b

=

a;

int

s

=

0;

while(b

/=

10)

n++;

b

=

a;

for(i

=

0;

i

<

n;

i

++){

s

+=

(int)pow(b%10,

n);

b

/=

10;

}

if(s

==

a){

printf("%d

是水仙花数!\n",

a);

}else{

printf("%d

不是水仙花数!\n",

a);

}

}

void

prime(int

a)

{

int

i;

if(a

<

2)

{

printf("%d

不是素数!\n",

a);

return

;

}

for(i

=

2;

i

<

a;

i

++){

if(a

%

i

==

0)

{

printf("%d

不是素数!\n",

a);

return

;

}

}

printf("%d

是素数!\n",

a);

}

void

max(int

a,

int

b,

int

c)

{

if(b

>

a)

a

=

b;

if(c

>

a)

a

=

c;

printf("最大值是:%d\n",

a);

}

void

factorial(int

a)

{

int

i;

int

f

=

1;

for(i

=

a;

i

>1;

i

--)

f

=

i;

printf("%d

的阶乘是:%d\n",

a,

f);

}

int

main(int

argc,

char

argv)

{

int

a,

b,

c;

int

type

;

int

run

=

1;

printf("本程序可完成某些数学计算:\n");

printf(

"\n"

"

1判断一个数是否为水仙花数。

\n"

"

2判断一个数是否为素数。

\n"

"

3求三个数的最大值。

\n"

"

4求一个数的阶乘。

\n"

"

0退出。

\n"

"\n");

do{

printf("请按菜单进行选择(0-4):");

scanf("%d",

&type);

switch(type){

case

0:

while(1){

getchar();

printf("你确定退出吗?(y/n)");

a

=

getchar();

switch(a){

case

'y':

run

=

0;

break;

case

'n':

run

=

1;

break;

default:

break;

}

if((a

==

'y')

||

(a

==

'n'))

break;

}

break;

case

1:

printf("请输入一个数:");

scanf("%d",

&a);

armstrong(a);

break;

case

2:

printf("请输入一个数:");

scanf("%d",

&a);

prime(a);

break;

case

3:

printf("请输入三个数:");

scanf("%d%d%d",

&a,

&b,

&c);

max(a,

b,

c);

break;

case

4:

printf("请输入一个数:

");

scanf("%d",

&a);

factorial(a);

break;

default:

break;

}

}while(run);

return

0;

}

分析:问题库(QAS)用外部的配置文件保存,每次运行前先载入库中已经建立的问题。然后再进入 *** 作选择。完整代码如下:/1暂时不考虑汉字;2可以用链表优化;3将每一种 *** 作封装成一个函数;/#include#include#include#include////////////////////////////////////////////////////////////////////////////字符串最大长度#defineMAX_STR_LEN2048//题库上限#defineMAX_QAS_LEN1024//配置文件路径#defineQAS_FILE_PATH"d:\\qasconfigtxt"////////////////////////////////////////////////////////////////////////////问题结点typedefstructtagQASNode{charQuestion[MAX_STR_LEN];charAnswer[MAX_STR_LEN];doubleScore;boolIsAnswered;}QASNode;//问题库QASNodeg_QASLib[MAX_QAS_LEN];intg_QASIndex=0;////////////////////////////////////////////////////////////////////////////添加boolInsertToLib(charpQuestion,charpAnswer,doublescore){if(pQuestion==NULL||strcmp(pQuestion,"")==0||pAnswer==NULL)returnfalse;QASNodepNewNode=(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(){//打开配置文件FILEpStream=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;}运行起来后先建立问题库!

/

@(#)SWjava

SW application

@author

@version 100 2013/11/21

/

import javautil;

public class SW {

public static void main(String[] args)

{

Systemoutprintln("1打开电视");

Systemoutprintln("2关闭电视");

Systemoutprintln("3打开窗帘");

Systemoutprintln("4关闭窗帘");

Scanner sc=new Scanner(Systemin);

int num=scnextInt();

switch(num)

{

case 1:{Systemoutprintln("电视on");break;}

case 2:{Systemoutprintln("电视off");break;}

case 3:{Systemoutprintln("窗帘open");break;}

case 4:{Systemoutprintln("窗帘close");break;}

default:Systemoutprintln("输入错误");

}

}

}

#include <stdioh>

struct mi

{

   char menuItem[20];

   float menuPrize;

};

struct mi menuList[3]=

{

   {"Egg",1},

   {"Rice",5},

   {"Nuddle",6}

};

void order()//定菜 算帐单 

{

     int i[3],j,k;

     double sum=0;

     double texafter;

     printf("\nyou could choose anyone with any food");

     for(j=0;j<3;j++)

     {

         printf("\n%d %-10s",j+1,menuList[j]menuItem);

         printf("   %31f 元\n",menuList[j]menuPrize);

         printf(" %s要几份:",menuList[j]menuItem);

         scanf("%d",&k);

         i[j]=k;

      }

         system("cls");

         printf("您已定了\n");

         for(j=0;j<3;j++)

         {

         if(i[j]!=0)

         printf("%d份 %s\n",i[j],menuList[j]menuItem);

         }

        for(j=0;j<3;j++)

        sum+=menuList[j]menuPrizei[j];

        texafter=sum105;

        printf("Sum=%-52lf  ",sum);

        printf("Texafter=%-52lf",texafter);

        printf("\npress any key to continue");

        getch();

        system("cls");

}

       

int main()

{

    int i=1,j=0;

    while(i)

    {

    printf("\n\n---------Please select----------\n");

    printf("---------1   菜单    ----------\n");

    printf("---------2   点菜    ----------\n");

    printf("---------0   退出    ----------\n");

    scanf("%d",&i);

    switch(i)

    {

         case 1:

              for(j=0;j<3;j++)

              {

                  printf("%d %-10s",j+1,menuList[j]menuItem);

                  printf("   %31f 元\n",menuList[j]menuPrize);

              }   getch();system("cls"); break;

         case 2:order();break;

         case 0:break;

    }

    } 

}

//新手学习,希望可以帮助你

给你一个简单的菜单程序吧。其中的子函数,填充成楼主所需即可。

#include <stdioh>

/子函数1/

fun1() {

printf ("子函数1\n");

}

/子函数2/

fun2() {

printf ("子函数2\n");

}

/子函数3/

fun3() {

printf ("子函数3\n");

}

/子函数4/

fun4() {

printf ("子函数4\n");

}

int main(void) {

int key; /命令编号/

do {

system("cls"); /清屏/

/打印菜单/

printf ("======================================================\n");

printf ("  #\t功能详情\n");

printf ("------------------------------------------------------\n");

printf ("  1\t功能1\n");

printf ("  2\t功能2\n");

printf ("  3\t功能3\n");

printf ("  4\t功能4\n");

printf ("  5\t退出\n");

printf ("======================================================\n");

printf ("\n");

printf("请输入命令编号以开启 *** 作:");

/输入命令编号/

scanf("%d",&key); 

printf ("\n");

/switch函数实现输入功能序号执行相应函数/

switch (key) {

case 1: fun1(); break; /子函数1/

  case 2: fun2(); break; /子函数2/

  case 3: fun3(); break; /子函数3/

  case 4: fun4(); break; /子函数4/

  case 5: printf("程序结束!按任意键退出\n\n"); break;

  default:printf("输入错误,请重新输入!\n\n"); break;

}

/屏幕暂留/

if (key!=5) {

printf ("\n");

printf("按Enter键继续\n");

printf ("\n");

fflush(stdin);

getch ();

}

} while (key!=5);

/屏幕暂留/

fflush(stdin);

getch ();

    return 0;

}

运行结果

将后台代码实现的功能以菜单的形式显示出来,供用户使用,直接调用过程名即可。

可在Workbooks_Open中调用可实现打开Excel即加载,易用。(1)先点击按钮生成菜单,然后再打开Excel时,此时新Excel中会自动加载同样的菜单。所以,我们让用户先打开我们的程序,然后再打开她要 *** 作的表,然后再她的表上点击按钮即可。(2)此菜单是临时菜单不会永久停留在Excel中,当当前Excel进程完全结束后,就会消失。所以一般不用担心,会对Excel程序造成损坏什么的。(3)当你的两个Excel不是一个进程时,那么菜单是不会在另一个上显示的。比如说,用了两次CreateObject创建的Excel就不属于同一个进程,一个显示菜单后,另一个,即使是后创建打开的,也是不显示菜单的。

以上就是关于C++编写一个菜单显示程序全部的内容,包括:C++编写一个菜单显示程序、C语言菜单程序编写、用c语言写一个简单的菜单程序,一个主函数至少四个子函数急求!!就像这样的。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10045125.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存