C Primer Plus 编程习题-第九章-函数
C Primer Plus 9.11
编程练习第一题
/* C Primer Plus 9.11 —— 编程练习第一题 */
/* 题目:设计一个函数min(x, y),返回两个double类型值的较小值。
在一个简单的驱动程序中测试该函数。 */
#include <stdio.h>
#include <stdlib.h>
void min(double * x, double * y);
int main(void)
{
double n1, n2;
printf("Please input two numbers: \n");
scanf("%f %f", &n1, &n2);
min(&n1, &n2);
system("pause");
return 0;
}
void min(double * x, double * y)
{
double temp, n1, n2;
n1 = *x;
n2 = *y;
if (n1 > n2)
printf("The smaller number is %f.\n", n2);
else
printf("The smaller number is %f.\n", n1);
}
编程练习第二题
/* C Primer Plus 9.11 —— 编程练习第二题 */
/* 题目:设计一个函数chline(ch, i, j),打印指定的字符j行i列,
在一个简单的驱动程序中测试该函数。 */
#include <stdio.h>
#include <stdlib.h>
void chline(char ch, int i, int j);
int main(void)
{
int row, column;
char ch;
printf("Enter a char a number of rows and columns: \n");
scanf("%c %d %d", &ch, &row, &column);
// if ((row >= 1 && row <= 5) && (column >= 1 && column <= 5))
chline(ch, row - 1, column - 1);
system("pause");
return 0;
}
void chline(char ch, int i, int j)
{
int row, column;
char alphabet[5][5] = {{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'}};
alphabet[i][j] = ch;
for (row = 0; row < 5; row++){
for (column = 0; column < 5;column++){
printf("%c ", alphabet[row][column]);
}
printf("\n");
}
}
编程练习第三题
/* C Primer Plus 9.11 —— 编程练习第三题 */
/* 题目:编写一个函数,接受3个参数:一个字符和两个整数。字符参数是
待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印
指定字符的行数。编写一个调用该函数的程序。 */
#include <stdio.h>
#include <stdlib.h>
void chline(char ch, int i, int j);
int main(void)
{
int row, num;
char ch;
printf("Enter a char a number of rows (<5) and columns (<5): \n");
scanf("%c %d %d", &ch, &num, &row);
// if ((row >= 1 && row <= 5) && (column >= 1 && column <= 5))
chline(ch, num, row);
system("pause");
return 0;
}
void chline(char ch, int n, int r)
{
int row, column;
char alphabet[5][5] = {{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'},
{'*', '*', '*', '*', '*'}};
for (column = 0; column < n; column++)
alphabet[r - 1][column] = ch;
for (row = 0; row < 5; row++)
{
for (column = 0; column < 5; column++)
printf("%c ", alphabet[row][column]);
printf("\n");
}
}
编程练习第四题
/* C Primer Plus 9.11 —— 编程练习第四题 */
/* 题目:两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数
的平均值,最后取计算结果的倒数,编写一个函数,接受两个double类型的参
数,返回这两个参数的调和平均数。 */
#include <stdio.h>
#include <stdlib.h>
double avr(double i, double j);
int main(void)
{
double n1, n2, res;
printf("Enter two number:\n");
scanf("%lf %lf", &n1, &n2);
res = avr(n1, n2);
printf("RESULT: %lf\n", res);
system("pause");
return 0;
}
double avr(double i, double j)
{
double res;
res = 1 / ((1 / i + 1 / j) / 2);
return res;
}
编程练习第五题
/* C Primer Plus 9.11 —— 编程练习第五题 */
/* 题目:编写并测试一个函数larger_of(),该函数把两个double类型变量的值
替换为较大的值。例如,larger_of(x, y)会把x和y中较大的值重新赋给两个变量。 */
#include <stdio.h>
void larger_of(double a, double b);
int main(void)
{
double n1, n2;
printf("Enter two number:\n");
scanf("%lf %lf", &n1, &n2);
larger_of(n1, n2);
getchar();
getchar();
return 0;
}
void larger_of(double a, double b)
{
if (a > b)
b = a;
else
a = b;
printf("RESULT: %lf %lf\n", a, b);
}
编程练习第六题
/* C Primer Plus 9.11 —— 编程练习第六题 */
/* 题目:编写并测试一个函数,该函数以3个double变量的地址作为参数,
把最小值放入第1个变量,中间值放入第2个变量,最大值放入第3个变量。 */
//本题学习了 Y_kia 的文章 https://blog.csdn.net/asdgyy/article/details/82915061
#include <stdio.h>
void insert_123(double * a, double * b, double * c);
int main(void)
{
double n1, n2, n3;
printf("Enter three number:\n");
scanf("%lf %lf %lf", &n1, &n2, &n3);
insert_123(&n1, &n2, &n3);
printf("RESULT: %lf < %lf < %lf\n", n1, n2, n3);
getchar();
getchar();
return 0;
}
void insert_123(double * a, double * b, double * c)
{
double temp;
*a < *b ? (*a < *c ? (*b < *c ? : (temp = *b, *b = *c, *c = temp)) : (temp = *b, *b = *a, *a = *c, *c = temp)) : (*b < *c ? : (*a < *c ? : (temp = *b, *b = *a, *a = *c, *c = temp)));
}
编程练习第七题
/* C Primer Plus 9.11 —— 编程练习第七题 */
/* 题目:编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告
每个字符是否是字母。如果是,还要报告字母在字母表中的数值位置。例如,c和C在
字母表的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母
则返回一个数值位置,否则返回-1。 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int char_pos(char c);
int main(void)
{
char ch;
int res;
FILE * fp;
char fname[50]; // 储存文件名
fp = fopen("C:\\Users\\SieYuan\\Desktop\\test9.11-7.txt", "r"); // 打开待读取文件
// getc(fp) 从打开的文件中获取一个字符
while ((ch = getc(fp)) != EOF){
res = char_pos(ch);
if (res > 0)
printf("%c 's index is %d.\n", ch, res);
}
fclose(fp); //关闭文件
system("pause");
return 0;
}
int char_pos(char c)
{
if (isupper(c))
return c - 'A' + 1;
else if (islower(c))
return c - 'a' + 1;
else
return -1;
}
编程练习第八题
/* C Primer Plus 9.11 —— 编程练习第八题 */
/* 题目:第6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。
改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都是0,任何次数的0
次幂都是1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并
在程序中测试该函数。 */
#include <stdio.h>
#include <stdlib.h>
double power(double base, int pow)
{
double res = 1;
int i, flag = 0;
if (pow == 0){
if(base == 0)
printf("0^0 is undefined\n");
return 1;
}
if (base == 0)
return 0;
if (pow < 0){
pow = -pow;
flag = 1;
}
for (i = 1; i <= pow; i++)
res *= base;
if (flag)
return 1 / res;
else
return res;
}
int main(void)
{
double num;
int pow;
printf("Enter the base number and power:\n");
while(scanf("%lf %d", &num, &pow) == 2)
printf("RESULT: %lf\n", power(num, pow));
system("pause");
return 0;
}
编程练习第九题
/* C Primer Plus 9.11 —— 编程练习第九题 */
/* 题目:使用递归函数重写编程练习8。 */
#include <stdio.h>
#include <stdlib.h>
double power(double base, int pow)
{
double res = 1;
int i, flag = 0;
if (pow == 0){
if(base == 0)
printf("0^0 is undefined\n");
return 1;
}
if (base == 0)
return 0;
if (pow > 0)
return base * power(base, pow - 1);
else
return 1 / (base * power(base, pow + 1));
}
int main(void)
{
double num;
int pow;
printf("Enter the base number and power:\n");
while(scanf("%lf %d", &num, &pow) == 2)
printf("RESULT: %lf\n", power(num, pow));
system("pause");
return 0;
}
编程练习第十题
/* C Primer Plus 9.11 —— 编程练习第十题 */
/* 题目:为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()函数
接受两个参数,且第二个参数在2~10范围内,然后以第2个参数中的指定的进制打印第1个
参数的数值。 */
#include <stdio.h>
#include <stdlib.h>
void to_base_n(unsigned long num, unsigned int op)
{
int res;
res = num % op;
if (num >= op)
to_base_n(num / op, op);
printf("%d", res);
}
int main(void)
{
unsigned long num;
unsigned int op;
scanf("%lu %hu", &num, &op);
to_base_n(num, op);
system("pause");
return 0;
}
编程练习第十一题
/* C Primer Plus 9.11 —— 编程练习第十一题 */
/* 题目:编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数。 */
#include <stdio.h>
#include <stdlib.h>
void Fibonacci(int n)
{
unsigned long long a = 1, b = 1, sum;
if (n == 1)
printf("1 ");
if (n == 2)
printf("1 ");
if (n >= 3){
for (int i = 3; i <= n; i++){
sum = a + b;
a = b;
b = sum;
}
printf("%llu ", sum);
}
return;
}
int main(void)
{
int n;
int i;
printf("Enter a number :\n");
scanf("%d", &n);
for (i = 1; i <= n; i++){
Fibonacci(i);
}
printf("\n");
system("pause");
return 0;
}