好文档 - 专业文书写作范文服务资料分享网站

C primer plus课后编程练习答案

天下 分享 时间: 加入收藏 我要投稿 点赞

for (i=0; i

printf(\ if (p[i].assign == 1)

printf(\ else

printf(\ } }

void assign_seat(struct seat *p) {

int number, i;

printf(\ scanf(\ getchar();

for (i=0; i

if( p[i].assign == 1 )

printf(\ else {

printf(\ scanf(\

printf(\ scanf(\ getchar(); p[i].assign = 1;

printf(\ } return; }

printf(\ }

void delete_seat(struct seat *p) {

int number, i;

printf(\ scanf(\ getchar();

for (i=0; i

{

if (p[i].assign == 0)

printf(\ else {

p[i].assign = 0;

printf(\ } return; }

printf(\ }

void confirm_seat(struct seat *p) {

int number, i;

printf(\ scanf(\ getchar();

for (i=0; i

if (p[i].assign == 0)

printf(\ else

printf(\seat is assigned! its holder is %s %s\\n\number, p[i].firstname, p[i].lastname);

return; }

printf(\ }

void write_file(struct flight *p, char* filename) {

FILE * pseats;

if ((pseats = fopen(filename, \ {

printf(\写一个程序,用指向函数的指针数组执行菜单。例如,在菜单中选择a会激活由数组第一个元素指向的函数。

#include <>

#define F 4

float add(float x, float y); float subtract(float x, float y); float multiply(float x, float y); float divide(float x, float y);

int main(void) {

char command[5]; float x, y;

float (*p[F])(float x, float y) = {add, subtract, multiply, divide}; while(1) {

printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ gets(command);

if ( command[0] < '1' || command[0] > '4' ) break; printf(\ scanf(\ getchar();

printf(\ }

return 0; }

float add(float x, float y) {

return x + y ; }

float subtract(float x, float y) {

return x - y ; }

float multiply(float x, float y) {

return x * y ;

}

float divide(float x, float y) {

return x / y ; }

11.编写—个transform()函数,它接受4个参数:包含double类型数据的源数组名,double类型的目标数组名,表示数组元素个数的int变量以及一个函数名(或者,等价的指向函数的指针)。transform()函数把指定的函数作用于源数组的每个元素,并将返回值放到目标数组中。例如:

transform(source, target, 100, sin);

这个函数调用把sin(source[0])赋给target[O),等等,共有100个元素。在一个程序中测试该函数,调用4次transform(),分别使用函数库中的两个函数以及自己设计的两个适合的函数作为参数。

#include <> #include <>

#define M 10

void transform( double [], double [], int , double (*)(double) ); double reciprocal(double x); double negative(double x);

int main(void) {

double source[M], target[M]; int i;

for(i=0; i

printf(\

transform( source, target, M, sin); for(i=0; i

printf(\

printf(\

transform( source, target, M, exp); for(i=0; i

printf(\

printf(\

transform( source, target, M, reciprocal); for(i=0; i

printf(\

printf(\

transform( source, target, M, negative); for(i=0; i

printf(\

printf(\

return 0; }

void transform(double source[], double target[], int n, double (*p)(double)) { int i;

for(i=0; i

target[i] = (*p)(source[i]); }

double reciprocal(double x) {

return 1 / x ; }

double negative(double x) {

return - x ; }

3ty247x5vm0a6ri16ozy38gut0xsx2013u8
领取福利

微信扫码领取福利

微信扫码分享