我的博客主要记录一下自己的学习代码过程。
大家可以在自己电脑上运行看看。
编译器为VS2019
第9章的运行结果如下:
9.1
# include <stdio.h>
double min(double x, double y);
int main(void)
{
double test;
test = min(20.4, 34.2);
printf("2个数中最小的是%.2lf.\n", test);
test = min(8.3, 3.2);
printf("2个数中最小的是%.2lf.\n", test);
return 0;
}
double min(double x, double y)
{
double result;
return result = (x > y) ? y : x;
}
运行结果:
9.2
# include <stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
int x, y;
char ch;
puts("请输入你想要打印的字符,和想要打印的行数和列数.");
ch = getchar();
scanf_s("%d,%d", &x, &y);
getchar();//去掉空格
chline(ch, x, y);
return 0;
}
void chline(char ch, int i, int j)
{
int n,k;
for (n = 0;n < i;n++)
{
for (k = 0;k < j;k++)
printf("%c", ch);
printf("\n");
}
}
运行结果:
9.3
这个题和上一个题感觉是一样的。就不放出代码了。
9.4
# include <stdio.h>
double average (double x,double y);
int main(void)
{
double result, x, y;
puts("请输入2个数字.");
scanf_s("%lf,%lf", &x, &y);
result = average(x, y);
printf("它们的调和平均数为%.2lf.\n", result);
return 0;
}
double average(double x, double y)
{
double x1, y1, average;
x1 = 1 / x;
y1 = 1 / y;
return average = 1/((x1 + y1) / 2);
}
运行结果:
9.5
#include <stdio.h>
void larger_of(double* p1, double* p2);
int main(void)
{
double x, y;
puts("请输入2个数字.");
scanf_s("%lf %lf", &x, &y);
larger_of(&x, &y);
printf("它们现在的数值为x:%.2lf y:%.2lf\n", x, y);
return 0;
}
void larger_of(double* p1, double* p2)
{
*p1 = *p2 = *p1 > * p2 ? *p1 : *p2;
}
运行结果:
9.6
直接在9.5的基础上改一下,就好了,简单的冒泡
#include <stdio.h>
void test(double* p1, double* p2, double* p3);
int main(void)
{
double x, y, z;
puts("请输入3个数字.");
scanf_s("%lf %lf %lf", &x, &y, &z);
test(&x, &y, &z);
printf("它们现在的数值为x:%.2lf y:%.2lf z:%.2lf\n", x, y, z);
return 0;
}
void test(double* p1, double* p2, double* p3)
{
double n;
if (*p1 > * p2)
{
n = *p1;
*p1 = *p2;
*p2 = n;
}
if (*p2 > * p3)
{
n = *p2;
*p2 = *p3;
*p3 = n;
}
if (*p1 > * p2)
{
n = *p1;
*p1 = *p2;
*p2 = n;
}
}
运行结果:
9.7
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int test(char ch);
int main(void)
{
int x, y;
x = y = 0;
printf("请输入字符串:\n");
puts("如果返回数值的话就是字母,返回-1的话就不是字母");
while ((x = getchar()) != EOF)//用int读取字符可以检查EOF
{
y = test((char)x);
printf("%3d", y);
}
getchar();
return 0;
}
int test(char ch)
{
int i,k,o;
if (i = islower(ch))
{
k = ch - 'a' + 1;
return k;
}
else if (i = isupper(ch))
{
k = ch - 'A' + 1;
return k;
}
else
{
return -1;
}
}
运行结果:
9.8
先导入,前面的主函数不用管,主要修改后面Power函数就行。
// power.c -- raises numbers to integer powers
#include <stdio.h>
double power(double n, int p); // ANSI prototype
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); // function call
printf("%.3g to the power %d is %.5g\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) // function definition
{
double pow = 1;
int i;
if (p == 0)
{
if (n == 0)
printf("0的0次幂未定义,把该值处理为1");
pow = 1;
}
else if (n == 0)
pow = 0;
else if (p > 0)
{
for (i = 0;i < p;i++)
pow *= n;
}
else
{
for (i = 0;i > p;i--)
pow *= 1 / n;
}
return pow;
}
运行结果:
9.9
// power.c -- raises numbers to integer powers
#include <stdio.h>
#include<math.h>
double power(double n, int p); // ANSI prototype
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); // function call
printf("%.3g to the power %d is %.5g\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) // function definition
{
double pow = 1;
int i, mark;
mark = 1;
if (p == 0)
{
if (n == 0)
printf("0的0次幂未定义,把该值处理为");
pow = 1;
}
else if (n == 0)
pow = 0;
else if (p > 0)
{
pow = n * power(n, p - 1);
}
else
{
pow = (1/n) * power(n, p + 1);
}
return pow;
}
运行结果:
9.10
仅仅改动了函数定义内的内容。
/* binary.c -- prints integer in binary form */
#include <stdio.h>
void to_binary(long int n, int k);
int main(void)
{
unsigned long number;
int x;
printf("输入一个数和要计算的进制(q to quit):\n");
while (scanf_s("%ld %d", &number, &x) == 2)
{
printf("你输入的数所对应的进制数为:");
to_binary(number, x);
printf("\n");
printf("输入一个数和要计算的进制 (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_binary(long int n, int k) /* recursive function */
{
int r;
r = n % k;
if (n >= k)
to_binary(n / k, k);
printf("%d", r);
return;
}
运算结果:
9.11
斐波那契数列(Fibonacci sequence),又称黄金分割数列,表达式为F[n]=F[n-1]+F[n-2] (n>=3,F[1]=1,F[2]=1)
#include <stdio.h>
int test(int x);
int main(void)
{
int i,k;
scanf_s("%d", &i);//输入的i就是斐波那契的第i项
for(;i>=0;i--)
printf("%3d", test(i));
return 0;
}
int test(int x)
{
if (x == 0)
return 0;
if (x == 1)
return 1;
if (x >= 2)
{
return test(x - 1) + test(x - 2);
}
}
运行结果: