当前位置:首页 > C语言练习材料(2014)及选择题解答
C语言补充材料(2014.6)
printf(\ puts(str_a);
fun(str_a,str_b);
printf(\\\n\
puts(str_b); getch(); }
9)改错题cmody631.c,实现任意输入一个年份和月份,输出该月有多少天。 提示:
(1) 二维数组day的第1行存放
非闰年每个月的天数,第二行存放闰年每个月的天数; (2) 判断输入年份是否为闰年,
若是则从day的第2行获取对应的月份天数,否则从day 的第一行获取对应的月份天数。
#include
int /**/ day /**/={{31,28,31,30,31, 30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30, 31}};
int year,month,/**/ flag=1 /**/; printf(\ scanf(\
printf(\ scanf(\
if(year@0==0 || year%4==0 && year0!=0) flag=1; printf(\of the month is %d.\ printf(\ getch(); }
(10) 修改程序cmody632.c,使函数pattern(int n)根据参数n(1 当n=5时: * *#* *#*#* *#*#*#* *#*#*#*#* 当n=4时: * *#* *#*#* *#*#*#* #include void pattern(/**/ int /**/) { int i,j,space; for(i=1;/**/ i { for(space=1;space<=20-i;space++) printf(\ for(j=1;j<=2*i-1;j++) { if(/**/ j%2==0 /**/) printf(\ else printf(\ } printf(\ } } void main() { int n; do { printf(\ scanf(\ }while(n<=1 || n>=10); pattern(n); getch(); } 填空题 1) 补充程序ccon591.c, 计算 k?1?4?7?10???25 的值 #include k=/**/ /**/; for(i=1; /**/ /**/; i+=3) /**/ /**/ 13 printf(\ getch(); } 2) 补充程序ccon592.c,求以下分数序列前15项之和。 21,32,53,138,2113,? #include int n,x,y,t; float sum; /**/ /**/; x=2; y=1; for(n=1;/**/ /**/ ;n++) { sum+=1.0*x/y; t=x; x=x+y; y=/**/ /**/; } printf(\ getch(); } (3)补充程序ccon601.c,输出100以内能被3整除且个位数字为6的所有正整数。 #include for(i=0; /**/ /**/; i++) { k=10*i+6; if(/**/ /**/) continue; printf(\ /**/); } printf(\ getch(); } (4)补充程序ccon602.c,使chang_w( )函数对字符串中的元音字母进行加密,方法是: ?a?转换成?e?、 ?e?转换成?i?、 ?i?转换 成?o?、 ?o?转换成?u?、 ?u?转换成?a?, 其他字符保持不变。例如: 输入原文:You are welcome! 输出密文:Yua eri wilcumi! #include void change_w(/**/ /**/) { int i=0; while( str[i] ) { switch( str[i] ) { case 'a': str[i]='e'; break; case 'e': /**/ /**/; case 'i': str[i]='o'; break ; case 'o': str[i]='u'; break; case 'u': str[i]='a'; } /**/ /**/ ; } } void main() { char src[80],tag[80]; printf(\ gets(src); strcpy(tag,src); change_w( tag ); printf(\source string: %s\\n\ printf(\ getch(); } (5)补充程序ccon611.c,逐一计算方阵x和y中主对角线上相应位置元素的和,并依此存入z数组)。例如: 方阵x为: 19 58 7 14 C语言补充材料(2014.6) 31 15 4 23 0 1 方阵y为: 11 62 23 4 5 16 37 8 109 #include x[SIZE][ SIZE]={19,58,7,31,15,4,23,0,1}; int y[SIZE][ SIZE]={11,62,23,4,5,16,37,8,109}; int z[SIZE],i; for(i=0; /**/ /**/; i++) z[i]=/**/ /**/; for(i=0;i printf(\ /**/); printf(\ getch(); } (6)补充程序ccon612.c,使sort( )函数用选择法对数组a中n个元素按从小到大排序。 #include void sort(/**/ /**/) { int i, j, mark, t; for( i = 0; i < N-1; i++ ) { mark = i; for(/**/ /**/; j< N; j++) if(a[j] < a[mark]) mark=j; if( mark != i ) { t= a[mark] ; a[mark]=/**/ /**/; a[i] =t; } } } void main() { int a[N]={12,0,7,-5,2,16,23,8,-3,10,19,5}; int i; sort(a); printf(\ for(i=0;i printf(\ \ printf(\ getch(); } (7)补充程序ccon621.c,计算[1,100]区间内所有奇数之和及所有偶数之和。 #include { int asum,bsum,a,i; asum=bsum=/**/ /**/; for(i=1; /**/ /**/; i+=2) { asum+=i; a=/**/ /**/; bsum+=a; } printf(\ printf(\ getch(); } (8)补充程序ccon622.c,统计字符串str中字母?a?和字母?A?的总个数。 #include int n; char *p= str; /**/ /**/; while(*p) { if(/**/ /**/ || *p=='A') n++; /**/ /**/; 15 } return n; } void main() { char str[255]; printf(\ gets(str); printf(\of 'a' and 'A' is: %d\\n\ getch(); } (9)补充程序ccon631.c, 对一组整数,求它们十位上的数的和 #include a[N]={45,69,123,78,90,102,60,300,51,999}; sum=/**/ /**/; for(i=0; /**/ /**/; i++) sum=sum+(/**/ /**/); printf(\ getch(); } (10)补充程序ccon632.c,对输入的一个正整数,从低位到高位依次取出各位上为偶数的数字,组成一个新的整数。例如: 输入:367281 输出:826 #include unsigned long fun(unsigned long x) { unsigned long k=0; int /**/ /**/; while(x) { t=x; if(t%2== 0 ) k=k*10 + /**/ /**/; x=x/10 ; } return /**/ /**/; } void main() { unsigned long x=-1; while(x<0 || x>99999999) { printf(\input x(0 printf(\result is:%ld\\n\ getch(); } 编程题 1) 打开程序cprog591.c,对double fun(float x,float y)的函数编程,使其计算: (x,y)?5.8x2fun?1.3y1.6y2?4.5x?1.8 例如:fun(2.250, 1.280)=3.335 #include double fun(float x,float y) { /**/ /**/ } void main() { float x,y; printf(\ scanf(\ printf(\= %.3lf\\n\ 16
共分享92篇相关文档