1
#include <stdio.h> double min(double x, double y); int main() { double a, b, c; printf("请输入2个浮点数: "); scanf_s("%lf%lf", &a, &b); c = min(a, b); printf("%g 和 %g 较小的为 %g\n", a, b, c); system("pause"); return 0; } double min(double x, double y) { return (x < y ? x : y); }
2
#include <stdio.h> //打印指定的字符j行i列 void chline(char ch, int i, int j); int main() { chline('*', 30, 10); system("pause"); return 0; } void chline(char ch, int i, int j) { int x, y; for (y = 0; y < j; y++) { for (x = 0; x < i; x++) putchar(ch); putchar('\n'); } }
3
#include <stdio.h> //打印指定的字符j行i列 void chline(char ch, int i, int j); int main() { char ch; int a, b; printf("请输入你要打印的字符: "); while ((ch = getchar()) != '\n') { printf("请输入你要打印的行数和列数: "); while (scanf_s("%d%d", &a, &b) == 2) { chline(ch, a, b); break; } break; } printf("完成\n"); system("pause"); return 0; } void chline(char ch, int i, int j) { int x, y; for (y = 0; y < i; y++) { for (x = 0; x < j; x++) putchar(ch); putchar('\n'); } }
4
#include <stdio.h> double h_mean(double i, double j); int main() { double i; i = h_mean(4, 5); printf("4 和 5 的调和平均数是 %g\n", i); system("pause"); return 0; } double h_mean(double i, double j) { double x, y; x = (1 / i + 1 / j) / 2; y = 1 / x; return y; }
5
#include <stdio.h> void larger_of(double *i, double *j); int main() { double a = 5.6, b = 10.12; printf("x = %g, y = %g\n", a, b); larger_of(&a, &b); printf("x = %g, y = %g\n", a, b); system("pause"); return 0; } void larger_of(double *i, double *j) { if (*i > *j) *j = *i; else *i = *j; }
6
#include <stdio.h> void number(double *a, double *b, double *c); int main() { double a, b, c; printf("请输入3组浮点数: "); while (scanf_s("%lf%lf%lf", &a, &b, &c) == 3) { printf("你输入的是 a = %g, b = %g, c = %g\n", a, b, c); number(&a, &b, &c); printf("从小到大排序: a = %g, b = %g, c = %g\n", a, b, c); printf("请输入3组浮点数[q退出]: "); } printf("再见\n"); system("pause"); return 0; } void number(double *a, double *b, double *c) { double x, y, z; if (*a > *b&&*a > *c) { z = *a; if (*b > *c) { y = *b; x = *c; } else { y = *c; x = *b; } } else if (*b > *a&&*b > *c) { z = *b; if (*a > *c) { y = *a; x = *c; } else { y = *c; x = *a; } } else { z = *c; if (*a > *b) { y = *a; x = *b; } else { y = *b; x = *a; } } *a = x; *b = y; *c = z; }
7
#include <stdio.h> #include <ctype.h> int ch_a(char ch); int main() { char ch; int num; printf("请输入内容: "); while ((ch = getchar()) != EOF) { num = ch_a(ch); if (num == -1) { putchar(ch); printf(" 不是字母 %d\n", num); } else { putchar(ch); printf(" 是字母, 该字母在字母表中的数组位置是 %d\n", num); } } printf("再见\n"); system("paues"); return 0; } int ch_a(char ch) { int i; if (isalpha(ch)) { if (isupper(ch)) { i = (int)(ch - 'A') + 1; } else i = (int)(ch - 'a') + 1; } else i = -1; return i; }
8
#include <stdio.h> double power(double n, int p); int main(void) { double x, xpow; int exp; printf("输入一个数字和该数字将被提升的整数次幕. 输入q退出: "); while (scanf_s("%lf%d", &x, &exp) == 2) { if (x == 0 && exp == 0) { printf("0 的 0 次幕未定义.\n"); } else { xpow = power(x, exp); printf("%g 的 %d 次幕是 %g\n", x, exp, xpow); } printf("输入下一对数字或q退出.\n"); } printf("希望你喜欢这次计算 - 再见!\n"); return 0; } double power(double n, int p) { double pow = 1; int i; if (p > 0) { for (i = 1; i <= p; i++) pow *= n; } else { for (i = p; i < 0; i++) pow *= n; pow = 1 / pow; } return pow; }
9
#include <stdio.h> //循环函数计算n的p次幕 double power(double n, int p); //递归函数计算n的p次幕 double power_a(double n, int p); int main(void) { double x, xpow; int exp; printf("输入一个数字和该数字将被提升的整数次幕. 输入q退出: "); while (scanf_s("%lf%d", &x, &exp) == 2) { if (x == 0 && exp == 0) { printf("0 的 0 次幕未定义.\n"); } else { xpow = power(x, exp); printf("循环: %g 的 %d 次幕是 %g\n", x, exp, xpow); xpow = power_a(x, exp); if (exp >= 0) printf("递归: %g 的 %d 次幕是 %g\n", x, exp, xpow); else printf("递归: %g 的 %d 次幕是 %g\n", x, exp, 1/xpow); } printf("输入下一对数字或q退出.\n"); } printf("希望你喜欢这次计算 - 再见!\n"); return 0; } double power(double n, int p) { double pow = 1; int i; if (p > 0) { for (i = 1; i <= p; i++) pow *= n; } else { for (i = p; i < 0; i++) pow *= n; pow = 1 / pow; } return pow; } double power_a(double n, int p) { double pow = 1; if (p > 0) pow = n * power_a(n, p - 1); else if (p < 0) pow = n * power_a(n, p + 1); return pow; }
10
#include <stdio.h> void to_base_n(int n, int m); int main() { int x; int y; bk:printf("请输入一个整数并输入要打印的格式: "); while (scanf_s("%lu%d", &x, &y) == 2) { if (y < 2 || y > 10) { printf("打印的格式请输入2-10进制之间: \n"); goto bk; } else { printf("%d 的 %d 进制为 ", x, y); to_base_n(x, y); putchar('\n'); } printf("请输入一个整数并输入要打印的格式: "); } return 0; } void to_base_n(int n, int m) { int r; r = n % m; if (n >= m) to_base_n(n / m, m); putchar('0' + r); }
11
#include <stdio.h> unsigned long Fibonacci(unsigned long n); int main() { unsigned long num; printf("请输入你要查询斐波那契数的第几列: "); while (scanf_s("%lu", &num) == 1) { if (num >= 2) { printf("1 1 "); Fibonacci(num); } else printf("请重新输入2以上的整数: \n"); printf("请输入你要查询斐波那契数的第几列[q退出]: "); } return 0; } unsigned long Fibonacci(unsigned long num) { int x = 2; unsigned long n, m, i, j; n = 1; m = 1; for (i = 3; i <= num; i++) { if (x % 10 == 0) putchar('\n'); x++; j = n + m; n = m; m = j; printf("%-7lu ", j); } putchar('\n'); }