最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

C語(yǔ)言結(jié)構(gòu)、聯(lián)合、枚舉

2023-01-31 09:49 作者:虛云幻仙  | 我要投稿

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>


void func1(void);

void func2(void);

void manybook(void);

char* s_gets(char* s, int limit);

void generate1(void);

void get_ins_info(struct insurance* sp);

void get_ins_info2(struct insurance s);

void booksave(void);

void seat_reserve(void);

void show_empty_num(struct seat* sp, int size);

void show_empty_list(struct seat* sp, int size);

void show_alphabetical_list(struct seat* sp, int size);

void assign_seat(struct seat* sp, int size);

void delete_seat(struct seat* sp, int size);

void other1(void);

void func1(void)

{

???? struct month { char name[20]; char abbre[4]; int days; int num; }; //structure結(jié)構(gòu),可將多種類型組成一個(gè)整體。

???? // 格式:struct 結(jié)構(gòu)標(biāo)識(shí)符 {字段1類型(field字段/member成員,結(jié)構(gòu)中包含的數(shù)據(jù)的類型) 字段1標(biāo)識(shí)符;字段2類型 字段2標(biāo)識(shí)符...} 變量名;

???? //不寫變量名只寫標(biāo)識(shí)符則為只聲明結(jié)構(gòu)類型,不創(chuàng)建變量,聲明結(jié)構(gòu)類型不會(huì)產(chǎn)生內(nèi)存塊。而寫變量名會(huì)創(chuàng)建該結(jié)構(gòu),不需要多次創(chuàng)建該結(jié)構(gòu)可以省略標(biāo)識(shí)符

???? struct month January = { "January","jan",31,1 }; // 創(chuàng)建結(jié)構(gòu)變量時(shí)格式: struct 結(jié)構(gòu)標(biāo)識(shí)符 變量名 ,定義變量時(shí)可以使用{value1,value2...}初始化結(jié)構(gòu),需按照結(jié)構(gòu)聲明的字段順序,如果不初始化則該內(nèi)存塊仍然保留之前的垃圾值

???? printf("%s has %d days", January.name, January.days); //結(jié)構(gòu)變量通過(guò) . 成員運(yùn)算符訪問(wèn)結(jié)構(gòu)的成員

???? struct month year[12] = { January,[1] = {"February",.num = 2} }; //聲明結(jié)構(gòu)數(shù)組,和普通類型的數(shù)組方法一樣,只寫了1月和2月,剩下的部分會(huì)初始化為0,結(jié)構(gòu)變量的初始化中通過(guò).num的形式(初始化器)給2月的num成員初始化為2,沒(méi)有顯式初始化的abbre和days會(huì)初始化為0

???? strcpy(year[1].abbre, "feb"); //給結(jié)構(gòu)的成員單獨(dú)賦值,字符數(shù)組需要strcpy/strncpy完成賦值,而對(duì)整個(gè)結(jié)構(gòu)賦值時(shí)不需要對(duì)每個(gè)字符數(shù)組字段使用strcpy

???? year[1].days = 28;

???? year[2] = (struct month){ "March","mar",31,3 }; //復(fù)合字面量,格式(struct 結(jié)構(gòu)標(biāo)識(shí)符){每個(gè)成員/字段的值}

???? // 省略數(shù)組剩余月份賦值過(guò)程

???? char user_input[20] = "";

???? int total = 0;

???? puts("輸入月份名:");

???? scanf("%19s", user_input);

???? for (int i = 11; i >= 0; i--)

???? {

???????? if (strcmp(user_input,year[i].name)==0)

???????? {

???????????? for (int j = 0; j <= i; j++)

???????????? {

???????????? ????total += year[j].days;

???????????? }

???????????? printf("當(dāng)年直到該月一共有%d天\n", total);

???????????? break;

???????? }

???? }

???? if (total==0)

???? {

???? ????puts("月份名錯(cuò)誤");

???? }

}

void func2(void)

