C Primer Plus
第五版
第九章课后练习
1、形式变量是在被调函数用中定义,实际参数被赋值给形式参数
2、void dount(int n);int gear(int,int);void stuff_it
(double,double*)
3、char n_to_char(int);int digits(double,int); int
random(void)
4、
int sum(int a,int b)
{
return a+b;
}
5
double sum(double a,double b)
{
return a+b;
}
6
int alter(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
}
7
void salami(int num)
{int count;for(count=1;count<=num;count++)printf("o saimi
mio!\n");}
8
int Biger(int a,int b,int c)
{ int temp;
if(a>b)
temp=a;
else temp=b;
if(temp<c)
temp=c;
return temp;
}
9
#include<stdio.h>
#include<stdbool.h>
//确认输入一个整数
int get_int (void);
//确认范围的上下界是否有效
bool bad_limits(int begin,int end,int low,int hight);
//计算从a到b之间整数的平方和
double sum_squares(int a,int b);
int main(void)
{
const int MIN=-1000;
const int MAX=1000;
int start;
int stop;
double answer;
printf("This program computes the sum of the
squares of \n");
printf("lower limit:\n");
start=get_int();
printf("upper limit:");
stop=get_int();
while(start!=0||stop!=0)
{
if(bad_limits(start,stop,MIN,MAX))
printf("Try again!\n");
else
{
answer=sum_squares(start,stop);
printf("The sum of the squares of
the integrers from \n");
printf("from %d to %d is
%f",start,stop,answer);
}
printf("Enter the limits(enter 0 for both limits
to quit);\n");
printf("lower limit:\n");
start=get_int();
printf("upper limit:");
stop=get_int();
}
printf("Done.\n");
return 0;
}
int get_int (void)
{
int num;
char ch;
while(scanf("%d",&num)!=1)
{
while((ch=getchar())!='\n')
putchar(ch);
printf(" is nor an integer.\n Please
enter an ");
printf("integer value,such as 25,-27,or
3: ");
}
return num;
}
bool bad_limits(int begin,int end,int low,int high)
{
bool not_good=false;
if(begin>end)
{
printf("%d is not smaller %d",begin,end);
not_good=true;
}
if(begin<low||end<low)
{
printf("Value must be %d or greater.
\n",low);
not_good=true;
}
if(begin>high||end>high)
{
printf("Value must be %d or greater.
\n",high);
not_good=true;
}
return not_good;
}
double sum_squares(int a,int b)
{
double total=0;
int i;
for(i=a;i<=b;i++)
total+=i*i;
return total;
}
编程练习
1
#include<stdio.h>
double MIN( double a,double b);
int main(void)
{
double num1,num2;
printf("enter number\n");
scanf("%lf%lf", &num1,&num2);
printf("Bigger number is %lf",MIN(num1,num2));
return 0;
}
double MIN( double a,double b)
{
if (a>b)
return a;
else
return b;
}
2
#include<stdio.h>
void CH( char,int,int);
int main(void)
{
int num1,num2;
char ch1;
printf("enter number\n");
scanf("%d%d", &num1,&num2);
printf("please enter charcater\n");
scanf("%c",&ch1);
CH(ch1,num1,num2);
return 0;
}
void CH(char ch ,int i,int j )
{
for(i;i>0;i--)
{
for(j;j>0;j--)
printf("%c ",ch);
printf("\n");
}
}
3
#include<stdio.h>
void CH( char,int,int);
int main(void)
{
int num1,num2,i,j;
char ch1;
printf("please enter charcater\n");
scanf("%c", &ch1);
printf("enter number\n");
scanf("%d%d", &num1,&num2);
CH(ch1,num1,num2);
return 0;
}
void CH(char ch ,int num1,int num2 )
{
// int i,j;
/*
for(i=0;i<num1;i++)
{
for(j=0;j<num2 ;j++)
printf("%c ",ch);
printf("\n");
}
*/
for( num1;num1>0;num1--)
{
for( num2;num2>0;num2--)
printf("%c ",ch);
printf("\n");
}
}
4
#include<stdio.h>
double NUMBER(double a,double b);
int main(void)
{
double num1,num2;
scanf("%lf%lf",&num1,&num2);
printf("%lf",NUMBER(num1,num2));
return 0;
}
double NUMBER(double a,double b)
{ double sum;
sum=1/((1/a+1/b)/2);
return sum ;
}
5
#include<stdio.h>
void large_of(double *a,double *b);
int main(void)
{
double num1, num2;
scanf("%lf%lf", &num1, &num2);
large_of(&num1,&num2);
printf("%lf %lf", num1, num2);
return 0;
}
void large_of(double *a,double *b)
{
if(*a>*b)
*b=*a;
else
*a=*b;
}
6
#include<stdio.h>
#include<stdbool.h>
int char_int(char );
int main(void)
{
char ch;
while((ch=getchar())!='#')
{
if( char_int(ch) )
printf("\n%c not zimu\n",ch) ;
}
return 0;
}
int char_int(char ch )
{
if(ch>=65&&ch<=90)
{
printf("%c is %d ",ch,ch-64);
return 0;
}
else if(ch>=97&&ch<=122)
{
printf("%c is %d ",ch,ch-96);
return 0;
}
return -1;
}
7\
#include<stdio.h>
void power(int num,int power_);
int main(void)
{
int number,_power;
printf("Please enter number and power");
scanf("%d%d",&number,&_power);
power(number,_power);
return 0;
}
void power(int num,int power_)
{
double sum=1.0;
int i;
if(num==0)
printf("%d power of number %d is
0",num,power_);
else if(power_==1)
printf("%d power of number %d is
1",num,power_);
else
{
for(i=power_;i>0;i--)
sum= sum*num;
printf("%d power of number %d is
%lf",power_,num,sum);
}
}
8
#include<stdio.h>
void power(int num,int power_);
int Di_Gui(int a,int b);
int main(void)
{
int number,_power;
printf("Please enter number and power");
scanf("%d%d",&number,&_power);
power(number,_power);
return 0;
}
void power(int num,int power_)
{
double sum=1.0;
int i;
if(num==0)
printf("%d power of number %d is
0",num,power_);
else if(power_==1)
printf("%d power of number %d is
1",num,power_);
else
{
//for(i=power_;i>0;i--)
// sum= sum*num;
printf("%d power of number %d is
%d",power_,num,Di_Gui(num , power_) );
}
}
int Di_Gui(int a,int b)
{
int sum=1;
if(b>0)
sum=a*Di_Gui(a,b-1);
return sum;
}