/*
PE 9-1
设计一个函数min(x, y),返回两个double类型值的较小值,在一个简单的驱动程序中测试该函数
*/#include<stdio.h>doublemin(double a,double b);intmain(void){printf("The min number between 1 and 5 is %.2f \n",min(1.0,2.0));return0;}doublemin(double a,double b){return a <= b ? a : b;}
/*
PE 9-2
设计一个函数chline(ch, j, i),打印指定的字符j行i列(书又翻译错了)
原文that prints the requested character in columns i through j .
在一个简单的驱动程序中测试该函数。
*/#include<stdio.h>voidchline(char ch,int row,int col);intmain(void){chline('*',3,5);return0;}voidchline(char ch,int row,int col){for(int i =1; i <= row; i++){for(int j =1; j <= col; j++)putchar(ch);putchar('\n');}return;}
/*
PE 9-3
编写一个函数,接受三个参数,一个字符和两个整数。
字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,
第2个整数指定打印指定字符的行数。编写一个调用该程序的函数。
注意:第1个参数是列数,第2个参数是行数
*/#include<stdio.h>intmain(void){print_ch('#',3,4);return0;}voidprint_ch(char ch,int col,int row){for(int i =1; i <= row; i++){for(int j =1; j <= col; j++)putchar(ch);putchar('\n');}return;}
/*
EP 9-4
两数的调和平均数这样计算:先得到两数的倒数,然后计算这两个倒数的平均值,
最后取计算结果的倒数,
编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数
*/
#include <stdio.h>
double harmonic(double d1, double d2);
int main(void)
{
double x, y;
double result;
printf("Enter two numbers and i will return it's harmonic mean: \n");
scanf("%lf %lf", &x, &y);
result = harmonic(x, y);
if (result > 0)
printf("Harmonic mean is %.2f", result);
else
printf("The number have no hamonic mean. \n");
return 0;
}
double harmonic(double d1, double d2)
{
if (d1 != 0 && d2 != 0)
{
return 1.0 / ((1.0/d1 + 1.0/d2) / 2.0);
}
else
return -1;
}
/*
PE 9-5
编写并测试一个函数large_of(),该函数把两个double类型变量的值替换为较大的值。
例如large_of(x, y)会把x,y中较大的值重新赋给两个变量
*/#include<stdio.h>voidlarge_of(double*,double*);intmain(void){double x =2.0;double y =3.0;printf("Original x is %.2f, y is %.2f. \n", x, y);large_of(&x,&y);printf("After large_of x is %.2f, y is %.2f. \n", x, y);}voidlarge_of(double*pa,double*pb){*pb =*pa >=*pb ?*pa :*pb;*pa =*pb;return;}
/*
PE 9-6
编写并测试一个函数,该函数以3个double变量的地址作为参数,
把最小值放入第1个变量(这里书又翻译错了= =!),
中间值放入第2个变量,最大值放入第三个变量。
*/#include<stdio.h>intmain(void){double x =5.0;double y =3.0;double z =6.0;printf("Original value x %.2f, y %.2f, z %.2f. \n", x, y, z);change3d(&x,&y,&z);printf("After change3d value x %.2f, y %.2f, z %.2f. \n", x, y, z);return0;}voidchange3d(double* smal,double* mid,double* larg){double temp;if(*smal >=*mid){
temp =*smal;*smal =*mid;*mid = temp;}if(*smal >=*larg){
temp =*smal;*smal =*larg;*larg = temp;}if(*mid >=*larg){
temp =*mid;*mid =*larg;*larg = temp;}return;}
/*
PE 9-7
编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。
如果是,还要报告该字母在字母表中的位置。
例如,c和C在字母表中的位置都是3。
合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1
*/#include<stdio.h>#include<ctype.h>intmain(void){char ch;while((ch =getchar())!=EOF){int location =ch_loc(ch);printf("%c location %d \n", ch, location);}}intch_loc(ch){int back;if(isalpha(ch)){if(islower(ch))
back = ch -96;if(isupper(ch))
back = ch -64;}else
back =-1;return back;}
每次后面都会打印-1是因为键入的换行符
/*
PE 9-8
第6章的程序清单6.20中,power()函数返回一个double类型书的正整数次幂。
改进该函数,使其能正确计算负幂。
另外,函数要处理0的任何次幂都为0,
任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1).
要使用一个循环,并在程序中测试该函数。
*/