當前位置:首頁 » 編程語言 » c語言mainmenu是啥意思
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言mainmenu是啥意思

發布時間: 2023-01-22 19:58:15

A. 通訊錄管理 c語言課程設計

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct record
{
char id[10];
char name[20];
char phone[20];
char adress[40];
char postcode[10];
char e_mail[30];
}student[500];
int num=0;//外部變數num為文件中的紀錄數
FILE *fp;
void mainmenu();//主菜單
void newrecord();//新添紀錄
void searchmenu();//查詢菜單
void searchbyid();//按學號查詢
void searchbyname();//按姓名查詢
void searchbyphone();//按電話查詢
void deletemenu();//刪除菜單
void deleteall();//刪除所有
void deleteone();//刪除單個
void showall();//顯示所有
void readfromfile();//讀取文件
void writetofile();//寫入文件
void deletebyid();//按學號刪除
void deletebyname();//按姓名刪除
void listbyid();//按學號排序
void listbyname();//按姓名排序
void listmenu();//排序菜單
void main()
{
readfromfile();
while (1)
{
mainmenu();
}
}
void readfromfile()//從文件導入
{
if((fp=fopen("student.bin","rb"))==NULL)
{
printf("\n\t\t通訊錄文件不存在");
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t建立失敗");
exit(0);
}
else
{
printf("\n\t\t通訊錄文件已建立");
printf("\n\t\t按任意鍵進入主菜單");
getch();
return;
}
exit(0);
}
fseek(fp,0,2); /*文件位置指針移動到文件末尾*/
if (ftell(fp)>0) /*文件不為空*/
{
rewind(fp); /*文件位置指針移動到文件開始位置*/
for (num=0;!feof(fp) && fread(&student[num],sizeof(struct record),1,fp);num++);
printf("\n\t\t文件導入成功");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
printf("\n\t\t文件導入成功");
printf("\n\t\t通訊錄文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
void mainmenu()//主菜單
{
char choic;
system("cls");
printf("\n\t\t******************** 主菜單 ********************");
printf("\n\t\t*********** 1-新添紀錄 2-查詢菜單 ************");
printf("\n\t\t*********** 3-刪除菜單 4-記錄排序 ************");
printf("\n\t\t*********** 5-保存退出 6-不保存退出***********");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':newrecord();break;
case '2':searchmenu();break;
case '3':deletemenu();break;
case '4':listmenu();break;
case '5':writetofile();break;
case '6':exit(0);
default:mainmenu();
}
}
void searchmenu()//查詢菜單
{
char choic;
system("cls");
printf("\n\t\t******************* 查詢菜單 *******************");
printf("\n\t\t********** 1-顯示所有 2-按學號查詢 ************");
printf("\n\t\t********** 3-按姓名查詢4-按電話查詢 ************");
printf("\n\t\t********** 5-返回主菜單 ************");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':showall();break;
case '2':searchbyid();break;
case '3':searchbyname();break;
case '4':searchbyphone();break;
case '5':mainmenu();break;
}
}
void deletemenu()//刪除菜單
{
char choic;
if(num==0)
{
printf("\n\t\t對不起,文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
system("cls");
printf("\n\t\t******************* 刪除菜單 *******************");
printf("\n\t\t*********** 1-刪除所有 2-刪除單個 ***********");
printf("\n\t\t*********** 3-返回主菜單 ***********");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':deleteall();break;
case '2':deleteone();break;
case '3':mainmenu();break;
default:mainmenu();break;
}
}
void deleteall()//刪除所有
{
printf("\n\t\t確認刪除?(y/n)");
if (getch()=='y')
{
fclose(fp);
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t不能打開文件,刪除失敗");
readfromfile();
}
num=0;
printf("\n\t\t紀錄已刪除,按任意鍵返回主菜單");
getch();
return;
}
else
return;
}
void deletebyname()//按姓名刪除
{
int a=0;
int findmark=0;
int j;
int deletemark=0;
int i;
char name[20];
printf("\n\t\t請輸入要刪除學生姓名:");
scanf("%s",name);
for (i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==NULL)
{
printf("\n\t\t以下是您要刪除的學生紀錄:");
findmark++;
printf("\n\t\t________________________________");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t________________________________");
printf("\n\t\t是否刪除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*紀錄移動,從stud數組中刪除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t刪除成功");
if((i+1)<num)
{
printf("\n\t\t是否繼續刪除相同姓名的同學信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf("\n\t\t是否繼續刪除相同姓名的同學信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
}
else
continue;
}
if ((deletemark==0)&&(findmark==0))
{
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
else if (findmark!=0)
{
printf("\n\t\t沒有重名信息");
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
}
void deletebyid()//按學號刪除
{
int i,j;
int deletemark=0;
char id[20];
printf("\n\t\t請輸入要刪除學生學號:");
scanf("%s",id);
if(num==0)
{
printf("\n\t\t對不起,文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
for (i=0;i<num;i++)
{
if (strcmp(student[i].id,id)==NULL)
{
printf("\n\t\t以下是您要刪除的學生紀錄:");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t是否刪除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*紀錄移動,從stud數組中刪除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t刪除成功");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
else
return;
}
continue;

}
if (deletemark==0)
{
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
}
void newrecord()//添加紀錄
{
printf("\n\t\t**************** 請輸入學生信息 ****************\n");
printf("\n\t\t輸入學號:");
scanf("%s",&student[num].id);
printf("\n\t\t輸入姓名:");
scanf("%s",&student[num].name);
printf("\n\t\t輸入電話號碼:");

scanf("%s",&student[num].phone);
printf("\n\t\t輸入地址:");
scanf("%s",&student[num].adress);
printf("\n\t\t輸入郵編:");
scanf("%s",&student[num].postcode);
printf("\n\t\t輸入e-mail:");
scanf("%s",&student[num].e_mail);
num++;
printf("\n\t\t是否繼續添加?(Y/N):");
if (getch()=='y')
newrecord();
return;
}
void showall()//顯示所有
{
int i;
system("cls");
if(num!=0)
{
printf("\n\t\t*************** 以下為通訊錄所有信息************");
for (i=0;i<num;i++)
{
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\t郵編: %s",student[i].postcode);
printf("\n\t\te-mail:%s",student[i].e_mail);

printf("\t\t");
if (i+1<num)
{
printf("\n\t\t__________________________");
system("pause");
}
}
printf("\n\t\t************************************************");
}
else
printf("\n\t\t通訊錄中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單:");
getch();
return;
}

//按電話查詢
void searchbyphone()
{
int mark=0;
int i;
int a=0;
printf("\n\t\t****************** 按電話查找 ******************");
char phone[10];
printf("\n\t\t請輸入學號:");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==0)
{
printf("\n\t\t************** 以下是您查找的學生信息 **********");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
printf("\n\t\t按任意鍵返回主菜單:");
mark++;
getch();
return;
}
}
if (mark==0)
{
printf("\n\t\t沒有改學生的信息");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
}
void searchbyname()//按姓名查詢
{
int mark=0;
int i;
int a=0;
printf("\n\t\t***************** 按姓名查找 *******************");
char name[20];
printf("\n\t\t請輸入姓名:");
scanf("%s",name);
for(i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==0)
{
printf("\n\t\t************* 以下是您查找的學生信息 ***********");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
mark++;
if ((i+1)<num)
{
printf("\n\t\t是否繼續查找相同名字的學生信息:(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
else
return;
}
else
{
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
}
}
if(mark!=0)
{
printf("\n\t\t沒有相同姓名的學生紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
else
{
printf("\n\t\t沒有相同姓名的學生紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
}
void searchbyid()//按學號查詢
{
int mark=0;
int i;
int a=0;
printf("\n\t\t****************** 按學號查找 ******************\n");
char id[10];
printf("\n\t\t請輸入學號:");
scanf("%s",id);
for(i=0;i<num;i++)
{
if (strcmp(student[i].id,id)==0)
{
printf("\n\t\t************** 以下是您查找的學生信息 **********");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
printf("\n\t\t按任意鍵返回主菜單:");
mark++;
getch();
return;
}

}
if (mark==0)
{
printf("\n\t\t沒有改學生的信息");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
}
void writetofile()//寫入文件
{
int i;
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t文件打開失敗");
}
for (i=0;i<num;i++)
{
if (fwrite(&student[i],sizeof(struct record),1,fp)!=1)
{
printf("\n\t\t寫入文件錯誤!\n");
}
}
fclose(fp);
printf("\n\t\t通訊錄文件已保存");
printf("\n\t\t按任意鍵退出程序\n\t\t");
exit(0);

}
void deleteone()//刪除單個
{
char choic;
printf("\n\t\t1-按學號刪除 2-按姓名刪除");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':deletebyid();break;
case '2':deletebyname();break;
}
}
void listbyname()//按姓名排序
{
int i,j;
struct record tmp;
for (i=1;i<num;i++)
{
if(strcmp(student[i].name,student[i-1].name)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while ((strcmp(tmp.name,student[j].name)<0&&j>=0));
student[j+1]=tmp;
}
}
printf("\n\t\t排序成功,是否顯示?(y/n)");
if (getch()=='y')
showall();
return;
}
void listbyid()//按學號排序
{
int i,j;
struct record tmp;
for (i=1;i<num;i++)
{
if(strcmp(student[i].id,student[i-1].id)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while ((strcmp(tmp.id,student[j].id)<0)&&j>=0);
student[j+1]=tmp;
}
}
printf("\n\t\t排序成功,是否顯示?(y/n)");
if (getch()=='y')
showall();
return;
}
void listmenu()//排序菜單
{
char choic;
if(num==0)
{
printf("\n\t\t對不起,文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
system("cls");
printf("\n\t\t******************* 排序菜單 *******************");
printf("\n\t\t********** 1-按學號排序 2-按姓名排序 **********");
printf("\n\t\t********** 3-返回主菜單 **********");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch(choic)
{
case '1':listbyid();break;
case '2':listbyname();break;
case '3':mainmenu();break;
default:mainmenu();break;
}
}

B. 求c語言中typedef的用法

不會報錯的。。我這里正常通過 除了char class[10];
剛才沒看到你的typedef
你的結構體類型聲明在void mainmenu(struct stmessage *)這個函數之後
編譯器編譯到這行時還沒看到stmessage的定義
加了struct的話可以告訴編譯器它是一個即定的類型(可能在代碼之其他部分被聲明過),所以編譯器就會自動去尋找其定義了,這其實是一種隱式聲明,而且和你想的完全不一樣
編譯器會把struct stmessage *當作是stmessage這個結構體類型的指針,而不是message結構體的類型別名的指針,如果你在函數里加入使用這個變數的代碼就要出錯了

#include <stdio.h>

void foo(struct stmessage* a)
{
printf("%s", a->name);
}

typedef struct message //學生的基本信息
{
char name[10];
int id;
char gender[10];
int phnumber;
char address[50];
char school[10];
char class[10];
struct message *next;
}stmessage;

int main()
{
stmessage a = {"123"};
foo(&a);
}