C Primer Plus 9
函数
Promgraming Test
(1)设计一个函数min(x,y)返回两个double类型值中的最小值。
#include<stdio.h>
double min(double x,double y);
int main(void)
{
double d1,d2;
printf("Enter tow float number:");
scanf("%lf %lf",&d1,&d2);
printf("You input %g and %g.The Min is %g.",d1,d2,min(d1,d2));
return 0;
}
double min(double x,double y)
{
if(x<y)
return x;
else
return y;
}
(2)设计一个函数chline(ch,i,j)打印指定字符j行i列,在一个简单的驱动程序中测试该程序。
#include<stdio.h>
#include<windows.h>
void chline(char ch,int cols,int rows);
int main(void)
{
char c;
int i,j;
printf("Enter the char you want to print:\n");
scanf("%c",&c);
printf("Enter the rows and cols you want to print:\n");
scanf("%d,%d",&j,&i);
chline(c,j,i);
system("pause");
return 0;
}
void chline(char ch,int cols,int rows)
{
for(int m = 0;m<rows;m++)
{
for(int n=0;n<cols;n++)
printf("%c\n",ch);
}
}
(3)编写一个函数,接受三个参数:一个字符和两个整数。字符参数是待打印的字符,第一个整数指定一行中打印字符的次数,第二个整数指定打印指定字符的函数。
#include<stdio.h>
#include<windows.h>
void print_char(char ch,int cols,int rows);
int main(void)
{
char c;
int i,j;
printf("Enter the char you want to print:\n");
scanf("%c",&c);
printf("Enter the rows and cols you want to print:\n");
scanf("%d,%d",&i,&j);
print_char(c,j,i);
system("pause");
return 0;
}
void print_char(char ch,int cols,int rows)
{
for(int m=0;m<rows;m++)
{
for(int n=0;n<cols;n++)
printf("%c",ch);
printf("\n");
}
}
(4)计算两数的调和平均值(先求两数的倒数,然后计算两个倒数的平均值,最后计算结果的倒数。)
#include<stdio.h>
#include<windows.h>
double calc(double x,double y);
int main(void)
{
double d1,d2;
printf("Enter tow float number:\n");
scanf("%lf,%lf",&d1,&d2);
printf("The result is %g.\n",calc(d1,d2));
system("pause");
return 0;
}
double calc(double x,double y)
{
return 2/(1/x + 1/y);
}
(5)编写一个函数,把两个double类型变量的值替换为较大值。
#include<stdio.h>
#include<windows.h>
void larger_of(double *x,double *y);
(6)编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第一个变量中,中间值放入第二个变量中,最大值放入第三个变量中。
#include<stdio.h>
#include<windows.h>
void ordering(double *x,double *y,double *z);
int main(void)
{
double d1,d2,d3;
printf("Enter three float numbers:\n");
scanf("%lf,%lf,%lf",&d1,&d2,&d3);
printf("Data you input is %g,%g and %g.\n",d1,d2,d3);
ordering(&d1,&d2,&d3);
printf("After funtion.Data is %g,%g and %g.\n",d1,d2,d3);
system("pause");
return 0 ;
}
void ordering(double *x,double *y,double *z)
{
double temp;
if(*x> *y)
{
temp = *x;
*x = *y;
*y = temp;
}
if(*x> *z)
{
temp = *x;
*x = *z;
*z = temp;
}
if(*y> *z)
{
temp = *y;
*y = *z;
*z = temp;
}
}
(7)编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母,如果是,还要报告该字母在字母表中的位置。再编写一个函数,一一个字符作为参数,如果该字符是一个字母则返回一个数值位置。否则,返回-1.
#include<stdio.h>
#include<windows.h>
void get_char_pos(void);
int position(char ch);
int main(void)
{
get_char_pos();
system("pause");
return 0;
}
void get_char_pos(void)
{
char ch;
printf("Enter the chars(ended by EOF,not enter):\n");
while((ch = getchar())!= EOF)
{
if((ch = getchar())=='\n')
continue;
if(position(ch)!=-1)
{
printf("The char %c's position in alphabet is %d.\n",ch,position(ch));
}
else
printf("%c is not a alphabet.\n",ch);
}
}
int position(char ch)
{
if(ch>='A'&&ch<='Z')
return ch-'A'+1;
if(ch>='a'&&ch<='z')
return ch-'a'+1;
return -1;
}
(8)改进power()函数,使其能够计算负幂。注意特殊情况,0的任何次幂都是0.其余任何数的0次幂都是1.
#include<stdio.h>
double power(double n,int p);
int main(void)
{
double x,xpow;
int exp;
printf("Enter a number and the integer power to which.\n");
printf("The number will be raised.Enter q 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 the next pair of numbers or q to quit.\n");
}
printf("Hope you enjoy this power trip - - bye!\n");
return 0;
}
double power(double n,int p)
{
double pow = 1;
int i;
if(n == 0 && p==0)
{
printf("The %g to the power %c is not define,return 1!\n",n,p);
return 1;
}
if(n==0)
return 0;
if(p==0)
return 1;
if(p>0)
{
for(i = 1;i<=p;i++)
pow*=n;
return pow;
}
else
{
for(i=1;i<=-p;i++)
pow *= n;
return 1/pow;
}
}
(9)编写Fibonacci()函数,该函数用循环计算斐波那契数。
#include<stdio.h>
void Fibonacci(int n);//计算斐波那契数列前n项值
int main(void)
{
int n;
printf("Enter the number Fibonacci (q to quit):\n");
while(scanf("%d",&n)==1)
{
if(n>=2)
{
Fibonacci(n);
printf("Enter the number of Fibonacci (q to quit):\n");
}
}
return 0;
}
void Fibonacci(int n)
{
unsigned long f1,f2,temp;
f1 = 1;
f2 = 1;
for(int i = 0;i<n;i++)
{
printf("%lu",f1);
temp = f1+f2;
f1 = f2;
f2 = temp;
}
printf("\n");
}