您的位置:首页 > 编程语言

C Primer Plus (第五版) 第九章 函数 编程练习

2015-05-26 16:13 441 查看
第九章 函数
编程练习

设计函数 min(x,y),返回两个double数值中较小的数值,同时用一个简单的驱动程序测试该函数。

#include <stdio.h>

double min(double x, double y);

int main(void)
{
double a, b;

printf("请输入两个数:\n");
scanf("%lf%lf", &a, &b);
printf("两个数中较小的数是:%lf\n", min(a, b));
return 0;
}

double min(double x, double y)
{
return x < y ? x : y;
}


2.设计函数chline(ch, i, j),实现指定字符在i列到j列的输出,并用一个简单的驱动程序测试该函数。
#include <stdio.h>

void chline(char, int, int);

int main(void)
{
char ch;
int x, y;

printf("请输入要显示的字符:\n");
scanf("%c", &ch);
printf("请输入字符开始输出和结束输出的列数\n");
scanf("%d%d", &x, &y);
chline(ch, x, y);

return 0;
}

void chline(char ch, int i, int j)
{
int k = 0;
while (k++ < i)
putchar('\n');
while (i++ <= j)
printf("%c\n", ch);
}


3.编写一个函数。函数的3个参数是一个字符和两个整数。字符参数是需要输出的字符。第一个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。
#include <stdio.h>

void chline(char, int, int);

int main(void)
{
char ch;
int x, y;

printf("请输入要显示的字符:");
scanf("%c", &ch);
printf("请输入每行字符输出的个数:");
scanf("%d", &x);
printf("请输入字符输出的行数:");
scanf("%d", &y);
chline(ch, x, y);

return 0;
}

void chline(char ch, int x, int y)
{
int i, j;
for (i = 0; i < y; i++)
{
for (j = 0; j < x; j++)
putchar(ch);
putchar('\n');
}
}


4.两数值的谐均值可以这样计算:首先对两数值的倒数取平均值,最后再取倒数。编写一个带有两个double参数的函数,计算这两个参数的谐均值。
#include <stdio.h>

double har(double, double);

int main(void)
{
double x, y;

printf("请输入要求谐均值的两个数:");
scanf("%lf%lf", &x, &y);

printf("%.4lf 和 %.4lf 的谐均值为:%.4lf\n", x, y, har(x, y));

return 0;
}

double har(double x, double y)
{
return 1.0 / ((1.0 / x + 1.0 / y) / 2);
}


5.编写并测试函数larger_of(),其功能是将两个double类型变量的数值替换成它们中的较大值。
例如,larger_of(x,y)会把x和y中的较大数值重新赋给变量x和y
#include <stdio.h>

void larger_of(double *, double *);

int main(void)
{
double x, y;

printf("请输入要求比较的两个数:");
scanf("%lf%lf", &x, &y);

larger_of(&x, &y);

printf("x = %.4lf\n", x);
printf("y = %.4lf\n", y);

return 0;
}

void larger_of(double *x, double *y)
{
//*x > *y ? *x = *y : *y = *x;
//error:表达式必须是可修改的左值。
//因为运算符的优先级问题,编译器看做是(*x > *y ? *x = *y : *y) = *x;
*x > *y ? *y = *x : (*x = *y);
}


6.编写一个程序,使其从标准输入读取字符,直到遇到文件结尾。对于每个字符,程序需要检查并报告该字符是否是一个字母。如果是的话,程序还应报告该字母在字母表中的数值位置。例如,c和C的字母位置都是3。可以先实现这样一个函数:接受一个字符参数,如果该字符为字母则返回该字母的数值位置,否则返回-1。
#include <stdio.h>
#include <ctype.h>
int fun(char);

int main(void)
{
char ch;
int a;

while ((ch = getchar()) != EOF)
{
a = fun(ch);
if (-1 == a)
printf("%c不是一个字母:\n", ch);
else
printf("%c字母排在字母表中第%d位。\n", ch, a);
}

return 0;
}
int fun(ch)
{
int a;
if (isalpha(ch))
{
ch = toupper(ch);
a = ch - 64;
}
else
return -1;
}


7.在第6章“C控制语句:循环”的程序清单6.20中,函数power()的功能是返回一个double类型数的某个正整数次幂。现在改进该函数,使其能正确地计算负幂。同时,用该函数实现0的任何次幂为0,并且任何数值的0次幂为l。使用循环的方法编写该函数并在一个程序中测试它。
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;

printf("输入一个数字和它的整数次幂(输入q退出):");
while (scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g的%d次幂为:%.5g\n", x, exp, xpow);
printf("再次输入一个数字和它的整数次幂(输入q退出):");
}
printf("Bye!");

return 0;
}

double power(double n, int p)
{
int i;
double pow = 1;
if (0 == n)
return 0;
if (0 == p)
return 1;
else
if (0 < p)
{
for (i = 0; i < p; i++)
pow *= n;
return pow;
}
else
{
for (i = 0; i < -p; i++)
pow *= n;
pow = 1.0 / pow;
return pow;
}
}


8.使用递归函数重做练习7。
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;

printf("输入一个数字和它的整数次幂(输入q退出):");
while (scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g的%d次幂为:%.5g\n", x, exp, xpow);
printf("再次输入一个数字和它的整数次幂(输入q退出):");
}
printf("Bye!");

return 0;
}

double power(double n, int p)
{
int i;
double pow = 1;
if (0 == n)
pow = 0;
if (0 == p)
pow = 1;
else
if (0 < p)
for (i = 0; i < p; i++)
pow *= n;
else
pow = 1.0 / power(n, -p);

return pow;
}


9.为了使程序清单 9.8 中的函数to_binary()更一般化,可以在新的函数to_base_n()中使用第二个参数,且该参数的范围从2到10。然后,这个新函数输出第一个参数在第二个参数规定的进制数下的数值结果。例如,to_base_n (129,8)的输出是201,也就是129的八进制数值。最后在一个完整的程序中对该函数进行测试。
#include <stdio.h>
void to_binary(unsigned long n, int a);
int main(void)
{
unsigned long number;
int a;
printf("请输入一个整数和一个2-10的进制(输入q退出): \n");

while (scanf("%lu%d", &number, &a) == 2)
{
printf("%lu的%d进制数为: ", number, a);
to_binary(number, a);
putchar('\n');
printf("请再次输入一个整数和一个2-10的进制(输入q退出): \n");
}
printf("结束。\n");

return 0;
}
void to_binary(unsigned long n, int a)
{
int x = n % a;
if (n >= a)
to_binary(n/a, a);
printf("%d", x);
}


10.编写并测试一个函数Fibonacci(),在该函数中使用循环代替递归完成斐波纳契数列的计算。
#include <stdio.h>

void fibonacci(int n);

int main(void)
{
int a = 30;

fibonacci(a);

return 0;
}
void fibonacci(int n)
{
int i, a, b, c;

a = 0;
b = 0;
c = 1;
for (i = 0; i < n; i++)
{
if (i < 2)
printf("%d\n", i);
else
{
a = b + c;
b = c;
c = a;
printf("%d\n", a);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息