关键字:struct、union、typedef
运算符: .、 ->
结构声明
形如:
struct book{
char title[MAXTITL];
float value;
};
调用
struct book doyle, panshin, * ptbook;
另外两种声明方式
比如说想要直接使用 library
struct book library;
可以简化成:
struct book{
char title[MAXTITL];
float value;
} library; /*声明的最后跟变量名 就能直接用library*/
或者:
struct { /*无结构标记*/
char title[MAXTITL];
float value;
} library;
不过若是想要多次使用这个结构模版,就要使用带标记的形式;或者,使用后面介绍的typedef
初始化结构
struct book library = {
"Renee vivvvvv",
1.95
};/*和初始化数组差不多*/
使用点号对结构访问
#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;
}
结构的初始化器
只想要初始化value
struct book library = { .value = 10.99};
指向结构的指针
声明
struct book * him;
him = &library; /*和数组不同,结构名并不是指针的地址,因此,要加上&运算符*/
用指针访问成员
/*第一种方法使用->符号*/
him -> value 就是 library.value
第二种方法
(*him).value
使用malloc()
假设如下
#define LEN 20
struct names{
char first[LEN];
char last[len];
};
struct names veep = {"Talia", "summers"};
/*这样会照成字节的浪费*/
使用malloc
void getinfo (struct namect * pst)
{
char temp[SLEN];
printf("Please enter your first name.\n");
s_gets(temp, SLEN);
//分配内存存储名
pst -> fname = (char *) malloc(strlen(temp) + 1);
//把名拷贝到已分配的内存
strcpy(pst->fname, temp);
} /*书上有详细例子建议多看看*/
typedef
- 与define不同,typedef创建的符号名只受限与类型,不能用于值。
- typedef 由编译器解释,不是预处理器
- 在其受限范围内,typedef 比#define更灵活
typedef unsigned char BYTE;
BYTE x, y;
typedef struct book{
char title[MAXTITL];
float value;
} BOOK;
BOOK library;
编程练习
1
/*
重写编写复习题5,用月份名的拼写代替月份号(别忘了使用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中的图书目录程序,使其按照输入图书的顺序输出图书的信息,
然后按照标题字母的声明输出图书的信息,最后按照价格的升序输出图书的信息
*/
#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); //一种用内置的c函数 qsort https://blog.csdn.net/asdgyy/article/details/82956108
void money_sort(struct book *pst[] , int 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,函数内再转换
{
return strcmp(((struct book*)a)->title, ((struct book*)b)->title);
}
4
/*
编写一个程序,创建一个有两个成员的结构模版
*/
#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
/*
编写一个程序满足下面的要求
a. 外部定义一个有两个成员的结构模版name:一个字符串储存名,资格存姓
b. 外部定义一个有3个成员的结构模版student:一个name类的结构
一个grade数组存3个浮点型分数,一个变量存储3个分数平均数。
c. 在main()函数中声明一个内含CSIZE(CSIZE=4)个student类型的结构的
数组,并初始化这些结构的名字部分,用函数执行g、e、f和g中描述的任务
d. 以交互式的方式获取每个学生的成绩,提示用户输入学生的姓名和分数
把分数储存到grade数组相应的结构中,可以在main()函数或其他函数
中用循环来完成
e. 计算每个结构的平均分,并把计算后的值付给合适的成员
f. 打印么个结构的信息
g. 打印班级的平均分, 即所有结构的数值成员的平均值
*/
#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
/*
一个文本文件里面保存着一个垒球队的信息。每行数据都是这样排列:
4 Jessie Joybat 5 2 1 1
第一项是球员号码,范围0~18.第二项是球员的名字第三是姓。第四是上场
次数。之后分别是击中数,走垒数,打点和安打率。编写一个程序将他们存进
结构数组中
*/
#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[]); //计算安打
void show(struct ball_result st[]); //打印数据
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, 从文件中读取每条记录并显示出来,允许用户删除记录
或修改记录的内容。如果删除记录,把空出来的空间留给下一个要读入的记录
要修改现有的文件内容,必须使用"r+b"模式,而不是"a+b"而且,必须更加注意
定位文件指针,防止新加入的记录覆盖现有的记录。最简单的方法是改动存储在内存
中的所有数据,然后再把最后的信息写入文件。跟踪的一个方法是在book结构中
添加一个成员表示是否该项被删除
*/
#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); //得到要删除第几行
char * s_gets(char * st, int n);
char menu(void); //菜单界面 有a, b, c三个选项
int delet(struct book *pst, int n, int *st); //删除函数
int change(struct book *pst, int n, int *st); //修改函数
int add(struct book *pst, int n, int *st); //添加函数
int main(void)
{
struct book library[MAXBKS];
char ch; //作为选择菜单,存储a,b,c选项的变量
int count = 0;
int index;
FILE * pbooks;
int size = sizeof(struct book);
if ((pbooks = fopen("wordy.bat", "a+b")) == NULL) //打开和写入的文件名
{
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}; //存储临时删除的值,之后添加便会添加到这个位置
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"); //将原来数据都删除,重写
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"); //不要重复删除
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"); //已经修改的不能再删除
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:"); //输入要删除第几排
scanf("%d", &i);
while (i > n || i <= 0)
{
while (getchar() != '\n')
continue;
puts("Error!! it's extrem!!"); // 表示范围超过
scanf("%d", &i);
puts("Please enter num:");
}
while (getchar() != '\n')
continue;
return i;
}

image.png
选择框界面
8
/*
巨人航空公司的机群由12个座位的飞机组成。他每天飞行一个航班。根据下面的要求
编写一个座位预定的程序。
a 程序使用一个内涵12个结构的数组。每个结构中包括: 一个成员表示座位编号、一个
成员表示座位是否已被预定。一个表示预定的人名。一个表示姓
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]; //将让指针指向结构,这样排序完,原数据不会改变
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) //当名字相等排序姓
{
if (strcmp(st[top]->lname, st[seek]->lname) > 0)
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
}
}
}
}
9
/*
修改第8题,改成有4个航线,让人选择做的航线在和第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]; //将让指针指向结构,这样排序完,原数据不会改变
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) //当名字相等排序姓
{
if (strcmp(st[top]->lname, st[seek]->lname) > 0)
{
temp = st[top];
st[top] = st[seek];
st[seek] = temp;
}
}
}
}
}
11
/**
* 编写一个名字叫做transform的函数,接受4个参数:内含double类型数据的源数组名
* 内含double类型数据的目标数组名、一个表示数组元素个数的int类型的参数、函数名
* (或等价的函数指针).transform函数应该把指定函数应用于源数组中的每个元素,并
* 把返回值存储在目标数组中。例如:
* transform(source, target, 100, sin);
* 该声明把target[0]设置为sin(source[0]),等等,共有100个元素.在一个程序
* 中调用transform()4次,以测试该函数,分别使用math.h函数库中的
* 两个函数以及自定义的两个函数作为参数。
*/
#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;
}