c++基础菜单问题,求高手指教

王朝知道·作者佚名  2009-12-02
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

//=====================================

// f0507.cpp

// 函数指针数组

//=====================================

#include<iostream>

using namespace std;

//-------------------------------------

typedef void (*MenuFun)();

void f1(){ cout<<"good!\n"; }

void f2(){ cout<<"better!\n"; }

void f3(){ cout<<"best!\n"; }

//-------------------------------------

int main(){

MenuFun fun[]={f1,f2,f3};

for(int choice=1; choice; ){

cout<<"1-----display good\n"

<<"2-----display better\n"

<<"3-----display best\n"

<<"0-----exit\n"

<<"Enter your choice:";

cin>>choice;

switch(choice){

case 1: fun[0](); break;

case 2: fun[1](); break;

case 3: fun[2](); break;

case 0: return 0;

default: cout<<"you entered a wrong key.\n";

}

}

}//====================================

按1-3键输出不同的欢迎词,按0退出程序

仿照以上例子编一个有0-9的10个菜单,谢谢了

參考答案:

#include<iostream>

using namespace std;

//-------------------------------------

typedef void (*MenuFun)();

void f1(){ cout<<"good!\n"; }

void f2(){ cout<<"better!\n"; }

void f3(){ cout<<"best!\n"; }

void f4(){ cout<<"good!4\n"; }

void f5(){ cout<<"better!5\n"; }

void f6(){ cout<<"best!6\n"; }

void f7(){ cout<<"good!7\n"; }

void f8(){ cout<<"better!8\n"; }

void f9(){ cout<<"best!9\n"; }

//-------------------------------------

int main(){

MenuFun fun[]={f1,f2,f3,f4,f5,f6,f7,f8,f9};

for(int choice=1; choice; ){

cout<<"1-----display good\n"

<<"2-----display better\n"

<<"3-----display best\n"

<<"4-----display good4\n"

<<"5-----display better5\n"

<<"6-----display best6\n"

<<"7-----display good7\n"

<<"8-----display better8\n"

<<"9-----display best9\n"

<<"0-----exit\n"

<<"Enter your choice:";

cin>>choice;

switch(choice){

case 1: fun[0](); break;

case 2: fun[1](); break;

case 3: fun[2](); break;

case 4: fun[3](); break;

case 5: fun[4](); break;

case 6: fun[5](); break;

case 7: fun[6](); break;

case 8: fun[7](); break;

case 9: fun[8](); break;

case 0: return 0;

default: cout<<"you entered a wrong key.\n";

}

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航