1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
double min(double,double);
int main(void ){
double a,b;
scanf("%lf %lf",&a,&b);
printf("%g",min(a,b));
return 0;
}
double min(double x,double y){
return x<y?x:y;
}
2.设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
char chline(char,int,int);
int main(void ){
char a;
int b,c;
scanf("%c %d %d",&a,&b,&c);
chline(a,b,c);
return 0;
}
char chline(char ch,int i,int j){
for (int k = 0; k < j; ++k) {
for (int l = 0; l < i; ++l) {
printf("%c",ch);
}
printf("\n");
}
}
3.编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。
#include <stdio.h>
char chline(char,int,int);
int main(void ){
char a;
int b,c;
scanf("%c %d %d",&a,&b,&c);
chline(a,b,c);
return 0;
}
char chline(char ch,int i,int j){
for (int k = 0; k < j; ++k) {
for (int l = 0; l < i; ++l) {
printf("%c",ch);
}
printf("\n");
}
}
4.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
#include <stdio.h>
double thsvgnum(double ,double );
int main(void ){
double a,b;
scanf("%lf %lf",&a,&b);
thsvgnum(a,b);
return 0;
}
double thsvgnum(double num1,double num2){
double num;
num = 1/((1/num1 + 1/num2)/2);
printf("%g",num);
}
5.编写并测试一个函数larger_of(),该函数把两个double类型变量的值替换为较大的值。例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变量。
#include <stdio.h>
double larger_of(double *x ,double *y);
int main(void){
double a,b;
scanf("%lf %lf",&a,&b);
larger_of(&a,&b);
printf("%g %g",a,b);
return 0;
}
double larger_of(double *x,double *y){
(*x>*y)?(*y=*x):(*x=*y);
return 0;
}
6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个函数,中间值放入第2个变量,最大值放入第3个变量。
#include <stdio.h>
void dec(double *x,double *y,double *z);
int main(void ){
double a,b,c;
scanf("%lf %lf %lf",&a,&b,&c);
dec(&a,&b,&c);
printf("%g %g %g",a,b,c);
}
void dec(double *x,double *y,double *z){
double temp;
if(*x>*z && *z>*y){
temp = *y;
*y=*z;
*z=temp;
}
else if(*y>*x && *x>*z){
temp = *x;
*x=*y;
*y=temp;
} else if(*y>*z && *z>*x){
temp=*x;
*x = *y;
*y = *z;
*z = temp;
} else if(*z>*y && *y>*x ){
temp=*x;
*x=*z;
*z=temp;
} else if(*z>*x && *x>*y){
temp=*x;
*x=*z;
*z=*y;
*y=temp;
} else{
printf("error");
}
}
7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
#include <stdio.h>
#include <ctype.h>
int check(char );
int main(void ){
char cc;
int isalpha2 = 1, tmp; //多这个步骤是为了解决输入的回车问题
while ((cc=getchar())!='\n'){
tmp = check(cc);
isalpha2 = (isalpha2 == 1 ? tmp : 0);
}
if(isalpha2 == 1){
printf("all charactor is letter. \n");
}
return 0;
}
int check(char ch){
if(isalpha(ch)){
if(isupper(ch)){
printf("%c %d\n",ch,ch-64);
} else{
printf("%c %d\n",ch,ch-96);
}
return 1;
} else{
printf("-1\n");
return 0;
}
}
8.第6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
ps: 感觉已经都实现了==
// power.c -- 计算数的整数幂
#include <stdio.h>
double power(double n, int p); // ANSI函数原型
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive 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;
}
385
double power(double n, int p) // 函数定义
{
double pow = 1;
int i;
for (i = 1; i <= p; i++)
pow *= n;
return pow; // 返回pow的值
}
9.使用递归函数重写编程练习8。
// power.c -- 计算数的整数幂
#include <stdio.h>
double power(double n, int p); // ANSI函数原型
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive 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;
if (p==0){
pow = 1;
printf("Any number to the power of 0 is 1\n");
} else{
if(p>0){
pow = n* power(n,p-1);
} else{
pow = (1/n)* power(n,p+1);
}
}
return pow; // 返回pow的值
}
10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()函数接受两个在2~10范围内的参数,然后以第2个参数中指定的进制打印第1个参数的数值。例如,to_base_n(129, 8)显示的结果为201,也就是129的 八进制数。在一个完整的程序中测试该函数。
/* binary.c -- 以二进制形式打印制整数 */
#include <stdio.h>
void to_binary(unsigned long n);
int main(void)
{
unsigned long number;
printf("Enter an integer (q to quit):\n");
while (scanf("%lu", &number) == 1)
{
printf("Binary equivalent: ");
to_binary(number);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_binary(unsigned long n) /* 递归函数 */
{
int r;
r = n % 2;
if (n >= 2){
to_binary(n / 2);
}
putchar(r == 0 ? '0' : '1');
return 0;
}