函数练习参考代码.docx
《函数练习参考代码.docx》由会员分享,可在线阅读,更多相关《函数练习参考代码.docx(13页珍藏版)》请在冰点文库上搜索。
函数练习参考代码
试验九函数
一、基础能力落实:
1)设计函数min(x,y),返回两个double数值中较小的数值。
#include
doublemin(doublea,doubleb);
intmain(void)
{
doublex,y;
printf("Entertwonumbers:
");
scanf("%lf%lf",&x,&y);
printf("Thesmallernumberis%f.\n",min(x,y));
return0;
}
doublemin(doublea,doubleb)
{
returna
a:
b;
}
2)编写一个程序,调函数实现功能:
(1)求两个数之和;
(2)求两个数之差;(3)求两个数之积。
#include
doubleadd(doublea,doubleb)
{
returna+b;
}
doublesubtract(doublea,doubleb)
{
returna-b;
}
doublemultiple(doublea,doubleb)
{
returna*b;
}
intmain()
{
doublenum1,num2;
printf(“pleaseinputtownumber:
”);
scanf(“%lf%lf”,&num1,&num2);
printf(“%5.2f+%5.2f=%5.2f\n”,num1,num2,add(num1,num2));
printf(“%5.2f+%5.2f=%5.2f\n”,num1,num2,subtract(num1,num2));
printf(“%5.2f+%5.2f=%5.2f\n”,num1,num2,multiple(num1,num2));
}
3)编写一个函数,函数的3个参数是一个字符和两个整数。
字符参数是需要输出的字符,第一个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数,编写一个调用该函数的程序。
#include
voidchLineRow(charch,intc,intr);
intmain(void)
{
charch;
intcol,row;
printf("Enteracharacter(#toquit):
");
while((ch=getchar())!
='#')
{
if(ch=='\n')
continue;
printf("Enternumberofcolumnsandnumberofrows:
");
if(scanf("%d%d",&col,&row)!
=2)
break;
chLineRow(ch,col,row);
printf("\nEnternextcharacter(#toquit):
");
}
printf("Bye!
\n");
return0;
}
voidchLineRow(charch,intc,intr)
{
intcol,row;
for(row=0;row{
for(col=0;col{
putchar(ch);
}
putchar('\n');
}
}
4)以下函数的功能是用递归的方法计算x的n阶勒让德多项式的值。
已有调用语句p(n,x);编写函数实现功能。
递归公式如下:
#include
floatp(intx,intn)
{
floatt,t1,t2;
if(n==0)return1;
elseif(n==1)returnx;
else
{
t1=(2*n-1)*x*p(x,(n-1));
t2=(n-1)*p(x,(n-2));
t=(t1-t2)/n;
returnt;
}
}
intmain()
{
intx,n;
printf(“inputtwoint(xandn):
”);
scanf(“%d%d”,&x,&n);
printf(“%.2f\n”,p(x,n));
return0;
}
5)编写一个函数taxis()实现数组的排序,在函数中调用swap()实现两个数的交换。
打印出排序结果。
#include
voidswap(int*a,int*b)
{
inttmp;
tmp=*a;
*a=*b;
*b=tmp;
}
voidtaxis(int*a,intnum)
{
inti,j;
for(i=0;i{
for(j=i+1;j{
if(a[i]>a[j])
swap(&a[i],&a[j]);
}
}
}
intmain()
{
inti,a[]={2,5,9,8,7,6,4,3,1};
taxis(a,sizeof(a)/sizeof(int));
for(i=0;i{
printf(“%d\t”,a[i]);
}
printf(“\n”);
return0;
}
二、进阶能力落实:
1)编写一个函数,实现两个字符串的比较。
#include
intmy_strcmp(char*p,char*q)
{
inti=0;;
while(*(p+i)==*(q+i))
{
if(*(p+i)==’\0’)return0;
i++;
}
return(*(p+i)-*(q+i));
}
intmain()
{
intm;
charstr1[20],str2[20];
printf(“Inputtowstrings:
”);
gets(str1);
gets(str2);
printf(“result:
%d\n”,my_strmp(str1,str2));
return0;
}
2)编写一个函数is-within().它接受两个参数,一个是字符,另一个是字符串指针。
其功能是如果字符在字符串中。
就返回一个非0值(真);如果字符不在字符串中,就返回0值(假)。
在一个使用循环语句为这个函数提供输入的完整程序中进行测试。
#include
#defineLEN80
intis_within(constchar*str,charc);
intmain(void)
{
charinput[LEN];
charch;
intfound;;
printf("Enterastring:
");
while(gets(input)&&input[0]!
='\0')
{
printf("Enteracharacter:
");
scanf(“\n%c”,&ch);
found=is_within(input,ch);
if(found==0)
printf("%cnotfoundinstring.\n",ch);
else
printf("%cfoundinstring%s\n",ch,input);
printf("Nextstring:
");
}
puts("Done.\n");
return0;
}
intis_within(constchar*str,charch)
{
while(*str!
=ch&&*str!
='\0')
{
str++;
}
return*str;/*=0if‘\0’isreached,non-zerootherwise*/
}
3)有一个班,有3个学生,各四门课,计算总平均分数,以及地n个学生的成绩。
(用函数average求平均成绩,用函数search找出并输出第i个学生的成绩)
intmain()
{
intnum;
floatscore[3][4]={{65,67,70,60},{80,87,90,81},{90,99,100,98}};
printf(“average=%5.2f\n”,average(score[0],12));
while
(1)
{
printf(“Inputanumber(1~3):
”);
scanf(“%d”,&num);
if((num>3)||(num<1))
printf(“error!
\n”);
elsesearch(score,num);
}
return0;
}
floataverage(float*p,intn)
{
float*p_end;
floatsum=0,aver;
p_end=p+n-1;
for(;p<=p_end;p++)
sum+=*p;
aver=sum/n;
returnaver;
}
intsearch(float(*p)[4],intn)
{
inti;
printf(“thescoreofNO.%dare:
”,n);
for(i=0;i<4;i++)
printf(“%2.0f”,*(*(p+n-1)+i));
return0;
}
4)编写并测试函数larger_of(),其功能是将两个double类型变量的数值替换成它们中的较大值,例如:
larger_of(x,y)会把x,y中的较大数值重新赋给变量x和y.
#inlcude
voidlarger_of(double*p1,double*p2);
intmain(void)
{
doublex,y;
printf("Entertwonumbers(qtoquit):
");
while(scanf("%lf%lf",&x,&y)==2)
{
larger_of(&x,&y);
printf("Themodifiedvaluesare%fand%f.\n",x,y);
printf("Nexttwovalues(qtoquit):
");
}
printf("Bye!
\n");
return0;
}
voidlarger_of(double*p1,double*p2)
{
doubletemp=*p1>*p2?
*p1:
*p2;
*p1=*p2=temp;
}
5)编写一个函数,其参数为一个字符串,函数删除字符串中的空格,在一个可以循环读取的程序中进行测试,直到用户输入空行,对于任何输入字符串,函数都应该使用和并可以显示结果
#include
#defineLEN81
intdrop_space(char*s);
intmain(void)
{
charorig[LEN];
while(gets(orig)&&orig[0]!
='\0')
{
drop_space(orig);
puts(orig);
}
puts("Bye!
");
return0;
}
intdrop_space(char*s)
{
char*p,*q;
p=q=s;
while(*q!
=‘\0’)
{
if(*q!
=‘‘)
{
*p++=*q++;
}
else
{
q++;
}
}
*p=‘\0’;
}
6)给主函数传递参数实现echo功能:
#incldue
intmain(intargc,char*argv[])
{inti=1;
while(i{
printf(“%s”,argv[i]);
i++;
}
printf(“\n”);
return0;
}