{

???? extern struct month

???? {

???????? char name[20];

???????? char abbre[4];

???????? int days;

???????? int num;

???? } months[]; //引用定義好的結(jié)構(gòu)數(shù)組,數(shù)組大小應(yīng)省略,但結(jié)構(gòu)類型的字段內(nèi)容不能省略,C將結(jié)構(gòu)類型的所有字段完全相同的結(jié)構(gòu)視為同一個(gè)結(jié)構(gòu)類型,不論結(jié)構(gòu)類型標(biāo)識(shí)符

???? printf("輸入年:");

???? int y, d;

???? char m[20]="";

???? int m_index;

???? if (scanf("%d", &y)==1)

???? {

???????? if (y%4==0)

???????? {

???????? ????months[1].days = 29; //處理閏年

???????? }

???????? printf("輸入月:");

???????? if (scanf("%19s", m)==1) //可以輸入英文全稱、縮寫、數(shù)字

???????? {

???????????? if ((m_index = (int)strtol(m, NULL, 10))!=0) //strtol()轉(zhuǎn)換失敗/字符串開(kāi)頭沒(méi)有讀取到數(shù)字返回0

???????????? {

???????????????? if (m_index<1||m_index>12) //檢測(cè)數(shù)字有效性

???????????????? {

???????????????? ????m_index = 12; //設(shè)置為無(wú)用值

???????????????? }

???????????????? else

???????????????? {

???????????????? ????m_index--; //月份號(hào)比數(shù)組索引多1

???????????????? }

???????????? }

???????????? else????//m_index 在if中判斷為0

???????????? {

???????????????? for (; m_index < 12; m_index++)

???????????????? {

???????????????????? if (strcmp(months[m_index].name, m) == 0 || strcmp(months[m_index].abbre, m) == 0) //比較英文,如果數(shù)組元素都不匹配m_index的值變?yōu)?2和上面的無(wú)用值一致

???????????????????? break;

???????????????? }

???????????? }

???????????? if (m_index<12) //小于12則一定為0-11中的一個(gè)有效數(shù)

???????????? {

???????????????? printf("輸入日:");

???????????????? if (scanf("%d", &d)==1&&d>0&&d<=months[m_index].days) //檢測(cè)日的有效性

???????????????? {

???????????????????? int total = d;

???????????????????? for ( int i = 0; i < m_index; i++)

???????????????????? {

???????????????????? ????total += months[i].days;

???????????????????? }

???????????????????? printf("%d年%s月%d日是這一年的第%d天\n", y, m, d, total);

???????????????????? return;

???????????????? }

???????????? }

???????? }

???? }

???? puts("輸入有誤");

}

void manybook(void)

{

???? struct book { //在函數(shù)中聲明的結(jié)構(gòu)類型具有塊作用域,在函數(shù)外聲明具有文件作用域

???????? char title[40];

???????? char author[40];

???????? float value;

???? } library[100];

???? int count = 0;

???? int index;

???? printf("Please enter the book title.\nPress [enter] at the start of a line to stop.\n");

???? while (s_gets(library[count].title, 40)!=NULL&&library[count].title[0]!='\0') //得到標(biāo)題

???? {

???????? puts("Now enter the author.");

???????? s_gets(library[count].author, 40);

???????? puts("Now enter the value.");

???????? scanf("%f", &library[count++].value); //完成一個(gè)結(jié)構(gòu)的賦值,count++

???????? while (getchar()!='\n')

???????????? continue; //清空行

???????? if (count < 100)

???????????? puts("Enter the next title.");

???????? else

???????????? break;

???? }

???? if (count>0)

???? {

???????? struct book** parr = (struct book**)malloc(count * sizeof(struct book*)); //動(dòng)態(tài)分配包含count個(gè)指向book結(jié)構(gòu)的指針元素的數(shù)組,*parr[]轉(zhuǎn)換為**parr

???????? //函數(shù)返回void*,需要強(qiáng)轉(zhuǎn)成parr的類型,malloc的參數(shù)是字節(jié)的數(shù)量,與類型無(wú)關(guān),返回的是內(nèi)存塊的首地址,需要轉(zhuǎn)換成什么類型就轉(zhuǎn)換成什么類型,應(yīng)轉(zhuǎn)換成和parr同類型,而不是struct book*

???????? puts("Here is the list of your books:");

???????? for ( index = 0; index < count; index++)

???????? {

???????????? printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);

???????????? parr[index] = library + index; //指針數(shù)組的指針逐一指向結(jié)構(gòu)數(shù)組的元素

???????? }

???????? struct book* temp;

???????? int complete;

???????? for ( index = 0; index < count; index++)

???????? {

???????????? complete = 1;

???????????? for (int j = count-1; j >index; j--)

???????????? {

???????????????? if (strcmp((*parr[j - 1]).title,parr[j]->title)>0) //通過(guò)指針訪問(wèn)結(jié)構(gòu)變量的兩種寫法,library[j]是結(jié)構(gòu),parr[j]=&library[j]是指針/地址,*parr[j]是結(jié)構(gòu),(*parr[j]).title結(jié)構(gòu)調(diào)用字段,解引用*優(yōu)先級(jí)低于. 成員運(yùn)算符需要括起來(lái),另一種常用寫法parr[j]->title和(*parr[j]).title等價(jià),結(jié)構(gòu)地址->結(jié)構(gòu)字段,(*結(jié)構(gòu)地址).結(jié)構(gòu)字段,->間接成員運(yùn)算符

???????????????? {

???????????????????? temp = parr[j];

???????????????????? parr[j] = parr[j - 1];

???????????????????? parr[j - 1] = temp;

???????????????????? complete = 0;

???????????????? }

???????????? }

???????????? if (complete)

???????????? break;

???????? }

???????? for (index = 0; index < count; index++)

???????? {

???????? ????printf("%s by %s: $%.2f\n", parr[index]->title, parr[index]->author, parr[index]->value);

???????? }

???????? for ( index = 0; index < count; index++)

???????? {

???????????? complete = 1;

???????????? for (int j = count-1; j >index; j--)

???????????? {

???????????????? if (parr[j-1]->value>parr[j]->value)

???????????????? {

???????????????????? temp = parr[j];

???????????????????? parr[j] = parr[j - 1];

???????????????????? parr[j - 1] = temp;

???????????????????? complete = 0;

???????????????? }

???????????? }

???????????? if (complete)

???????????? ????break;

???????? }

???????? for (index = 0; index < count; index++)

???????? {

???????? ????printf("%s by %s: $%.2f\n", parr[index]->title, parr[index]->author, parr[index]->value);

???????? }

???????? free(parr); //動(dòng)態(tài)生成的內(nèi)存最好在創(chuàng)建時(shí)就附上釋放的代碼避免遺忘

???? }

