`
dawuafang
  • 浏览: 1100267 次
文章分类
社区版块
存档分类
最新评论

C Primer Plus(第五版)第9章 函数

 
阅读更多
9.1
#include <stdio.h>
double min(double,double);
int main(void)
{
double x,y;
while(scanf("%lf %lf",&x,&y)!=2)
{
printf("please,input two numbers.\n");
while(getchar()!='\n')
;
}
double smaller=min(x,y);
printf("%g is smaller between %g %g.\n",smaller,x,y);
return 0;
}
double min(double x,double y)
{
return x<y?x:y;
}

9.2
#include <stdio.h>
#define LINE 40
void chline(char,int,int);
int main(void)
{
char ch;
int i,j;
printf("please,input a character and two integers.the first number is smaller than the other.\n");
while(3!=scanf("%c %d %d",&ch,&i,&j))
{
printf("please,input a character and two integers.the first number is smaller than the other.\n");
while(getchar()!='\n')
;
}
chline(ch,i,j);
return 0;
}
void chline(char ch,int i,int j)
{
int k,l;
for(l=1;l<=LINE;l++)
{
for(k=0;k<i;k++);
for (;k<=j;k++)
putchar(ch);
printf("\n");
}
}

9.3
#include <stdio.h>
void charater(char,int,int);
int main(void)
{
char ch;
int i,j;
printf("please,input a characer and two intergers.\n");
while(scanf("%c %d %d",&ch,&i,&j)!=3)
{
printf("please,input a characer and two intergers.\n");
while(getchar()!='\n')
;
}
charater(ch,i,j);
return 0;
}
void charater(char ch,int i,int j)
{
int k,l;
for(l=1;l<=j;l++)
{
for(k=1;k<=i;k++)
putchar(ch);
printf("\n");
}
}

9.4
#include <stdio.h>
double average(double,double);
int main(void)
{
double x,y,value;
printf("please,input two numbers.\n");
while(scanf("%lf %lf",&x,&y)!=2)
{
printf("please,input two numbers.\n");
while(getchar()!='\n')
;
}
value=average(x,y);
printf("the average is %g",value);
return 0;
}
double average(double x,double y)
{
return 2*x*y/(x+y);
}

9.5
#include <stdio.h>
void larger_of(double*,double*);
int main(void)
{
double x,y;
printf("please,input two numbers.\n");
while(scanf("%lf %lf",&x,&y)!=2)
{
printf("please,input two numbers.\n");
while(getchar()!='\n')
;
}
larger_of(&x,&y);
printf("now x is %g ,and y is %g",x,y);
return 0;
}
void larger_of(double*x,double*y)
{
double temp=*x>*y?*x:*y;
*x=*y=temp;
}

9.6
#include <stdio.h>
#include <ctype.h>
int alphaornot(char);
int main(void)
{
int i;
char ch;
while(scanf(" %c",&ch)!=EOF)
{
i=alphaornot(ch);
if (i>0)printf("yes,%c is a letter,it's order is %d.\n",ch,i);
else printf("no ,%c it is not a letter.\n",ch);
}
return 0;
}
int alphaornot(char ch)
{
if (isalpha(ch))
return tolower(ch)-96;
else return -1;
}

9.7
#include <stdio.h>
double powerall(double,int);
double power(double,int);
int main(void)
{
int d;
double f,pow;
printf("please,input two numbers\n");
while(scanf("%lf %d",&f,&d)!=2)
{
printf("please,input two numbers\n");
while(getchar()!='\n')
;
}
pow=powerall(f,d);
printf("%g's%d power is %g.\n",f,d,pow);
return 0;
}
double powerall(double x,int y)
{
if (x!=0)
if(y<0) return 1/power(x,-y);
else return power(x,y);
else return 0;
}
double power(double x,int y)
{
int i;
double sum=1;
if(y==0)
return 1;
else
for(i=1;i<=y;i++)
sum*=x;
return sum;
}

9.8
#include <stdio.h>
double powerall(double,int);
double power(double,int);
int main(void)
{
int d;
double f,pow;
printf("please,input two numbers\n");
while(scanf("%lf %d",&f,&d)!=2)
{
printf("please,input two numbers\n");
while(getchar()!='\n')
;
}
pow=powerall(f,d);
printf("%g's%d power is %g.\n",f,d,pow);
return 0;
}
double powerall(double x,int y)
{
if (x!=0)
if(y<0) return 1/power(x,-y);
else return power(x,y);
else return 0;
}
double power(double x,int y)
{
if (y==1) return x;
else return x*power(x,y-1);
}

9.9
#include <stdio.h>
void to_base_n(unsigned long,unsigned long);
int main(void)
{
unsigned long number;
unsigned long rule;
printf("Enter two integer (q to quit):\n");
while (scanf("%lu %lu", &number,&rule) == 2)
{
printf("%lu equivalent: ",rule);
to_base_n(number,rule);
putchar('\n');
printf("Enter two integer (q to quit):\n");
}
printf("Done.\n");
return 0;
}

void to_base_n(unsigned long n,unsigned long i)
{
int r;
r = n % i;
if (n >= i)
to_base_n(n/i,i);
putchar('0' + r);
return;
}

9.10
#include <stdio.h>
#define size 50
int main(void)
{
int i,j,a[size];
printf("input a integer for Fibonacci(n). q to quit.\n");
while(scanf("%d",&i)==1)
{
if(i<=2&&i>=1)
{
printf("the Fibonacci(%d) is 1\n",i);
continue;
}
else if (i<=0)
printf("you input a wrong value.\n");
else
{
a[0]=1,a[1]=1;
for(j=2;j<=i-1;j++)
a[j]=a[j-1]+a[j-2];
printf("the Fibonacci(%d) is %d.\n",i,a[i-1]);
}
printf("input a integer for Fibonacci(n). q to quit.\n");
}
return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics