内容正文:
7.6节:结构体和共用体应用实例
一、单项选择题
1.以下关于typedef定义结构体的方式,正确的是( )
A. typedef struct { int id; char name[20]; } Stu; Stu s;
B. struct typedef Stu { int id; char name[20]; }; Stu s;
C. typedef struct Stu { int id; char name[20]; }; Stu s;
D. struct Stu { int id; char name[20]; }; typedef Stu Student; Student s;
2.定义struct A { int a; }; struct B { struct A a; char b; };,则struct B的大小是()(不考虑内存对齐)
A. 2 字节
B. 4 字节
C. 5 字节
D. 8 字节
3.若有typedef union { int i; char c; } Data; Data d[2];,以下引用正确的是()
A. d[0].i = 10;
B. d.i[0] = 10;
C. d[0][0] = 10;
D. d->i = 10;
二、程序运行结果题
4.执行以下程序的输出结果是()
#include <stdio.h>
struct complex {float real; float imag;};
struct complex add(struct complex a, struct complex b) {
struct complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
main() {
struct complex x = {1.5, 2.5}, y = {3.5, 4.5}, z;
z = add(x, y);
printf("%.1f+%.1fi", z.real, z.imag);
}
5.执行以下程序的输出结果是()
#include <stdio.h>
struct fraction {int numerator; int denominator;};
void simplify(struct fraction *f) {
int a = f->numerator, b = f->denominator;
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
f->numerator /= a;
f->denominator /= a;
}
main() {
struct fraction f = {12, 18};
simplify(&f);
printf("%d/%d", f.numerator, f.denominator);
}
6.执行以下程序的输出结果是()
#include <stdio.h>
struct student {int id; char name[10];};
main() {
struct student s = {1001, "张三"};
struct student t = s;
t.id = 1002;
printf("%d %d", s.id, t.id);
}
7.执行以下程序的输出结果是()
#include <stdio.h>
struct date {int year; int month; int day;};
int is_leap(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int days_in_month(struct date d) {
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (d.month == 2 && is_leap(d.year)) return 29;
return days[d.month-1];
}
main() {
struct date d = {2024, 2, 1};
printf("%d", days_in_month(d));
}
8.执行以下程序的输出结果是()
#include <stdio.h>
struct point {int x; int y;};
struct rectangle {struct point top_left; struct point bottom_right;};
int area(struct rectangle r) {
int width = r.bottom_right.x - r.top_left.x;
int height = r.top_left.y - r.bottom_right.y;
return width * height;
}
main() {
struct rectangle r = {{10, 20}, {30, 10}};
printf("%d", area(r));
}
9.执行以下程序的输出结果是()
#include <stdio.h>
struct time {int hour; int minute; int second;};
void add_second(struct time *t) {
t->second++;
if (t->second >= 60) {
t->second = 0;
t->minute++;
if (t->minute >= 60) {
t->minute = 0;
t->hour++;
if (t->hour >= 24) {
t->hour = 0;
}
}
}
}
main() {
struct time t = {23, 59, 59};
add_second(&t);
printf("%02d:%02d:%02d", t.hour, t.minute, t.second);
}
10.执行以下程序的输出结果是()
#include <stdio.h>
struct book {char title[50]; float price;};
void increase_price(struct book *b, float percent) {
b->price *= (1 + percent / 100);
}
main() {
struct book b = {"C Programming", 59.9};
increase_price(&b, 10);
printf("%.2f", b.price);
}
11.执行以下程序的输出结果是()
#include <stdio.h>
typedef struct {
char c;
int i;
} Data;
main() {
Data d = {'A', 65};
printf("%c %d", d.c, d.i);
}
12.执行以下程序的输出结果是()
#include <stdio.h>
typedef struct {
int id;
char name[20];
} Stu;
void fun(Stu s[]) {
s[0].id = 1001;
}
main() {
Stu s[2] = {{101, "Zhang"}, {102, "Li"}};
fun(s);
printf("%d %s", s[0].id, s[0].name);
}
13.执行以下程序的输出结果是()
#include <stdio.h>
struct S {
union {
int a;
char b;
} u;
int c;
};
main() {
struct S s;
s.u.a = 65;
s.c = s.u.a + 10;
printf("%d %c", s.c, s.u.b);
}
14.执行以下程序的输出结果是()
#include <stdio.h>
typedef struct {
int type;
union {
int num;
char ch;
} data;
} Data;
main() {
Data d[2];
d[0].type = 1;
d[0].data.num = 100;
d[1].type = 2;
d[1].data.ch = 'A';
printf("%d %c", d[0].data.num, d[1].data.ch);
}
15.执行以下程序的输出结果是()
#include <stdio.h>
struct A {
char c;
int i;
};
struct B {
struct A a;
struct A b;
};
main() {
struct B b = {{'A', 65}, {'B', 66}};
printf("%c %d", b.b.c, b.a.i);
}
16.执行以下程序的输出结果是()
#include <stdio.h>
struct S {
union {
short s;
char c[2];
} u;
};
main() {
struct S s[2];
s[0].u.s = 0x4142;
s[1].u.c[0] = 'C';
s[1].u.c[1] = 'D';
printf("%c%c %x", s[0].u.c[0], s[0].u.c[1], s[1].u.s); // 小端存储
}
17.执行以下程序的输出结果是()
#include <stdio.h>
struct Stu {
int id;
float score;
};
main() {
struct Stu s[3] = {{101, 85.5}, {102, 90.0}};
s[2].id = 103;
s[2].score = (s[0].score + s[1].score) / 2;
printf("%.1f", s[2].score);
}
18.执行以下程序的输出结果是()
#include <stdio.h>
struct A {
int a;
};
struct B {
struct A a;
union {
int b;
char c;
} u;
};
main() {
struct B b;
b.a.a = 10;
b.u.b = b.a.a * 2;
printf("%d %d", b.u.b, b.u.c);
}
19.执行以下程序的输出结果是()
#include <stdio.h>
struct Stu {
char name[20];
int age;
};
struct Group {
struct Stu s;
int count;
};
main() {
struct Group g = {{"Zhang", 20}, 1};
g.s.age += 5;
printf("%d %d", g.s.age, g.count);
}
20.执行以下程序的输出结果是()
#include <stdio.h>
typedef struct {
int id;
char sex;
} Stu;
main() {
Stu s[2] = {{101, 'M'}, {102, 'F'}};
Stu temp = s[0];
s[0] = s[1];
s[1] = temp;
printf("%c %d", s[0].sex, s[1].id);
}
三、编程题
1.定义结构体struct Item,包含成员:类型(int type,1 = 商品,2 = 服务)、名称(char name [20])和共用体(商品包含 “价格(float)、库存(int)”,服务包含 “时长(int)、单价(float)”)。要求:
输入 2 个项目的信息;
计算每个项目的总价值(商品:价格 × 库存;服务:时长 × 单价);
输出每个项目的类型、名称、总价值。
2.定义结构体struct Person,包含成员:姓名(char [20])、身份(int,1 = 学生,2 = 教师)、信息(共用体:学生包含 “班级(char [10])”,教师包含 “部门(char [20])、教龄(int)”)。要求:
输入 3 个人的信息;
统计教师的平均教龄;
输出所有人员的信息及教师平均教龄。
3.定义typedef struct表示 “图书”,包含成员:编号(int)、书名(char [30])、库存(int)、价格(共用体:单价(float)、折扣价(float,单价 ×0.8))。要求:
输入 2 本图书的编号、书名、库存、单价;
计算折扣价并存储到共用体;
输出每本图书的编号、书名、库存、单价、折扣价。
4.定义结构体struct Order,包含成员:订单号(int)、商品类型(int,1 = 电子,2 = 服装)、信息(共用体:电子包含 “保修时长(int)”,服装包含 “尺码(char [5])、颜色(char [10])”)、金额(float)。要求:
输入 2 个订单的信息;
输出每个订单的订单号、商品类型、信息、金额;
计算总金额。
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$
7.6节:结构体和共用体应用实例
一、单项选择题
1.以下关于typedef定义结构体的方式,正确的是( )
A. typedef struct { int id; char name[20]; } Stu; Stu s;
B. struct typedef Stu { int id; char name[20]; }; Stu s;
C. typedef struct Stu { int id; char name[20]; }; Stu s;
D. struct Stu { int id; char name[20]; }; typedef Stu Student; Student s;
【答案】A
2.定义struct A { int a; }; struct B { struct A a; char b; };,则struct B的大小是( )(不考虑内存对齐)
A. 2 字节
B. 4 字节
C. 5 字节
D. 8 字节
【答案】C
3.若有typedef union { int i; char c; } Data; Data d[2];,以下引用正确的是( )
A. d[0].i = 10;
B. d.i[0] = 10;
C. d[0][0] = 10;
D. d->i = 10;
【答案】A
二、程序运行结果题
4.执行以下程序的输出结果是( )
#include <stdio.h>
struct complex {float real; float imag;};
struct complex add(struct complex a, struct complex b) {
struct complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
main() {
struct complex x = {1.5, 2.5}, y = {3.5, 4.5}, z;
z = add(x, y);
printf("%.1f+%.1fi", z.real, z.imag);
}
【答案】5.0+7.0i
5.执行以下程序的输出结果是( )
#include <stdio.h>
struct fraction {int numerator; int denominator;};
void simplify(struct fraction *f) {
int a = f->numerator, b = f->denominator;
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
f->numerator /= a;
f->denominator /= a;
}
main() {
struct fraction f = {12, 18};
simplify(&f);
printf("%d/%d", f.numerator, f.denominator);
}
【答案】2/3
6.执行以下程序的输出结果是( )
#include <stdio.h>
struct student {int id; char name[10];};
main() {
struct student s = {1001, "张三"};
struct student t = s;
t.id = 1002;
printf("%d %d", s.id, t.id);
}
【答案】1001 1002
7.执行以下程序的输出结果是( )
#include <stdio.h>
struct date {int year; int month; int day;};
int is_leap(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int days_in_month(struct date d) {
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (d.month == 2 && is_leap(d.year)) return 29;
return days[d.month-1];
}
main() {
struct date d = {2024, 2, 1};
printf("%d", days_in_month(d));
}
【答案】29
8.执行以下程序的输出结果是( )
#include <stdio.h>
struct point {int x; int y;};
struct rectangle {struct point top_left; struct point bottom_right;};
int area(struct rectangle r) {
int width = r.bottom_right.x - r.top_left.x;
int height = r.top_left.y - r.bottom_right.y;
return width * height;
}
main() {
struct rectangle r = {{10, 20}, {30, 10}};
printf("%d", area(r));
}
【答案】200
9.执行以下程序的输出结果是( )
#include <stdio.h>
struct time {int hour; int minute; int second;};
void add_second(struct time *t) {
t->second++;
if (t->second >= 60) {
t->second = 0;
t->minute++;
if (t->minute >= 60) {
t->minute = 0;
t->hour++;
if (t->hour >= 24) {
t->hour = 0;
}
}
}
}
main() {
struct time t = {23, 59, 59};
add_second(&t);
printf("%02d:%02d:%02d", t.hour, t.minute, t.second);
}
【答案】00:00:00
10.执行以下程序的输出结果是( )
#include <stdio.h>
struct book {char title[50]; float price;};
void increase_price(struct book *b, float percent) {
b->price *= (1 + percent / 100);
}
main() {
struct book b = {"C Programming", 59.9};
increase_price(&b, 10);
printf("%.2f", b.price);
}
【答案】65.89
11.执行以下程序的输出结果是( )
#include <stdio.h>
typedef struct {
char c;
int i;
} Data;
main() {
Data d = {'A', 65};
printf("%c %d", d.c, d.i);
}
【答案】A 65
12.执行以下程序的输出结果是( )
#include <stdio.h>
typedef struct {
int id;
char name[20];
} Stu;
void fun(Stu s[]) {
s[0].id = 1001;
}
main() {
Stu s[2] = {{101, "Zhang"}, {102, "Li"}};
fun(s);
printf("%d %s", s[0].id, s[0].name);
}
【答案】1001 Zhang
13.执行以下程序的输出结果是( )
#include <stdio.h>
struct S {
union {
int a;
char b;
} u;
int c;
};
main() {
struct S s;
s.u.a = 65;
s.c = s.u.a + 10;
printf("%d %c", s.c, s.u.b);
}
【答案】75 A
14.执行以下程序的输出结果是( )
#include <stdio.h>
typedef struct {
int type;
union {
int num;
char ch;
} data;
} Data;
main() {
Data d[2];
d[0].type = 1;
d[0].data.num = 100;
d[1].type = 2;
d[1].data.ch = 'A';
printf("%d %c", d[0].data.num, d[1].data.ch);
}
【答案】100 A
15.执行以下程序的输出结果是( )
#include <stdio.h>
struct A {
char c;
int i;
};
struct B {
struct A a;
struct A b;
};
main() {
struct B b = {{'A', 65}, {'B', 66}};
printf("%c %d", b.b.c, b.a.i);
}
【答案】B 65
16.执行以下程序的输出结果是( )
#include <stdio.h>
struct S {
union {
short s;
char c[2];
} u;
};
main() {
struct S s[2];
s[0].u.s = 0x4142;
s[1].u.c[0] = 'C';
s[1].u.c[1] = 'D';
printf("%c%c %x", s[0].u.c[0], s[0].u.c[1], s[1].u.s); // 小端存储
}
【答案】BA 4443
17.执行以下程序的输出结果是( )
#include <stdio.h>
struct Stu {
int id;
float score;
};
main() {
struct Stu s[3] = {{101, 85.5}, {102, 90.0}};
s[2].id = 103;
s[2].score = (s[0].score + s[1].score) / 2;
printf("%.1f", s[2].score);
}
【答案】87.8
18.执行以下程序的输出结果是( )
#include <stdio.h>
struct A {
int a;
};
struct B {
struct A a;
union {
int b;
char c;
} u;
};
main() {
struct B b;
b.a.a = 10;
b.u.b = b.a.a * 2;
printf("%d %d", b.u.b, b.u.c);
}
【答案】20 20
19.执行以下程序的输出结果是( )
#include <stdio.h>
struct Stu {
char name[20];
int age;
};
struct Group {
struct Stu s;
int count;
};
main() {
struct Group g = {{"Zhang", 20}, 1};
g.s.age += 5;
printf("%d %d", g.s.age, g.count);
}
【答案】25 1
20.执行以下程序的输出结果是( )
#include <stdio.h>
typedef struct {
int id;
char sex;
} Stu;
main() {
Stu s[2] = {{101, 'M'}, {102, 'F'}};
Stu temp = s[0];
s[0] = s[1];
s[1] = temp;
printf("%c %d", s[0].sex, s[1].id);
}
【答案】F 101
三、编程题
1.定义结构体struct Item,包含成员:类型(int type,1 = 商品,2 = 服务)、名称(char name [20])和共用体(商品包含 “价格(float)、库存(int)”,服务包含 “时长(int)、单价(float)”)。要求:
输入 2 个项目的信息;
计算每个项目的总价值(商品:价格 × 库存;服务:时长 × 单价);
输出每个项目的类型、名称、总价值。
【答案】
#include <stdio.h>
#include <string.h>
union ItemData {
struct {
float price;
int stock;
} goods;
struct {
int duration;
float unit_price;
} service;
};
struct Item {
int type;
char name[20];
union ItemData data;
};
main() {
int i;
struct Item items[2];
for (i=0; i<2; i++) {
printf("请输入第%d个项目(类型 名称 信息):", i+1);
scanf("%d %s", &items[i].type, items[i].name);
if (items[i].type == 1) {
scanf("%f %d", &items[i].data.goods.price, &items[i].data.goods.stock);
} else if (items[i].type == 2) {
scanf("%d %f", &items[i].data.service.duration, &items[i].data.service.unit_price);
}
}
printf("
项目信息:
");
for (i=0; i<2; i++) {
printf("类型:%s
名称:%s
总价值:",
(items[i].type == 1 ? "商品" : "服务"), items[i].name);
if (items[i].type == 1) {
printf("%.2f
", items[i].data.goods.price * items[i].data.goods.stock);
} else {
printf("%.2f
", items[i].data.service.duration * items[i].data.service.unit_price);
}
}
}
2.定义结构体struct Person,包含成员:姓名(char [20])、身份(int,1 = 学生,2 = 教师)、信息(共用体:学生包含 “班级(char [10])”,教师包含 “部门(char [20])、教龄(int)”)。要求:
输入 3 个人的信息;
统计教师的平均教龄;
输出所有人员的信息及教师平均教龄。
【答案】
#include <stdio.h>
#include <string.h>
union Info {
char cls[10];
struct {
char dept[20];
int year;
} teacher;
};
struct Person {
char name[20];
int identity;
union Info info;
};
main() {
struct Person p[3];
int teacher_count = 0;
int year_sum = 0,i;
for (i=0; i<3; i++) {
printf("请输入第%d个人(身份 姓名 信息):", i+1);
scanf("%d %s", &p[i].identity, p[i].name);
if (p[i].identity == 1) {
scanf("%s", p[i].info.cls);
} else if (p[i].identity == 2) {
scanf("%s %d", p[i].info.teacher.dept, &p[i].info.teacher.year);
year_sum += p[i].info.teacher.year;
teacher_count++;
}
}
printf("
人员信息:
");
for (i=0; i<3; i++) {
printf("姓名:%s
身份:%s
",
p[i].name, (p[i].identity == 1 ? "学生" : "教师"));
if (p[i].identity == 1) {
printf("班级:%s
", p[i].info.cls);
} else {
printf("部门:%s
教龄:%d
", p[i].info.teacher.dept, p[i].info.teacher.year);
}
}
if (teacher_count > 0) {
printf("教师平均教龄:%.1f
", (float)year_sum / teacher_count);
} else {
printf("无教师信息
");
}
}
3.定义typedef struct表示 “图书”,包含成员:编号(int)、书名(char [30])、库存(int)、价格(共用体:单价(float)、折扣价(float,单价 ×0.8))。要求:
输入 2 本图书的编号、书名、库存、单价;
计算折扣价并存储到共用体;
输出每本图书的编号、书名、库存、单价、折扣价。
【答案】
#include <stdio.h>
#include <string.h>
typedef union {
float price;
float discount_price;
} Price;
typedef struct {
int id;
char title[30];
int stock;
Price p;
} Book;
main() {
int i;
Book b[2];
for (i=0; i<2; i++) {
printf("请输入第%d本图书(编号 书名 库存 单价):", i+1);
scanf("%d %s %d %f", &b[i].id, b[i].title, &b[i].stock, &b[i].p.price);
// 计算折扣价
b[i].p.discount_price = b[i].p.price * 0.8;
}
printf("
图书信息:
");
for (i=0; i<2; i++) {
printf("编号:%d
书名:%s
库存:%d
单价:%.2f
折扣价:%.2f
",
b[i].id, b[i].title, b[i].stock, b[i].p.price, b[i].p.discount_price);
}
}
4.定义结构体struct Order,包含成员:订单号(int)、商品类型(int,1 = 电子,2 = 服装)、信息(共用体:电子包含 “保修时长(int)”,服装包含 “尺码(char [5])、颜色(char [10])”)、金额(float)。要求:
输入 2 个订单的信息;
输出每个订单的订单号、商品类型、信息、金额;
计算总金额。
【答案】
#include <stdio.h>
#include <string.h>
union OrderInfo {
int warranty;
struct {
char size[5];
char color[10];
} clothes;
};
struct Order {
int id;
int type;
union OrderInfo info;
float amount;
};
main() {
struct Order o[2];
float total = 0;
int i;
for (i=0; i<2; i++) {
printf("请输入第%d个订单(类型 订单号 信息 金额):", i+1);
scanf("%d %d", &o[i].type, &o[i].id);
if (o[i].type == 1) {
scanf("%d", &o[i].info.warranty);
} else {
scanf("%s %s", o[i].info.clothes.size, o[i].info.clothes.color);
}
scanf("%f", &o[i].amount);
total += o[i].amount;
}
printf("
订单信息:
");
for (i=0; i<2; i++) {
printf("订单号:%d
类型:%s
",
o[i].id, (o[i].type == 1 ? "电子" : "服装"));
if (o[i].type == 1) {
printf("保修时长:%d个月
", o[i].info.warranty);
} else {
printf("尺码:%s
颜色:%s
", o[i].info.clothes.size, o[i].info.clothes.color);
}
printf("金额:%.2f
", o[i].amount);
}
printf("总金额:%.2f
", total);
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$