???? else

???? {

???? ????puts("No books? Too bad.");

???? }

}

char* s_gets(char* s, int limit)

{

???? char* p1 = fgets(s, limit, stdin);

???? if (p1)

???? {

???????? char* p2 = strchr(s, '\n');

???????? if (p2)

???????? *p2 = '\0';

???????? else

???????????? while (getchar() != '\n')

???????????????? continue;

???? }

???? return p1;

}

struct insurance { //結(jié)構(gòu)儲(chǔ)存保險(xiǎn)的編號(hào)和人名

???? unsigned long num;

???? struct fullname { // 結(jié)構(gòu)中使用結(jié)構(gòu),fullname結(jié)構(gòu)類型字段包含名、中間名和姓,字段的標(biāo)識(shí)符為name,如果之前已經(jīng)聲明了fullname結(jié)構(gòu),則簡(jiǎn)寫為struct fullname name; 如果該結(jié)構(gòu)只在這里使用,可不寫fullname結(jié)構(gòu)類型標(biāo)識(shí)符

???????? char first[20];

???????? char mid[20];

???????? char last[20];

???? } name;

};

void generate1(void)

{

????struct insurance customers[5] = {

???????? {302038923,{"Dribble","Mac","Flossie"}},

???????? {302038543,{"Abel","Ken","Smith"}},

???????? {302345922,{"Caleb","","Jones"}},

???????? {302876924,{"Ford",.last = "williams"}}, // 跳過(guò)mid給last賦值

???????? {305678926,{"Eddie","Que","Brown"}},

???? }; //結(jié)構(gòu)數(shù)組,每個(gè)元素內(nèi)又有一個(gè)結(jié)構(gòu)需要再加一層大括號(hào)

???? get_ins_info(customers); //第一個(gè)元素的地址

???? get_ins_info2(customers[1]); //第二個(gè)元素的值

}

void get_ins_info(struct insurance* sp)

{

???? printf("%s , %s ", sp->name.first,sp->name.last); // 指針->結(jié)構(gòu)字段標(biāo)識(shí)符.char字段標(biāo)識(shí)符,如果聲明insurance結(jié)構(gòu)時(shí)省略了內(nèi)部結(jié)構(gòu)字段的標(biāo)識(shí)符name,則insurance類型變量在使用時(shí)直接通過(guò).即可訪問(wèn)內(nèi)部結(jié)構(gòu)的字段,寫作sp->first

???? if (sp->name.mid[0])

???? {

???? ????printf("%c. ", sp->name.mid[0]);

???? }

???? printf("--%lu\n", sp->num);

}

void get_ins_info2(struct insurance s) //將結(jié)構(gòu)作為值接收,形參s初始化為函數(shù)調(diào)用時(shí)實(shí)參/值的備份

{

???? struct insurance s2 = s; //結(jié)構(gòu)可以賦值,結(jié)構(gòu)名不是地址(不同于數(shù)組)

???? printf("%s , %s ", s2.name.first, s2.name.last);

???? if (s2.name.mid[0])

???? {

???? ????printf("%c. ", s2.name.mid[0]);

???? }

???? printf("--%lu\n", s2.num);

???? // 這也意味著,更改s或s2的值不會(huì)影響主調(diào)函數(shù)的變量

}

typedef struct { struct { char first[20]; char last[20]; }; double score[3]; double average; } STUDENT; //使用typedef取別名,將整個(gè)嵌套結(jié)構(gòu)取別名為student,好處是定義結(jié)構(gòu)變量時(shí)不需要寫struct,壞處是不能顯式表明該類型為結(jié)構(gòu),所以建議用大寫來(lái)表示該類型為復(fù)雜的類型

//由于使用typedef,結(jié)構(gòu)的標(biāo)識(shí)符就沒(méi)用了省略,內(nèi)部結(jié)構(gòu)沒(méi)有聲明字段名,所以直接.調(diào)用內(nèi)部的字段,如果函數(shù)的形參使用了typedef聲明的別名,需要將typedef聲明放在函數(shù)原型之前才能生效

