福建省C语言精选习题集
- 格式:doc
- 大小:95.50 KB
- 文档页数:17
第一部分:选择题
1.以下程序段的运行结果是(A)。
int a=1;
printf("%d, %d, %d\n", a, ++a, a++); 从右往左运算
A.3,3,1
B. 1, 2, 2
C. 1, 2, 3
D. 3, 2, 1
2.以下程序段执行后p的值是(A)。
int a[3][3]={3,2,1,3,2,1,3,2,1};
int j,k,p=1;
for(j=0;j<2;j++)
for(k=j;k<4;k++) p*=a[j][k];
A.108
B. 18
C. 12
D. 2
3.用数组名作为函数的实参时,错误的说法是(B)。
A.定义形参数组时,元素的个数必须与实参相同
B.可以使用数组名作为形参
C.实参传递给形参的值是数组的首地址
D.可以使用指针变量作为形参
4.以下程序段的运行结果是(D )。
union
{ int n;
char str[2];
}t;
t.n=80;
t.str[0]='a';
t.str[1]=0;
printf("%d\n", t.n);
A.80
B. a
C.0
D.97
5.定义结构体类型变量teach1,不正确的是(D)。
A.struct teacher
{ int num;
int age;
};
struct teacher teach1;
B.struct teacher
{ int num;
int age;
}teach1;
C.struct
{ int num;
int age;
}teach1;
D.struct
{ int num;
int age;
}teacher;
struct teacher teach1;
6.若有定义:
struct stuent
{int num;
char sex;
int age;
}stu1;
下列叙述不正确的是(A)。
A.student是结构体类型名
B.struct student是结构体类型名
C.stu1是用户定义的结构体类型变量名
D.num,sex,age都是结构体变量stu1的成员
7.设有如下语句:
struct stu
{int num;
int age;
};
struct stu s[3]={{101,18},{102,21},{103,19}};
struct stu *p=s;
则下面表达式的值为102的是(B)。
A.(p++)->num
B.(*++p).num
C.(*p++).num
D.*(++p)->num
8.若有下面定义,对结构体变量成员不正确引用的语句是(B)。
struct pup
{char name[20];
int age;
int sex;
}p[3],*q;
q=p;
A.scanf("%s",p[0].name);
B.scanf("%d",q->age);
C.scanf("%d",&(q->sex));
D.scanf("%d",&p[0].age);
9.错误的枚举类型定义语句是(A)。
A.enum car {A, B, C};
B.enum car {1, 2, 3};
C.enum car {X=0, Y=5, Z=9};
D.enum car {D=3, E, F};
10.以下程序的功能是(C)。
#include <stdio.h>
main()
{ FILE *fp;
long int n;
fp=fopen("wj.txt","rb");
fseek(fp,0,SEEK_END);
n=ftell(fp);
fclose(fp);
printf("%ld",n);
}
A.计算文件wj.txt的起始地址
B.计算文件wj.txt的终止地址
C.计算文件wj.txt的长度
D.将文件指针定位到文件末尾
11.当顺利执行了文件关闭操作时,fclose函数的返回值是(B)。
A. 1
B.0
C.-1
D.一个非0值
第二部分:改错题
1. 程序Cmody051.C,其功能是统计输入字符串中大写英文字母的个数。
如输入:abcDEFGH123
输出:5
#include <stdio.h>
#include <string.h>
main( )
{
/**/ char str1 /**/; char str1[50] 数组长度足够就行
int i,len, sum=0;
printf("Please input a string:\n");
scanf("%s", str1);
len = strlen(str1);
for(i=0; i<len; i++)
{
if( str1[i] >= 'A' && str1[i] <= 'Z')
/**/ sum--; /**/ sum++;
}
printf("%d\n",sum);
getch( );
}
2. 程序Cmody062.C,其功能是将程序中的两个字符串“ABC”、“xyz”连接在一起,并输出“ABCxyz”。
#include <stdio.h>
#include <string.h>
void main( )
{
char s1[12]="ABC", s2[]="xyz";
char * ps1=s1,*ps2;
/**/ ps2 = NULL; /**/ ps2=s2;
/**/ while(*ps1 == NULL) /**/while(*ps1!=NULL)
ps1++;
while(*ps2) *(ps1++) = *(ps2++);
printf("%s\n",s1);
getch( );
}
3.程序Cmody122.C 中函数void chg(char *s),实现将字符串中所有偶数位置上的小写
英文字母转换为大写英文字母(不是英文字母不转换)。
#include <stdio.h>
void chg(char *s)
{int i,n=0;
char *p=s;
while(*p)
{n++;
p++;
}
for (i=0;i<n;i++)
/**/ if ((s[i]>='a' && s[i]<='z') || i%2==0) /**/ if ((s[i]>='a' && s[i]<='z') && i%2==0) s[i]=s[i]-32;
/**/ s[i]='0'; /**/s[i]='\0';
}
void main( )
{
char ss[100];
printf("Enter string: \n");
gets(ss);
chg(ss);
printf("\nNow string is :\n");
puts(ss);
getch( );
}
第三部分:填空题
1.补充程序Ccon013.C,程序实现从10个整数中找出最大值和最小值。
#include <stdio.h>
#include <stdlib.h>
int max,min;
void find_max_min(int *p,int n)
{
int *q;
max=min=*p;
for(q=p; q</**/ p+n /**/; q++)
if(/**/ max<*q /**/ ) max=*q;
else if(min>*q) min=*q;
}
void main( )
{
int i,num[10];
printf("Input 10 numbers: ");
for(i=0;i<10;i++) scanf("%d",&num[i]);
find_max_min(/**/ num /**/,10);
printf("max=%d,num=%d\n",max,min);
}
2. 补充程序Ccon02
3.C,该程序中可测试歌德巴赫猜想:从键盘上输入一个大于6的偶数,
总能找到两个素数,使得这两个素数之和正好等于该偶数。
#include <stdio.h>
#include <conio.h>
int prime(int n)
{ int k,flag=1;
for(k=2; k<=n/2+1; k++)
if (n%k==0) { flag=/**/ 0 /**/ ; break;}
return flag;
}
void main( )
{ int num, a;
clrscr( );
do
{ printf("Please input an even number:");
scanf("%d", &num);
}while(num<=6||num%2==1);
for(a=2;a<=num/2+1;a++)
if(prime(a) && prime(/**/ num-a /**/))
printf("\n %d = %d + %d ", num, a, num-a);
}
3. 补充程序Ccon033.C,其中main函数通过调用average函数计算数组元素的平均值。
#include <stdio.h>
float average(int *pa,int n)
{
int k;
/**/ float avg=0.0;/**/
for(k=0;k<n;k++)
avg = avg+/**/ pa[k] /**/;
avg = avg/n;
return avg;
}
void main( )
{ int a[5]={20,30,45,64,23};
float m;
m=average(/**/a/**/, 5);
printf("Average=%f\n",m);
}
4. 补充程序Ccon052.C,使程序中的sort ( )函数用选择法对数组a中的m个元素从小到大
排序。
#include <stdio.h>
#include <math.h>
void sort(int a[], int m)
{ int i, j, k, t;
for( i = 0; i < m-1; i++ )
{ k = i;
for( j=i+1; j< /**/ m/**/; j++)
if( a[j] < a[k] ) /**/ k=j /**/;
if( k != i )
{ t = a[k];
a[k]= a[i];
a[i] = /**/ t /**/;
}
}
}
void main( )
{
int a[] = {72,25,58,32,2,15,7,64};
int i,m = sizeof(a)/sizeof(int);
sort(a,m);
for(i=0;i<m;i++)
printf("%d ",a[i]);
printf("\n");
getch( );
}
5.补充程序Ccon083.C,实现将结构体数组mystudent中存储的各学生信息按其学号的升序排列。
#include<stdio.h>
#include<string.h>
typedef struct{
int num;
char name[20],sex[2];
int age,score;
}STU;
STU mystudent[]={
{1111,"Zhangqiang","m",20,80},
{2104,"Liminghong","w",18,82},
{3121,"Wangxingda","m",21,78},
{4118,"Liushaotao","m",20,90},
{1456,"Wuminghong","w",35,86}
};
void sort(STU* ps, int size)
{
int i,flag,pass;
/**/ STU temp; /**/
for(pass=1;pass<size;pass++)
{
flag=0;
for(i=0;i<size-pass;i++)
if(/**/ ps[i].num>ps[i+1].num /**/) {
flag+=1;
temp=ps[i];ps[i]=ps[i+1];ps[i+1]=temp;
}
if(/**/ flag= =0 /**/) break;
}
}
void main( )
{
int i,size=sizeof(mystudent)/sizeof(STU);
clrscr( );
printf("Students\' information before sort:\n\n");
printf("Number Name age Sex score\n\n");
for(i=0;i<size;i++)
printf("%-7d%s%10d\t%s%8d\n",(mystudent+i)->num,
(mystudent+i)->name,(mystudent+i)->age,
(mystudent+i)->sex, (mystudent+i)->score);
sort(mystudent,size);
printf("\nStudents\' information after sort:\n\n");
printf("Number Name age Sex score\n\n");
for(i=0;i<size;i++)
printf("%-7d%s%5d\t %s%7d\n",(mystudent+i)->num,
(mystudent+i)->name,(mystudent+i)->age,
(mystudent+i)->sex,(mystudent+i)->score);
getch( );
}
6.补充程序Ccon0310.c ,使其实现用递归算法求平方根。
求平方根的迭代公式如下:
100
1()2a x x x =+ #include <math.h>
main( )
{
double x,y;
/**/ double mysqrt(double,double); /**/
printf("Please input x:\n");
scanf("%lf",&x);
y=mysqrt(x,1.0);
printf("The sqrt of %f=%f\n",x,y);
getch( );
}
double mysqrt(double a,double x0)
{
double x1,y;
x1=/**/ (x0+a/x0)/2.0; /**/
if(fabs(x1-x0)>0.00001)
y=mysqrt(/**/ a,x1 /**/);
else
y=x1;
return(y);
}
7.将程序Ccon123.c 填写完整,函数fun ()返回公式!!()!
m p n m n =
-的值,其中m 、n 为整数,且0m n >≥。
#include <stdio.h>
double fun(unsigned m,unsigned n)
{
unsigned i;
double y;
/**/ y=1.0; /**/
for(i=1;i<=m;i++)
y=y*i;
for(i=1;i<=n;i++)
/**/ y=y/i; /**/
for(i=1;i<=m-n;i++)
y=y/i;
return /**/ y/**/;
}
void main()
{
clrscr();
printf("fun(13,8)=%f\n",fun(13,8));
getch();
}
第四部分:编程题
1. 打开考生文件夹中的Cprog01
2.C,完成其中的函数fun2(int a[ ], int n, int b[ ], int c[ ]),实现:
(1)将数组a中大于-20的元素,依次存放到数组b中;
(2)将数组b中的元素按照从小到大的顺序存放到数组c中;
(3)函数返回数组b中的元素个数。
#include <string.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
int fun2(int a[],int n,int b[],int c[])
{
/**/ int j = 0;
int i,t,k;
for(i=0;i<n;i++)
if(a[i]>-20) { c[j]=b[j]=a[i]; j++; }
for(i=0;i<j-1;i++)
for(k=i+1;k<j;k++)
if(c[i]>c[k])
{ t=c[i]; c[i]=c[k]; c[k]=t; }
return j;/**/
}
void main( )
{ int n = 10, i, nb;
int aa[10] = {12, -10, -31, -18, -15, 50, 17, 15, -20, 20};
int bb[10], cc[10];
clrscr( );
printf("There are %2d elements in aa.\n", n);
printf("They are: ");
for(i=0; i<n; i++) printf("%6d", aa[i]);
printf("\n");
nb = fun2(aa, n, bb, cc);
printf("Elements in bb are: ");
for (i=0; i<nb; i++) printf("%6d", bb[i]);
printf("\n");
printf("Elements in cc are: ");
for(i=0; i<nb; i++) printf("%6d", cc[i]);
printf("\n");
printf("There are %2d elements in bb.\n", nb);
}
2. 打开考生文件夹中的Cprog022.C,完成其中的函数fun2(char a[ ], char b[ ], char c[ ]),实现:将三个字符串a、b、c从小到大排序后输出。
注意:字符串比较函数为strcmp(str1,str2),字符串赋值函数为strcpy(str1,str2)。
#include <string.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
void fun2(char a[],char b[],char c[])
{
/**/ char str[100];
if (strcmp(a,b)>0)
{ strcpy(str,a);strcpy(a,b);strcpy(b,str);}
if(strcmp(b,c)>0)
{ strcpy(str,b);strcpy(b,c);strcpy(c,str);}
if(strcmp(a,b)>0)
{ strcpy(str,a);strcpy(a,b);strcpy(b,str);}/**/
}
void main( )
{ char str1[15]="Fuzhou",str2[15]="Fujian",str3[15]="China";
clrscr( );
fun2(str1,str2,str3);
printf("The ordered strings is : %s, %s, %s\n",str1,str2,str3);
getch( );
}
3. 打开程序Cprog072.C ,完成其中的fun ( )函数,该函数将4阶矩阵A 的各行中0之前的所有正数依次存放到数组b 中,并返回这些正数之和。
如矩阵A 为
1234012131421230243132330--⎡⎤⎢⎥--⎢⎥⎢⎥--⎢⎥--⎣⎦
则调用函数fun( )后,b[0]为1,b[1]为2,b[2]为23,b[3]为32,函数返回58。
#include<stdio.h>
#define ROW 4
#define COL 4
int fun(int a[][COL],int row,int b[])
{
/**/ int x=0,i,j,k=0;
for(i=0;i<row;i++)
for(j=0;j<COL;j++)
if(a[i][j]>0) b[k++]=a[i][j];
else if (a[i][j]==0) break;
for(i=0;i<k;i++)
x=x+b[i];
return x;/**/
}
void main( )
{
int sss=0, b[16]={0};
int a[ROW][COL]={{1,2,-3,-4},{0,-12,-13,14},{-21,23,0,-24},{-31,32,-33,0}};
clrscr( );
sss=fun(a,ROW,b);
printf("Sum of positive elements is %d\n",sss);
getch( );
}
4. 打开程序Cprog082.C ,完成其中的fun ( )函数,该函数将以指针数组的形式存放的n 个串升序排序。
(提示:字符串复制函数是strcpy(char *, char *),字符串比较函数是strcmp(char *, char *) )
#include <string.h>
#include <stdio.h>
void f(char p[][20],int n);
void main( )
{
int i;
char p[][20]={"abc","xabdfg","abbd","dcdbe","cd"};
f(p,5);
clrscr( );
for(i=0;i<5;i++)
printf("%s\n",p[i]);
getch( );
}
void f(char p[][20],int n)
{
/**/ char t[20];
int i, j;
for(i=1; i<n; i++)
for(j=0; j<n-i; j++)
if(strcmp(p[j], p[j+1])>0)
{strcpy(t,p[j]); strcpy(p[j],p[j+1]); strcpy(p[j+1],t); }
/**/
}
5. 打开程序Cprog092.C,完成其中的strcmp1 ( )函数,该函数实现判别两字符串str1和str2的大小。
#include <stdio.h>
int strcmp1(const char* str1,const char* str2)
{
/**/while(*str1||*str2)
{
if(*str1-*str2)
return *str1-*str2;
else
{ str2++;
str1++;
}
}
return 0;/**/
}
void main( )
{
int com;
char* ps1="uvwx",*ps2="uvwxyz";
clrscr( );
com=strcmp1(ps1,ps2);
if(com>0)
printf("%s>%s",ps1,ps2);
if(com==0)
printf("%s=%s",ps1,ps2);
if(com<0)
printf("%s<%s",ps1,ps2);
getch( );
}
6. 打开程序Cprong102.c,完成其中的fun( )函数,该函数的功能是:将已按升序排列好的数组a和已按降序排列好的数组b中的所有元素按降序存入数组c中。
#define N 6
#define M 9
void fun(int a[],int b[],int c[])
{
/**/ int i,j,k,t;
for(i=0;i<M;i++)
c[i]=b[i];
for(i=0;i<N;i++)
c[i+M]=a[i];
for(i=0; i<N+M; i++)
{ k = i;
for (j=i+1; j<N+M; j++)
if ( c[k]<c[j] ) k = j;
if (k!=i)
{ t=c[i]; c[i]=c[k]; c[k]=t; }
} /**/
}
main( )
{
int a[N]={2,5,8,10,18,24},b[M]={96,88,70,36,24,18,11,10,2};
int c[N+M],n;
fun(a,b,c);
printf("The result is: ");
for(n=0;n<N+M;n++)
printf("%d ",c[n]);
getch( );
}
7. 打开考生文件夹中的Cprog111.c ,完成其中的函数fun ,该函数的数学表达式是:
⎪⎩⎪⎨⎧>----==--1/))()1()()12((101)(21n n x P n x P x n n x
n x P n n n
例如:当x=5.2,n=6时,函数的值为4.724444。
#include <stdio.h>
void main( )
{
double x;int n;
double fun(double x,int n);
printf("Please enter x,n:");
scanf("%lf%d",&x,&n);
printf("fun=%lf\n",fun(x,n));
getch( );
}
double fun(double x,int n)
{ /**/ double y;
if(n==0)
y=1;
else if(n==1)
y=x;
else if(n>1)
y=((2*n-1)*x-fun(x,n-1)-(n-1)*fun(x,n-2))/n;
return(y);/**/
}
8. 打开程序Cprog112.c ,完成其中的fun 函数,该函数返回数组a 中的次小数(即仅 大于最小数的数),设数组a 中没有重复的元素。
#include <stdio.h>
#define ROW 9
int find(int a[])
{
/**/ int i,j,k,t;
for (i=0; i<ROW; i++)
{ k = i;
for (j=i+1; j<ROW; j++)
if ( a[k]<a[j] ) k = j;
if (k!=i)
{ t=a[i]; a[i]=a[k]; a[k]=t; }
}
return(a[ROW-2]);/**/
}
void main()
{
int arra[ROW]={25,19,4,15,26,11,30,28,58};
int min_pre,i;
min_pre=find(arra);
printf("array arra is:");
for(i=0;i<ROW;i++)
printf("%d ",arra[i]);
printf("\nmin_pre is %d",min_pre);
getch();
}
9.打开程序Cprog122.c,完成其中的fun函数:将二维数组a表示的矩阵的各元素进行本行循环右移指定位数后存入数组b中。
----------Cprog122.C---------------------------------------------------------------------------------
#include <stdio.h>
#define ROW 2
#define COL 5
void fun(int a[][COL],int b[][COL],int m)
{
/**/ int i,j;
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
if(j-m<0)
b[i][j]=a[i][COL-m+j];
else
b[i][j]=a[i][j-m];/**/
}
void main()
{ int arra[ROW][COL] = {{6,7,8,9,0},{1,2,3,4,5}};
int arrb[ROW][COL];
int i,j,k;
clrscr();
printf("Sites of moving: "); scanf("%d",&k);
fun(arra,arrb,k);
printf("The array arra is:\n"); for(i=0; i<ROW; i++)
{
for(j=0;j<COL;j++)
printf("%6d", arra[i][j]);
printf("\n");
}
printf("The array arrb is:\n"); for(i=0; i<ROW; i++)
{
for(j=0;j<COL;j++)
printf("%6d", arrb[i][j]);
printf("\n");
}
getch();
}。