习题九

来源:互联网 发布:excel比对重复数据 编辑:程序博客网 时间:2024/04/30 13:47
1、设计函数 min(x,y),返回两个double数值中较小的数值,同时用一个简单的驱动程序测试该函数。
#include<stdio.h>
double min(double ,double );

int main(void)
{
double x,y;
printf("input two doubles:");
scanf("%lf%lf",&x,&y);
printf("the smaller is:%.2lf\n",min(x,y));
return(0);
}

double min(double a,double b)
{
return a<b?a:b;
}
2、设计函数chline(ch, i, j),实现指定字符在i列到j列的输出,并用一个简单的驱动程序测试该函数。
#include<stdio.h>
void chline(char , int , int );

int main(void)
{
int x,y;
char ch;
printf("input a char:");
scanf("%c",&ch);
printf("input two int:");
scanf("%d%d",&x,&y);
chline(ch,x,y);
return 0;
}

void chline(char ch, int i, int j)
{
int k;
for(k=1; k<i; k++)
printf(" ");
for( ; k<=j; k++)
printf("%c",ch);
printf("\n");
}

3、编写一个函数。函数的3个参数是一个字符和两个整数。字符参数是需要输出的字符。第一个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。
#include<stdio.h>
void chline(char , int , int );

int main(void)
{
int x,y;
char ch;
printf("input a char:");
scanf("%c",&ch);
printf("input column and row:");
scanf("%d%d",&x,&y);
chline(ch,x,y);
return 0;
}

void chline(char ch, int column, int row)
{
int x,y;
for(y=0; y<row; y++)
{
for(x=0; x<column; x++)
printf("%c",ch);
printf("\n");
}
}

 /4、两数值的谐均值可以这样计算:首先对两数值的倒数取平均值,最后再取倒数。编写一个带有两个double参数的函数,计算这两个参数的谐均值。
#include<stdio.h>
double calculate(double ,double );

int main(void)
{
double a,b;
printf("input two doubles:");
scanf("%lf%lf",&a,&b);
printf("1/( (1/x+1/y) / 2 ) = %0.3lf\n",calculate(a,b) );
return 0;
}

double calculate(double x,double y)
{
return 1/( (1/x+1/y) / 2 );
}
/5、编写并测试函数larger_of(),其功能是将两个double类型变量的数值替换成它们中的较大值。例如,larger_of(x,y)会把x和y中的较大数值重新赋给变量x和y
#include<stdio.h>
void large_of(double *,double *);

int main(void)
{
double a,b;
printf("input two doubles:");
scanf("%lf%lf",&a,&b);
large_of(&a,&b);
printf("the result is: a = %0.3lf, b = %0.3lf\n",a,b );
return 0;
}

void large_of(double *x,double *y)
{
*x = *y = *x>*y? *x:*y;
}
/6、编写一个程序,使其从标准输入读取字符,直到遇到文件结尾。对于每个字符,程序需要检查并报告该字符是否是一个字母。如果是的话,程序还应报告该字母在字母表中的数值位置。例如,c和C的字母位置都是3。可以先实现这样一个函数:接受一个字符参数,如果该字符为字母则返回该字母的数值位置,否则返回-1。
#include<stdio.h>
#include<ctype.h>
int ABC(char ch);

int main(void)
{
char ch;
printf("input a char:");
scanf("%c",&ch);
printf("the position of the char in ABC is: %d\n",ABC(ch));
return 0;
}

int ABC(char ch)
{
if(isalpha(ch)) return tolower(ch) - 'a' + 1 ;
else return -1;
}
/7、在第6章“C控制语句:循环”的程序清单6.20中,函数power()的功能是返回一个double类型数的某个正整数次幂。现在改进该函数,使其能正确地计算负幂。同时,用该函数实现0的任何次幂为0,并且任何数值的0次幂为l。使用循环的方法编写该函数并在一个程序中测试它。
#include<stdio.h>
double power(double , int );

int main(void)
{
double x;
int exp;
printf("input the base number and the exponent:");
scanf("%lf%d",&x,&exp);
printf("%.3g to the power %d is %.5g\n", x, exp, power(x,exp));
return 0;
}

double power(double n, int p)
{
int i;
double pow=1;
if (p>0)
for(i=1;i<=p;i++)
pow *= n;
else if(p<0)
for(i=-1;i>=p;i--)
pow /= n;
else if(n != 0)
pow = 1;
else pow = 1 / n; //0的0次幂无意义,所以用1/0这个无意义数(1.#INF)代替

return pow;
}
/8、使用递归函数重做练习7。
#include<stdio.h>
double power(double , int );

int main(void)
{
double x;
int exp;
printf("input the base number and the exponent:");
scanf("%lf%d",&x,&exp);
printf("%.3g to the power %d is %.5g\n", x, exp, power(x,exp));
return 0;
}

double power(double n, int p)
{
int i;
double pow=1;
if (p>0)
for(i=1;i<=p;i++)
pow *= n;
else if(p<0)
pow = 1 / power(n,-p);
else if(n != 0)
pow = 1;
else pow = 1 / n; //0的0次幂无意义,所以用1/0这个无意义数(1.#INF)代替

return pow;
}
/9、为了使程序清单 9.八 中的函数to_binary()更一般化,可以在新的函数to_base_n()中使用第二个参数,且该参数的范围从2到10。然后,这个新函数输出第一个参数在第二个参数规定的进制数下的数值结果。例如,to_base_n (129,8)的输出是201,也就是129的八进制数值。最后在一个完整的程序中对该函数进行测试。
#include <stdio.h>
void to_base_n(unsigned long , unsigned int);

int main(void)
{
unsigned long number;
unsigned int base;
printf("Enter an integer (q to quit):\n");
while (scanf("%lu%u", &number, &base) == 2) //原书有误:不应该是%ul,而是%lu
{
printf("%lu's base %u equivalent: ", number, base);
to_base_n(number,base);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");

return 0;
}

void to_base_n(unsigned long n, unsigned int base) //* recursive function
{
int r;

r = n % base;
if (n >= base)
to_base_n(n / base, base);
putchar('0' + r);

return;
}
/10、编写并测试一个函数Fibonacci(),在该函数中使用循环代替递归完成斐波纳契数列的计算。
#include <stdio.h>
long Fibonacci (int );

int main(void)
{
int n;
printf("Enter an integer (q to quit):\n");
while (scanf("%d", &n) == 1)
{
printf("The term %d of Fibonacci sequence is %d\n", n, Fibonacci (n));
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");

return 0;
}

long Fibonacci (int n)
{
int n1,n2,temp,i;
if(n >2)
for(n1=1,n2=1,i=3; i<=n; i++)
{
temp = n1 + n2;
n1 = n2;
n2 = temp;
}
else n2=1;

return n2;
}
0 0
原创粉丝点击