關(guān)鍵字:struct、union胸私、typedef
運(yùn)算符: .、 ->
結(jié)構(gòu)聲明
形如:
struct book{
char title[MAXTITL];
float value;
};
調(diào)用
struct book doyle, panshin, * ptbook;
另外兩種聲明方式
比如說(shuō)想要直接使用 library
struct book library;
可以簡(jiǎn)化成:
struct book{
char title[MAXTITL];
float value;
} library; /*聲明的最后跟變量名 就能直接用library*/
或者:
struct { /*無(wú)結(jié)構(gòu)標(biāo)記*/
char title[MAXTITL];
float value;
} library;
不過(guò)若是想要多次使用這個(gè)結(jié)構(gòu)模版,就要使用帶標(biāo)記的形式线召;或者,使用后面介紹的typedef
初始化結(jié)構(gòu)
struct book library = {
"Renee vivvvvv",
1.95
};/*和初始化數(shù)組差不多*/
使用點(diǎn)號(hào)對(duì)結(jié)構(gòu)訪問(wèn)
#include <stdio.h>
#define MAXTITL 40
struct book{
char title[MAXTITL];
float value;
};
int main(void)
{
struct book library = {
"vvvvvvv",
12.9
};
printf("%s %f", library.title, library.value);
return 0;
}
結(jié)構(gòu)的初始化器
只想要初始化value
struct book library = { .value = 10.99};
指向結(jié)構(gòu)的指針
聲明
struct book * him;
him = &library; /*和數(shù)組不同多矮,結(jié)構(gòu)名并不是指針的地址缓淹,因此哈打,要加上&運(yùn)算符*/
用指針訪問(wèn)成員
/*第一種方法使用->符號(hào)*/
him -> value 就是 library.value
第二種方法
(*him).value
使用malloc()
假設(shè)如下
#define LEN 20
struct names{
char first[LEN];
char last[len];
};
struct names veep = {"Talia", "summers"};
/*這樣會(huì)照成字節(jié)的浪費(fèi)*/
使用malloc
void getinfo (struct namect * pst)
{
char temp[SLEN];
printf("Please enter your first name.\n");
s_gets(temp, SLEN);
//分配內(nèi)存存儲(chǔ)名
pst -> fname = (char *) malloc(strlen(temp) + 1);
//把名拷貝到已分配的內(nèi)存
strcpy(pst->fname, temp);
} /*書(shū)上有詳細(xì)例子建議多看看*/
typedef
- 與define不同,typedef創(chuàng)建的符號(hào)名只受限與類(lèi)型讯壶,不能用于值料仗。
- typedef 由編譯器解釋?zhuān)皇穷A(yù)處理器
- 在其受限范圍內(nèi),typedef 比#define更靈活
typedef unsigned char BYTE;
BYTE x, y;
typedef struct book{
char title[MAXTITL];
float value;
} BOOK;
BOOK library;
編程練習(xí)
1
/*
重寫(xiě)編寫(xiě)復(fù)習(xí)題5伏蚊,用月份名的拼寫(xiě)代替月份號(hào)(別忘了使用strcmp)
*/
#include <stdio.h>
#include <string.h>
struct month
{
char name[10];
char abbrev[4];
int day;
char monumb;
};
struct month months[12] =
{
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
int days(char a[]);
int main(void)
{
int total;
char temp[10];
puts("Enter month:");
scanf("%s", temp);
total = days(temp);
printf("total: %d", total);
}
int days(char a[])
{
int day = 0;
int i;
for ( i = 0; i < 12; i++)
{
day += months[i].day;
if (!strcmp(a, months[i].abbrev))
{
return day;
}
}
return 0;
}
3
/*
修改程序清單14.2中的圖書(shū)目錄程序立轧,使其按照輸入圖書(shū)的順序輸出圖書(shū)的信息,
然后按照標(biāo)題字母的聲明輸出圖書(shū)的信息躏吊,最后按照價(jià)格的升序輸出圖書(shū)的信息
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int (*pfun) (const void *, const void *); //兩種排序方法
int sortByAscii(const void* a, const void* b); //一種用內(nèi)置的c函數(shù) qsort https://blog.csdn.net/asdgyy/article/details/82956108
void money_sort(struct book *pst[] , int n); //一種用書(shū)上的指針排序法 都以升序進(jìn)行排序
char * s_gets(char * st, int n);
int main(void)
{
struct book library[MAXBKS];
struct book *ptstr[MAXBKS];
int count = 0;
int index;
printf("Please enter the book title.\n");
printf("Press [enter] at the start of a line to stop.\n");
while (count < MAXBKS && s_gets(library[count].title, MAXTITL)
&& library[count].title[0] != '\0')
{
printf("Now enter the author.\n");
s_gets(library[count].author, MAXAUTL);
printf("Now enter the value.\n");
scanf("%f", &library[count].value);
while (getchar() != '\n')
continue;
ptstr[count] = &library[count];
count++;
if (count < MAXBKS)
printf("Enter the next title.\n");
}
if (count > 0)
{
printf("Here is the list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title,
library[index].author, library[index].value);
puts("money sort:");
money_sort(ptstr, count);
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", ptstr[index]->title,
ptstr[index]->author, ptstr[index]->value);
puts("ascii sort:");
pfun = sortByAscii;
qsort(library, count == MAXBKS ? MAXBKS - 1 : count, sizeof(library[0]), pfun);
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title,
library[index].author, library[index].value);
}
else
printf("No books? Too bad.\n");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void money_sort(struct book *pst[] , int n)
{
struct book *temp;
int top, seek;
for (top = 0; top < n - 1; top++)
for (seek = top + 1; seek < n; seek++)
if (pst[top] -> value > pst[seek] -> value)
{
temp = pst[top];
pst[top] = pst[seek];
pst[seek] = temp;
}
}
int sortByAscii(const void* a, const void* b)//格式要符合qsort氛改,函數(shù)內(nèi)再轉(zhuǎn)換
{
return strcmp(((struct book*)a)->title, ((struct book*)b)->title);
}
4
/*
編寫(xiě)一個(gè)程序,創(chuàng)建一個(gè)有兩個(gè)成員的結(jié)構(gòu)模版
*/
#include <stdio.h>
#define SIZE 20
struct name
{
char first[SIZE];
char mid[SIZE];
char last[SIZE];
};
struct list
{
long number;
struct name names;
};
void show__(struct list namee[], int n); //a
void show_two(struct list namee); //b
int main(void)
{
struct list many_list[4] = {
{302039823, {"Dribble", "Maaa", "Flossie"}},
{.number = 258, {"Tom", "OOO", "MMM"}},
{456, {"Tony", "a","Flossie"}},
{123, {"Tom", "OO", "MMM"}}
};
show__(many_list, 4);
for (int i = 0; i < 4; i++)
{
show_two(many_list[i]);
}
return 0;
}
void show__(struct list namee[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
printf("%s, %s %c. -- %ld\n", namee[i].names.first,
namee[i].names.last, namee[i].names.mid[0], namee[i].number);
}
}
void show_two(struct list namee)
{
printf("%s, %s %c. -- %ld\n", namee.names.first,
namee.names.last, namee.names.mid[0], namee.number);
}
5
/*
編寫(xiě)一個(gè)程序滿足下面的要求
a. 外部定義一個(gè)有兩個(gè)成員的結(jié)構(gòu)模版name:一個(gè)字符串儲(chǔ)存名比伏,資格存姓
b. 外部定義一個(gè)有3個(gè)成員的結(jié)構(gòu)模版student:一個(gè)name類(lèi)的結(jié)構(gòu)
一個(gè)grade數(shù)組存3個(gè)浮點(diǎn)型分?jǐn)?shù)胜卤,一個(gè)變量存儲(chǔ)3個(gè)分?jǐn)?shù)平均數(shù)。
c. 在main()函數(shù)中聲明一個(gè)內(nèi)含CSIZE(CSIZE=4)個(gè)student類(lèi)型的結(jié)構(gòu)的
數(shù)組赁项,并初始化這些結(jié)構(gòu)的名字部分葛躏,用函數(shù)執(zhí)行g(shù)、e悠菜、f和g中描述的任務(wù)
d. 以交互式的方式獲取每個(gè)學(xué)生的成績(jī)舰攒,提示用戶輸入學(xué)生的姓名和分?jǐn)?shù)
把分?jǐn)?shù)儲(chǔ)存到grade數(shù)組相應(yīng)的結(jié)構(gòu)中,可以在main()函數(shù)或其他函數(shù)
中用循環(huán)來(lái)完成
e. 計(jì)算每個(gè)結(jié)構(gòu)的平均分悔醋,并把計(jì)算后的值付給合適的成員
f. 打印么個(gè)結(jié)構(gòu)的信息
g. 打印班級(jí)的平均分, 即所有結(jié)構(gòu)的數(shù)值成員的平均值
*/
#include <stdio.h>
#include "221.h"
#define CSIZE 4
struct name
{
char fname[20];
char lname[20];
};
struct student
{
struct name names;
float grade[3];
float avg;
};
void average(struct student pavg[], int n);
void all_average(struct student pavg[], int n);
int main(void)
{
int i = 0;
struct student students[CSIZE];
for (i = 0; i < CSIZE; i++)
{
puts("Enter first name");
scanf("%s", students[i].names.fname);
puts("Enter last name");
scanf("%s", students[i].names.lname);
puts("Enter grade");
scanf("%f %f %f", &students[i].grade[0], &students[i].grade[1],
&students[i].grade[2]);
}
average(students, CSIZE);
for (int i = 0; i < CSIZE; i++)
{
printf("name: %s %s\n", students[i].names.fname,students[i].names.lname);
printf("grad: %.2f %.2f %.2f avg: %.2f\n", students[i].grade[0],
students[i].grade[1], students[i].grade[2], students[i].avg);
}
all_average(students, 4);
}
void average(struct student pavg[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
pavg[i].avg = (pavg[i].grade[0] + pavg[i].grade[1] + pavg[i].grade[2]) / 3;
}
}
void all_average(struct student pavg[], int n)
{
float avg;
avg = pavg[0].avg + pavg[1].avg + pavg[2].avg + pavg[3].avg;
printf("%f", avg / 4);
}
9
/*
一個(gè)文本文件里面保存著一個(gè)壘球隊(duì)的信息芒率。每行數(shù)據(jù)都是這樣排列:
4 Jessie Joybat 5 2 1 1
第一項(xiàng)是球員號(hào)碼,范圍0~18.第二項(xiàng)是球員的名字第三是姓篙顺。第四是上場(chǎng)
次數(shù)偶芍。之后分別是擊中數(shù),走壘數(shù)德玫,打點(diǎn)和安打率匪蟀。編寫(xiě)一個(gè)程序?qū)⑺麄兇孢M(jìn)
結(jié)構(gòu)數(shù)組中
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ALL_ID 19
#define NA_SIZE 20
struct name
{
char fname[NA_SIZE];
char lname[NA_SIZE];
};
struct ball_result
{
int num;
struct name names;
int counts;
int shot_num;
int walk_num;
int defeat_num;
float safe_defeat;
};
void num_safe(struct ball_result st[]); //計(jì)算安打
void show(struct ball_result st[]); //打印數(shù)據(jù)
int main(void)
{
struct ball_result all_people[ALL_ID] =
{
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0},
{0, "", "", 0, 0, 0, 0, 0}
};
int tool_count;
int tool_ID;
char tool_name[NA_SIZE];
char tool_name2[NA_SIZE];
int tool_shot;
int tool_walk;
int tool_defate;
FILE *fp;
if ((fp = fopen("258.txt", "r")) == NULL)
{
fprintf(stderr, "Can't open 258.txt!!!!");
exit(EXIT_FAILURE);
}
while (fscanf(fp, "%d",&tool_ID) != EOF) //EOF 按位取反是空
{
if (all_people[tool_ID].names.fname[0] == '\0')
{
all_people[tool_ID].num = tool_ID;
fscanf(fp, "%s %s %d %d %d %d",tool_name, tool_name2,
&tool_count,&tool_shot, &tool_walk, &tool_defate);
strcpy(all_people[tool_ID].names.fname, tool_name);
strcpy(all_people[tool_ID].names.lname, tool_name2);
all_people[tool_ID].counts = tool_count;
all_people[tool_ID].shot_num = tool_shot;
all_people[tool_ID].walk_num = tool_walk;
all_people[tool_ID].defeat_num = tool_defate;
}
else
{
fscanf(fp, "%*s %*s %d %d %d %d",
&tool_count,&tool_shot, &tool_walk, &tool_defate);
all_people[tool_ID].counts += tool_count;
all_people[tool_ID].shot_num += tool_shot;
all_people[tool_ID].walk_num += tool_walk;
all_people[tool_ID].defeat_num += tool_defate;
}
}
fclose(fp);
num_safe(all_people);
show(all_people);
return 0;
}
void num_safe(struct ball_result st[])
{
int i;
for ( i = 0; i < ALL_ID; i++)
{
st[i].safe_defeat = (float)st[i].shot_num / (float)st[i].counts;
}
}
void show(struct ball_result st[])
{
int i;
for (i = 0; i < ALL_ID; i++)
{
printf("%5d %8s %8s %5d %5d %5d %5d %7.2f\n", st[i].num,
st[i].names.fname, st[i].names.lname, st[i].counts,
st[i].shot_num, st[i].walk_num, st[i].defeat_num, st[i].safe_defeat);
}
}
7
/*
修改程序清單 14.14, 從文件中讀取每條記錄并顯示出來(lái)宰僧,允許用戶刪除記錄
或修改記錄的內(nèi)容材彪。如果刪除記錄,把空出來(lái)的空間留給下一個(gè)要讀入的記錄
要修改現(xiàn)有的文件內(nèi)容琴儿,必須使用"r+b"模式段化,而不是"a+b"而且,必須更加注意
定位文件指針造成,防止新加入的記錄覆蓋現(xiàn)有的記錄显熏。最簡(jiǎn)單的方法是改動(dòng)存儲(chǔ)在內(nèi)存
中的所有數(shù)據(jù),然后再把最后的信息寫(xiě)入文件晒屎。跟蹤的一個(gè)方法是在book結(jié)構(gòu)中
添加一個(gè)成員表示是否該項(xiàng)被刪除
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
int judge;
};
int get_num(int n); //得到要?jiǎng)h除第幾行
char * s_gets(char * st, int n);
char menu(void); //菜單界面 有a, b, c三個(gè)選項(xiàng)
int delet(struct book *pst, int n, int *st); //刪除函數(shù)
int change(struct book *pst, int n, int *st); //修改函數(shù)
int add(struct book *pst, int n, int *st); //添加函數(shù)
int main(void)
{
struct book library[MAXBKS];
char ch; //作為選擇菜單喘蟆,存儲(chǔ)a,b,c選項(xiàng)的變量
int count = 0;
int index;
FILE * pbooks;
int size = sizeof(struct book);
if ((pbooks = fopen("wordy.bat", "a+b")) == NULL) //打開(kāi)和寫(xiě)入的文件名
{
fputs("Can't open wordy.bat file\n", stderr);
exit(1);
}
rewind(pbooks); //回到文件首
while (count < MAXBKS && fread(&library[count], size,
1,pbooks) == 1)
{
if (count == 0)
puts("Current contents of wordy.bat:");
printf("%s by %s: $%.2f %d\n", library[count].title,
library[count].author, library[count].value, library[count].judge);
count++;
}
if (count == MAXBKS)
{
fputs("The wordy.txt file is full.", stderr);
exit(2);
}
puts("Press [enter] at the start of a line to stop.");
int temp[MAXBKS] = {0}; //存儲(chǔ)臨時(shí)刪除的值缓升,之后添加便會(huì)添加到這個(gè)位置
while (count < MAXBKS && (ch = menu()) != '\0')
{
switch (ch)
{
case 'a':
delet(library, count, &temp[0]);
break;
case 'b':
change(library, count, &temp[0]);
break;
case 'c':
count = add(library, count, &temp[0]);
break;
default:
puts("Error please enter a, b, c");
break;
}
}
fclose(pbooks);
pbooks = fopen("wordy.bat", "wb"); //將原來(lái)數(shù)據(jù)都刪除,重寫(xiě)
if (count > 0)
{
puts("Here is the list of your books:");
for ( index = 0; index < count; index++)
{
if (library[index].judge == 1)
{
printf("%s by %s: $%.2f \n", library[index].title,
library[index].author, library[index].value);
fwrite(&library[index], size, 1, pbooks);
}
}
}
else
puts("No books? Too bad.\n");
puts("Bye.\n");
fclose(pbooks);
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
char menu(void)
{
char temp[MAXTITL];
puts("Enter a, b, c");
puts("a) delete b) change");
puts("c) add");
s_gets(temp, MAXTITL);
return temp[0];
}
int delet(struct book *pst, int n, int *st)
{
int i;
i = get_num(n);
while (*st)
{
if (i == *st++)
{
printf("Your enter error\n"); //不要重復(fù)刪除
return 0;
}
}
pst[i - 1].judge = 0;
*st = i;
puts("success!"); //表示刪除成功
return 1;
}
int change(struct book *pst, int n, int *st)
{
int i;
i = get_num(n) - 1;
while (*st)
{
if (i == *st++)
{
printf("That's empty\n"); //已經(jīng)修改的不能再刪除
return 0;
}
}
puts("Now enter the book.");
s_gets(pst[i].title, MAXTITL);
puts("Now enter the author.");
s_gets(pst[i].author, MAXAUTL);
puts("Now enter the value.");
scanf("%f", &pst[i].value);
while (getchar() != '\n')
continue;
puts("success");
}
int add(struct book *pst, int n, int *st)
{
int i;
int j;
int flag = 1;
for (j = 0; j < 10; j++)
{
if (*(st + j))
{
i = *(st + j) - 1;
flag = 0;
break;
}
}
if (flag)
{
i = n++;
}
pst[i].judge = 1;
puts("Now enter the book.");
s_gets(pst[i].title, MAXTITL);
puts("Now enter the author.");
s_gets(pst[i].author, MAXAUTL);
puts("Now enter the value.");
scanf("%f", &pst[i].value);
while (getchar() != '\n')
continue;
puts("success!");
return n;
}
int get_num(int n)
{
int i;
puts("Please enter num:"); //輸入要?jiǎng)h除第幾排
scanf("%d", &i);
while (i > n || i <= 0)
{
while (getchar() != '\n')
continue;
puts("Error!! it's extrem!!"); // 表示范圍超過(guò)
scanf("%d", &i);
puts("Please enter num:");
}
while (getchar() != '\n')
continue;
return i;
}
image.png
選擇框界面
8
/*
巨人航空公司的機(jī)群由12個(gè)座位的飛機(jī)組成蕴轨。他每天飛行一個(gè)航班港谊。根據(jù)下面的要求
編寫(xiě)一個(gè)座位預(yù)定的程序。
a 程序使用一個(gè)內(nèi)涵12個(gè)結(jié)構(gòu)的數(shù)組橙弱。每個(gè)結(jié)構(gòu)中包括: 一個(gè)成員表示座位編號(hào)歧寺、一個(gè)
成員表示座位是否已被預(yù)定。一個(gè)表示預(yù)定的人名棘脐。一個(gè)表示姓
b 該程序顯示下面的菜單
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 12
struct pre_order
{
int id;
int isnull;
char fname[SIZE];
char lname[SIZE];
};
char menu(void);
void show_emptynum(struct pre_order st[], int n); //顯示空的座位
void show_emptylist(struct pre_order st[], int n); //顯示空的座位位置
void show_seate(struct pre_order st[], int n); //按升序顯示有座位的
void delet(struct pre_order st[], int n); //刪除
void add(struct pre_order st[], int n); //添加
void paixu(struct pre_order *st[], int n); //和升序顯示一起的
int main(void)
{
char ch;
struct pre_order orders[SIZE] = {
{1, 0, "", ""},
{2, 0, "", ""},
{3, 0, "", ""},
{4, 0, "", ""},
{5, 0, "", ""},
{6, 0, "", ""},
{7, 0, "", ""},
{8, 0, "", ""},
{9, 0, "", ""},
{10, 0, "", ""},
{11, 0, "", ""},
{12, 0, "", ""}
};
while ((ch = menu()) != 'f')
{
switch (ch)
{
case 'a':
show_emptynum(orders, SIZE);
break;
case 'b':
show_emptylist(orders, SIZE);
break;
case 'd':
add(orders, SIZE);
break;
case 'e':
delet(orders, SIZE);
break;
case 'c':
show_seate(orders, SIZE);
break;
default:
puts("Err");
break;
}
}
return 0;
}
char menu(void)
{
char ch;
puts("To choose a function, enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of empty seats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) Quit");
ch = getchar();
while (ch < 'a' || ch > 'f')
{
puts("Please enter right");
while (getchar() != '\n')
continue;
ch = getchar();
}
while (getchar() != '\n')
continue;
return ch;
}
void show_emptynum(struct pre_order st[], int n)
{
int i;
int count = 0;
for ( i = 0; i < n; i++)
{
if (st[i].isnull == 0)
count++;
}
printf("number of empty seats: %d\n", count);
}
void show_emptylist(struct pre_order st[], int n)
{
int i;
printf("list of empty seats: ");
for ( i = 0; i < n; i++)
{
if (st[i].isnull == 0)
printf("%d ", st[i].id);
}
printf("\n");
}
void delet(struct pre_order st[], int n)
{
int i;
puts("enter num");
if (scanf("%d", &i) == 1)
{
if (i > 0 && i <= 12)
{
st[i - 1].isnull = 0;
printf("success\n");
}
else
{
puts("overstep");
}
}
else
{
puts("Error , please enter num");
}
while (getchar() != '\n')
continue;
}
void add(struct pre_order st[], int n)
{
int nums;
puts("Please enter seats");
scanf("%d", &nums);
while (getchar() != '\n')
continue;
if (nums > 0 && nums <= 12)
{
if (st[nums - 1].isnull == 0)
{
puts("Enter frist name");
scanf("%s", st[nums - 1].fname);
puts("Enter last name:");
scanf("%s", st[nums - 1].lname);
st[nums - 1].isnull = 1;
puts("Success");
while (getchar() != '\n')
continue;
}
else
{
puts("repeat!!");
}
}
else
{
puts("error!!");
}
}
void show_seate(struct pre_order st[], int n)
{
struct pre_order *pst[n];
int i;
int num = 0;
for ( i = 0; i < n; i++)
{
if (st[i].isnull != 0)
{
pst[num] = &st[i]; //將讓指針指向結(jié)構(gòu)成福,這樣排序完,原數(shù)據(jù)不會(huì)改變
num++;
}
}
paixu(pst, num);
for ( i = 0; i < num; i++)
{
printf("%d %d %s %s", pst[i]->id, pst[i]->isnull,
pst[i]->fname, pst[i]->lname);
printf("\n");
}
}
void paixu(struct pre_order *st[], int n)
{
int top, seek;
struct pre_order * temp;
for ( top = 0; top < n - 1; top++)
{
for ( seek = top + 1; seek < n; seek++)
{
if (strcmp(st[top]->fname, st[seek]->fname) > 0) //排序名字
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
else if (strcmp(st[top]->fname, st[seek]->fname) == 0) //當(dāng)名字相等排序姓
{
if (strcmp(st[top]->lname, st[seek]->lname) > 0)
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
}
}
}
}
9
/*
修改第8題荆残,改成有4個(gè)航線奴艾,讓人選擇做的航線在和第8題一樣
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 12
struct pre_order
{
int air_;
int id;
int isnull;
char fname[SIZE];
char lname[SIZE];
};
char menu(void);
char show_airline(void); //選擇航班界面
void show_emptynum(struct pre_order st[], int n, int air); //顯示空的座位
void show_emptylist(struct pre_order st[], int n, int air); //顯示空的座位位置
void show_seate(struct pre_order st[], int n); //按升序顯示有座位的
void delet(struct pre_order st[], int n); //刪除
void add(struct pre_order st[], int n); //添加
void paixu(struct pre_order *st[], int n); //和升序顯示一起的
int main(void)
{
char ch;
int air_num;
int air_tem[4] = {102, 311, 444, 519};
struct pre_order orders[4][SIZE];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < SIZE; j++)
{
orders[i][j].id = j + 1;
orders[i][j].isnull = 0;
orders[i][j].air_ = air_tem[i];
}
}
while ((ch = show_airline()) != 'e')
{
switch (ch)
{
case 'a':
air_num = 0;
break;
case 'b':
air_num = 1;
break;
case 'c':
air_num = 2;
break;
case 'd':
air_num = 3;
break;
default:
puts("error");
break;
}
while ((ch = menu()) != 'f')
{
switch (ch)
{
case 'a':
show_emptynum(&orders[air_num][0], SIZE, air_num);
break;
case 'b':
show_emptylist(&orders[air_num][0], SIZE, air_num);
break;
case 'd':
add(&orders[air_num][0], SIZE);
break;
case 'e':
delet(&orders[air_num][0], SIZE);
break;
case 'c':
show_seate(&orders[air_num][0], SIZE);
break;
default:
puts("Err");
break;
}
}
}
return 0;
}
char menu(void)
{
char ch;
puts("To choose a function, enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of empty seats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) Quit");
ch = getchar();
while (ch < 'a' || ch > 'f')
{
puts("Please enter right");
while (getchar() != '\n')
continue;
ch = getchar();
}
while (getchar() != '\n')
continue;
return ch;
}
char show_airline(void)
{
char ch;
puts("Enter a, b, c, e");
puts("a) 102 b) 311");
puts("c) 444 d) 519");
puts("e) quit");
ch = getchar();
while (ch < 'a' || ch > 'e')
{
puts("Please enter right");
while (getchar() != '\n')
continue;
ch = getchar();
}
while (getchar() != '\n')
continue;
return ch;
}
void show_emptynum(struct pre_order st[], int n, int air)
{
int i;
int count = 0;
for ( i = 0; i < n; i++)
{
if (st[i].isnull == 0)
count++;
}
printf("%d number of empty seats: %d\n", st[air].air_, count);
}
void show_emptylist(struct pre_order st[], int n, int air)
{
int i;
printf("%d list of empty seats: ", st[air].air_);
for ( i = 0; i < n; i++)
{
if (st[i].isnull == 0)
printf("%d ", st[i].id);
}
printf("\n");
}
void delet(struct pre_order st[], int n)
{
int i;
puts("enter num");
if (scanf("%d", &i) == 1)
{
if (i > 0 && i <= 12)
{
st[i - 1].isnull = 0;
printf("success\n");
}
else
{
puts("overstep");
}
}
else
{
puts("Error , please enter num");
}
while (getchar() != '\n')
continue;
}
void add(struct pre_order st[], int n)
{
int nums;
puts("Please enter seats");
scanf("%d", &nums);
while (getchar() != '\n')
continue;
if (nums > 0 && nums <= 12)
{
if (st[nums - 1].isnull == 0)
{
puts("Enter frist name");
scanf("%s", st[nums - 1].fname);
puts("Enter last name:");
scanf("%s", st[nums - 1].lname);
st[nums - 1].isnull = 1;
puts("Success");
while (getchar() != '\n')
continue;
}
else
{
puts("repeat!!");
}
}
else
{
puts("error!!");
}
}
void show_seate(struct pre_order st[], int n)
{
struct pre_order *pst[n];
int i;
int num = 0;
for ( i = 0; i < n; i++)
{
if (st[i].isnull != 0)
{
pst[num] = &st[i]; //將讓指針指向結(jié)構(gòu),這樣排序完内斯,原數(shù)據(jù)不會(huì)改變
num++;
}
}
paixu(pst, num);
for ( i = 0; i < num; i++)
{
printf("%d %d %d %s %s", pst[i]->air_, pst[i]->id, pst[i]->isnull,
pst[i]->fname, pst[i]->lname);
printf("\n");
}
}
void paixu(struct pre_order *st[], int n)
{
int top, seek;
struct pre_order * temp;
for ( top = 0; top < n - 1; top++)
{
for ( seek = top + 1; seek < n; seek++)
{
if (strcmp(st[top]->fname, st[seek]->fname) > 0) //排序名字
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
else if (strcmp(st[top]->fname, st[seek]->fname) == 0) //當(dāng)名字相等排序姓
{
if (strcmp(st[top]->lname, st[seek]->lname) > 0)
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
}
}
}
}
11
/**
* 編寫(xiě)一個(gè)名字叫做transform的函數(shù)蕴潦,接受4個(gè)參數(shù):內(nèi)含double類(lèi)型數(shù)據(jù)的源數(shù)組名
* 內(nèi)含double類(lèi)型數(shù)據(jù)的目標(biāo)數(shù)組名、一個(gè)表示數(shù)組元素個(gè)數(shù)的int類(lèi)型的參數(shù)俘闯、函數(shù)名
* (或等價(jià)的函數(shù)指針).transform函數(shù)應(yīng)該把指定函數(shù)應(yīng)用于源數(shù)組中的每個(gè)元素潭苞,并
* 把返回值存儲(chǔ)在目標(biāo)數(shù)組中。例如:
* transform(source, target, 100, sin);
* 該聲明把target[0]設(shè)置為sin(source[0]),等等真朗,共有100個(gè)元素.在一個(gè)程序
* 中調(diào)用transform()4次此疹,以測(cè)試該函數(shù),分別使用math.h函數(shù)庫(kù)中的
* 兩個(gè)函數(shù)以及自定義的兩個(gè)函數(shù)作為參數(shù)遮婶。
*/
#include <stdio.h>
#include <math.h>
void transform(double source[], double target[], int n, double (* pfun)(double ));
double mul(double n);
double add(double n);
double chu(double n);
int main(void)
{
double (*pfunc)(double);
double source[100];
double target[100];
for (int i = 0; i < 100; i++)
{
source[i] = i;
}
transform(source, target, 100, sin);
for (int i = 40; i < 45; i++)
{
printf("%.2lf %.2lf ", source[i], target[i]);
}
printf("\n");
transform(source, target, 100, mul);
for (int i = 40; i < 45; i++)
{
printf("%.2lf %.2lf ", source[i], target[i]);
}
pfunc = add;
printf("\n");
transform(source, target, 100, pfunc);
for (int i = 40; i < 45; i++)
{
printf("%.2lf %.2lf ", source[i], target[i]);
}
printf("\n");
transform(source, target, 100, chu);
for (int i = 40; i < 45; i++)
{
printf(" %.0lf ", target[i]);
}
return 0;
}
void transform(double source[], double target[], int n, double (* pfun)(double ))
{
for (int i = 0; i < n; i++)
{
target[i] = pfun(source[i]);
}
}
double mul(double n)
{
return n * 2;
}
double add(double n)
{
return n + n;
}
double chu(double n)
{
return n / n;
}