第9章 第5节:结构体与函数(练习)-《C语言程序设计》(电子工业出版社)上好课

2026-06-29
| 2份
| 50页
| 33人阅读
| 0人下载

内容正文:

9.5节:结构体与函数 一、单项选择题 1.将结构体单个成员作为函数实参传递时,传递机制为( ) A. 地址传递 B. 值传递 C. 指针传递 D. 双向传递 2.定义struct Student{int id;float score;}s={101,92.5};,调用函数void print(int x),合法实参是() A. print (s) B. print (&s.id) C. print (s.id) D. print (&s) 3.关于完整结构体变量作为函数形参,下列说法正确的是() A. 函数内修改形参成员,外部实参同步改变 B. 传递时仅拷贝结构体首地址 C. 传递时完整拷贝结构体所有成员数据 D. 传递效率高于结构体指针作参数 4.void modify(struct Student *p)函数接收结构体指针,想要修改外部结构体变量的 score 成员,正确写法是() A. p.score = 95; B. *p.score = 95; C. p->score = 95; D. Student.score = 95; 5.值传递(结构体变量作参数)与地址传递(结构体指针作参数)的核心区别是() A. 值传递只能传 int 成员,地址传递可传 float B. 值传递拷贝完整结构体副本,地址传递仅传递地址 C. 值传递函数不能修改数据,地址传递一定能修改 D. 值传递语法不合法,地址传递语法合法 6.定义struct Book{int page;};,合法的结构体指针形参定义是() A. void func (struct Book p) B. void func (struct Book *p) C. void func (Book *p) D. void func (*struct Book p) 7.设有定义:struct Game{int num;}; struct Game g={12}; void change(struct Game *p){p->num=20;},主函数调用change(&g);,打印 g.num 结果为() A. 12 B. 20 C. 随机值 D. 0 8.设有定义:struct Cat{int age;}; struct Cat c={3}; void test(struct Cat t){t.age=5;},主函数调用test(c);,打印 c.age 结果为() A. 3 B. 5 C. 0 D. 随机值 9.执行以下程序段的输出结果是() #include<stdio.h> struct Box{int w;}; void setW(int x){x=100;} int main(){ struct Box b={20}; setW(b.w); printf("%d",b.w); return 0; } A. 20 B. 100 C. 0 D. 随机数字 10.执行以下程序段的输出结果是() #include<stdio.h> struct Box{int length;}; void modify(struct Box *p){ p->length = p->length * 2; } int main() { struct Box b = {18}; modify(&b); printf("%d", b.length); return 0;} A. 18 B. 36 C. 9 D. 20 二、填空题。 1.函数形参为结构体指针struct Student *p,访问成员 age 的标准写法为___________。 2.执行以下程序段的输出结果是___________。 struct Box{int w;}; void func(struct Box *p){p->w = p->w * 3;} int main(){struct Box b={12}; func(&b); printf("%d",b.w);} 3.想要在函数内修改主函数原始结构体的数据,应当采用________作为函数形参。 4.执行以下程序段的输出结果是___________。 struct Cat{int age;}; void test(struct Cat t){t.age=10;} int main(){struct Cat c={4}; test(c); printf("%d",c.age);} 5.执行以下程序段的输出结果是___________。 struct Num{int x;int y;}; void add(struct Num *p){p->x += 4; p->y -= 1;} int main(){struct Num n={9,7}; add(&n); printf("%d",n.x+n.y);} 6.执行以下程序段的输出结果是___________。 struct Book{int page;}; void plus(int x){x = x + 15;} int main(){struct Book b={60}; plus(b.page); printf("%d",b.page);} 7.执行以下程序段的输出结果是___________。 struct Fruit{int num;}; void change(struct Fruit *p){p->num /= 2;} int main(){struct Fruit f={46}; change(&f); printf("%d",f.num);} 8.执行以下程序段的输出结果是___________。 struct Toy{int a;int b;}; void sub(struct Toy t){t.a = t.a - 3; t.b = t.b + 2;} int main(){struct Toy toy={14,5}; sub(toy); printf("%d",toy.a-toy.b);} 9.执行以下程序段的输出结果是___________。 struct Cube{int len;}; void mul(struct Cube *p){p->len = p->len * p->len;} int main(){struct Cube c={7}; mul(&c); printf("%d",c.len);} 10.函数形参为结构体指针struct Goods *pr,使用带括号的等价写法读取成员 num,完整表达式为___________。 三、阅读程序题。 1.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Data { int val; }; void calcSum(struct Data *arr, int len, int *res) { int i; *res = 0; for(i = 0; i < len; i++) { if(arr->val % 2 != 0) *res = *res + arr->val; arr++; } } int main() { struct Data num[5] = {{12},{27},{9},{34},{15}}; int total; calcSum(num, 5, &total); printf("%d", total); return 0; } 2.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Score { int s; }; int diffTotal(struct Score *p, int n) { int i, sum = 0; for(i = 0; i < n - 1; i++) { sum = sum + (p->s - (p+1)->s); p++; } return sum; } int main() { struct Score stu[4] = {{86},{72},{91},{65}}; printf("%d", diffTotal(stu, 4)); return 0; } 3.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Goods { int cost; }; int countMid(struct Goods *p) { int cnt = 0, i; for(i = 0; i < 4; i++) { if(p->cost >= 60 && p->cost <= 100) cnt++; p++; } return cnt; } int main() { struct Goods shop[4] = {{42},{75},{96},{110}}; printf("%d", countMid(shop)); return 0; } 4.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Fruit { int cnt; }; void addAll(struct Fruit *arr, int len, int add) { int i; for(i = 0; i < len; i++) { arr->cnt = arr->cnt + add; arr++; } } int main() { struct Fruit farm[4] = {{12},{18},{9},{24}}; int max, min, i; addAll(farm, 4, 6); max = farm[0].cnt; min = farm[0].cnt; for(i = 1; i < 4; i++) { if(farm[i].cnt > max) max = farm[i].cnt; if(farm[i].cnt < min) min = farm[i].cnt; } printf("%d", max - min); return 0; } 5.写出下列程序段的输出结果:________________。 #include <stdio.h> int gcd(int a, int b) { int r; while(b != 0) { r = a % b; a = b; b = r; } return a; } struct Brick { int x; int y; }; int main() { struct Brick blk = {48, 18}; printf("%d", gcd(blk.x, blk.y)); return 0; } 6.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Seed { int rate; }; int sliceCalc(struct Seed *p, int mid) { int sum1 = 0, sum2 = 0, i; for(i = 0; i < mid; i++) { sum1 += p->rate; p++; } for(i = mid; i < 5; i++) { sum2 += p->rate; p++; } return sum1 - sum2; } int main() { struct Seed arr[5] = {{11},{13},{7},{16},{9}}; printf("%d", sliceCalc(arr, 2)); return 0; } 7.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Gear { int tooth; }; void changeOne(struct Gear *p) { p->tooth = p->tooth * 3; } int main() { struct Gear device[4] = {{12},{18},{24},{30}}; int i, squareSum = 0; changeOne(device + 1); for(i = 0; i < 4; i++) { squareSum += device[i].tooth * device[i].tooth; } printf("%d", squareSum); return 0; } 8.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Mineral { int val; }; int primeSum(struct Mineral *p, int len) { int i,j,sum=0,flag; for(i=0;i<len;i++) { flag=1; for(j=2;j<p->val;j++) { if(p->val%j==0) { flag=0; break; } } if(flag) sum+=p->val; p++; } return sum; } int main() { struct Mineral ore[4]={{13},{22},{17},{9}}; printf("%d",primeSum(ore,4)); return 0; } 9.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Pet { int snack; }; void swapTwo(struct Pet *p1, struct Pet *p2) { struct Pet temp = *p1; *p1 = *p2; *p2 = temp; } int main() { struct Pet animal[4] = {{28},{17},{26},{14}}; int i, total = 0; swapTwo(animal, animal + 3); for(i = 0; i < 4; i++) total += animal[i].snack; printf("%d", total); return 0; } 10.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Box { int gold; int diamond; }; int countGap(struct Box *arr, int limit) { int i, cnt = 0, gap; for(i = 0; i < 4; i++) { gap = arr->gold - arr->diamond; if(gap > limit) cnt++; arr++; } return cnt; } int main() { struct Box map[4] = {{78,29},{55,41},{88,22},{66,33}}; printf("%d", countGap(map, 30)); return 0; } 四、程序填空题(根据题意,将空缺的语句补上) 1.结构体与函数实现“大整数”乘法运算:定义一个 BigInt 结构体,包含整型数组 digits 和长度 len。下列三个函数:initBigInt(将整数初始化为大整数结构体)、multiplyBigInt(实现两个大整数结构体的乘法,结果存入第三个结构体)、printBigInt(打印大整数)。在 main 函数中调用这些函数计算 12345 × 67890 并输出结果。 #include <stdio.h> #include <string.h> struct BigInt { int digits[50]; int len; }; void initBigInt(struct BigInt *b, int num) { int i = 0; if (num == 0) { b->digits[0] = 0; b->len = 1; return; } while (num > 0) { b->digits[i++] = _______①______ num /= 10; } ________②________ } void multiplyBigInt(struct BigInt *a, struct BigInt *b, struct BigInt *res) { int i, j; memset(res, 0, sizeof(struct BigInt)); for (i = 0; i < a->len; i++) { for (j = 0; j < b->len; j++) { res->digits[i + j] +=___________③_________ res->digits[i + j + 1] += ___________④__________ res->digits[i + j] %= 10; } } res->len = a->len + b->len; while (res->len > 1 && __________⑤__________) res->len--; } void printBigInt(struct BigInt *b) { int i; for (i = ______⑥______; i >= 0; i--) printf("%d", b->digits[i]); printf(" "); } int main() { struct BigInt a, b, res; initBigInt(&a, 12345); initBigInt(&b, 67890); multiplyBigInt(&a, &b, &res); printf("Result: "); printBigInt(&res); return 0; } 2.“汉诺塔”移动步骤记录:定义一个 MoveStep 结构体,包含盘子编号 disk、起始柱 from 和目标柱 to。递归函数 hanoi,将移动步骤记录到一个 MoveStep 结构体数组中。在 main 函数中调用该函数解决3层汉诺塔问题,并遍历数组输出所有移动步骤。 #include <stdio.h> struct MoveStep { int disk; char from; char to; }; int stepCount = 0; void hanoi(struct MoveStep steps[], int n, char from, char to, char aux) { if (________①______) { steps[stepCount].disk = n; steps[stepCount].from = from; steps[stepCount].to = to; stepCount++; return; } hanoi(____________②__________); steps[stepCount].disk = n; steps[stepCount].from = from; steps[stepCount].to = to; stepCount++; hanoi(___________③_________); } int main() { struct MoveStep steps[20]; int i; hanoi(steps, 3, 'A', 'C', 'B'); for (i = 0; i < stepCount; i++) { printf("Move disk %d from %c to %c ", steps[i].disk, steps[i].from, steps[i].to); } return 0; } 3.“多项式求导”:定义一个 Term 结构体(系数 coef,指数 exp)。编写函数 differentiate,接收一个多项式结构体数组及其项数,对每一项进行求导(系数乘以指数,指数减1),将结果存入新的结构体数组并返回有效项数。main 函数中测试对 3x^3 + 2x^2 + 5x 求导。 #include <stdio.h> struct Term { float coef; int exp; }; int differentiate(struct Term src[], int n, struct Term dest[]) { int i, count = 0; for (i = 0; i < n; i++) { if (src[i].exp > 0) { dest[count].coef = _________①__________ dest[count].exp = src[i].exp - 1; ________②________ } } return count; } int main() { struct Term poly[3] = {{3.0, 3}, {2.0, 2}, {5.0, 1}}; struct Term deriv[3]; int n, i; n = differentiate(poly, 3, deriv); printf("Derivative: "); for (i = 0; i < n; i++) { printf("%.0fx^%d ", deriv[i].coef, deriv[i].exp); if (________③______) printf("+ "); } printf(" "); return 0; } 4.“学生成绩排序与筛选”:定义 Student 结构体(学号 id,姓名 name,分数 score)。函数 sortByScore(冒泡排序,按分数降序)和 filterHighScore(筛选分数大于等于80的学生,返回人数)。main 函数初始化5个学生,调用函数并输出排序后的高分学生名单。 #include <stdio.h> #include <string.h> struct Student { int id; char name[20]; float score; }; void sortByScore(struct Student arr[], int n) { int i, j; struct Student temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (_____________①__________) { temp = arr[j]; arr[j] = arr[j + 1]; __________②_________ } } } } int filterHighScore(struct Student src[], int n, struct Student dest[]) { int i, count = 0; for (i = 0; i < n; i++) { if (src[i].score >= 80.0) { ____________③___________ } } return count; } int main() { struct Student class1[5] = {{101,"Alice",85.5}, {102,"Bob",72.0}, {103,"Charlie",90.0},{104,"David",68.5}, {105,"Eve",88.0}}; struct Student high[5]; int i, count; sortByScore(class1, 5); count = filterHighScore(class1, 5, high); printf("High Score Students: "); for (i = 0; i < count; i++) { printf("%d %s %.1f ", high[i].id, high[i].name, high[i].score); } return 0; } 五、编程题。 1. “日期差值计算”:定义 Date 结构体(年 year,月 month,日 day)。编写函数 isLeap(判断闰年)、daysInMonth(获取某月天数)和 dateDiff(计算两个日期之间相差的天数)。main 函数中计算 2023年1月1日 到 2023年3月15日 的天数差。 2.“矩阵乘法”:定义 Matrix 结构体,包含二维数组 data、行数 rows、列数 cols。编写函数 multiplyMatrix,接收两个矩阵结构体指针,计算乘积并存入第三个矩阵结构体。main 函数中计算 2x3 矩阵与 3x2 矩阵的乘积。 3.“字符串词频统计”:定义 WordFreq 结构体(单词 word,频率 count)。编写函数 addWord,遍历字符串提取单词,在结构体数组中查找并更新频率。编写 sortByFreq 按频率降序排序。main 函数统计 "hello world hello c world c c" 的词频并输出。 4.“质数筛选与存储”:定义一个 PrimeSet 结构体,包含整型数组 primes 和记录质数个数的 count。编写函数 sieveOfEratosthenes,使用经典的“埃拉托斯特尼筛法”高效找出100以内的所有质数,并将结果存入结构体数组中。在 main 函数中调用该函数,并遍历输出所有筛选出的质数。 5.“贪心算法解决找零问题”:定义一个 Change 结构体,包含面额 denomination 和所需张数 count。编写函数 makeChange,接收一个 Change 结构体数组(预设好人民币面额:100, 50, 20, 10, 5, 1),使用贪心算法计算找零给定金额所需的最少纸币张数,并将结果填入结构体数组中。main 函数测试找零 278 元。 原创精品资源学科网独家享有版权,侵权必究! 学科网(北京)股份有限公司 $ 9.5节:结构体与函数 一、单项选择题 1.将结构体单个成员作为函数实参传递时,传递机制为( ) A. 地址传递 B. 值传递 C. 指针传递 D. 双向传递 【答案】B 【解析】单个成员本质是普通基础类型变量,传递时拷贝数值,属于值传递,函数内修改不会影响外部原数据。 2.定义struct Student{int id;float score;}s={101,92.5};,调用函数void print(int x),合法实参是( ) A. print (s) B. print (&s.id) C. print (s.id) D. print (&s) 【答案】C 【解析】函数形参为 int,只能传入结构体 int 类型成员s.id;A 传完整结构体、D 传结构体地址类型不匹配,B 传成员地址是指针类型,均报错。 3.关于完整结构体变量作为函数形参,下列说法正确的是( ) A. 函数内修改形参成员,外部实参同步改变 B. 传递时仅拷贝结构体首地址 C. 传递时完整拷贝结构体所有成员数据 D. 传递效率高于结构体指针作参数 【答案】C 【解析】结构体变量值传递会完整复制全部成员;A 错误,修改形参不影响外部;B、D 是指针参数的特点。 4.void modify(struct Student *p)函数接收结构体指针,想要修改外部结构体变量的 score 成员,正确写法是( ) A. p.score = 95; B. *p.score = 95; C. p->score = 95; D. Student.score = 95; 【答案】C 【解析】结构体指针访问成员专用->运算符;A 指针不能用.,B 运算符优先级错误,D 缺少变量 / 指针。 5.值传递(结构体变量作参数)与地址传递(结构体指针作参数)的核心区别是( ) A. 值传递只能传 int 成员,地址传递可传 float B. 值传递拷贝完整结构体副本,地址传递仅传递地址 C. 值传递函数不能修改数据,地址传递一定能修改 D. 值传递语法不合法,地址传递语法合法 【答案】B 【解析】核心差异在数据拷贝方式;A 类型无关;C 指针也可只读不修改;D 两种传递语法均合法。 6.定义struct Book{int page;};,合法的结构体指针形参定义是( ) A. void func (struct Book p) B. void func (struct Book *p) C. void func (Book *p) D. void func (*struct Book p) 【答案】B 【解析】结构体指针形参标准格式struct 结构体名 *指针名;A 是结构体变量形参,C 缺少 struct 关键字,D 语法顺序错误。 7.设有定义:struct Game{int num;}; struct Game g={12}; void change(struct Game *p){p->num=20;},主函数调用change(&g);,打印 g.num 结果为( ) A. 12 B. 20 C. 随机值 D. 0 【答案】B 【解析】传入结构体地址,函数通过指针修改原变量成员,外部g的 num 同步变为 20。 8.设有定义:struct Cat{int age;}; struct Cat c={3}; void test(struct Cat t){t.age=5;},主函数调用test(c);,打印 c.age 结果为( ) A. 3 B. 5 C. 0 D. 随机值 【答案】A 【解析】完整结构体值传递,t 是独立副本,修改副本 age 不会影响外部变量 c。 9.执行以下程序段的输出结果是( ) #include<stdio.h> struct Box{int w;}; void setW(int x){x=100;} int main(){ struct Box b={20}; setW(b.w); printf("%d",b.w); return 0; } A. 20 B. 100 C. 0 D. 随机数字 【答案】A 【解析】传递结构体成员 b.w 属于值传递,函数内修改的是副本 x,原始 b.w 数值不变。 10.执行以下程序段的输出结果是( ) #include<stdio.h> struct Box{int length;}; void modify(struct Box *p){ p->length = p->length * 2; } int main() { struct Box b = {18}; modify(&b); printf("%d", b.length); return 0;} A. 18 B. 36 C. 9 D. 20 【答案】B 【解析】传入结构体地址,指针直接操作原变量;原始 length=18,执行 18×2=36,外部变量同步修改。 二、填空题。 1.函数形参为结构体指针struct Student *p,访问成员 age 的标准写法为___________。 【答案】p->age 【解析】结构体指针专属->运算符访问成员。 2.执行以下程序段的输出结果是___________。 struct Box{int w;}; void func(struct Box *p){p->w = p->w * 3;} int main(){struct Box b={12}; func(&b); printf("%d",b.w);} 【答案】36 【解析】指针直接修改原变量,12×3=36。 3.想要在函数内修改主函数原始结构体的数据,应当采用________作为函数形参。 【答案】结构体指针 【解析】指针传递共享原变量内存,函数修改会同步作用外部数据。 4.执行以下程序段的输出结果是___________。 struct Cat{int age;}; void test(struct Cat t){t.age=10;} int main(){struct Cat c={4}; test(c); printf("%d",c.age);} 【答案】4 【解析】完整结构体值传递,形参 t 是副本,修改不影响外部变量 c。 5.执行以下程序段的输出结果是___________。 struct Num{int x;int y;}; void add(struct Num *p){p->x += 4; p->y -= 1;} int main(){struct Num n={9,7}; add(&n); printf("%d",n.x+n.y);} 【答案】19 【解析】x=9+4=13,y=7-1=6,13+6=19。 6.执行以下程序段的输出结果是___________。 struct Book{int page;}; void plus(int x){x = x + 15;} int main(){struct Book b={60}; plus(b.page); printf("%d",b.page);} 【答案】60 【解析】传递 page 成员属于值传递,函数内 x 修改不影响原变量。 7.执行以下程序段的输出结果是___________。 struct Fruit{int num;}; void change(struct Fruit *p){p->num /= 2;} int main(){struct Fruit f={46}; change(&f); printf("%d",f.num);} 【答案】23 【解析】指针修改原数据,46÷2=23。 8.执行以下程序段的输出结果是___________。 struct Toy{int a;int b;}; void sub(struct Toy t){t.a = t.a - 3; t.b = t.b + 2;} int main(){struct Toy toy={14,5}; sub(toy); printf("%d",toy.a-toy.b);} 【答案】9 【解析】值传递不修改原数据,14-5=9。 9.执行以下程序段的输出结果是___________。 struct Cube{int len;}; void mul(struct Cube *p){p->len = p->len * p->len;} int main(){struct Cube c={7}; mul(&c); printf("%d",c.len);} 【答案】49 【解析】指针修改原长度,7×7=49。 10.函数形参为结构体指针struct Goods *pr,使用带括号的等价写法读取成员 num,完整表达式为___________。 【答案】(*pr).num 【解析】(*指针).成员与指针->成员是结构体指针访问成员的两种等价标准写法。 三、阅读程序题。 1.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Data { int val; }; void calcSum(struct Data *arr, int len, int *res) { int i; *res = 0; for(i = 0; i < len; i++) { if(arr->val % 2 != 0) *res = *res + arr->val; arr++; } } int main() { struct Data num[5] = {{12},{27},{9},{34},{15}}; int total; calcSum(num, 5, &total); printf("%d", total); return 0; } 【答案】51 2.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Score { int s; }; int diffTotal(struct Score *p, int n) { int i, sum = 0; for(i = 0; i < n - 1; i++) { sum = sum + (p->s - (p+1)->s); p++; } return sum; } int main() { struct Score stu[4] = {{86},{72},{91},{65}}; printf("%d", diffTotal(stu, 4)); return 0; } 【答案】21 3.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Goods { int cost; }; int countMid(struct Goods *p) { int cnt = 0, i; for(i = 0; i < 4; i++) { if(p->cost >= 60 && p->cost <= 100) cnt++; p++; } return cnt; } int main() { struct Goods shop[4] = {{42},{75},{96},{110}}; printf("%d", countMid(shop)); return 0; } 【答案】2 4.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Fruit { int cnt; }; void addAll(struct Fruit *arr, int len, int add) { int i; for(i = 0; i < len; i++) { arr->cnt = arr->cnt + add; arr++; } } int main() { struct Fruit farm[4] = {{12},{18},{9},{24}}; int max, min, i; addAll(farm, 4, 6); max = farm[0].cnt; min = farm[0].cnt; for(i = 1; i < 4; i++) { if(farm[i].cnt > max) max = farm[i].cnt; if(farm[i].cnt < min) min = farm[i].cnt; } printf("%d", max - min); return 0; } 【答案】15 5.写出下列程序段的输出结果:________________。 #include <stdio.h> int gcd(int a, int b) { int r; while(b != 0) { r = a % b; a = b; b = r; } return a; } struct Brick { int x; int y; }; int main() { struct Brick blk = {48, 18}; printf("%d", gcd(blk.x, blk.y)); return 0; } 【答案】6 6.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Seed { int rate; }; int sliceCalc(struct Seed *p, int mid) { int sum1 = 0, sum2 = 0, i; for(i = 0; i < mid; i++) { sum1 += p->rate; p++; } for(i = mid; i < 5; i++) { sum2 += p->rate; p++; } return sum1 - sum2; } int main() { struct Seed arr[5] = {{11},{13},{7},{16},{9}}; printf("%d", sliceCalc(arr, 2)); return 0; } 【答案】-8 7.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Gear { int tooth; }; void changeOne(struct Gear *p) { p->tooth = p->tooth * 3; } int main() { struct Gear device[4] = {{12},{18},{24},{30}}; int i, squareSum = 0; changeOne(device + 1); for(i = 0; i < 4; i++) { squareSum += device[i].tooth * device[i].tooth; } printf("%d", squareSum); return 0; } 【答案】4536 8.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Mineral { int val; }; int primeSum(struct Mineral *p, int len) { int i,j,sum=0,flag; for(i=0;i<len;i++) { flag=1; for(j=2;j<p->val;j++) { if(p->val%j==0) { flag=0; break; } } if(flag) sum+=p->val; p++; } return sum; } int main() { struct Mineral ore[4]={{13},{22},{17},{9}}; printf("%d",primeSum(ore,4)); return 0; } 【答案】30 9.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Pet { int snack; }; void swapTwo(struct Pet *p1, struct Pet *p2) { struct Pet temp = *p1; *p1 = *p2; *p2 = temp; } int main() { struct Pet animal[4] = {{28},{17},{26},{14}}; int i, total = 0; swapTwo(animal, animal + 3); for(i = 0; i < 4; i++) total += animal[i].snack; printf("%d", total); return 0; } 【答案】85 10.写出下列程序段的输出结果:________________。 #include <stdio.h> struct Box { int gold; int diamond; }; int countGap(struct Box *arr, int limit) { int i, cnt = 0, gap; for(i = 0; i < 4; i++) { gap = arr->gold - arr->diamond; if(gap > limit) cnt++; arr++; } return cnt; } int main() { struct Box map[4] = {{78,29},{55,41},{88,22},{66,33}}; printf("%d", countGap(map, 30)); return 0; } 【答案】3 四、程序填空题(根据题意,将空缺的语句补上) 1.结构体与函数实现“大整数”乘法运算:定义一个 BigInt 结构体,包含整型数组 digits 和长度 len。下列三个函数:initBigInt(将整数初始化为大整数结构体)、multiplyBigInt(实现两个大整数结构体的乘法,结果存入第三个结构体)、printBigInt(打印大整数)。在 main 函数中调用这些函数计算 12345 × 67890 并输出结果。 #include <stdio.h> #include <string.h> struct BigInt { int digits[50]; int len; }; void initBigInt(struct BigInt *b, int num) { int i = 0; if (num == 0) { b->digits[0] = 0; b->len = 1; return; } while (num > 0) { b->digits[i++] = _______①______ num /= 10; } ________②________ } void multiplyBigInt(struct BigInt *a, struct BigInt *b, struct BigInt *res) { int i, j; memset(res, 0, sizeof(struct BigInt)); for (i = 0; i < a->len; i++) { for (j = 0; j < b->len; j++) { res->digits[i + j] +=___________③_________ res->digits[i + j + 1] += ___________④__________ res->digits[i + j] %= 10; } } res->len = a->len + b->len; while (res->len > 1 && __________⑤__________) res->len--; } void printBigInt(struct BigInt *b) { int i; for (i = ______⑥______; i >= 0; i--) printf("%d", b->digits[i]); printf(" "); } int main() { struct BigInt a, b, res; initBigInt(&a, 12345); initBigInt(&b, 67890); multiplyBigInt(&a, &b, &res); printf("Result: "); printBigInt(&res); return 0; } 【答案】①num % 10; ②b->len = i; ③ a->digits[i] * b->digits[j]; ④res->digits[i + j] / 10; ⑤res->digits[res->len - 1] == 0 ⑥b->len - 1 2.“汉诺塔”移动步骤记录:定义一个 MoveStep 结构体,包含盘子编号 disk、起始柱 from 和目标柱 to。递归函数 hanoi,将移动步骤记录到一个 MoveStep 结构体数组中。在 main 函数中调用该函数解决3层汉诺塔问题,并遍历数组输出所有移动步骤。 #include <stdio.h> struct MoveStep { int disk; char from; char to; }; int stepCount = 0; void hanoi(struct MoveStep steps[], int n, char from, char to, char aux) { if (________①______) { steps[stepCount].disk = n; steps[stepCount].from = from; steps[stepCount].to = to; stepCount++; return; } hanoi(____________②__________); steps[stepCount].disk = n; steps[stepCount].from = from; steps[stepCount].to = to; stepCount++; hanoi(___________③_________); } int main() { struct MoveStep steps[20]; int i; hanoi(steps, 3, 'A', 'C', 'B'); for (i = 0; i < stepCount; i++) { printf("Move disk %d from %c to %c ", steps[i].disk, steps[i].from, steps[i].to); } return 0; } 【答案】①n == 1 ②steps, n - 1, from, aux, to ③steps, n - 1, aux, to, from 3.“多项式求导”:定义一个 Term 结构体(系数 coef,指数 exp)。编写函数 differentiate,接收一个多项式结构体数组及其项数,对每一项进行求导(系数乘以指数,指数减1),将结果存入新的结构体数组并返回有效项数。main 函数中测试对 3x^3 + 2x^2 + 5x 求导。 #include <stdio.h> struct Term { float coef; int exp; }; int differentiate(struct Term src[], int n, struct Term dest[]) { int i, count = 0; for (i = 0; i < n; i++) { if (src[i].exp > 0) { dest[count].coef = _________①__________ dest[count].exp = src[i].exp - 1; ________②________ } } return count; } int main() { struct Term poly[3] = {{3.0, 3}, {2.0, 2}, {5.0, 1}}; struct Term deriv[3]; int n, i; n = differentiate(poly, 3, deriv); printf("Derivative: "); for (i = 0; i < n; i++) { printf("%.0fx^%d ", deriv[i].coef, deriv[i].exp); if (________③______) printf("+ "); } printf(" "); return 0; } 【答案】①src[i].coef * src[i].exp; ②count++; ③i < n - 1 4.“学生成绩排序与筛选”:定义 Student 结构体(学号 id,姓名 name,分数 score)。函数 sortByScore(冒泡排序,按分数降序)和 filterHighScore(筛选分数大于等于80的学生,返回人数)。main 函数初始化5个学生,调用函数并输出排序后的高分学生名单。 #include <stdio.h> #include <string.h> struct Student { int id; char name[20]; float score; }; void sortByScore(struct Student arr[], int n) { int i, j; struct Student temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (_____________①__________) { temp = arr[j]; arr[j] = arr[j + 1]; __________②_________ } } } } int filterHighScore(struct Student src[], int n, struct Student dest[]) { int i, count = 0; for (i = 0; i < n; i++) { if (src[i].score >= 80.0) { ____________③___________ } } return count; } int main() { struct Student class1[5] = {{101,"Alice",85.5}, {102,"Bob",72.0}, {103,"Charlie",90.0},{104,"David",68.5}, {105,"Eve",88.0}}; struct Student high[5]; int i, count; sortByScore(class1, 5); count = filterHighScore(class1, 5, high); printf("High Score Students: "); for (i = 0; i < count; i++) { printf("%d %s %.1f ", high[i].id, high[i].name, high[i].score); } return 0; } 【答案】①arr[j].score < arr[j + 1].score ②arr[j + 1] = temp; ③dest[count++] = src[i]; 五、编程题。 1. “日期差值计算”:定义 Date 结构体(年 year,月 month,日 day)。编写函数 isLeap(判断闰年)、daysInMonth(获取某月天数)和 dateDiff(计算两个日期之间相差的天数)。main 函数中计算 2023年1月1日 到 2023年3月15日 的天数差。 【参考代码】 #include <stdio.h> struct Date { int year, month, day; }; int isLeap(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int daysInMonth(int year, int month) { int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeap(year)) return 29; return days[month - 1]; } int dateDiff(struct Date d1, struct Date d2) { int days = 0; while (d1.year < d2.year || d1.month < d2.month || d1.day < d2.day) { d1.day++; days++; if (d1.day > daysInMonth(d1.year, d1.month)) { d1.day = 1; d1.month++; if (d1.month > 12) { d1.month = 1; d1.year++; } } } return days; } int main() { struct Date d1 = {2023, 1, 1}; struct Date d2 = {2023, 3, 15}; printf("Days difference: %d ", dateDiff(d1, d2)); return 0; } 2.“矩阵乘法”:定义 Matrix 结构体,包含二维数组 data、行数 rows、列数 cols。编写函数 multiplyMatrix,接收两个矩阵结构体指针,计算乘积并存入第三个矩阵结构体。main 函数中计算 2x3 矩阵与 3x2 矩阵的乘积。 【参考代码】 #include <stdio.h> #include <string.h> struct Matrix { int data[10][10]; int rows, cols; }; void multiplyMatrix(struct Matrix *a, struct Matrix *b, struct Matrix *res) { int i, j, k; res->rows = a->rows; res->cols = b->cols; memset(res->data, 0, sizeof(res->data)); for (i = 0; i < a->rows; i++) { for (j = 0; j < b->cols; j++) { for (k = 0; k < a->cols; k++) { res->data[i][j] += a->data[i][k] * b->data[k][j]; } } } } int main() { struct Matrix a = {{{1,2,3},{4,5,6}}, 2, 3}; struct Matrix b = {{{7,8},{9,10},{11,12}}, 3, 2}; struct Matrix res; int i, j; multiplyMatrix(&a, &b, &res); printf("Result Matrix: "); for (i = 0; i < res.rows; i++) { for (j = 0; j < res.cols; j++) { printf("%d ", res.data[i][j]); } printf(" "); } return 0; } 3.“字符串词频统计”:定义 WordFreq 结构体(单词 word,频率 count)。编写函数 addWord,遍历字符串提取单词,在结构体数组中查找并更新频率。编写 sortByFreq 按频率降序排序。main 函数统计 "hello world hello c world c c" 的词频并输出。 【参考代码】 #include <stdio.h> #include <string.h> struct WordFreq { char word[50]; int count; }; void addWord(struct WordFreq arr[], int *n, char word[]) { int i; for (i = 0; i < *n; i++) { if (strcmp(arr[i].word, word) == 0) { arr[i].count++; return; } } strcpy(arr[*n].word, word); arr[*n].count = 1; (*n)++; } void sortByFreq(struct WordFreq arr[], int n) { int i, j; struct WordFreq temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (arr[j].count < arr[j + 1].count) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { char text[] = "hello world hello c world c c"; struct WordFreq freq[20]; int n = 0, i = 0, start = 0; while (text[i] != '\0') { if (text[i] == ' ') { text[i] = '\0'; addWord(freq, &n, text + start); start = i + 1; } i++; } addWord(freq, &n, text + start); sortByFreq(freq, n); printf("Word Frequency: "); for (i = 0; i < n; i++) { printf("%s: %d ", freq[i].word, freq[i].count); } return 0; } 4.“质数筛选与存储”:定义一个 PrimeSet 结构体,包含整型数组 primes 和记录质数个数的 count。编写函数 sieveOfEratosthenes,使用经典的“埃拉托斯特尼筛法”高效找出100以内的所有质数,并将结果存入结构体数组中。在 main 函数中调用该函数,并遍历输出所有筛选出的质数。 【参考代码】 #include <stdio.h> #include <string.h> #define MAX_NUM 100 struct PrimeSet { int primes[MAX_NUM]; int count; }; void sieveOfEratosthenes(struct PrimeSet *ps) { int is_prime[MAX_NUM + 1]; int i, j; memset(is_prime, 1, sizeof(is_prime)); is_prime[0] = is_prime[1] = 0; for (i = 2; i * i <= MAX_NUM; i++) { if (is_prime[i]) { for (j = i * i; j <= MAX_NUM; j += i) { is_prime[j] = 0; } } } ps->count = 0; for (i = 2; i <= MAX_NUM; i++) { if (is_prime[i]) { ps->primes[ps->count++] = i; } } } int main() { struct PrimeSet ps; int i; sieveOfEratosthenes(&ps); printf("Primes within 100: "); for (i = 0; i < ps.count; i++) { printf("%d ", ps.primes[i]); } printf(" Total count: %d ", ps.count); return 0; } 5.“贪心算法解决找零问题”:定义一个 Change 结构体,包含面额 denomination 和所需张数 count。编写函数 makeChange,接收一个 Change 结构体数组(预设好人民币面额:100, 50, 20, 10, 5, 1),使用贪心算法计算找零给定金额所需的最少纸币张数,并将结果填入结构体数组中。main 函数测试找零 278 元。 【参考代码】 #include <stdio.h> struct Change { int denomination; int count; }; void makeChange(struct Change coins[], int n, int amount) { int i; for (i = 0; i < n; i++) { coins[i].count = amount / coins[i].denomination; amount %= coins[i].denomination; } } int main() { struct Change rmb[6] = {{100,0}, {50,0}, {20,0}, {10,0}, {5,0}, {1,0}}; int amount = 278; int i; makeChange(rmb, 6, amount); printf("Change for %d yuan: ", amount); for (i = 0; i < 6; i++) { if (rmb[i].count > 0) { printf("%d yuan: %d notes ", rmb[i].denomination, rmb[i].count); } } return 0; } 原创精品资源学科网独家享有版权,侵权必究! 学科网(北京)股份有限公司 $

资源预览图

第9章 第5节:结构体与函数(练习)-《C语言程序设计》(电子工业出版社)上好课
1
第9章 第5节:结构体与函数(练习)-《C语言程序设计》(电子工业出版社)上好课
2
第9章 第5节:结构体与函数(练习)-《C语言程序设计》(电子工业出版社)上好课
3
所属专辑
由于学科网是一个信息分享及获取的平台,不确保部分用户上传资料的 来源及知识产权归属。如您发现相关资料侵犯您的合法权益,请联系学科网,我们核实后将及时进行处理。