自己学习过程中编写的,仅供参考,如有错误,欢迎评论区指正!
01
#include <stdio.h>
double n_min(double x, double y);
int main(void)
{
double n1 = 5.0, n2 = 10.0;
double min;
min = n_min(n1, n2);
printf("%g\n", min);
return 0;
}
double n_min(double x, double y)
{
return (x < y ? x : y);
}
02
#include <stdio.h>
void chline(ch, i, j);
int main(void)
{
int h, l;
char ch;
printf("请输入要打印的字符:");
scanf("%c", &ch);
printf("请输入要打印的行数和列数:");
scanf("%d %d", &h, &l);
chline(ch, l, h);
return 0;
}
void chline(ch, i, j)
{
for (int r = 0; r < j; r++)
{
for (int c = 0; c < i; c++)
{
putchar(ch);
}
putchar('\n');
}
}
03
#include <stdio.h>
double ave(double n1, double n2);
int main(void)
{
double x, y, av;
printf("请输入两个数:");
scanf("%lf %lf", &x, &y);
av = ave(x, y);
printf("%lf和%lf的调和平均数为:%lf\n", x, y, av);
return 0;
}
double ave(double n1, double n2)
{
double average;
average = 1.0 / ((1.0 / n1 + 1.0 / n2) / 2);
return average;
}
05
#include <stdio.h>
void large_of(double*, double*);
void large_of(double* d1, double* d2)
{
double max;
max = (*d1 > *d2) ? *d1 : *d2;
*d1 = max;
*d2 = max;
}
int main(void)
{
double n1, n2;
printf("请输入两个数:");
scanf("%lf %lf", &n1, &n2);
printf("%lf %lf\n", n1, n2);
large_of(&n1, &n2);
printf("%lf %lf", n1, n2);
return 0;
}
06
#include <stdio.h>
void test(double*, double*, double*);
int main(void)
{
double x, y, z;
printf("请输入三个数:");
while (scanf("%lf %lf %lf", &x, &y, &z) == 3)
{
test(&x, &y, &z);
printf("最小值为:%lf\n", x);
printf("中间值为:%lf\n", y);
printf("最大值为:%lf\n", z);
printf("请再输入三个数(q退出):");
}
printf("完成!\n");
return 0;
}
void test(double* n1, double* n2, double* n3)
{
double temp;
if (*n1 > *n2)
{
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
if (*n1 > *n3)
{
temp = *n1;
*n1 = *n3;
*n3 = temp;
}
if (*n2 > *n3)
{
temp = *n2;
*n2 = *n3;
*n3 = temp;
}
}
07
#include <stdio.h>
#include <ctype.h>
int zimu(char);
int main(void)
{
char ch;
int p;
printf("请输入一些字符:");
while ((ch = getchar()) != EOF)
{
if ((p = zimu(ch)) > 0)
{
printf("字母%c的位置为%d\n", ch, p);
}
else
{
printf("%c不是字母.\n");
}
while (getchar() != '\n')
{
continue;
}
printf("请继续输入一些字符(EOF退出):");
}
printf("完成!\n");
return 0;
}
int zimu(char c)
{
int sta;
if (isalpha(c))
{
if (islower(c))
{
return c - 'a' + 1;
}
if (isupper(c))
{
return c - 'A' + 1;
}
}
else
return -1;
}
08
#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("%lf %d", &x, &exp) == 2)
{
xpow = power(x, exp);
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)
{
int i;
double pow = 1.0;
if ((0 == p) && (0 == n))
{
printf("0 to the 0 undefined, using 1 as the value.\n");
return pow;
}
if (0 == n)
{
pow = 0.0;
return pow;
}
if (0 == p)
{
return pow;
}
if (p > 0)
{
for (i = 1; i <= p; i++)
{
pow *= n;
}
return pow;
}
else
{
for (i = 1; i <= -p; i++)
{
pow *= 1 / n;
}
return pow;
}
}
09
#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("%lf %d", &x, &exp) == 2)
{
xpow = power(x, exp);
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)
{
double pow = 1.0;
if ((0 == p) && (0 == n))
{
printf("0 to the 0 undefined, using 1 as the value.\n");
return pow;
}
if (0 == n)
{
pow = 0.0;
return pow;
}
if (0 == p)
{
return pow;
}
if (p > 0)
{
return n * power(n, p - 1);
}
else
{
return power(n, p + 1) / n;
}
}
10
#include <stdio.h>
void to_binary(int x, int n);
int main(void)
{
int number, b;
printf("Enter tow integers (q to quit):\n");
while (scanf("%d %d", &number, &b) == 2)
{
printf("%d equals in: ", number);
to_binary(number, b);
putchar('\n');
printf("Enter tow integers (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_binary(int x, int n)
{
int r;
r = x % n;
if (x >= n)
{
to_binary(x / n, n);
}
printf("%d", r);
}
11
#include <stdio.h>
void Fibonacci(int l);
int main(void)
{
int len;
printf("请输入数列长度(q退出):\n");
while (scanf("%d", &len) == 1)
{
printf("前%d项的斐波拉契数列为:\n", len);
Fibonacci(len);
printf("请再次输入数列长度(q退出):\n");
}
printf("完成\n");
return 0;
}
void Fibonacci(int l)
{
unsigned long x, y, t;
x = y = 1;
for (int i = 0; i < l; i++)
{
printf("%lu ", x);
t = x + y;
x = y;
y = t;
}
putchar('\n');
}