1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单 的驱动程序中测试该函数。
#include <stdio.h>
#include <stdlib.h>
double min(double x,double y);
int main()
{
double a,b;
printf("Input:\n");
scanf("%lf %lf",&a,&b);
printf("The small one is %lf",min(a,b));
return 0;
}
double min(double x,double y){
double small;
if(x>y)
small=y;
else
small=x;
return small;
}
2.设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
#include <stdlib.h>
void chline(char ch,int row,int column);
int main()
{
char ch;
int row, column;
printf("Please enter the character, row and column:\n");
scanf("%c %d %d",&ch,&row,&column);
chline(ch,row,column);
return 0;
}
void chline(char ch,int row,int column){
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{ putchar(ch);}
printf("\n");
}
}
3.编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。
同第三题
4.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数 的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的 参数,返回这两个参数的调和平均数。
#include <stdio.h>
#include <stdlib.h>
float harmonic(float a,float b);
int main()
{
float a,b;
printf("Enter two numbers:\n");
scanf("%f %f",&a,&b);
printf("The harmonic mean is %f",harmonic(a,b));
return 0;
}
float harmonic(float a,float b){
float mean;
mean=2/(1/a+1/b);
return mean;
}
5…编写并测试一个函数larger_of(),该函数把两个double类型变量的值替 换为较大的值。例如,larger_of(x, y)会把x和y中较大的值重新赋给两个变量。
#include <stdio.h>
#include <stdlib.h>
void large_of(double * x,double * y);
int main()
{
double x,y;
printf("Enter two number:\n");
scanf("%lf %lf",&x,&y);
printf("Originally, x=%lf, y=%lf\n",x,y);
large_of(&x,&y);
printf("Now, x=%lf, y=%lf\n",x,y);
return 0;
}
void large_of(double * x,double * y){
double back;
if(*x>*y)
*y=*x;
else
*x=*y;
}
6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最 小值放入第1个函数,中间值放入第2个变量,最大值放入第3个变量。
#include <stdio.h>
#include <stdlib.h>
void pointe(double*x3, double*x2, double*x1);
int main()
{
double a,b,c;
printf("input:");
scanf("%lf %lf %lf",&a,&b,&c);
printf("Originally a=%.2lf b=%.2lf c=%.2lf\n",a,b,c);
pointe(&a,&b,&c);
printf(" Now a=%.2lf b=%.2lf c=%.2lf\n",a,b,c);
return 0;
}
void pointe(double*x3, double*x2, double*x1)
{
double temp;
if(*x3>*x2){temp=*x3;*x3=*x2;*x2=temp;}
if(*x3>*x1){temp=*x1;*x1=*x3;*x3=temp;}
if(*x2>*x1){temp=*x2;*x2=*x1;*x1=temp;}
}
7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为 参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
#include <stdio.h>
#include <stdlib.h>
int return_value(char ch);
int show_position(char ch);
int main()
{
char charac;
printf("Please enter a character:\n");
while( ( charac=getchar() ) !=EOF)
{
while ( charac=='\n'||charac==' ')
charac=getchar();
if (charac==EOF) break;
show_position(charac);}
printf("Done!\n");
return 0;
}
int return_value(char ch){
if(ch>=65&&ch<=90)
return ch-64;
else if(ch>=97&&ch<=122)
return ch-96;
else
return -1;
}
int show_position(char ch){
int flag;
flag=return_value(ch);
if ( flag==-1 )
printf("%c is not a letter\n",ch);
else printf("%c is the %d th letter in alphabet\n",ch,flag);
}
8.第6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
// power.c -- raises numbers to integer powers
#include <stdio.h>
double power(double n, int p); // ANSI prototype
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); // function call
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) // function definition
{
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 <= (-1*p); i++)
pow *= n;
return 1/pow;
}
else //p=0
{
if(n==0)
printf("The 0 power of 0 is not defined\n");
return 1;
}
}
9.使用递归函数重写编程练习8。.
// power.c -- raises numbers to integer powers
#include <stdio.h>
double power(double n, int p); // ANSI prototype
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); // function call
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) // function definition
{
if(0==n)
{
if(0==p){
printf("the value of 0^0 is not defined\n");
return 1;
}
return 0;
}
else
{
if(p>0)
{return n*power(n,p-1);}
else if (p<0)
{ return 1/n*power(n,p+1);}
else
return 1;
}
}
10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n() 函数接受两个在2~10范围内的参数,然后以第2个参数中指定的进制打印第 1个参数的数值。例如,to_base_n(129, 8)显示的结果为201,也就是129的 八进制数。在一个完整的程序中测试该函数。
/* binary.c -- prints integer in binary form */
#include <stdio.h>
void to_binary_n(unsigned long n,int radix);
int main(void)
{
unsigned long number;
int radix;
printf("Enter an integer and radix (q to quit):\n");
while (scanf("%lu %d", &number,&radix) == 2)
{
if(radix>10 ||radix<2)
{
printf("Please select the radix from 2 to 10\n");
printf("Enter an integer and radix (q to quit):\n");
continue;
}
printf(" radix %d equivalent: ",radix);
to_binary_n(number,radix);
putchar('\n');
printf("Enter an integer and radix (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_binary_n(unsigned long n,int radix) /* recursive function */
{
int r;
r = n % radix;
if (n >= radix)
to_binary_n(n/radix,radix);
printf("%d",r);
return;
}
11.编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数
#include <stdio.h>
#include <stdlib.h>
unsigned long Fibonacci(unsigned n);
int main()
{
int n;
printf("Please enter:\n");
while( scanf("%d",&n)==1){
printf("The %d th term in Fibonacci() is %ld\n",n,Fibonacci(n));
printf("Please enter:\n");
}
printf("Done!");
return 0;
}
unsigned long Fibonacci(unsigned n){
unsigned long pre_n,state_n;
unsigned long temp;
if(n>2)
{ pre_n=1;
state_n=1;
for(int i=2;i<n;i++){
temp=state_n;
state_n+=pre_n;
pre_n=temp;}
return state_n;}
else
return 1;
}