void get_student_info(STUDENT* sp) //STUDENT是某個(gè)結(jié)構(gòu)的類型,所以sp是結(jié)構(gòu)的指針

{

????printf("%s %s : %.2f %.2f %.2f avg:%.2f\n", sp->first, sp->last, sp->score[0], sp->score[1], sp->score[2], sp->average); //匿名內(nèi)部結(jié)構(gòu)的first/last通過(guò)外部結(jié)構(gòu)直接.first調(diào)用

}

#define MAXTITL 40 //define預(yù)處理只對(duì)該行以下的MAXTITL生效

#define MAXAUTL 40

#define MAXBKS 10

struct book

{

???? char title[MAXTITL];

???? char author[MAXAUTL];

???? float value;

};

void booksave(void)

{

???? struct book library[MAXBKS]; // 自動(dòng)存儲(chǔ)類別的對(duì)象被儲(chǔ)存在棧中,如果結(jié)構(gòu)數(shù)組大小很大,會(huì)造成棧溢出,可以使用編譯器選項(xiàng)設(shè)置棧大小為10000或創(chuàng)建靜態(tài)數(shù)組(包括靜態(tài)塊作用域),編譯器不會(huì)將靜態(tài)數(shù)組放在棧中

???? struct book *ptra[MAXBKS], *temp;

???? int count = 0;

???? int index, filecount;

???? char s[2];

???? FILE* pbooks;

???? int size = sizeof(struct book); //將book結(jié)構(gòu)的尺寸記錄

???? if ((pbooks=fopen("data01.txt","rb"))==NULL)

???? {

???????? fputs("Can't open data01.txt file\n", stderr);

???????? exit(1);

???? }

???? while (count<MAXBKS&&fread(library+count,size,1,pbooks)==1) //將結(jié)構(gòu)在內(nèi)存中的值原樣保存到二進(jìn)制文件中,讀取時(shí)按結(jié)構(gòu)的尺寸逐個(gè)結(jié)構(gòu)讀取,fread()返回讀取到的數(shù)據(jù)塊

???? {

???????? if (count==0)

???????? {

???????? ????puts("Current contents of data01.txt:");

???????? }

???????? printf("No.%d %s by %s: $%.2f\n", count, library[count].title, library[count].author, library[count].value);

???????? count++; //count記錄圖書(shū)列表中圖書(shū)的總數(shù)

???? }

???? for (int i = 0; i < MAXBKS; i++)

???? {

???? ????ptra[i] = library + i; // 將指針數(shù)組和結(jié)構(gòu)數(shù)組逐一對(duì)應(yīng),指針數(shù)組的元素是指向結(jié)構(gòu)的指針,結(jié)構(gòu)數(shù)組的元素是結(jié)構(gòu)

???? }

???? while (puts("a)add new book\tu)update exist book\td)delete exist book\tq)quit"),s_gets(s,2)!=NULL&&s[0]!='q') //增刪改查

???? {

???????? switch (s[0])

???????? {

???????????? case 'a':

???????????????? if (count>=MAXBKS)

???????????????? {

???????????????? ????puts("The data01.txt file is full. You can delete some books or update.");

???????????????? }

???????????????? else

???????????????? {

???????????????????? puts("Please add new book titles.\nPress [enter] at the start of a line to stop.");

???????????????????? while (s_gets(ptra[count]->title,MAXTITL)!=NULL&& ptra[count]->title[0]!='\0') //使用指針數(shù)組添加book

???????????????????? {

???????????????????????? puts("Now enter the author.");

???????????????????????? s_gets(ptra[count]->author, MAXAUTL);

???????????????????????? puts("Now enter the value.");

???????????????????????? scanf("%f", &ptra[count++]->value); //當(dāng)前元素所有信息錄完,遞增count

???????????????????????? while (getchar()!='\n')

???????????????????????? {

???????????????????????? ????continue;

???????????????????????? }

???????????????????????? if (count<MAXBKS)

???????????????????????? {

???????????????????????? ????puts("Enter the next title.");

???????????????????????? }

???????????????????????? else

???????????????????????? {

???????????????????????? ????break;

???????????????????????? }

???????????????????? }

???????????????? }

???????????????? break;

???????????? case 'u':

???????????????? if (count<=0)

???????????????? {

???????????????????? puts("There are not books in the file.");

???????????????????? continue;

???????????????? }

???????????????? else

???????????????? {

???????????????????? printf("Which book do you want to update(0-%d,other to quit)?", count-1); //0到count-1

???????????????????? while (s_gets(s,2)!=NULL)

???????????????????? {

???????????????????????? index = s[0] - '0'; //ASCII字符集中,數(shù)字字符減'0'字符等于數(shù)字的值

???????????????????????? if (index>=0&&index<count)

???????????????????????? {

???????????????????????????? printf("No.%d %s by %s: $%.2f\nt)title\ta)author\tv)value\n", index, ptra[index]->title, ptra[index]->author, ptra[index]->value);

???????????????????????????? if (s_gets(s, 2) != NULL && s[0] != '\0' && strchr("tav", s[0]) != NULL) //strchr函數(shù)參數(shù)2是\0時(shí)會(huì)返回參數(shù)1字符串的末尾\0的位置,不為null

???????????????????????????? {

???????????????????????????????? printf("new content:");

???????????????????????????????? switch (s[0])

???????????????????????????????? {

???????????????????????????????????? case 't':

???????????????????????????????????????? s_gets(ptra[index]->title, MAXTITL);

???????????????????????????????????????? break;

???????????????????????????????????? case 'a':

???????????????????????????????????????? s_gets(ptra[index]->author, MAXAUTL);

???????????????????????????????????????? break;

???????????????????????????????????? case 'v':

???????????????????????????????????????? scanf("%f", &ptra[index]->value);

???????????????????????????????????????? while (getchar() != '\n')

???????????????????????????????????????????? continue;

???????????????????????????????? }

???????????????????????????????? printf("No.%d %s by %s: $%.2f\nt)title\ta)author\tv)value\n", index, ptra[index]->title, ptra[index]->author, ptra[index]->value);

???????????????????????????? }

???????????????????????? }

???????????????????????? else

???????????????????????? {

???????????????????????? ????break;

???????????????????????? }

???????????????????????? printf("Which book do you want to update(0-%d)?", count - 1);

???????????????????? }

???????????????? }

???????????????? break; //每個(gè)case的break

???????????? case 'd':

???????????????? if (count <= 0)

???????????????? {

???????????????????? puts("There are not books in the file.");

???????????????????? continue;

???????????????? }

???????????????? else

???????????????? {

???????????????????? printf("Which book do you want to delete(0-%d)?", count - 1);

???????????????????? if (s_gets(s,2)!=NULL&&(index=s[0]-'0')>=0&&index<count)

???????????????????? {

???????????????????????? printf("No.%d %s by %s: $%.2f\n", index, ptra[index]->title, ptra[index]->author, ptra[index]->value);

???????????????????????? printf("Delete(y/n)?");

???????????????????????? if (s_gets(s, 2) != NULL && tolower(s[0]) == 'y')

???????????????????????? {

???????????????????????????? if (index<MAXBKS-1)

???????????????????????????? {

???????????????????????????????? temp = ptra[index];

???????????????????????????????? memmove(ptra + index, ptra + index + 1, (MAXBKS - 1 - index) * sizeof(struct book*)); //操作指針數(shù)組,將指向要?jiǎng)h除的book的指針元素后面的元素前移,使用memmove復(fù)制整塊數(shù)據(jù)對(duì)象,將指向要?jiǎng)h除的book的地址(結(jié)構(gòu)數(shù)組中該book元素的地址)交換到指針數(shù)組的最后一位元素上,這一步操作并沒(méi)有清除數(shù)據(jù)

???????????????????????????????? ptra[MAXBKS - 1] = temp;

???????????????????????????? }

???????????????????????????? count--; //記錄總數(shù)的count遞減,因?yàn)橐苿?dòng)了指針數(shù)組的部分元素,前count個(gè)元素依然是當(dāng)前的圖書(shū),而要?jiǎng)h除的book的地址被移動(dòng)到count范圍外,不能讀取相當(dāng)于刪除,這也完成了刪除book使后面的book前移的操作,而如果要?jiǎng)h除的是索引9即指針數(shù)組的最后一個(gè)指針,則只需要遞減count將該book排除,經(jīng)過(guò)刪除操作后,指針數(shù)組的元素順序和結(jié)構(gòu)數(shù)組的元素順序不再相同,但依舊保持一對(duì)一的關(guān)系?

???????????????????????????? puts("Delete success.");

???????????????????????? }

???????????????????? }

???????????????? }

???????????????? break;

???????????? default:

???????????? ????continue;

???????? }

???????? if (count>0)

???????? {

???????????? puts("Here is the list of your books:");

???????????? for (index = 0; index < count; index++)

???????????? {

???????????? ????printf("No.%d %s by %s: $%.2f\n", index, ptra[index]->title, ptra[index]->author, ptra[index]->value); //順序以指針數(shù)組為準(zhǔn)

???????????? }

???????? }

???????? else

???????? {

???????? ????puts("The list of your books is empty now.");

???????? }

???? }

???? fclose(pbooks);

???? if (pbooks = fopen("data01.txt", "wb")) //清空文件

???? {

???????? for ( index = 0; index < count; index++)

???????? {

???????? ????fwrite(ptra[index], size, 1, pbooks); //只保存指針數(shù)組中前count個(gè)元素指向的結(jié)構(gòu)

???????? }

???????? fclose(pbooks);

???? }

???? if (count<=0)

???? {

???? ????puts("No books? Too bad.\n");

???? }

???? puts("Bye.\n");

}

