怎么用C语言编写菜单?

怎么用C语言编写菜单?,第1张

#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)

}

大概就是这样做的。。

分析:问题库(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]doubleScoreboolIsAnswered}QASNode//问题库QASNode*g_QASLib[MAX_QAS_LEN]intg_QASIndex=0////////////////////////////////////////////////////////////////////////////添加boolInsertToLib(char*pQuestion,char*pAnswer,doublescore){if(pQuestion==NULL||strcmp(pQuestion,"")==0||pAnswer==NULL)returnfalseQASNode*pNewNode=(QASNode*)malloc(sizeof(QASNode))memset(pNewNode,0L,sizeof(QASNode))strcpy_s(pNewNode->Question,pQuestion)strcpy_s(pNewNode->Answer,pAnswer)pNewNode->Score=scoreg_QASLib[g_QASIndex++]=pNewNodereturntrue}//随机一个不重复的序号intGetQuestionIndex(){intretVal=-1inttryCnt=0while(tryCnt++IsAnswered){retVal=randValbreak}}returnretVal}//////////////////////////////////////////////////////////////////////////intmain(){//打开配置文件FILE*pStream=NULLif(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]->Scoreg_QASLib[currentIndex]->IsAnswered=true}else{printf("Wrong!\n")}}else{printf("你已经答对了所有的问题!\n")}}breakcase2:{if(currentIndex!=-1){printf("Quest:%s\nScore=%.2f\n",g_QASLib[currentIndex]->Question,g_QASLib[currentIndex]->Score)}else{printf("请先选择\"回答问题\"!\n")}}breakcase3:{//一次只添加一个问题if(g_QASIndexQuestion,g_QASLib[i]->Answer,g_QASLib[i]->Score)}}breakdefault:break}}printf("\n")return0}运行起来后先建立问题库!


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

原文地址: http://outofmemory.cn/yw/11341438.html

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

发表评论

登录后才能评论

评论列表(0条)

保存