ex9.1
// ex_9.1
#include
double min(double, double);
int main(void)
{
double num1, num2 = 0;
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
printf("The smaller of %lf and %lf is %lf", num1, num2, min(num1, num2));
return 0;
}
double min(double a, double b)
{
return (a < b? a: b);
}
ex9.2
// ex_9.2
#include
void chline(char, int, int);
int main(void)
{
chline('*',4,6);
return 0;
}
void chline(char ch, int i, int j)
{
int m,n;
for (m = 0; m < j; m++)
{
for (n = 0; n < i; n++)
{
printf("%c", ch);
}
printf("\n");
}
}
ex9.3 感觉和第2题是一样的,于是偷懒,复制了第二题的代码
// ex_9.3
#include
void chline(char, int, int);
int main(void)
{
chline('*',4,6);
return 0;
}
void chline(char ch, int i, int j)
{
int m,n;
for (m = 0; m < j; m++)
{
for (n = 0; n < i; n++)
{
printf("%c", ch);
}
printf("\n");
}
}
ex9.4
// ex_9.4
#include
double harmonic_mean(double, double);
int main(void)
{
double num1 = 0.0;
double num2 = 0.0;
double result = 0.0;
printf("Ener the first number: ");
scanf("%lf", &num1);
printf("Ener the second number: ");
scanf("%lf", &num2);
result = harmonic_mean(num1, num2);
printf("The harmonic mean of %lf and %lf is %lf\n", num1, num2, result);
return 0;
}
double harmonic_mean(double a, double b)
{
return (2 / (1 / a + 1 / b));
}
ex9.5
// ex_9.5
#include
void larger_of(double*, double*);
int main(void)
{
double num1 = 0;
double num2 = 0;
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
printf("The original two numbers are %lf and %lf\n", num1, num2);
larger_of(&num1, &num2);
printf("After larger_of, the two number are %lf and %lf\n", num1, num2);
return 0;
}
void larger_of(double* x, double* y)
{
if (*x > *y)
*y = *x;
else
*x = *y;
}
ex9.6
// ex_9.6
#include
void sort(double*, double*, double*);
int main(void)
{
double num1, num2, num3 = 0;
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
printf("Enter the third number: ");
scanf("%lf", &num3);
printf("Before sort, %lf %lf %lf\n", num1, num2, num3);
sort(&num1, &num2, &num3);
printf("After sort, %lf %lf %lf\n", num1, num2, num3);
return 0;
}
void sort(double* a, double* b, double* c)
{
double temp;
if (*b < *a)
{
temp = *a;
*a = *b;
*b = temp;
}
if (*c < *b)
{
temp = *b;
*b = *c;
*c = temp;
}
if (*b < *a)
{
temp = *a;
*a = *b;
*b = temp;
}
}
ex9.7
ex9.8
ex9.9
ex9.10
// ex_9.10
#include
int main(void)
{
unsigned long number;
int base;
printf("Enter an integer and base(q to quit):\n");
while (scanf("%lu %d", &number, &base) == 2)
{
printf("Binary equivalent: ");
to_base_n(number, base);
putchar('\n');
printf("Enter an integer and base(q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_base_n(unsigned long n, int b)
{
int r;
r = n % b;
if (n >= b)
to_base_n(n / b, b);
printf("%d",r);
return;
}
ex9.11
// ex_9.11
#include
unsigned long Fibonacci(unsigned n);
int main(void)
{
unsigned long n;
printf("Enter a number for n of Fibonacci(n): ");
scanf("%d", &n);
printf("Fibonacci(%lu) is %lu", n, Fibonacci(n));
return 0;
}
unsigned long Fibonacci(unsigned n)
{
int j = 0;
unsigned long fib1 = 1;
unsigned long fib2 = 1;
if ((n > 2) && (n % 2 == 0))
{
j = n / 2;
for (int i = 1; i < j; i++)
{
fib1 = fib1 + fib2;
fib2 = fib1 + fib2;
}
return fib2;
}
else if (n > 2)
{
j = (n + 1) / 2;
for (int i = 1; i < j; i++)
{
fib1 = fib1 + fib2;
fib2 = fib1 + fib2;
}
return fib1;
}
else
return 1;
}
读了SICP第一章后,用Iteration(迭代)的方式又重写了一遍Fibonacci函数,相比之前的版本,这个版本更简洁,运行效率也更高。难怪大家都推荐读这本书,果然是程序员修炼内功的葵花宝典。
// ex_9.11 利用迭代的方式求fib数列
#include
int Fibonacci(int);
int fib_iter(int, int ,int);
int main(void)
{
printf("%d", Fibonacci(10));
return 0;
}
int fib_iter(int a, int b,int count)
{
if (count == 0)
return a;
else
fib_iter(b, a+b, count-1);
}
int Fibonacci(int n)
{
fib_iter(0, 1, n);
}
本章完