給你一個(gè)簡(jiǎn)單的菜單程序吧。其中的子函數(shù),填充成樓主所需即可。
創(chuàng)新互聯(lián)專注于易門企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站開發(fā)。易門網(wǎng)站建設(shè)公司,為易門等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
#include
/*子函數(shù)1*/
fun1() {
printf ("子函數(shù)1\n");
}
/*子函數(shù)2*/
fun2() {
printf ("子函數(shù)2\n");
}
/*子函數(shù)3*/
fun3() {
printf ("子函數(shù)3\n");
}
/*子函數(shù)4*/
fun4() {
printf ("子函數(shù)4\n");
}
int main(void) {
int key; /*命令編號(hào)*/
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("請(qǐng)輸入命令編號(hào)以開啟操作:");
/*輸入命令編號(hào)*/
scanf("%d",key);
printf ("\n");
/*switch函數(shù)實(shí)現(xiàn)輸入功能序號(hào)執(zhí)行相應(yīng)函數(shù)*/
switch (key) {
case 1: fun1(); break; /*子函數(shù)1*/
case 2: fun2(); break; /*子函數(shù)2*/
case 3: fun3(); break; /*子函數(shù)3*/
case 4: fun4(); break; /*子函數(shù)4*/
case 5: printf("程序結(jié)束!按任意鍵退出...\n\n"); break;
default:printf("輸入錯(cuò)誤,請(qǐng)重新輸入!\n\n"); break;
}
/*屏幕暫留*/
if (key!=5) {
printf ("\n");
printf("按Enter鍵繼續(xù)...\n");
printf ("\n");
fflush(stdin);
getch ();
}
} while (key!=5);
/*屏幕暫留*/
fflush(stdin);
getch ();
return 0;
}運(yùn)行結(jié)果
#includestdio.h
int?jia(int?a,int?b)
{
return?a+b;
}
int?jian(int?a,int?b)
{
return?a-b;
}
int?cheng(int?a,int?b)
{
return?a*b;
}
int?zhengchu(int?a,int?b)
{
if(!b)return?0;
else?return?a/b;
}
int?mod(int?a,int?b)
{
if(!b)return?0;
else?return?a%b;
}
int?main()
{
int?x=5,y=2;
printf("x+y=%d\n",jia(x,y));
printf("x-y=%d\n",jian(x,y));
printf("x*y=%d\n",cheng(x,y));
printf("x/y=%d......%d\n",zhengchu(x,y),mod(x,y));
}
分析:?jiǎn)栴}庫(QAS)用外部的配置文件保存,每次運(yùn)行前先載入庫中已經(jīng)建立的問題。然后再進(jìn)入操作選擇。完整代碼如下:/*1.暫時(shí)不考慮漢字;2.可以用鏈表優(yōu)化;3.將每一種操作封裝成一個(gè)函數(shù);*/#include#include#include#include////////////////////////////////////////////////////////////////////////////字符串最大長(zhǎng)度#defineMAX_STR_LEN2048//題庫上限#defineMAX_QAS_LEN1024//配置文件路徑#defineQAS_FILE_PATH"d:\\qasconfig.txt"http:////////////////////////////////////////////////////////////////////////////問題結(jié)點(diǎn)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;}//隨機(jī)一個(gè)不重復(fù)的序號(hào)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("你已經(jīng)答對(duì)了所有的問題!\n");}}break;case2:{if(currentIndex!=-1){printf("Quest:%s\nScore=%.2f\n",g_QASLib[currentIndex]-Question,g_QASLib[currentIndex]-Score);}else{printf("請(qǐng)先選擇\"回答問題\"!\n");}}break;case3:{//一次只添加一個(gè)問題if(g_QASIndexQuestion,g_QASLib[i]-Answer,g_QASLib[i]-Score);}}break;default:break;}}printf("\n");return0;}運(yùn)行起來后先建立問題庫!
#includestdio.h
float?sum(float?x,?float?y){
return?x?+?y;
}
float??quotient?(float?x,?float?y){
return?x?/?y;
}
float?product(float?x,?float?y){
return?x?*?y;
}
float?differrent(float?x,?float?y){
return?x?-?y;
}
int?main(void){
float?a,?b;
a?=?12;?
b?=?2;
printf("sum?is%f\n",?sum(a,?b));
printf("quotient?is%f\n",?quotient(a,?b));
printf("product?is%f\n",?product(a,?b));
printf("differrent?is%f\n",?differrent(a,?b));
}
函數(shù)的知識(shí)和函數(shù)的調(diào)用
上面的代碼是函數(shù)的嵌套使用。函數(shù)作為另一個(gè)函數(shù)(printf)的參數(shù)
網(wǎng)站題目:c語言編寫4個(gè)子函數(shù),c語言程序有幾個(gè)子函數(shù)
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article46/dssehhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、App開發(fā)、用戶體驗(yàn)、服務(wù)器托管、網(wǎng)站營(yíng)銷、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)