欢迎来到天天文库
浏览记录
ID:12322968
大小:15.98 KB
页数:7页
时间:2018-07-16
《c语言程序设计之函数》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、三、函数1、使用函数判断数的符号程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(02、x=10时sign(x)的值为1)sign(-5)=-1(x=-5时sign(x)的值为-1)sign(0)=0(x=0时sign(x)的值为0)#includeintsign(intx);intmain(void){intx,y;intrepeat,ri;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d",&x);y=sign(x);printf("sign(%d)=%d",x,y);}}intsign(intx3、){intt=0;if(x<0)t=-1;elseif(x==0)t=0;elset=1;returnt;}2、使用函数求奇数和程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(04、011861721019-1输出Thesumoftheoddnumbersis30.Thesumoftheoddnumbersis47.#includeinteven(intn);intmain(void){intn,sum;intri,repeat;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){sum=0;scanf("%d",&n);while(n>0){if(even(n)==0)sum=sum+n;scanf("%d",&n)5、;}printf("Thesumoftheoddnumbersis%d.",sum);}}inteven(intn){intt;if(n%2==0)t=1;elset=0;returnt;}3、使用函数统计一个整数中数字的个数程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(06、umber,digit),它的功能是统计整数number中数字digit的个数,函数形参number和digit的类型是int,函数类型是int。例如,countdigit(10090,0)的返回值是3。输入输出示例:括号内是说明输入2(repeat=2)212522(number=21252,digit=2)-11119(number=-1111,digit=9)输出Number21252ofdigit2:3(21252中有3个2)Number-1111ofdigit9:0(-1111中有0个97、)#include"stdio.h"intmain(void){intcount,digit,in;intrepeat,ri;intcountdigit(intnumber,intdigit);scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d%d",&in,&digit);count=countdigit(in,digit);printf("Number%dofdigit%d:%d",in,digit,count);}}intcou8、ntdigit(intnumber,intdigit){intc,t;c=0;if(number<0)number=-number;/*这里若没将负数转成正数,判断就会出错*/while(number!=0){t=number%10;number=number/10;if(t==digit)c++;}returnc;}4、数字金字塔程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0
2、x=10时sign(x)的值为1)sign(-5)=-1(x=-5时sign(x)的值为-1)sign(0)=0(x=0时sign(x)的值为0)#includeintsign(intx);intmain(void){intx,y;intrepeat,ri;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d",&x);y=sign(x);printf("sign(%d)=%d",x,y);}}intsign(intx
3、){intt=0;if(x<0)t=-1;elseif(x==0)t=0;elset=1;returnt;}2、使用函数求奇数和程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(04、011861721019-1输出Thesumoftheoddnumbersis30.Thesumoftheoddnumbersis47.#includeinteven(intn);intmain(void){intn,sum;intri,repeat;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){sum=0;scanf("%d",&n);while(n>0){if(even(n)==0)sum=sum+n;scanf("%d",&n)5、;}printf("Thesumoftheoddnumbersis%d.",sum);}}inteven(intn){intt;if(n%2==0)t=1;elset=0;returnt;}3、使用函数统计一个整数中数字的个数程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(06、umber,digit),它的功能是统计整数number中数字digit的个数,函数形参number和digit的类型是int,函数类型是int。例如,countdigit(10090,0)的返回值是3。输入输出示例:括号内是说明输入2(repeat=2)212522(number=21252,digit=2)-11119(number=-1111,digit=9)输出Number21252ofdigit2:3(21252中有3个2)Number-1111ofdigit9:0(-1111中有0个97、)#include"stdio.h"intmain(void){intcount,digit,in;intrepeat,ri;intcountdigit(intnumber,intdigit);scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d%d",&in,&digit);count=countdigit(in,digit);printf("Number%dofdigit%d:%d",in,digit,count);}}intcou8、ntdigit(intnumber,intdigit){intc,t;c=0;if(number<0)number=-number;/*这里若没将负数转成正数,判断就会出错*/while(number!=0){t=number%10;number=number/10;if(t==digit)c++;}returnc;}4、数字金字塔程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0
4、011861721019-1输出Thesumoftheoddnumbersis30.Thesumoftheoddnumbersis47.#includeinteven(intn);intmain(void){intn,sum;intri,repeat;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){sum=0;scanf("%d",&n);while(n>0){if(even(n)==0)sum=sum+n;scanf("%d",&n)
5、;}printf("Thesumoftheoddnumbersis%d.",sum);}}inteven(intn){intt;if(n%2==0)t=1;elset=0;returnt;}3、使用函数统计一个整数中数字的个数程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(06、umber,digit),它的功能是统计整数number中数字digit的个数,函数形参number和digit的类型是int,函数类型是int。例如,countdigit(10090,0)的返回值是3。输入输出示例:括号内是说明输入2(repeat=2)212522(number=21252,digit=2)-11119(number=-1111,digit=9)输出Number21252ofdigit2:3(21252中有3个2)Number-1111ofdigit9:0(-1111中有0个97、)#include"stdio.h"intmain(void){intcount,digit,in;intrepeat,ri;intcountdigit(intnumber,intdigit);scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d%d",&in,&digit);count=countdigit(in,digit);printf("Number%dofdigit%d:%d",in,digit,count);}}intcou8、ntdigit(intnumber,intdigit){intc,t;c=0;if(number<0)number=-number;/*这里若没将负数转成正数,判断就会出错*/while(number!=0){t=number%10;number=number/10;if(t==digit)c++;}returnc;}4、数字金字塔程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0
6、umber,digit),它的功能是统计整数number中数字digit的个数,函数形参number和digit的类型是int,函数类型是int。例如,countdigit(10090,0)的返回值是3。输入输出示例:括号内是说明输入2(repeat=2)212522(number=21252,digit=2)-11119(number=-1111,digit=9)输出Number21252ofdigit2:3(21252中有3个2)Number-1111ofdigit9:0(-1111中有0个9
7、)#include"stdio.h"intmain(void){intcount,digit,in;intrepeat,ri;intcountdigit(intnumber,intdigit);scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d%d",&in,&digit);count=countdigit(in,digit);printf("Number%dofdigit%d:%d",in,digit,count);}}intcou
8、ntdigit(intnumber,intdigit){intc,t;c=0;if(number<0)number=-number;/*这里若没将负数转成正数,判断就会出错*/while(number!=0){t=number%10;number=number/10;if(t==digit)c++;}returnc;}4、数字金字塔程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0
此文档下载收益归作者所有