struct seat {

???? int num;

???? _Bool reserved;

???? char first[20];

???? char last[20];

};

#define SEATSIZE 12

void seat_reserve(void)

{

???? struct seat seats[SEATSIZE] = {

???? ????{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12} //初始化座位編號(hào),reserved字段預(yù)定狀態(tài)默認(rèn)初始化為0

???? };

???? puts("To choose a function, enter its letter label:\na)Show number of empty seats\tb)Show list of empty seats\nc)Show alphabetical list of seats\td)Assign a customer to a seat assignment\ne)Delete a seat assignment\tf)Quit");

???? char ch;

???? typedef void (*func_ptr)(struct seat*, int); //逐步分析,void func()為函數(shù),無(wú)返回,void* func()為返回指向void指針的函數(shù)(因?yàn)槔ㄌ?hào)優(yōu)先級(jí)高于解引用,func和()結(jié)合成為函數(shù),圓括號(hào)(2+3)和方括號(hào)[i]優(yōu)先級(jí)相同), void (*func)()為函數(shù)指針,因?yàn)?先和func結(jié)合,使func成為指針,然后將*func看成一個(gè)整體和()結(jié)合成為函數(shù),也就是說(shuō)*func是函數(shù),func是指針,然后void聲明該函數(shù)為void類型,函數(shù)的類型+函數(shù)的形參列表合稱函數(shù)簽名,所以func是指向函數(shù)的指針,該函數(shù)為void類型有兩個(gè)形參

????//typedef 類型 別名, 對(duì)于函數(shù),typedef 函數(shù)簽名 別名,(*func_ptr)以外的部分為函數(shù)簽名,*func_ptr為函數(shù),func_ptr為函數(shù)指針類型的別名

???? //聲明函數(shù)指針的方法為,先寫出函數(shù)原型,void func(),再將函數(shù)名替換為 (*標(biāo)識(shí)符) 的形式,標(biāo)識(shí)符代表的就是函數(shù)指針

???? func_ptr func_ptrs[5] = { show_empty_num,show_empty_list ,show_alphabetical_list ,assign_seat ,delete_seat }; //聲明包含五個(gè)函數(shù)指針元素的數(shù)組,函數(shù)名為函數(shù)的地址,加括號(hào)為調(diào)用

???? //如果不使用typedef,聲明指針數(shù)組的格式為:void (*func[5])() ,func先和[5]結(jié)合成為數(shù)組,再和*結(jié)合成為存放指針的數(shù)組,再()存放函數(shù)指針,再void。如果寫成void (*func)[5](),因?yàn)槔ㄌ?hào)()和[]優(yōu)先級(jí)相同,根據(jù)結(jié)合律從左到右,func和*成為指針,和[5]成為指向5個(gè)元素?cái)?shù)組的指針,這時(shí)(*func)[5]整體為一個(gè)數(shù)組,數(shù)組外的部分都是數(shù)組元素的定義,所以和()結(jié)合使元素成為函數(shù),再void,但C不允許函數(shù)數(shù)組,這樣的聲明時(shí)非法的,只能聲明函數(shù)指針的數(shù)組

???? while ((ch=tolower(getchar()))!='f')

???? {

???????? if (ch != '\n')

???????????? while (getchar() != '\n')

???????????????? continue;

???????? if (strchr("abcde", ch) && ch != '\0')

???????????? (*func_ptrs[ch - 'a'])(seats, SEATSIZE); //func_ptrs為函數(shù)指針的數(shù)組,func_ptrs[0]為函數(shù)指針,(*func_ptrs[0])為函數(shù),后面加(參數(shù))來(lái)調(diào)用,因?yàn)?優(yōu)先級(jí)低于()函數(shù)調(diào)用所以要括起來(lái),C允許(*函數(shù)指針)(參數(shù))的形式,也允許函數(shù)指針(參數(shù))的形式(不推薦)

???????? //a-a=0 e-a=4,對(duì)應(yīng)指針數(shù)組的五個(gè)函數(shù)

???????? puts("To choose a function, enter its letter label:\na)Show number of empty seats\tb)Show list of empty seats\nc)Show alphabetical list of seats\td)Assign a customer to a seat assignment\ne)Delete a seat assignment\tf)Quit");

???? }

}

