1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
double min(double x, double y);
int main() {
double a;
a = min(2.3, 7.0);
printf("%lf\n", a);
return 0;
}
double min(double x, double y)
{
return x < y ? x : y;
}
2.设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
void chline(char ch, int i, int j);
int main() {
chline('Z', 4, 6);
printf("\n");
return 0;
}
void chline(char ch, int i, int j)
{
for (int n = 0; n < j; n++)
{
for (int m = 0; m < i; m++)
{
putchar(ch);
}
putchar('\n');
}
}
3.编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。
#include <stdio.h>
// 函数原型声明
void printCharRows(char ch, int times, int rows);
int main() {
char ch;
int times, rows;
// 获取用户输入
printf("请输入要打印的字符:");
scanf("%c", &ch);
printf("请输入每行中字符打印的次数:");
scanf("%d", ×);
printf("请输入要打印的行数:");
scanf("%d", &rows);
// 调用函数打印字符
printCharRows(ch, times, rows);
return 0;
}
// 函数定义
void printCharRows(char ch, int times, int rows) {
for (int i = 0; i < rows; i++) { // 外层循环控制行数
for (int j = 0; j < times; j++) { // 内层循环控制每行中字符打印的次数
putchar(ch);
}
putchar('\n'); // 每行结束后换行
}
}
4.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
#include <stdio.h>
double tavg(double a, double b)
{
return 1 / ((1 / a + 1 / b)/2);
}
int main() {
double res = tavg(3.12, 6.2);
printf("测试结果为:%lf\n", res);
return 0;
}
5.编写并测试一个函数larger_of(),该函数把两个double类型变量的值替换为较大的值。例如,larger_of(x, y)会把x和y中较大的值重新赋给两个变量。
#include <stdio.h>
void larger_of(double* a, double* b)
{
if (*a > *b) {
*b = *a;
}
else {
*a = *b;
}
}
int main() {
double x = 3.14;
double* px = &x;
double y = 23.1;
double* py = &y;
printf("原始值: x = %lf, y = %lf\n", x, y);
larger_of(px, py);
printf("修改后的值: x = %lf, y = %lf\n", x, y);
return 0;
}
6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个变量,中间值放入第2个变量,最大值放入第3个变量。
#include <stdio.h>
void minToMax(double* a, double* b, double* c) {
double temp;
// 确保 a 是最小的
if (*a > *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a > *c) {
temp = *a;
*a = *c;
*c = temp;
}
// 确保 b 是第二小的
if (*b > *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
int main() {
double x = 3.14;
double* px = &x;
double y = 23.1;
double* py = &y;
double z = 2.1;
double* pz = &z;
printf("原始值: x = %lf, y = %lf, z = %lf\n", x, y, z);
minToMax(px, py, pz);
printf("修改后的值: x = %lf, y = %lf, z = %lf\n", x, y, z);
return 0;
}
7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
#include <stdio.h>
int checkCh(char ch) {
if (ch >= 'A' && ch <= 'Z') {
printf("%c是大写字母!位置为%d\n", ch, ch - 64);
return 1; // 返回 1 表示是大写字母
}
else if (ch >= 'a' && ch <= 'z') {
printf("%c是小写字母!位置为%d\n", ch, ch - 96);
return 1; // 返回 1 表示是小写字母
}
else if (ch!='\n')
{
printf("%c不是字母!\n",ch);
return -1; // 返回 -1 表示不是字母
}
}
int main() {
char ch;
printf("请输入字符:");
while ((ch = getchar()) != EOF) {
checkCh(ch);
// 如果用户输入了换行符,我们不需要打印额外的消息,直接跳出
if (ch == '\n') {
break;
}
}
printf("输入结束。\n");
return 0;
}
8.第6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
#include <stdio.h>
double power(double n, int p);
int main()
{
double x, xpow;
int exp;
printf("Enter a number and the interger 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 %.6g\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;
int i;
if (p > 0)
{
for (i = 1; i <= p; i++)
{
pow *= n;
}
return pow;
}
else if (p < 0)
{
for (i = 1; i <=-p; i++)
{
pow = 1.0 / power(n, -p);
}
return pow;
}
else if (n == 0)
{
return 0;
}
else
{
printf("0 to the 0 undefined; using 1 as the value\n");
return 1;
}
}