分析:问题库(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;}运行起来后先建立问题库!
创新互联公司专注于清水河企业网站建设,自适应网站建设,商城建设。清水河网站建设公司,为清水河等地区提供建站服务。全流程按需求定制网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
#include iostream
#include stdlib.h
#include conio.h
#include ctype.h
using namespace std;
void xuanzepaixu() //选择排序函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void erfenpaixu() //二分查找函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void menu() //主菜单
{
system("cls"); //清屏
printf("\n\t\t\t 菜单\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t# 1选择排序 #\n");
printf("\t\t\t# 2二分查找 #\n");
printf("\t\t\t# 3返回 #\n");
printf("\t\t\t# 4---关闭 #\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t 请选择(1-4) =:");
}
void main()
{
char select;
while(1)
{
menu();
system("COLOR 9f");
scanf("%c",select);
if(select=='3')
break; //返回上级菜单
else
{
getchar(); //读入回车符
if(!isdigit(select)) //如果不是数字字符
{
printf("\n\7Your select may be wrong, must enter the digit!\n");_getch();
}
else
{
switch (select)
{
case '1': xuanzepaixu();break;
case '2': erfenpaixu();break;
case '4':exit(0); //直接退出
default:
{
printf("\n\7\7Your selected digit may be wrong, select again!\n");
_getch();
break;
}
}
}
}
}
}
给你一个简单的菜单程序吧。其中的子函数,填充成楼主所需即可。
#include stdio.h
/*子函数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;
}
运行结果
1、对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。
例程:
#includestdio.h
#includegraphics.h
#includeconio.h
void main()
{
char str;
int i,k,choice=1;
int gd=DETECT,gm;
initgraph(gd,gm," ");
setbkcolor(2);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
outtextxy(140,150,"B. The Digital Clock.");
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)
{ case 1: setlinestyle(0,0,3);
rectangle(170,115,400,145);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
settextstyle(3,0,3);
outtextxy(140,150,"B. The Digital Clock.");
outtextxy(140,180,"C. Exit.");
break;
case 2: setlinestyle(0,0,3);
rectangle(170,145,400,175);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
settextstyle(3,0,3);
outtextxy(140,150,"B. The Digital Clock.");
settextstyle(3,0,3);
outtextxy(140,180,"C. Exit.");
break;
case 3: settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
outtextxy(140,150,"B. The Digital Clock.");
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)/*这里引用函数,实现所要的功能*/
{
case 1: cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,120,"No.1 have not built."); break;
case 2: cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,150,"No.2 have not built.");
break;
case 3: exit(0);
}
getch();
closegraph();
}
2、对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。
例程:
#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;nk;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");
}