void show_empty_num(struct seat* sp, int size)

{

???? int count = 0;

???? for (int i = 0; i < size; i++)

???? {

???????? if (sp[i].reserved)

???????? continue;

???????? printf("%d\t", sp[i].num);

???????? count++;

???? }

???? if (count)

???? ????putchar('\n');

???? else

???? ????puts("These are not empty seats now.");

}

void show_empty_list(struct seat* sp, int size)

{

???? for (int i = 0; i < size; i++)

???? {

???????? if (sp[i].reserved)

???????? ????continue;

???????? printf("seat number:%d? status:not reserved? customer information:none\n", sp[i].num); //sp是指針,sp[i]是結(jié)構(gòu),*sp是結(jié)構(gòu),sp+i是地址,所以也可以寫成(sp+i)->num

???? }

????

}

void show_alphabetical_list(struct seat* sp, int size)

{

???? struct seat** temparr = (struct seat**)malloc(size*sizeof(struct seat*)); //動(dòng)態(tài)分配內(nèi)存

???? for (int i = 0; i < size; i++)

???? {

???? ????temparr[i] = sp + i;

???? }

???? int complete;

???? for (int i = size-1; i >0; i--)

???? {

???????? complete = 1;

???????? for (int j = 0; j < i; j++)

???????? {

???????????? if (temparr[j]->first[0])

???????????? {

???????????????? if (temparr[j + 1]->first[0]&& strcmp(temparr[j]->first, temparr[j + 1]->first) > 0)

???????????????? {

???????????????????? sp = temparr[j]; //sp傳參給temparr之后就沒(méi)用了,當(dāng)做交換用臨時(shí)變量

???????????????????? temparr[j] = temparr[j + 1];

???????????????????? temparr[j + 1] = sp;

???????????????????? complete = 0;

???????????????? }

???????????? }

???????????? else if (temparr[j + 1]->first[0])

???????????? {

???????????????? sp = temparr[j];

???????????????? temparr[j] = temparr[j + 1];

???????????????? temparr[j + 1] = sp;

???????????????? complete = 0;

???????????? }

???????? }

???????? if (complete)

???????? ????break;

???? }

???? for (int i = 0; i < size; i++)

???? {

???? ????printf("seat number:%d? status:%s? customer information:%s %s\n", temparr[i]->num, temparr[i]->reserved?"reserved" : "not reserved", temparr[i]->first, temparr[i]->last); //temparr是指向指針的指針,temparr[i]是指針,(*temparr[i])是結(jié)構(gòu),等價(jià)于temparr[i][0]

???? }

???? free(temparr);

}

