一、阅读程序写出程序运行结果
阅读以下程序并写出其运行结果 #include #include int fun(char s[],int c) {
char *q=s; for(; *q; q++) if(*q != c) *(s++)=*q; *s=0; } main() { static char str[]="turbo c and borland c++"; char ch; fun(str,'c');
printf("str[]=%s\n",str); system("pause");
}
程序的运行结果是str[]=turbo and Borland ++ 阅读以下程序并写出其运行结果 #include #include
void fun(int a, int b, long *c) { *c=a/10*10+a%10*1000+b/10*100+b%10; } main()
{ int a=42,b=25; long c; fun(a, b, &c);
printf("The result is: %ld\n", c); system("pause"); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(int *s, int nl, int n2)
the result is: 2245 请按任意键继续…
{ int i, j, t ; i=nl; j=n2;
while(i
{ int a[10]={1,2,3,4,5,6,7,8,9,0},k; fun(a,0,3); fun(a,4,9); fun(a,0,9);
for(k=0;k
程序运行的结果是 5678901234
Press any key to continue 阅读以下程序并写出其运行结果 #include main() {
int i=1;
while(!((i%2==1)&&(i%3==2)&&(i%5==4)&&(i%6==5)&&(i%7==0))) ++i;
printf("\n >> The ladder has %d stages.\n",i); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include #define ROW 3 #define COL 4 main() { int matrixA[ROW][COL]={11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}; int matrixB[COL][ROW];
int i,j;
printf("%d*%d:\n",ROW,COL); for( i=0; i
for( j=0; j
matrixB[j][i] = matrixA[i][j];
}
}
printf("MatrixB,");
printf("%d*%d:\n",COL,ROW); for( i=0; i
}
printf("\n");
}
system("pause");
}
程序的运行结果是3*4
Matrix B 4*3
11 15 19 12 16 20 13 17 21 14 18 22
阅读以下程序并写出其运行结果 #include #define N 10 main() { int primes[N]; int pc,m,k;
printf("\n The first %d prime numbers are:\n",N); primes[0]=2; pc =1; m =3; while(pc
while(primes[k]*primes[k]
if(m%primes[k]==0) {
m+=2; k=1; }
else
k++;
primes[pc++]=m; m+=2; }
for(k=0;k
printf("%4d",primes[k]); system("pause");
}
程序的运行结果是 the fiest 10 prime numbers are
2 3 5 7 11 13 17 19 23 29
阅读以下程序并写出其运行结果 #include #include void fun(char *s[ ],int n) { char *t; int i,j;
for(i=0;i
if(strlen(s[i])>strlen(s[j])) {t=s[i];s[i]=s[j];s[j]=t;} } main()
{ char *ss[]={“bcc”,”bbcc”,”xy”,”aaaacc”,”aabcc” }; fun(ss,5); printf(“%s,%s\n”,ss[0],ss[4]); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(char *a, char *b) { while(*a==’*’) a++; while(*b=*a) {b++;a++;} } main()
{ char *s=”****a*b****”,t[80]; fun(s,t);
puts(t); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include main() { FILE *fp;
int a[10]={1,2,3},i,n; fp=fopen(“dl.dat”,”w”);
for(i=0;i
fp=fopen(“dl.dat”,”r”); fscanf(fp,”%d”,&n); fclose(fp);
printf(“%d\n”,n); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void swap( int *a, int *b ) { int *t ; t=a; a=b; b=t; } main()
{ int i=3,j=5,*p=&i,*q=&j;
swap(p,q); printf(“%d %d\n”,*p,*q); }
程序的运行结果是
阅读以下程序并写出其运行结果 int circle(int n, int d) { int s=0,m=n; while(m) { s=s*d+m%d; m/=d;
}
return s==n;
}
int num[]={232,27,851}; int scale[]={2,10,16}; main() { int i,j;
for(i=0;i (%d) is a Circle Number!\n",num[i],scale[j]); else
printf("%d -> (%d) is not a Circle Number!\n",num[i],scale[j]);
}
程序的运行结果是 答案太长不想写
阅读以下程序并写出其运行结果 #include #define NULL 0
int *search2(int *pa,int *pb,int an,int bn) {
int *ca,*cb; ca=pa;cb=pb;
while(ca*cb) cb++; else
return ca;/*返回在这两个数表中找到相等元素*/
}
return NULL;
}
main( ) { int *vp,i;
int a[ ]={1,3,5,7,9,13,15,27,29,37}; int b[ ]={2,4,6,8,10,13,14,27,29,37}; puts("The elements of array a is:"); for(i=0;i
printf(" %d",a[i]);
puts("\nThe elements of array b is:"); for(i=0;i
printf(" %d",b[i]);
vp=search2(a,b,sizeof a/sizeof a[0],sizeof b/sizeof b[0]);
if(vp) printf("\nThe first same number in both arrays is %d\n",*vp); else printf("Not found!\n");
}
程序的运行结果是
阅读以下程序并写出其运行结果
#include main() { int i, j; char ch; i=1; while(i
}
printf("\n"); i++;
}
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include rest(int a[], int n) { int i,low,high,t;
for(i=0,low=0,high=n-1;i0) {
t=a[i];
a[i]=a[high]; a[high]=t; high--;
}
else if(a[i]==0) i++; else
{ t=a[i]; a[i]=a[low]; a[low]=t; low++; i++; }
}
}
int s[]={8,4,0,-1,6,0,-5}; main() { int i;
printf("\n The arry before rest is:\n"); for(i=0;i
printf("%4d",s[i]); rest(s,sizeof(s)/sizeof(s[0])); printf("\n The arry after rest is:\n"); for(i=0;i
printf("%4d",s[i]);
} }
程序的运行结果是the arry before rest is 8 4 0 -1 6 0 -5 the arry after rest is -5 -1 0 0 6 4 8
阅读以下程序并写出其运行结果 #include #include
typedef struct { char name[9]; char sex; float score[2]; void f( STU a)
} STU;
{ STU b={“Zhao” ,’m’,85.0,90.0} ; int i; strcpy(a.name,b.name); a.sex=b.sex;
for(i=0;i
{ STU c={“Qian”,’p’,95.0,92.0}; f(c);
printf(“%s,%c,%2.0f,%2.0f\n”,c.name,c.sex,c.score[0],c.score[1]); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include main() {
int x,y,z,j=0;
printf("\n The possible plans to buy 100 fowls with 100 Yuan are:\n\n"); for(x=0;x
if(z%3==0&&5*x+3*y+z/3==100) printf("%2d: cock=%2d hen=%2d chicken=%2d\n",++j,x,y,z); }
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include void fun(int x)
{ if(x/2>0) fun(x/2); printf(“%d ”,x); } main()
{ fun(3); printf(“\n”); }
程序的运行结果是
阅读以下程序并写出其运行结果 #define MAX(a,b) (a>b)?a:b main() { int x, y; x=25; y=56;
printf("the result is: %d\n", MAX(x,y));
}
程序的运行结果是the result is 56 阅读以下程序并写出其运行结果 #include #define ROWS 4 #define COLS 4
int nums[ROWS][COLS]={{1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1}};
char *roms[ROWS][COLS]={{"m","m","m","m"},
{"cm","d","cd","c"}, {"xc","l","xl","x"},
{"ix","v","iv","i"}};
main(int argc,char *argv[ ]) {
int low=1000,high=1012; char roman[25];
for(;low
}
system("pause");
}
to_roman(int decimal,char roman[ ]) { int power,index; roman[0]='\0';
for(power=0;power=nums[power][index]) { strcat(roman,roms[power][index]); decimal-=nums[power][index];
}
}
程序的运行结果是1000 m 1001 mi 1002 mii 1004 miv 1005 mv 1006 mvi 1008 mviii 1009 mix 1010 mx 1012 mxii
#include
int Fun(int x) {
static int y = 0; y += x; return y; }
main()
1003 miii 1007 mvii 1011 mxi
{
int x = 10, y = 0, k;
for(k=0; k
printf("y = %d\n", y); } }
运行结果是:
#include main ( ) { char a[] = "programming" , b[] = "language" ; char *p1, *p2 ; int i; p1 = a;
p2 = b;
for (i=0; i
printf("%c" ,*(p1+i)) ;
}
printf("\n");
}
运行结果是:
#include
main() { int a=10; {
int a=15;
printf(“a.1=%d,”,a); }
printf(“a.2=%d\n”,a); }
运行结果是:
#include main() { int i=0; while(1) {
printf(“*”); i++;
if(i
printf(“\n”); }
运行结果是:
int runc(int a,int b) {return(a+b);} main() {
int x=2,y=5,z=8,r; r=runc(runc(x,y),z);
printf(“%d\n”,r); }
执行结果是: 15
阅读以下程序并写出其运行结果 #incude main() { int i, n3, n2, n1; i=1;
while(i
if((i%3==0)&&(n2==5||n1==5))
printf("%5d", i); i++;
}
}
程序的运行结果是 15 45 51 54 57 75 阅读以下程序并写出其运行结果 main ( )
{ int a[6][6],i,j ; for (i=1; i
程序的运行结果是 10000
01000 00100 00010 00001
阅读以下程序并写出其运行结果 #include #include
struct ks { int a; int *b ; } s[4], *p ; main ( ) { int n=1, i ;
for (i=0 ; i
s[i].a = n ; s[i].b=&s[i].a ; n=n+2 ; }
p=&s[0] ; p++ ;
printf(“%d, %d\n”,(++p)->a,(p++)->a) ; }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(int *s, int nl, int n2) { int i, j, t ; i=nl; j=n2;
while(i
{ int a[10]={1,2,3,4,5,6,7,8,9,0},k; fun(a,0,3); fun(a,4,9); fun(a,0,9);
for(k=0;k
程序运行的结果是
阅读以下程序并写出其结果 func (int a,int b) { static int m=0,i=2; i+=m+1; m=i+a+b; return (m); } main ( )
{ int k=4,m=1,p1,p2;
p1=func(k,m) ; p2=func(k,m) ; printf(“%d,%d\n”,p1,p2) ; }
程序的运行结果是 8 17
阅读以下程序并写出其运行结果 struct man {
char name[20] ; int age ;
} person[ ] = { “liming”, 18, “wanghua”, 19,”zhangping”,20 } ;main ( ) { int old = 0 ;
struct man *p=person, *q ; for ( ; p
if (old
age) { q=p ; old=p->age } ; printf(“$s %d\n”,q->name,q->age) ; }
程序的运行结果是 zhongping 20
阅读以下程序并写出其运行结果
#include main() {
int x,y,z,j=0;
printf("\n The possible plans to buy 100 fowls with 100 Yuan are:\n\n"); for(x=0;x
if(z%3==0&&5*x+3*y+z/3==100) printf("%2d: cock=%2d hen=%2d chicken=%2d\n",++j,x,y,z); }
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include void fun(int x)
{ if(x/2>0) fun(x/2); prin tf(“%d ”,x); } main()
{ fun(3); printf(“\n”); }
程序的运行结果是
阅读以下程序并写出其运行结果 #define MAX(a,b) (a>b)?a:b
main() { int x, y; x=25; y=56;
printf("the result is: %d\n", MAX(x,y));
}
程序的运行结果是 没找上答案 阅读以下程序并写出其运行结果 #include #define ROWS 4 #define COLS 4
int nums[ROWS][COLS]={{1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1}};
char *roms[ROWS][COLS]={{"m","m","m","m"},
{"cm","d","cd","c"}, {"xc","l","xl","x"},
{"ix","v","iv","i"}};
main(int argc,char *argv[ ]) { int low=1000,high=1012; char roman[25]; for(;low
system("pause");
}
to_roman(int decimal,char roman[ ]) { int power,index;
roman[0]='\0';
for(power=0;power=nums[power][index]) { strcat(roman,roms[power][index]); decimal-=nums[power][index];
}
}
程序的运行结果是 没找上答案
阅读以下程序并写出其运行结果 #incude main() { int i, n3, n2, n1; i=1;
while(i
if((i%3==0)&&(n2==5||n1==5))
printf("%5d", i); i++;
}
}
程序的运行结果是 没找上答案
二.填空题,在下列代码段中的空白处填入正确的语句 以下程序求3个整数的最小公倍数,请填空 #inlcude int fun(int, tint , int ); main() {
int x1, x2, x3, i=1, j, x0; printf("input 3 integers:");
scanf("%d%d%d", &x1, &x2, &x3); x0=fun(x1, x2, x3);
while(1){
j=x0*i;
if___j%x1==0&&_j%x2==0&&j%x3==0 ______break;
i++;
}
printf("Result is %d\n",j);
}
int fun(int x, int y, int z)
{
if(x>y && x>z)
return x;
else if__y>x &&y>z__________
return y;
else
return z;
} F (x , y , z ) =
以下程序的功能是计算函数
# include
x +y z +y +x -y z -y , 请填空使程序完整。
;
main ( ) {
float x,y,z,f ;
scanf(“%f,%f,%f”,&x,&y,&z);
f = fun ( );
f += fun ( );
printf(“f=%d”,f);
}
float fun(float a,float b){
return (a/b) ;
}
下述程序用“碾转相除法”计算两个整数m 和n 的最大公约数。该方法的基本思想是计算m 和n 相除的余数,如果余数为0则结束,此时的被除数就是最大公约数。否则,将除数作为新的被除数,余数作为新的除数,继续计算m 和n 相除的余数,判断是否为0,等等,请填空使程序完整。
main ( ){
int m,n,w;
scanf(“%d,%d”,&m,&n);
while (n) {
w = m%n ;
m = n ;
n = m ;
}
printf(“%d”,m);
}
以下函数求s=∑n n k ! +
k =1∑12
k =1k 请填空
float fun(int n)
{
int i;
double s1=0, s2=_________,s3=________, s=0;
for(i=1; i
s3=s3*i;
s1=__________;
}
for(i=1; i
s2=s2+_______;
____________;
return s;
}
下面以下程序输出a, b, c三个变量中的最小值,请填空。
#include
main()
{
int a, b, c, t1, t2;
scanf("%d%d%d", &a, &b, &c);
t1=a
t2=c
printf("%d\n", t2);
}
已知如下公式:
π
2=1+1
3+1
3⨯2
5+1231234
3⨯5⨯7+3⨯5⨯7⨯
9+
下面程序的功能使根据上述公式输出满足精度要求的 eps 的π值,请填空使程序完整。
main ( ){
double s = 0.0, eps, t=1.0;
int n ;
scanf (“%lf”,&eps);
for (n=1 ; ; n++) {
s+=t ;
t= ;
}
;
}
计算1+1/2+1/4+…+1/50的值,并显示出来。
main()
{
int i=2;
float sum=1.0;
while(i
{ sum+=1/ double i ;
i+=2;
}
printf(“sum=%f\n”,sum);
}
Fibonacci 数列中的头两个数是1和1,从第三个数开始,每个数等于前两个数之和。下述程序计算此数列的前20个数,且每行输出5个数,请填空。
#include
main()
{
int f,f1=1,f2=1;
int i;
printf(“%6d,%6d”,f1,f2);
for (i=3;i
{
f= f1+f2 ;
printf(“%6d”,f);
if( !ci%5 )
printf(“\n”);
f1=f2;
f2=f ;
}
}
以下函数用于求出数组grade[]中n 个值的平均数aver ,将大于 aver 的值置于数组over 中, ,返回大于平均
数的数值的个数。请填空。
int fun(int* grade, intn, int* over)
{
}
以下程序从名为 filea.dat 的文本文件中逐个读入字符并显示在屏幕上。请填空:
#include
main()
{ FILE *fp;
char ch ;
fp=fopen( “filea.dat”, ”r ” ) ; ch=fgetc(fp) ;
whlie( !feof(fp) )
{ putchar(ch) ;
ch=fgetc(fp);
}
putchar(‘\n’);
fclose(fp);
}
下面函数的功能是将一个整数字符串转换为一个整数,例如: "1234" 转换为1234,请填空使程序完整。 int chnum(char *p){
int num=0, k, len, j ;
len = strlen(p) ;
for ( ; *p!=’10’ ; p++) {
k = *p’0’ ;
j=(--len) ;
while ( j--!=0 ) k = k*10 ;
num = num + k ;
}
return (num);
int i, j=0; flaot aver, s=0.0; for(i=0; iaver)_______=grade[i]; retrun j;
}
完成以下字符串拷贝函数(将*from串拷贝到*to串) :
void copy_string(char *from, char *to)
{
for(; *from != '\0'; __________________;)
________________________;
to[i]='\0';
}
以下程序从键盘输入10个整数到数组a , 然后依次输出10个数组元素
#include
void main()
{
int *p,i,a[10];
p=a;
for(i=0;i
scanf("%d",p++);
printf("\n");
____________________;
for(__________________________)
printf("%d ",*p);
}
所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数本身。例如,153是一个“水仙花数”,因为153=13+53+33。完成以下程序使之可打印出100~1000内的全部”水仙花数“。
main ( )
{
int i,j,k,n;
printf(““水仙花数”是:”);
for (n=100; n
{
i= n/100;; /*取百位数*/
j=___________; /*取十位数*/
k=n%10; /*取个位数*/
if (_________________________)
printf(“%4d”,n);
}
printf(“\n”);
}
以下程序用来求两整数的绝对值和。请填空。
#include
void main()
{ int x,y;
printf("Please input x,y:");
scanf("%d%d",&x,&y);
if (x
if (_________) y=-y;
printf("\n|x|+|y|=%d\n",x+y);
}
以下程序可计算1名学生10门课成绩的平均分,将缺省语句填上。
#include
float average(float array[10])
{ int i;float aver,sum=array[0];
for(i=1;i
sum += ______________;
}
void main()
{
}
以下程序中函数 fun 的功能是:统计 person 所指结构体数组中所有性别(sex)为 M 的记录的个数,存入变量 n 中,并做为函数值返回。请填空:
#include
#define N 3
typedef struct
{ int num ;
char nam[10] ;
char sex ;
} SS ;
int fun(SS person[])
{ int i, n=0 ;
for(i=0;i
return n;
float score[10],aver; int i; printf("\n input 10 scores:"); for(i=0;i
main()
{ SS W[N]={ {1,”AA”,’F’}, {2,”BB”,’M’}, {3,”CC”,’M’} };
int n;
n=fun(W);
printf(“n=%d\n”,n);
} F (x , y , z ) =x +y z +y
以下程序的功能是计算函数x -y +z -y , 请填空使程序完整。
# include
;
main ( ) {
float x,y,z,f ;
scanf(“%f,%f,%f”,&x,&y,&z);
f = fun ( );
f += fun ( );
printf(“f=%d”,f);
}
float fun(float a,float b){
return (a/b) ;
}
下面函数的功能使统计子串substr 在母串str 中出现的次数,请填空使程序完整。
int count(char *str, char *substr){
int i,j,k,num=0;
for ( i=0; ; i++)
for ( , k=0; substr[k] == str[j]; k++; j++)
if (substr [ ] ==’\0’) {
num++ ; break ;
}
return (num) ;
}
已知如下公式:
π1121231
2=1+3+3⨯5+3⨯5⨯7+3⨯234
5⨯7⨯
9+
下面程序的功能使根据上述公式输出满足精度要求的 eps 的π值,请填空使程序完整。
double s = 0.0, eps, t=1.0;
int n ;
scanf (“%lf”,&eps);
for (n=1 ; ; n++) {
s+=t ;
t= ;
}
;
}
下面这个程序功能:读入5个整数,当程序读入的数据为正整数时,则显示该数,否则,不显示。读入5个数据后,程序结束运行。
#include
main()
{
}
韩信点兵。韩信有一队兵,他想知道有多少人,便让士兵排队报数:按从1至5报数,最末一个士兵报的数为1;按从1至6报数,最末一个士兵报的数为5;按从1至7报数,最末一个士兵报的数为4;最后再按从1至11报数,最末一个士兵报的数为10。下面程序的主要功能是计算韩信至少有多少兵。
#include
main()
{
for (x=1; ________; x++) { if (x%5==1 && x%6==5 && x%7==4 && x%11==10)
int x = 1; int find = 0; for (i=1; i
} } printf(" x = %d\n", x); ____________; }
函数 squeeze(char s[], char c)的功能是删除字符串s 中所出现的与变量c 相同的字符。
void squeeze(char s[], char c){
int i, j;
for ( i = 0, j = 0; _____________; i++){
if( s[i] != c) {
__________________;
j++;
}
}
s[j] = '\0';
}
下面程序可求出矩阵a 的主对角线上的元素之和,请填空使程序完整。
main ( )
{ int a[3][3]={1,3,5,7,9,11,13,15,17} , sum=0, i, j ;
for (i=0 ; i
for (j=0 ; j
if (___________________________________)
sum=sum+____________________________;
printf(“sum=%d”,sum);
}
下面函数的功能是将一个整数字符串转换为一个整数,例如:”1234”转换为1234,请填(3)空使程序完整。 int chnum(char *p)
{ int num=0,k,len,j ;
len = strlen(p) ;
for ( ; ___________________________; p++) {
k=_______________________________; j=(--len) ;
while (__________________________) k=k*10 ;
num = num + k ;
}
return (num);
}
从键盘输入任意一个年份,判断该年是否为闰年。是则输出“Yes ”,否则输出”No ”。请填空。 #inclue
main()
{
int year, flag;
printf(“Enter year:”);
scanf(“%d”, &year);
if(__________________________)
flag = 1;
else
flag = 0;
if(___________________________)
printf(“%d is a leap year!\n”, year);
else
printf(“%d is not a leap year!\n”, year);
三.程序设计
编写函数rep(char *s,char *s1,char *s2),将s 串中出现的s1中的字符串替换为s2中相应的字符串 编写程序统计一个文本文件中字符的数量。
编写程序将一正整数序列{K1,K2,...,K9}重新排列成一个新的序列。新序列中,比K1小的数都在K1的左面(后继的再向左存放) ,比K1大的数都在K1的右面(后续的再向右存放) ,从k1向右扫描。
例:序列{6,8,9,1,2,5,4,7,3}
经重排后成为{3,4,5,2,1,6,8,9,7}
编写一个函数int findStr(char *str,char *substr),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为*str="asd asasdfg asd as zx67 asd mklo",子字符串为*substr="as",函数返回值为6。
已知在文件IN.DAT 中存有若干个(个数
(1)求出这文件中共有多少个正整数totNum ;
(2)求这些数中的各位数字之和是偶数的数的个数totCnt ,以及满足此条件的这些数的算术平均值totPjz ;
(3)把所求的结果totNum, totCnt, totPjz输出到文件OUT.DAT 中。
设计一个程序,从键盘上输入文本文件名,分别 统计该文件中所包含的英文字母(不分大小写) 、数字字符和其它字符的个数。
编写函数原型为:
void sort(int *, int )
的冒泡排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
编写函数 int strchr(str, 'd'),返回字符d 在字符串str 中第一次出现的位置 ,如果找不到则返回-1。 编写一个函数,将一个长度为N 的整型数组中所有的元素向前移动一个位置,原数组中的最前面的元素移动到最后。
编写函数原型为:
void sort(int *, int )
的选择排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
编程打印出如下形式的乘法表
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
从键盘输入20个任意整数,找出其中的最大值和最小值,并求出这20个整数的算术平均数。
从键盘输入如下矩阵,保存到一个二维数组a, 将其行列互换后保存到数组b 中,再求出这2个矩阵的主对角线上各项的和。
. 编写函数实现两个整型变量值的交换。
从键盘任意输入10个不相同的整数,然后,输入要查找的数x ,如果在这10个数中找到x ,则打印“Found! ”;
第 31 页 共 33 页 | 2 4 6 8 9 | | 1 5 8 9 0 | | 8 5 2 4 1 | | 0 3 8 7 1 | | 8 2 2 5 1 |
如果找不到,则打印“Not found!”。
从键盘任意输入某班20个学生的成绩,输出最高分并统计出不及格人数。
设当前路径下有一个名为"chang.txt" 文件,编写程序将文件中所有的小写英文字母更改为大写字母。
编写函数原型为:
void sort(int *, int )
的冒泡排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
已知2001年1月1日是星期一,编程输出2001年1月起的yyyy 年mm 月的如下形式的月历表
June 2010
sun mon
6 7 8 tue wedthu fri sat 2 3 4 5 9 10 11 12 1
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
四.填空
C 语言的数据类型有______基本类型________、__ _枚举类型_____________、_____派生类型_________和____空类型_____。
根据数据组织形式,C 语言中将文件分为_______文本文件____________和____二进制文件_____________。
在函数中未指定存储类别的局部变量,其隐含的存储类别是__ auto ______。
组成C 程序的基本单位是____函数____。
若主调用函数为double 型,被调用函数定义中没有函数类型说明,而return 语句中的表达式为float 型,则被调用函数返回值的类型是___double 型_____。
设int a=5,b=2;float c=32.8;,表达式b*(int)c%a的值为___4___。
C 语言中用____0___表示逻辑值“假”,用____1____表示逻辑值“真”。
char *p[10];这条语句的含义是:_ ___
设有语句“int a=3;”,执行语句“printf(”%d”,-a++ );”后,输出结果 是 -4 , 变量a 的值是 4 。
如果在程序中定义静态变量和全局变量时,未明确指明其初始值,那么它们可 以在程序编译阶段自动被初始化为0 。
在C 语言中,可以用 struct
定义一种新的数据类型。
五、选择题
1.在C 语言中,函数的隐含存储类别是( A )。
A) auto B) static C) extern D) 无存储类别
2.若用数组名作为函数调用时的实参,则实际上传递给形参的是(A )。
A .数组的首地址
3.已知 struct sk
{
第 32 页 共 33 页 B. 数组的第一个元素值 C .数组中全部元素的值 D. 数组元素的个数
int a;
float b;
}data,*p;
若有p=&data,则对data 中的成员a 正确引用是(B )
A. (*p).data B.(*p).a C.p->data.a D. p.data.a
4.在while (x )语句中的x 与下面条件表达式等价的是: A 。
A. x!=0 B. x==1 C. x!=1 D. x==0
5.二维数组a 有m 行n 列,则在a[i][j]之前的元素个数为( B )。
A. j*n+i B. i*n+j C. i*n+j-1 D. i*n+j+1
6.设有声明语句:int a=1,b=0;则执行以下语句后输出为:
switch (a)
{
case 1:
switch (b)
{
case 0: printf("**0**");
break;
case 1: printf("**1**");
break;
}
case 2: printf("**2**");
break;
}
A **0** B **0****2**
C **0****1****2** D 有语法错误
7.以下程序运行结果是(C )
main()
{char s[]=”Address ”;
printf(”%d\n”, strlen(s));
}
A) 7 B) 8
C) 9 D) 10
8.以下不正确的描述是(D )
A) 在函数外部定义的变量是全局变量
B) 在函数内部定义的变量是局部变量
C) 函数的形参是局部变量
D) 局部变量不能与全局变量同名
B 。
第 33 页共 33 页
一、阅读程序写出程序运行结果
阅读以下程序并写出其运行结果 #include #include int fun(char s[],int c) {
char *q=s; for(; *q; q++) if(*q != c) *(s++)=*q; *s=0; } main() { static char str[]="turbo c and borland c++"; char ch; fun(str,'c');
printf("str[]=%s\n",str); system("pause");
}
程序的运行结果是str[]=turbo and Borland ++ 阅读以下程序并写出其运行结果 #include #include
void fun(int a, int b, long *c) { *c=a/10*10+a%10*1000+b/10*100+b%10; } main()
{ int a=42,b=25; long c; fun(a, b, &c);
printf("The result is: %ld\n", c); system("pause"); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(int *s, int nl, int n2)
the result is: 2245 请按任意键继续…
{ int i, j, t ; i=nl; j=n2;
while(i
{ int a[10]={1,2,3,4,5,6,7,8,9,0},k; fun(a,0,3); fun(a,4,9); fun(a,0,9);
for(k=0;k
程序运行的结果是 5678901234
Press any key to continue 阅读以下程序并写出其运行结果 #include main() {
int i=1;
while(!((i%2==1)&&(i%3==2)&&(i%5==4)&&(i%6==5)&&(i%7==0))) ++i;
printf("\n >> The ladder has %d stages.\n",i); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include #define ROW 3 #define COL 4 main() { int matrixA[ROW][COL]={11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}; int matrixB[COL][ROW];
int i,j;
printf("%d*%d:\n",ROW,COL); for( i=0; i
for( j=0; j
matrixB[j][i] = matrixA[i][j];
}
}
printf("MatrixB,");
printf("%d*%d:\n",COL,ROW); for( i=0; i
}
printf("\n");
}
system("pause");
}
程序的运行结果是3*4
Matrix B 4*3
11 15 19 12 16 20 13 17 21 14 18 22
阅读以下程序并写出其运行结果 #include #define N 10 main() { int primes[N]; int pc,m,k;
printf("\n The first %d prime numbers are:\n",N); primes[0]=2; pc =1; m =3; while(pc
while(primes[k]*primes[k]
if(m%primes[k]==0) {
m+=2; k=1; }
else
k++;
primes[pc++]=m; m+=2; }
for(k=0;k
printf("%4d",primes[k]); system("pause");
}
程序的运行结果是 the fiest 10 prime numbers are
2 3 5 7 11 13 17 19 23 29
阅读以下程序并写出其运行结果 #include #include void fun(char *s[ ],int n) { char *t; int i,j;
for(i=0;i
if(strlen(s[i])>strlen(s[j])) {t=s[i];s[i]=s[j];s[j]=t;} } main()
{ char *ss[]={“bcc”,”bbcc”,”xy”,”aaaacc”,”aabcc” }; fun(ss,5); printf(“%s,%s\n”,ss[0],ss[4]); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(char *a, char *b) { while(*a==’*’) a++; while(*b=*a) {b++;a++;} } main()
{ char *s=”****a*b****”,t[80]; fun(s,t);
puts(t); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include main() { FILE *fp;
int a[10]={1,2,3},i,n; fp=fopen(“dl.dat”,”w”);
for(i=0;i
fp=fopen(“dl.dat”,”r”); fscanf(fp,”%d”,&n); fclose(fp);
printf(“%d\n”,n); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void swap( int *a, int *b ) { int *t ; t=a; a=b; b=t; } main()
{ int i=3,j=5,*p=&i,*q=&j;
swap(p,q); printf(“%d %d\n”,*p,*q); }
程序的运行结果是
阅读以下程序并写出其运行结果 int circle(int n, int d) { int s=0,m=n; while(m) { s=s*d+m%d; m/=d;
}
return s==n;
}
int num[]={232,27,851}; int scale[]={2,10,16}; main() { int i,j;
for(i=0;i (%d) is a Circle Number!\n",num[i],scale[j]); else
printf("%d -> (%d) is not a Circle Number!\n",num[i],scale[j]);
}
程序的运行结果是 答案太长不想写
阅读以下程序并写出其运行结果 #include #define NULL 0
int *search2(int *pa,int *pb,int an,int bn) {
int *ca,*cb; ca=pa;cb=pb;
while(ca*cb) cb++; else
return ca;/*返回在这两个数表中找到相等元素*/
}
return NULL;
}
main( ) { int *vp,i;
int a[ ]={1,3,5,7,9,13,15,27,29,37}; int b[ ]={2,4,6,8,10,13,14,27,29,37}; puts("The elements of array a is:"); for(i=0;i
printf(" %d",a[i]);
puts("\nThe elements of array b is:"); for(i=0;i
printf(" %d",b[i]);
vp=search2(a,b,sizeof a/sizeof a[0],sizeof b/sizeof b[0]);
if(vp) printf("\nThe first same number in both arrays is %d\n",*vp); else printf("Not found!\n");
}
程序的运行结果是
阅读以下程序并写出其运行结果
#include main() { int i, j; char ch; i=1; while(i
}
printf("\n"); i++;
}
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include rest(int a[], int n) { int i,low,high,t;
for(i=0,low=0,high=n-1;i0) {
t=a[i];
a[i]=a[high]; a[high]=t; high--;
}
else if(a[i]==0) i++; else
{ t=a[i]; a[i]=a[low]; a[low]=t; low++; i++; }
}
}
int s[]={8,4,0,-1,6,0,-5}; main() { int i;
printf("\n The arry before rest is:\n"); for(i=0;i
printf("%4d",s[i]); rest(s,sizeof(s)/sizeof(s[0])); printf("\n The arry after rest is:\n"); for(i=0;i
printf("%4d",s[i]);
} }
程序的运行结果是the arry before rest is 8 4 0 -1 6 0 -5 the arry after rest is -5 -1 0 0 6 4 8
阅读以下程序并写出其运行结果 #include #include
typedef struct { char name[9]; char sex; float score[2]; void f( STU a)
} STU;
{ STU b={“Zhao” ,’m’,85.0,90.0} ; int i; strcpy(a.name,b.name); a.sex=b.sex;
for(i=0;i
{ STU c={“Qian”,’p’,95.0,92.0}; f(c);
printf(“%s,%c,%2.0f,%2.0f\n”,c.name,c.sex,c.score[0],c.score[1]); }
程序的运行结果是
阅读以下程序并写出其运行结果 #include main() {
int x,y,z,j=0;
printf("\n The possible plans to buy 100 fowls with 100 Yuan are:\n\n"); for(x=0;x
if(z%3==0&&5*x+3*y+z/3==100) printf("%2d: cock=%2d hen=%2d chicken=%2d\n",++j,x,y,z); }
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include void fun(int x)
{ if(x/2>0) fun(x/2); printf(“%d ”,x); } main()
{ fun(3); printf(“\n”); }
程序的运行结果是
阅读以下程序并写出其运行结果 #define MAX(a,b) (a>b)?a:b main() { int x, y; x=25; y=56;
printf("the result is: %d\n", MAX(x,y));
}
程序的运行结果是the result is 56 阅读以下程序并写出其运行结果 #include #define ROWS 4 #define COLS 4
int nums[ROWS][COLS]={{1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1}};
char *roms[ROWS][COLS]={{"m","m","m","m"},
{"cm","d","cd","c"}, {"xc","l","xl","x"},
{"ix","v","iv","i"}};
main(int argc,char *argv[ ]) {
int low=1000,high=1012; char roman[25];
for(;low
}
system("pause");
}
to_roman(int decimal,char roman[ ]) { int power,index; roman[0]='\0';
for(power=0;power=nums[power][index]) { strcat(roman,roms[power][index]); decimal-=nums[power][index];
}
}
程序的运行结果是1000 m 1001 mi 1002 mii 1004 miv 1005 mv 1006 mvi 1008 mviii 1009 mix 1010 mx 1012 mxii
#include
int Fun(int x) {
static int y = 0; y += x; return y; }
main()
1003 miii 1007 mvii 1011 mxi
{
int x = 10, y = 0, k;
for(k=0; k
printf("y = %d\n", y); } }
运行结果是:
#include main ( ) { char a[] = "programming" , b[] = "language" ; char *p1, *p2 ; int i; p1 = a;
p2 = b;
for (i=0; i
printf("%c" ,*(p1+i)) ;
}
printf("\n");
}
运行结果是:
#include
main() { int a=10; {
int a=15;
printf(“a.1=%d,”,a); }
printf(“a.2=%d\n”,a); }
运行结果是:
#include main() { int i=0; while(1) {
printf(“*”); i++;
if(i
printf(“\n”); }
运行结果是:
int runc(int a,int b) {return(a+b);} main() {
int x=2,y=5,z=8,r; r=runc(runc(x,y),z);
printf(“%d\n”,r); }
执行结果是: 15
阅读以下程序并写出其运行结果 #incude main() { int i, n3, n2, n1; i=1;
while(i
if((i%3==0)&&(n2==5||n1==5))
printf("%5d", i); i++;
}
}
程序的运行结果是 15 45 51 54 57 75 阅读以下程序并写出其运行结果 main ( )
{ int a[6][6],i,j ; for (i=1; i
程序的运行结果是 10000
01000 00100 00010 00001
阅读以下程序并写出其运行结果 #include #include
struct ks { int a; int *b ; } s[4], *p ; main ( ) { int n=1, i ;
for (i=0 ; i
s[i].a = n ; s[i].b=&s[i].a ; n=n+2 ; }
p=&s[0] ; p++ ;
printf(“%d, %d\n”,(++p)->a,(p++)->a) ; }
程序的运行结果是
阅读以下程序并写出其运行结果 #include
void fun(int *s, int nl, int n2) { int i, j, t ; i=nl; j=n2;
while(i
{ int a[10]={1,2,3,4,5,6,7,8,9,0},k; fun(a,0,3); fun(a,4,9); fun(a,0,9);
for(k=0;k
程序运行的结果是
阅读以下程序并写出其结果 func (int a,int b) { static int m=0,i=2; i+=m+1; m=i+a+b; return (m); } main ( )
{ int k=4,m=1,p1,p2;
p1=func(k,m) ; p2=func(k,m) ; printf(“%d,%d\n”,p1,p2) ; }
程序的运行结果是 8 17
阅读以下程序并写出其运行结果 struct man {
char name[20] ; int age ;
} person[ ] = { “liming”, 18, “wanghua”, 19,”zhangping”,20 } ;main ( ) { int old = 0 ;
struct man *p=person, *q ; for ( ; p
if (old
age) { q=p ; old=p->age } ; printf(“$s %d\n”,q->name,q->age) ; }
程序的运行结果是 zhongping 20
阅读以下程序并写出其运行结果
#include main() {
int x,y,z,j=0;
printf("\n The possible plans to buy 100 fowls with 100 Yuan are:\n\n"); for(x=0;x
if(z%3==0&&5*x+3*y+z/3==100) printf("%2d: cock=%2d hen=%2d chicken=%2d\n",++j,x,y,z); }
}
程序的运行结果是
阅读以下程序并写出其运行结果 #include void fun(int x)
{ if(x/2>0) fun(x/2); prin tf(“%d ”,x); } main()
{ fun(3); printf(“\n”); }
程序的运行结果是
阅读以下程序并写出其运行结果 #define MAX(a,b) (a>b)?a:b
main() { int x, y; x=25; y=56;
printf("the result is: %d\n", MAX(x,y));
}
程序的运行结果是 没找上答案 阅读以下程序并写出其运行结果 #include #define ROWS 4 #define COLS 4
int nums[ROWS][COLS]={{1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1}};
char *roms[ROWS][COLS]={{"m","m","m","m"},
{"cm","d","cd","c"}, {"xc","l","xl","x"},
{"ix","v","iv","i"}};
main(int argc,char *argv[ ]) { int low=1000,high=1012; char roman[25]; for(;low
system("pause");
}
to_roman(int decimal,char roman[ ]) { int power,index;
roman[0]='\0';
for(power=0;power=nums[power][index]) { strcat(roman,roms[power][index]); decimal-=nums[power][index];
}
}
程序的运行结果是 没找上答案
阅读以下程序并写出其运行结果 #incude main() { int i, n3, n2, n1; i=1;
while(i
if((i%3==0)&&(n2==5||n1==5))
printf("%5d", i); i++;
}
}
程序的运行结果是 没找上答案
二.填空题,在下列代码段中的空白处填入正确的语句 以下程序求3个整数的最小公倍数,请填空 #inlcude int fun(int, tint , int ); main() {
int x1, x2, x3, i=1, j, x0; printf("input 3 integers:");
scanf("%d%d%d", &x1, &x2, &x3); x0=fun(x1, x2, x3);
while(1){
j=x0*i;
if___j%x1==0&&_j%x2==0&&j%x3==0 ______break;
i++;
}
printf("Result is %d\n",j);
}
int fun(int x, int y, int z)
{
if(x>y && x>z)
return x;
else if__y>x &&y>z__________
return y;
else
return z;
} F (x , y , z ) =
以下程序的功能是计算函数
# include
x +y z +y +x -y z -y , 请填空使程序完整。
;
main ( ) {
float x,y,z,f ;
scanf(“%f,%f,%f”,&x,&y,&z);
f = fun ( );
f += fun ( );
printf(“f=%d”,f);
}
float fun(float a,float b){
return (a/b) ;
}
下述程序用“碾转相除法”计算两个整数m 和n 的最大公约数。该方法的基本思想是计算m 和n 相除的余数,如果余数为0则结束,此时的被除数就是最大公约数。否则,将除数作为新的被除数,余数作为新的除数,继续计算m 和n 相除的余数,判断是否为0,等等,请填空使程序完整。
main ( ){
int m,n,w;
scanf(“%d,%d”,&m,&n);
while (n) {
w = m%n ;
m = n ;
n = m ;
}
printf(“%d”,m);
}
以下函数求s=∑n n k ! +
k =1∑12
k =1k 请填空
float fun(int n)
{
int i;
double s1=0, s2=_________,s3=________, s=0;
for(i=1; i
s3=s3*i;
s1=__________;
}
for(i=1; i
s2=s2+_______;
____________;
return s;
}
下面以下程序输出a, b, c三个变量中的最小值,请填空。
#include
main()
{
int a, b, c, t1, t2;
scanf("%d%d%d", &a, &b, &c);
t1=a
t2=c
printf("%d\n", t2);
}
已知如下公式:
π
2=1+1
3+1
3⨯2
5+1231234
3⨯5⨯7+3⨯5⨯7⨯
9+
下面程序的功能使根据上述公式输出满足精度要求的 eps 的π值,请填空使程序完整。
main ( ){
double s = 0.0, eps, t=1.0;
int n ;
scanf (“%lf”,&eps);
for (n=1 ; ; n++) {
s+=t ;
t= ;
}
;
}
计算1+1/2+1/4+…+1/50的值,并显示出来。
main()
{
int i=2;
float sum=1.0;
while(i
{ sum+=1/ double i ;
i+=2;
}
printf(“sum=%f\n”,sum);
}
Fibonacci 数列中的头两个数是1和1,从第三个数开始,每个数等于前两个数之和。下述程序计算此数列的前20个数,且每行输出5个数,请填空。
#include
main()
{
int f,f1=1,f2=1;
int i;
printf(“%6d,%6d”,f1,f2);
for (i=3;i
{
f= f1+f2 ;
printf(“%6d”,f);
if( !ci%5 )
printf(“\n”);
f1=f2;
f2=f ;
}
}
以下函数用于求出数组grade[]中n 个值的平均数aver ,将大于 aver 的值置于数组over 中, ,返回大于平均
数的数值的个数。请填空。
int fun(int* grade, intn, int* over)
{
}
以下程序从名为 filea.dat 的文本文件中逐个读入字符并显示在屏幕上。请填空:
#include
main()
{ FILE *fp;
char ch ;
fp=fopen( “filea.dat”, ”r ” ) ; ch=fgetc(fp) ;
whlie( !feof(fp) )
{ putchar(ch) ;
ch=fgetc(fp);
}
putchar(‘\n’);
fclose(fp);
}
下面函数的功能是将一个整数字符串转换为一个整数,例如: "1234" 转换为1234,请填空使程序完整。 int chnum(char *p){
int num=0, k, len, j ;
len = strlen(p) ;
for ( ; *p!=’10’ ; p++) {
k = *p’0’ ;
j=(--len) ;
while ( j--!=0 ) k = k*10 ;
num = num + k ;
}
return (num);
int i, j=0; flaot aver, s=0.0; for(i=0; iaver)_______=grade[i]; retrun j;
}
完成以下字符串拷贝函数(将*from串拷贝到*to串) :
void copy_string(char *from, char *to)
{
for(; *from != '\0'; __________________;)
________________________;
to[i]='\0';
}
以下程序从键盘输入10个整数到数组a , 然后依次输出10个数组元素
#include
void main()
{
int *p,i,a[10];
p=a;
for(i=0;i
scanf("%d",p++);
printf("\n");
____________________;
for(__________________________)
printf("%d ",*p);
}
所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数本身。例如,153是一个“水仙花数”,因为153=13+53+33。完成以下程序使之可打印出100~1000内的全部”水仙花数“。
main ( )
{
int i,j,k,n;
printf(““水仙花数”是:”);
for (n=100; n
{
i= n/100;; /*取百位数*/
j=___________; /*取十位数*/
k=n%10; /*取个位数*/
if (_________________________)
printf(“%4d”,n);
}
printf(“\n”);
}
以下程序用来求两整数的绝对值和。请填空。
#include
void main()
{ int x,y;
printf("Please input x,y:");
scanf("%d%d",&x,&y);
if (x
if (_________) y=-y;
printf("\n|x|+|y|=%d\n",x+y);
}
以下程序可计算1名学生10门课成绩的平均分,将缺省语句填上。
#include
float average(float array[10])
{ int i;float aver,sum=array[0];
for(i=1;i
sum += ______________;
}
void main()
{
}
以下程序中函数 fun 的功能是:统计 person 所指结构体数组中所有性别(sex)为 M 的记录的个数,存入变量 n 中,并做为函数值返回。请填空:
#include
#define N 3
typedef struct
{ int num ;
char nam[10] ;
char sex ;
} SS ;
int fun(SS person[])
{ int i, n=0 ;
for(i=0;i
return n;
float score[10],aver; int i; printf("\n input 10 scores:"); for(i=0;i
main()
{ SS W[N]={ {1,”AA”,’F’}, {2,”BB”,’M’}, {3,”CC”,’M’} };
int n;
n=fun(W);
printf(“n=%d\n”,n);
} F (x , y , z ) =x +y z +y
以下程序的功能是计算函数x -y +z -y , 请填空使程序完整。
# include
;
main ( ) {
float x,y,z,f ;
scanf(“%f,%f,%f”,&x,&y,&z);
f = fun ( );
f += fun ( );
printf(“f=%d”,f);
}
float fun(float a,float b){
return (a/b) ;
}
下面函数的功能使统计子串substr 在母串str 中出现的次数,请填空使程序完整。
int count(char *str, char *substr){
int i,j,k,num=0;
for ( i=0; ; i++)
for ( , k=0; substr[k] == str[j]; k++; j++)
if (substr [ ] ==’\0’) {
num++ ; break ;
}
return (num) ;
}
已知如下公式:
π1121231
2=1+3+3⨯5+3⨯5⨯7+3⨯234
5⨯7⨯
9+
下面程序的功能使根据上述公式输出满足精度要求的 eps 的π值,请填空使程序完整。
double s = 0.0, eps, t=1.0;
int n ;
scanf (“%lf”,&eps);
for (n=1 ; ; n++) {
s+=t ;
t= ;
}
;
}
下面这个程序功能:读入5个整数,当程序读入的数据为正整数时,则显示该数,否则,不显示。读入5个数据后,程序结束运行。
#include
main()
{
}
韩信点兵。韩信有一队兵,他想知道有多少人,便让士兵排队报数:按从1至5报数,最末一个士兵报的数为1;按从1至6报数,最末一个士兵报的数为5;按从1至7报数,最末一个士兵报的数为4;最后再按从1至11报数,最末一个士兵报的数为10。下面程序的主要功能是计算韩信至少有多少兵。
#include
main()
{
for (x=1; ________; x++) { if (x%5==1 && x%6==5 && x%7==4 && x%11==10)
int x = 1; int find = 0; for (i=1; i
} } printf(" x = %d\n", x); ____________; }
函数 squeeze(char s[], char c)的功能是删除字符串s 中所出现的与变量c 相同的字符。
void squeeze(char s[], char c){
int i, j;
for ( i = 0, j = 0; _____________; i++){
if( s[i] != c) {
__________________;
j++;
}
}
s[j] = '\0';
}
下面程序可求出矩阵a 的主对角线上的元素之和,请填空使程序完整。
main ( )
{ int a[3][3]={1,3,5,7,9,11,13,15,17} , sum=0, i, j ;
for (i=0 ; i
for (j=0 ; j
if (___________________________________)
sum=sum+____________________________;
printf(“sum=%d”,sum);
}
下面函数的功能是将一个整数字符串转换为一个整数,例如:”1234”转换为1234,请填(3)空使程序完整。 int chnum(char *p)
{ int num=0,k,len,j ;
len = strlen(p) ;
for ( ; ___________________________; p++) {
k=_______________________________; j=(--len) ;
while (__________________________) k=k*10 ;
num = num + k ;
}
return (num);
}
从键盘输入任意一个年份,判断该年是否为闰年。是则输出“Yes ”,否则输出”No ”。请填空。 #inclue
main()
{
int year, flag;
printf(“Enter year:”);
scanf(“%d”, &year);
if(__________________________)
flag = 1;
else
flag = 0;
if(___________________________)
printf(“%d is a leap year!\n”, year);
else
printf(“%d is not a leap year!\n”, year);
三.程序设计
编写函数rep(char *s,char *s1,char *s2),将s 串中出现的s1中的字符串替换为s2中相应的字符串 编写程序统计一个文本文件中字符的数量。
编写程序将一正整数序列{K1,K2,...,K9}重新排列成一个新的序列。新序列中,比K1小的数都在K1的左面(后继的再向左存放) ,比K1大的数都在K1的右面(后续的再向右存放) ,从k1向右扫描。
例:序列{6,8,9,1,2,5,4,7,3}
经重排后成为{3,4,5,2,1,6,8,9,7}
编写一个函数int findStr(char *str,char *substr),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为*str="asd asasdfg asd as zx67 asd mklo",子字符串为*substr="as",函数返回值为6。
已知在文件IN.DAT 中存有若干个(个数
(1)求出这文件中共有多少个正整数totNum ;
(2)求这些数中的各位数字之和是偶数的数的个数totCnt ,以及满足此条件的这些数的算术平均值totPjz ;
(3)把所求的结果totNum, totCnt, totPjz输出到文件OUT.DAT 中。
设计一个程序,从键盘上输入文本文件名,分别 统计该文件中所包含的英文字母(不分大小写) 、数字字符和其它字符的个数。
编写函数原型为:
void sort(int *, int )
的冒泡排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
编写函数 int strchr(str, 'd'),返回字符d 在字符串str 中第一次出现的位置 ,如果找不到则返回-1。 编写一个函数,将一个长度为N 的整型数组中所有的元素向前移动一个位置,原数组中的最前面的元素移动到最后。
编写函数原型为:
void sort(int *, int )
的选择排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
编程打印出如下形式的乘法表
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
从键盘输入20个任意整数,找出其中的最大值和最小值,并求出这20个整数的算术平均数。
从键盘输入如下矩阵,保存到一个二维数组a, 将其行列互换后保存到数组b 中,再求出这2个矩阵的主对角线上各项的和。
. 编写函数实现两个整型变量值的交换。
从键盘任意输入10个不相同的整数,然后,输入要查找的数x ,如果在这10个数中找到x ,则打印“Found! ”;
第 31 页 共 33 页 | 2 4 6 8 9 | | 1 5 8 9 0 | | 8 5 2 4 1 | | 0 3 8 7 1 | | 8 2 2 5 1 |
如果找不到,则打印“Not found!”。
从键盘任意输入某班20个学生的成绩,输出最高分并统计出不及格人数。
设当前路径下有一个名为"chang.txt" 文件,编写程序将文件中所有的小写英文字母更改为大写字母。
编写函数原型为:
void sort(int *, int )
的冒泡排序函数,要求将整型数组中的前n 个元素按从小到大的次序排序。
已知2001年1月1日是星期一,编程输出2001年1月起的yyyy 年mm 月的如下形式的月历表
June 2010
sun mon
6 7 8 tue wedthu fri sat 2 3 4 5 9 10 11 12 1
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
四.填空
C 语言的数据类型有______基本类型________、__ _枚举类型_____________、_____派生类型_________和____空类型_____。
根据数据组织形式,C 语言中将文件分为_______文本文件____________和____二进制文件_____________。
在函数中未指定存储类别的局部变量,其隐含的存储类别是__ auto ______。
组成C 程序的基本单位是____函数____。
若主调用函数为double 型,被调用函数定义中没有函数类型说明,而return 语句中的表达式为float 型,则被调用函数返回值的类型是___double 型_____。
设int a=5,b=2;float c=32.8;,表达式b*(int)c%a的值为___4___。
C 语言中用____0___表示逻辑值“假”,用____1____表示逻辑值“真”。
char *p[10];这条语句的含义是:_ ___
设有语句“int a=3;”,执行语句“printf(”%d”,-a++ );”后,输出结果 是 -4 , 变量a 的值是 4 。
如果在程序中定义静态变量和全局变量时,未明确指明其初始值,那么它们可 以在程序编译阶段自动被初始化为0 。
在C 语言中,可以用 struct
定义一种新的数据类型。
五、选择题
1.在C 语言中,函数的隐含存储类别是( A )。
A) auto B) static C) extern D) 无存储类别
2.若用数组名作为函数调用时的实参,则实际上传递给形参的是(A )。
A .数组的首地址
3.已知 struct sk
{
第 32 页 共 33 页 B. 数组的第一个元素值 C .数组中全部元素的值 D. 数组元素的个数
int a;
float b;
}data,*p;
若有p=&data,则对data 中的成员a 正确引用是(B )
A. (*p).data B.(*p).a C.p->data.a D. p.data.a
4.在while (x )语句中的x 与下面条件表达式等价的是: A 。
A. x!=0 B. x==1 C. x!=1 D. x==0
5.二维数组a 有m 行n 列,则在a[i][j]之前的元素个数为( B )。
A. j*n+i B. i*n+j C. i*n+j-1 D. i*n+j+1
6.设有声明语句:int a=1,b=0;则执行以下语句后输出为:
switch (a)
{
case 1:
switch (b)
{
case 0: printf("**0**");
break;
case 1: printf("**1**");
break;
}
case 2: printf("**2**");
break;
}
A **0** B **0****2**
C **0****1****2** D 有语法错误
7.以下程序运行结果是(C )
main()
{char s[]=”Address ”;
printf(”%d\n”, strlen(s));
}
A) 7 B) 8
C) 9 D) 10
8.以下不正确的描述是(D )
A) 在函数外部定义的变量是全局变量
B) 在函数内部定义的变量是局部变量
C) 函数的形参是局部变量
D) 局部变量不能与全局变量同名
B 。
第 33 页共 33 页