很簡單 在 shuru shuhu 后面 加個 main()

創(chuàng)新互聯(lián)建站是一家專業(yè)提供依安企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、H5場景定制、小程序制作等業(yè)務(wù)。10年已為依安眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
就可以實現(xiàn)返回main函數(shù)了
#include stdlib.h
#include stdio.h
#include math.h
struct student
{
char name[10];
int xuehao;
int yuwen;
int shuxue;
}Xinxi[49];
void shuru()
{
int b;
int ch;
for(b=0;b49;b++)
{
printf("請輸入第%d名同學(xué)的學(xué)號:",b+1);
scanf("%d",Xinxi[b].xuehao);
system("cls");
printf("請輸入該同學(xué)的姓名:");
scanf("%s",Xinxi[b].name);
system ("cls");
printf("輸入語文成績:");
scanf("%d",Xinxi[b].yuwen);
system ("cls");
printf("輸入數(shù)學(xué)成績:");
scanf("%d",Xinxi[b].shuxue);
system("cls");
}
printf("輸入成功按任意鍵繼續(xù)");
ch = getchar();
main(); //就是這里
}
void shuchu()
{
int i;
int ch;
printf("請輸入要查詢的學(xué)生學(xué)號:");
scanf("%d",i);
system ("cls");
printf(" 學(xué)號 姓名 語文成績 數(shù)學(xué)成績\n");
printf("-----------------------------------------------------");
printf(" %d %s %d %d ",Xinxi[i-1].xuehao,Xinxi[i-1].name,Xinxi[i-1].yuwen,Xinxi[i-1].shuxue);
printf("按任意鍵返回主菜單");
ch = getchar();
system ("cls");
main(); //還有這里
}
void main()
{
int e;
printf("1.輸入學(xué)生成績\n");
printf("2.輸出學(xué)生成績\n");
printf("輸入選項:");
scanf("%d",e);
switch(e)
{
case 1:shuru();
system ("cls");
case 2:shuchu();
system ("cls");
}
}
C語言中主函數(shù)指的是main函數(shù)
主函數(shù)的兩個形參形式中的形參,允許從執(zhí)行環(huán)境中傳遞任意的多字節(jié)字符串(它們通常被稱為命令行參數(shù)),各個指針 argv.. argv[argc-1] 指向每個這些字符串的第一個字符。
argv[0] 是指向一個表示用于執(zhí)行該程序自身的名字的空結(jié)尾多字節(jié)字符串(或者當(dāng)執(zhí)行環(huán)境不支持時,為空字符串 "")的開頭字符的指針。這些字符串是可以改動的,雖然對它們的改動并不會被傳回給執(zhí)行環(huán)境:比如可以用 std::strtok 來使用它們。由 argv 所指向的數(shù)組的大小至少為 argc+1,其最后一個元素 argv[argc] 保證為一個空指針。
擴(kuò)展資料:
C++繼承了C語言的大部分特性,因此保留了“程序總是從main函數(shù)開始執(zhí)行,且總是默認(rèn)從main函數(shù)的return語句或結(jié)尾處結(jié)束運行”這一傳統(tǒng),但是要注意,C++中的main函數(shù)要想作為程序執(zhí)行的出入口,必須寫在全局(Global)范圍,不能寫成某個結(jié)構(gòu)體或某個類的成員。雖然main函數(shù)可以作為結(jié)構(gòu)體或者類的成員函數(shù),但相應(yīng)地會失去作為程序出入口的功能。
C++中全局main函數(shù)的書寫格式與C語言完全相同,功能也完全相同,且同一C++程序同樣只能有一個全局main函數(shù)。
1、對于窗口組件菜單,需要根據(jù)不同平臺,通過圖形編程接口,進(jìn)行菜單的編制。
例程:
#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;?/*按回車鍵確認(rèn)*/?
/*畫圖做菜單*/?
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)/*這里引用函數(shù),實現(xiàn)所要的功能*/?
{?
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出現(xiàn)的次數(shù)????????????????????**\n");
printf("\t\t**???2.統(tǒng)計字符串中大小寫字母,數(shù)字出現(xiàn)的次數(shù)????????**\n");
printf("\t\t**???3.將數(shù)字翻譯成英語??????????????????????????????**\n");
printf("\t\t**???4.結(jié)束??????????????????????????????????????????**\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++;?/*記錄相同的字符數(shù)*/
do
{????
if(a[++n]==b[++t])
{?
j++;
if(j==m)?
{
l++;/*子字符串相同數(shù)*/
j=0;/*判斷后相同字符數(shù)歸零*/
t=-1;/*判斷中if中++t;t將會歸零*/
}
}
else
{
j=0;
t=0;
break;/*如果不同跳出while循環(huán)讓for使n+1繼續(xù)判斷*/
}
}while(a[n]!='\0');/*查找完字符數(shù)組a結(jié)束*/
}
printf("子字符串出現(xiàn)次數(shù):\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("有數(shù)字:\n%d\n",k);
}
void?number()
{
l=num;
printf("請輸入一個數(shù)字:(0-10)\n");
fflush(stdin);
scanf("%d",m);
printf("%d對應(yīng)的英文是:\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");
}
給你一個簡單的菜單程序吧。其中的子函數(shù),填充成樓主所需即可。
#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; /*命令編號*/
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函數(shù)實現(xiàn)輸入功能序號執(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("輸入錯誤,請重新輸入!\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;
}運行結(jié)果
分析:問題庫(QAS)用外部的配置文件保存,每次運行前先載入庫中已經(jīng)建立的問題。然后再進(jìn)入操作選擇。完整代碼如下:/*1.暫時不考慮漢字;2.可以用鏈表優(yōu)化;3.將每一種操作封裝成一個函數(shù);*/#include#include#include#include////////////////////////////////////////////////////////////////////////////字符串最大長度#defineMAX_STR_LEN2048//題庫上限#defineMAX_QAS_LEN1024//配置文件路徑#defineQAS_FILE_PATH"d:\\qasconfig.txt"http:////////////////////////////////////////////////////////////////////////////問題結(jié)點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ī)一個不重復(fù)的序號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)答對了所有的問題!\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;}運行起來后先建立問題庫!
main(),在c語言中稱之為“主函數(shù)”,一個c程序有且僅有一個main函數(shù),任何一個c程序總是從main函數(shù)開始執(zhí)行,main函數(shù)后面的一對圓括號不能省略。
具體形式如下:
main()
{ ? }
被大括號{ }括起來的內(nèi)容稱為main函數(shù)的函數(shù)體,這部分內(nèi)容就是計算機(jī)要執(zhí)行的內(nèi)容。
在{ ?}里面每一句話后面都有一個分號(;),在c語言中,我們把以一個分號結(jié)尾的一句話叫做一個c語言的語句,分號是語句結(jié)束的標(biāo)志。
擴(kuò)展資料:
函數(shù)是一組一起執(zhí)行一個任務(wù)的語句。每個 C 程序都至少有一個函數(shù),即主函數(shù)?main()?,所有簡單的程序都可以定義其他額外的函數(shù)。
您可以把代碼劃分到不同的函數(shù)中。如何劃分代碼到不同的函數(shù)中是由您來決定的,但在邏輯上,劃分通常是根據(jù)每個函數(shù)執(zhí)行一個特定的任務(wù)來進(jìn)行的。
函數(shù)聲明告訴編譯器函數(shù)的名稱、返回類型和參數(shù)。函數(shù)定義提供了函數(shù)的實際主體。
C 標(biāo)準(zhǔn)庫提供了大量的程序可以調(diào)用的內(nèi)置函數(shù)。例如,函數(shù)?strcat()?用來連接兩個字符串,函數(shù)?memcpy()?用來復(fù)制內(nèi)存到另一個位置。
函數(shù)還有很多叫法,比如方法、子例程或程序,等等。
參考資料來源:百度百科-C語言函數(shù)
網(wǎng)站欄目:c語言主函數(shù)菜單,c語言返回主菜單函數(shù)
網(wǎng)址分享:http://chinadenli.net/article36/dsgpgsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站改版、網(wǎng)站設(shè)計公司、App開發(fā)、域名注冊、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)