void assign_seat(struct seat* sp, int size)

{

???? printf("Enter the number of seat to assign(1-%d other to quit):", size);

???? int a;

???? char ch;

???? while (scanf("%d",&a)==1&&a>0&&a<=size)

???? {

???????? while (getchar() != '\n')

???????????? continue;

???????? if (sp[a-1].reserved)

???????? {

???????????? puts("The seat have been reserved. Try another:");

???????????? continue;

???????? }

???????? printf("Enter the first name and last name:");

???????? scanf("%19s%19s", sp[a - 1].first, sp[a - 1].last);

???????? while (getchar() != '\n')

???????????? continue;

???????? printf("Sure(y/n)?");

???????? if ((ch=tolower(getchar()))=='y')

???????? {

???????????? sp[a - 1].reserved = 1;

???????????? puts("Success.");

???????? }

???????? if(ch!='\n')

???????? while (getchar() != '\n')

???????? ???? continue;

???????? printf("Enter the number of seat to assign(1-%d other to quit):", size);

???? }

???? while (getchar() != '\n')

???? ????continue;

}

void delete_seat(struct seat* sp, int size)

{

???? printf("Enter the number of seat to delete(1-%d other to quit):", size);

???? int a;

???? char ch;

???? while (scanf("%d", &a) == 1 && a > 0 && a <= size)

???? {

???????? while (getchar() != '\n')

???????? ????continue;

???????? if (sp[a - 1].reserved)

???????? {

???????????? printf("seat number:%d? status:%s? customer information:%s %s\n", sp[a - 1].num, sp[a - 1].reserved ? "reserved" : "not reserved", sp[a - 1].first, sp[a - 1].last);

???????????? printf("Sure(y/n)?");

???????????? if ((ch = tolower(getchar())) == 'y')

???????????? {

???????????????? sp[a - 1].reserved = 0;

???????????????? sp[a - 1].first[0] = '\0';

???????????????? sp[a - 1].last[0] = '\0';

???????????????? puts("Success.");

???????????? }

???????????? if (ch != '\n')

???????????????? while (getchar() != '\n')

???????????????????? continue;

???????????? printf("Enter the number of seat to delete(1-%d other to quit):", size);

???????? }

???????? else

???????? {

???????????? puts("The seat is empty. Try another:");

???????????? continue;

???????? }

???? }

???? while (getchar() != '\n')

???? ????continue;

}

void other1(void)

