1.设计一个函数min(x, y), 返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
double min(double a, double b);
int main(void)
{
double a, b;
printf("请输入一个double类型的值:");
scanf_s("%lf", &a);
printf("请再次输入一个double类型的值:");
scanf_s("%lf", &b);
printf("%f较小。", min(a, b));
return 0;
}
double min(double a, double b)
{
return a > b ? b : a;
}
2.设计一个函数chline ( ch,i, i),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
int chline(char ch, int i, int j);
int main(void)
{
char ch;
int i, j;
printf("请输入你想打印的字符:\n");
ch = getchar();
printf("请输入你想打印的行数:\n");
scanf_s("%d", &i);
printf("请输入你想打印的列数:\n");
scanf_s("%d", &j);
chline(ch, i, j);
return 0;
}
int chline(char ch,int i,int j)
{
for (int m = 1; m <= j; m++)
{
for (int n = 1; n <= i; n++)
printf("%c", ch);
printf("\n");
}
return 0;
}
3.编写一个函数,接受3个参数,一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。(同上)
4.两数的调和平均数这样计算: 先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
#include <stdio.h>
double H_mean(double, double);
int main(void)
{
double a, b;
printf("请输入一个double类型的数字:");
scanf_s("%lf", &a);
printf("请再输入一个double类型的数字:");
scanf_s("%lf", &b);
printf("两个数的调和平均数为:%f", H_mean(a, b));
return 0;
}
double H_mean(double a, double b)
{
double a1, b1,c;
a1 = 1 / a;
b1 = 1 / b;
c = 1 / ((a1 + b1) / 2);
return c;
}
5.编写并测试一个函数larger_of(),该函数把两个 double类型变量的值替换为较大的值。例如large_of(x, y)会把x和y中较大的值重新赋给两个变量。
#include <stdio.h>
double large_of(double, double);
int main(void)
{
double a, b;
printf("请输入一个double类型的数字:");
scanf_s("%lf", &a);
printf("请再输入一个double类型的数字:");
scanf_s("%lf", &b);
a = b = large_of(a, b);
printf("x = y = %f", a);
return 0;
}
double large_of(double x, double y)
{
return x > y ? x : y;
}
6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第一个变量,中间值放入第2个变量,最大值放入第3个变量。
#include <stdio.h>
double nine_6(double*, double*, double*);
int main(void)
{
double a, b, c;
printf("请输入第一个double类型的数字: ");
scanf_s("%lf", &a);
printf("请输入第二个double类型的数字: ");
scanf_s("%lf", &b);
printf("请输入第三个double类型的数字: ");
scanf_s("%lf", &c);
nine_6(&a, &b, &c);
return 0;
}
double nine_6(double* a, double* b, double* c)
{
double d;
d = *a;
*a = ((*a < *b) ? *a : *b);
*b = ((d > *b) ? d : *b);
d= *b;
*b = ((*b < *c) ? *b : *c);
*c = ((d> *c) ?d: *c);
d = *a;
*a = ((*a < *c) ? *a : *c);
*c = ((d > *c) ?d: *c);
printf("按变量地址大小顺序,变量排列为:%f %f %f", *a,*b,*c);
return 0;
}
7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
#include <stdio.h>
#include <ctype.h>
int nine_7(char);
int main(void)
{
char ch = 0;
printf("请输入需要读取的字符:");
//ch = getchar();
while (ch != '\n')
{
ch = getchar();
printf("这个字符的位置是:%d", nine_7(ch));
}
//printf("这个字符的位置是:%d",nine_7(getchar()));
return 0;
}
int nine_7(char ch)
{
if (islower(ch))
return ch - 'a' + 1;
else if (isupper(ch))
return ch - 'A' + 1;
else
return -1;
}
8.第6章的程序清单6.20中,power()函数返回一个 double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
#include<stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf_s("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3f to the power %d is %.5f\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
double pow1 = 0;
int i;
if (p >= 1)
{
for (i = 1; i <= p; i++)
pow *= n;
return pow;
}
if (p == 0)
{
if (n == 0)
{
printf("The power of 0 of 0 is undefined. Treat the value as 1.\n");
pow = 1;
}
else
pow = 1;
return pow;
}
else
{
//return (1 / power(n,-p));
for (i = 1; i <= -p; i++)
pow *= n;
pow1 = 1 / pow;
return pow1;
}
}
9.使用递归函数重写编程练习8。
#include<stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf_s("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3f to the power %d is %.5f\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
double pow1 = 0;
int i;
if (p >= 1)
{
for (i = 1; i <= p; i++)
pow *= n;
return pow;
}
if (p == 0)
{
if (n == 0)
{
printf("The power of 0 of 0 is undefined. Treat the value as 1.\n");
pow = 1;
}
else
pow = 1;
return pow;
}
else
{
return (1 / power(n,-p)); //递归
}
}
10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n函数接受两个参数,且第二个参数在2~10范围内,然后以第2个参数中指定的进制打印第1个参数的数值。例如,to_base_n(129, 8)显示的结果为201,也就是129的八进制数在一个完整的程序中测试该函数。
#include <stdio.h>
void to_base_n(unsigned long number, int base);//number是输入的数,base是进制
int main(void)
{
int base;
unsigned long number;
do
{
printf("Enter the integer(q to quit):\n");
if (scanf_s("%ld", &number) != 1)//unsigned long 是长整形
{
printf("请重新输入\n");
continue;
}
printf("Enter the base:\n");
if (!((scanf_s("%d", &base) == 1) && ((base >= 2) && (base) <= 10)))
{
printf("请重新输入\n");
continue;
}
printf("Binary equivalent:");
to_base_n(number, base);
putchar('\n');
putchar('\n');
} while (1);
printf("Done.\n");
return 0;
}
void to_base_n(unsigned long number, int base)
{
int r;
r = number % base;
if (number >= base)
to_base_n(number / base, base);
//putchar(r);
printf("%d", r);//2_r是整形数据,不能用putchar输出
return;
}
11.编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数。
#include<stdio.h>
unsigned long Fibonacci(unsigned long);
int main(void)
{
unsigned long n;
printf("请输入n:");
while (scanf_s("%lu", &n))
{
printf("斐波那契数第%lu项为:%lu\n", n,Fibonacci(n));
printf("请输入n:");
}
return 0;
}
unsigned long Fibonacci(unsigned long n)
{
unsigned long f;
unsigned long f1 = 1;
unsigned long f2 = 1;
if (n > 2)
{
for (int i = 3; i <= n; i++)
{
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
else
return 1;
}