Menu
CoddyTech

回顾:选择一种策略

Coddy 面向 Java 开发者的 C 课程的第 42 课,共 48 课。

challenge icon

挑战

中等

输入包括一个整数模式和值。模式为 1 时选择翻倍,其他模式则选择加 5。实现 evaluate(value,callback),它必须调用传入的兼容回调函数。main 会选择函数指针并打印结果。

编辑未锁定的源文件和头文件。确保声明的公共 API 与可见的锁定文件 main.c 兼容,因为该文件会直接调用它。不要添加另一个 main。所有数值测试数据都足够小,不会导致溢出,除非任务明确检查某个范围。只打印指定的输出;如果展示了驱动程序,它会负责解析输入并生成输出。

亲手试试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
static int twice(int n){return n*2;}
static int plus_five(int n){return n+5;}
int main(void){int mode,value;if(scanf("%d%d",&mode,&value)!=2)return 1;printf("%d\n",evaluate(value,mode==1?twice:plus_five));return 0;}

面向 Java 开发者的 C中的所有课

自己练习: C 在线编译器