{

???? union MyUnion // 聯(lián)合union,是一種數(shù)據(jù)類型,創(chuàng)建一個(gè)空間儲(chǔ)存字段,大小為字段中最大的字段的大小,同一時(shí)間只能存儲(chǔ)其中一個(gè)字段

???? {

???????? int i;

???????? float f;

???????? char ch;

???????? double d;

???????? struct { short s1; short s2; };

???? } u = {1.2}; //聯(lián)合初始化默認(rèn)初始化為第一個(gè)字段的類型,1.2發(fā)生截?cái)?,如果要指定初始化為d字段,使用{.d=1.2}

???? u.s1 = 20; //匿名結(jié)構(gòu)直接調(diào)用,對(duì)聯(lián)合的其他字段賦值會(huì)覆蓋之前的字段值,同時(shí)間只能存儲(chǔ)一個(gè)字段,即便該字段默認(rèn)大小遠(yuǎn)小于聯(lián)合的大小

???? struct { int uniontype; union MyUnion value} su = { 1,{20} }; //用一個(gè)標(biāo)識(shí)記錄當(dāng)前聯(lián)合中存儲(chǔ)的類型,如type=1說(shuō)明value.i有效,這樣做使得數(shù)據(jù)塊的大小一致,存儲(chǔ)和讀取時(shí)通過(guò)type訪問(wèn)聯(lián)合正確的成員

???? struct ss {

???????? int status;

???????? union {

???????????? char c;

???????????? short s;

???????? }; // 匿名聯(lián)合,可通過(guò)結(jié)構(gòu)變量.c直接訪問(wèn)聯(lián)合成員

???? };

???? struct flexible //在結(jié)構(gòu)中使用伸縮型數(shù)組成員

???? {

???????? int size;

???????? double arr[]; //最后一個(gè)成員聲明為一個(gè)不寫大小的數(shù)組

???? }; //這個(gè)結(jié)構(gòu)類型的大小為伸縮型數(shù)組成員以外所有字段大小的和,這里是1個(gè)int的大小,也就是說(shuō)這時(shí)結(jié)構(gòu)的.arr并不存在

???? struct flexible* sp = malloc(sizeof(struct flexible) + 5*sizeof(double)); //這種結(jié)構(gòu)需要配合動(dòng)態(tài)分配內(nèi)存,在結(jié)構(gòu)的大小之外增加數(shù)組所需的大小,這里設(shè)定數(shù)組有5個(gè)double元素

???? sp->size = 5; //在結(jié)構(gòu)中記錄數(shù)組的尺寸

???? for (int i = 0; i < sp->size; i++)

???? {

????????sp->arr[i] = i * 1.0; //對(duì)每個(gè)元素賦值

???? }

???? free(sp); //因?yàn)榻Y(jié)構(gòu)實(shí)際上不包含伸縮型數(shù)組成員,所以使用結(jié)構(gòu)賦值時(shí)只會(huì)拷貝除數(shù)組以外的部分

???? enum MyEnum //枚舉類型,enumerated type,聲明符號(hào)名稱來(lái)表示整形常量,enum實(shí)際類型為int

???? {

???? ????ZERO,ONE,THREE=3,FOUR,EIGHT=8,NINE //這里聲明的ZERO的值為0,接著的ONE的值為1,枚舉默認(rèn)從0開(kāi)始,通過(guò)符號(hào)名稱=來(lái)對(duì)特定符號(hào)賦值,之后的符號(hào)順著上一個(gè)的值遞增

???? };

???? enum MyEnum i; //聲明枚舉變量,實(shí)際類型為int,可以是任意int的值

???? char st[NINE]; //可以使用枚舉常量定義數(shù)組大小,和#define NINE 9作用類似

???? //補(bǔ)充: *解引用&取址運(yùn)算符優(yōu)先級(jí)低于.成員運(yùn)算符->間接成員運(yùn)算符,所以ptr->xx改為(*ptr).xx,而*ptr->ptr2->struct3.memberptr會(huì)解引用memberptr,&ptr->member會(huì)取址member,三種括號(hào)(無(wú)論用法)的優(yōu)先級(jí)和.->同級(jí)(同為最高級(jí)),所以ptr->func()根據(jù)結(jié)合律從左往右先找到ptr指向的結(jié)構(gòu)的func成員再作為函數(shù)調(diào)用,funcs[23]()同樣從左往右

}


C語(yǔ)言結(jié)構(gòu)、聯(lián)合、枚舉的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
长沙县| 浮山县| 巧家县| 宜兴市| 孝感市| 铁力市| 海门市| 芮城县| 静乐县| 观塘区| 平潭县| 郎溪县| 绥棱县| 鄯善县| 乌鲁木齐市| 河东区| 牡丹江市| 万源市| 凤城市| 射阳县| 贵州省| 正阳县| 广德县| 桓仁| 蓝山县| 乾安县| 岱山县| 兰溪市| 得荣县| 辛集市| 丰原市| 通州市| 三河市| 皮山县| 宁夏| 井研县| 西吉县| 呼图壁县| 会泽县| 阳曲县| 深水埗区|