无忧小子 发表于 2012-6-27 07:26:10

中石油华东12春《C语言》作业答案

中石油华东12春《C语言》作业答案
中石油华东12春《C语言》课程综合复习资料
一、单选题
1. 在C语言中,字符型数据在内存中的存储形式是
    A)原码       B)补码      C)反码      D)ASCII码
2. 在C语言中,十进制数47可等价地表示为
A)2f      B)02f       C)57       D)057
3. 设有定义:int x=12,n=5; 则表达式 x%=(n%2) 的值为
A)0      B) 1       C) 2      D) 3
4. 设有定义语句:char str[]={,"Beijing","中国石油大学"},*p=str;
则printf("%d\n",strlen(p+20)); 输出结果是
A)10      B) 6       C) 0       D) 20
5. 已定义以下函数:   fun(int *p)
{return *p;}
该函数的返回值是
A)不确定的值                  B)形参p所指存储单元中的值
C)形参p中存放的值            D)形参p的地址值
6. C语言中,函数返回值的类型是由
A)return语句中的表达式类型决定
B)调用函数的主调函数类型决定
C)调用函数时的临时类型决定
D)定义函数时所指定的函数类型决定
7. 有以下函数定义:
       voidfun( intn , doublex ){ …… }
   若以下选项中的变量都已正确定义并赋值,则对函数fun的正确调用语句是
   A) fun( inty , doublem );    B) k=fun( 10 , 12.5 );
C) fun( 10 , 12.5 );      D) voidfun( 10 , 12.5 );
8. 以下选项中不能正确赋值的是
A)charb[]={′H′,′e′,′l′,′l′,′o′,′!′};
B)charb;b="Hello!";
C)charb= "Hello!";
D)char*str="Hello!";
9. 若有以下定义:char s= "programming",*ps=s;则不能代表字符g的表达式是
A) ps+3      B)s       C) ps       D) ps+=3,*ps
10. 当对两个字符串进行比较时,应该使用的函数是
A) strcat       B) strcmp      C) strcpy      D) strlen
11. 若i为整型变量,则以下循环的次数是
for(i=2;i==0;)printf(“%d”,i--);
A) 无限次      B) 0次      C) 1次      D) 2次
12. 以下关于数组的描述正确的是
A) 数组大小固定,但是可以有不同类型的数组元素
B) 数组大小可变,但是所有数组元素的类型必须相同
C) 数组大小固定,所有元素的类型必须相同
D) 数组大小可变,可以有不同类型的数组元素
13.以下能正确定义数组并正确赋初值的语句是
    A)int N=5,b;                  B)int a={{1},{3}};
    C)int c[]= {{1,2},{3,4}};      D)int d={{1,2},{34}};
14.设有定义int a[ ]={1,5,7,9,11,13}, *p=a+3; 则*(p-2) , *(a+4) 的值是
A)511   B)19   C)59   D)有错误
15.已知char b,*p=b; ,则正确的赋值语句是
A)b=“abcd” ;   B)*b=“abcd”;    C) p=“abcd”;   D)*p=“abcd”;
16. 用数组名作为函数调用时的实参,则实际传递给形参的是
A)数组的第一个元素值      B)数组中全部元素值
C)数组的首地址            D)数组的元素个数
17. 以下叙述中不正确的是
A)在不同的函数中可以使用相同名字的变量
B)函数中的形式参数是局部变量
C)在一个函数内定义的变量只在本函数范围内有效
D)在一个函数内的复合语句中定义的变量在本函数范围内有效
18. 当对两个字符串进行比较时,应该使用的函数是
A) strcat       B) strcmp       C) strcpy      D) strlen
19. 有如下定义:longm;charc;floatx;doubley;则表达式c+m*x/y的值的类型是
A) long      B) char    C) float    D) double
20. 假设已定义 charc= "test";inti; 则下面的输出函数调用中错误的是
A) printf("%s",c);          B) for(i=0;i<8;i++)printf("%c",c);
C) puts(c)                   D) for(i=0;i<8;i++)puts(c);
21. 若有以下定义:char s= "programming",*ps=s;则不能代表字符g的表达式是
A) ps+3       B) s      C) ps    D) ps+=3,*ps
22. 以下选项中不能正确赋值的是
A) charb[]={′H′,′e′,′l′,′l′,′o′,′!′};
B) charb;b="Hello!";
C) charb= "Hello!";
D) char*str="Hello!";
23. 有以下函数定义:
            voidfun( intn , doublex ){ …… }
   若以下选项中的变量都已正确定义并赋值,则对函数fun的正确调用语句是_________。
   A) fun( inty , doublem );      B)k=fun( 10 , 12.5 );
C) fun( 10 , 12.5 );         D)voidfun( 10 , 12.5 );
24.从变量的作用域来分,可分为全局变量和局部变量,形参是
A)局部变量   B)全局变量      C) 静态变量   D) 动态变量
二、读程序,写出程序的执行结果
1.#include <stdio.h>
void main()
{int x[]={1,2,3};
   int s,i,*p=x;
   s=1;
   for(i=0;i<3;i++)
      s*=*(p+i);
   printf(“%d\n”,s);
}
2. #include <stdio.h>
char fun(char *s)
{ if( *s >= ?a? && *s <= ?z? )
*s=*s-32;
return *s;
}
void main()
{ char a="Welcome",*p,ch;
for(p=a;*p!=?\0?;p++)
{ ch=fun(p);
putchar(ch);
}
}
3. #include <stdio.h>
void main()
{ int x=26587,x1,r,sum=0;
x1=x;
while(x>0)
{ r=x%10;
sum+=r;
x/=10;
}
printf( "Sum of the digits in %d is %d", x1,sum);
}
4.#include <stdio.h>
int fun(int x)
{ int y=1;
static int z=4;
z+=1;++y;
return(x+y+z);
    }
void main()
{ int i;
for(i=1;i<=3;i++)
printf("%3d",fun(i));
}
5.#include <stdio.h>
void main()
   {int x=3,y=4;
      int j;
for(j=1 ; y>0 ; y -- )j=j*x;
printf(“j=%d\n”,j);
}
运行结果:
6.#include <stdio.h>
void main()
{ int x,y;
    for(y=1,x=1; y<=20;y++)
    {if(x>=8) break;
       if(x%2==1){ x+=5;continue ; }
       x-=3;
    }
   printf("x=%d, y=%d\n",x,y);
}
运行结果:
7. #include <stdio.h>
void main()
{ charx[]="language";
char *ptr=x;
while(*ptr)
{printf("%c\n",*ptr-32);
   ptr++;
}
}
运行结果:
8. #include <stdio.h>
   void main()
   {int x[]={5,6,7};
      int s=1,i,*p=x;
      for(i=0;i<3;i++)
      s*=*(p+i);
      printf(“%d\n”,s);
}
运行结果:
9.#include <stdio.h>
int fun(int x)
   {return(x>0 ? x : -x);
}
void main()
{int a=12;
   printf(“%d, %d\n”,a,fun(a));
}
运行结果:
10. #include <stdio.h>
void ex( )
{static int x=3 ;
--x ;
printf(“%d”,x) ;
}
voidmain ( )
{ex( );
ex( );
}
运行结果:
11.#include <stdio.h>
void main()
{ int i=0,a=0;
while( i<20 )
{for(;;)
if((i%10)= =0) break;
else i- -;
         i+=11;
         a+=i;
}
printf("%d\n",a);
}
12. #include <stdio.h>
void main()
{ int a={{1,2,3,4},{9,7,10,6},{-1,8,-5,5}};
int i,j,row=0,colum=0,max;
max=a;
for(i=0;i<=2;i++)
for(j=0;j<=3;j++)
   if(a>max)
   { max=a;
    row=i;
    colum=j;
   }
printf("max=%d,row=%d,colum=%d",max,row,colum);
}
13.#include <stdio.h>
int f(int b[], int m,int n)
    { int i,s=0;
      for(i=m;i<n;i+=2) s+=b ;   
return s;
}
      void main()
   { int x, a[]={1,2,3,4,5,6,7,8,9,10};
       x=f(a,3,8);
       printf("%d\n",x);
}
14. #include <stdio.h>
void swap(intb[])
{   int *p,*p1,*p2;
      p1=&b ;p2=&b;
p=p1;p1=p1+1;p2=p;
}
void main()
{   int a[]={5,9};
printf("%d,%d\n",*a,*(a+1));
       swap(a);
         printf("%d,%d\n",a,a);
}
15.#include <stdio.h>
intb=3;
int fun(int *a)
{b+=*a;
return(b);
}
void main()
{int a=2, b=2;
b+=fun(&a);
printf(“%d\n”,b);
}
16.#include <stdio.h>
void main()
   { int a,*p,*s,i;
      for(i=0;i<10;i++)scanf(%d”,a+i);
      for(p=a,s=a;p-a<10;p++)if(*p>*s)s=p;
       printf(“max=%d,index=%d\n”,*s, s-a);
}
简述上列程序完成的功能:
17. #include <stdio.h>
void main()
{ int x,y;
for(y=1,x=1; y<=20;y++)
   {if(x>=8) break;
if(x%2==1){ x+=5;continue ; }
      x-=3;   
}
printf(“x=%d, y=%d\n”,x ,y);
   }
18. #include <stdio.h>
#define N 3
void zz(int x)
{int i,j,t;
for(i=0;i<N;i++)
for(j=0;j<i;j++)
{ t=x; x=x; x=t; }   
}         
void main()
{intstr={1,2,3,4,5,6,7,8,9}, i,j;
   zz(str);
for(i=0;i<N;i++)
{for(j=0;j<N;j++) printf(“%3d”,str) ;
printf("\n") ;
      }
}
19.#include <stdio.h>
void main ( )
{chara= "abcXYZ", c ;
   inti,j;
j= strlen(a)-1 ;
         for (i=0;j>i;i++,j--)
{c=*(a+i); *(a+i)=*(a+j); *(a+j)=c;}
         puts(a);   
}
20. #include <stdio.h>
int a=100,b=200;
voidf( )
{ printf(“%d,%d\n”,a,b);
a=1;b=2;
}
void main()
{int a=5,b=7 ;
   f( );
printf(“%d,%d\n”, a,b) ;
}
21. #include <stdio.h>
int d=1 ;
int fun(int p)
{ static int d=3 ;
      d+=p;
      printf("%3d" , d) ;
       return(d) ;
}
void main( )
{ printf("%3d\n" , fun(2+fun(d))) ;
}
22.#include <stdio.h>
void main()
{char x[]="123456789",*p=x ;   
   int i=0;
   while(*p)
    {if(i%2==0) *p='*’;
       p++; i++;
    }
puts(x);
}
三、编程题
1.编程输入实数x,计算下面函数的值,并输出y值。
         x 2          x<1
    y=    3x-1      1 ? x ? 10
            x > 10
2.编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,并输出统计结果。
3. 编写打印如下图形的程序 (必须用循环语句实现)
*
* *
* * *
* * * *
* * * * *
4. 编写程序,从键盘上任意输入20个整数,存入一个数组中,然后输出这些数中最大数及其下标以及最小数及其下标
5.编程打印下列的杨辉三角形。(设8行)
      1
      11
      121
      133   1
      146   4   1
      15101051
6.求分段函数的值   
7.编程打印以下图形 (要求用循环控制语句实现)
            &
            & &
          & & &
      & & & &
      & & & & &
    & & & & & &

《C语言》课程综合复习资料答案
一、单选题:
1D2D3A4A5D6B7C8B9A10B11B12C 13D 14A15C16C17D18B19D20D21A22B23C   24A
二、读程序写出程序执行结果:
1、6
2、WELCOME
3、Sum of the digits in 26587 is 28
4、8 10 12
5、j=81      
6、x=8,y=4      
7、L
A
N
G
U
A
G
E
8、210       9、12,12         10、21
11、32      12、max=10,row=1,colum=2      
13、18      14、5,9       15、7         16、查找数组中最大值并记录元素下标
17、x=8,y=4
18、147                   19、ZYXcba
258                   20、5,7
369                   21、5,12,12
22、*2*4*6*8*
三、编程题
1、#include <stdio.h>
void main()
{
int x,y;
scanf(“%d”,&x);
if(x<1)
   y=x*x;
else
       if(x>=1&&x<=10)y=3*x-1;
       else y=x/5;
    printf(“x=%d,y=%d\n”,x,y);
}
2. #include<stdio.h>
#include<conio.h>
void main()                     /*主函数*/
{
char c;                            /*定义变量*/
int m=0,n=0,a=0,b=0;
printf("请输入任意字符串.\n");   /*输入提示部分,提示输入*/
while((c=getchar())!='\n')       /*循环语句,将由键盘输入的非'\n'字符赋值给c*/
{
   if((c>=65&&c<=90)||(c>=97&&c<=122))    /*第一个选择语句,统计英文字符个数*/
   {
    m=m+1;
   }
         else if(c==32)                        /*第二个选择语句,统计空格字符的个数*/
   {
   n=n+1;
   }
   else if(c>=48&&c<=57)                        /*第三个选择语句,统计数字字符的个数*/
   {
    a=a+1;
   }
   else                                        /*统计其他字符的个数*/
    b=b+1;
}
printf("%d%d%d%d",m,a,n,b);                  /*输出最后统计结果*/
getch();                                    /*输出结果后屏幕停留*/
}
3. #include<stdio.h>
main( )
{
int n,i,j;
printf(“n=%d\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++) Printf(“”);
for(j=0;j<i+1;j++) Printf(“*”);
printf(“\n”);
}
4. #include<stdio.h>
void main( )
{
int a,i,max,min,num1,num2;
for(i=0;i<20;i++)
{printf(“input the number%d:”,i);
scanf(“%d”,&a);
printf(“\n”);
}
for(i=0;i<20;i++)
{
If(a<= a)
   {min=a;
Num1=i;
}
   Else
   {max=a;
num2=i;
}
}
printf(“min=%d,num1=%d\n”,min,num1);
printf(“max=%d,num2=%d\n”,max,num2);}
5. #include <stdio.h>
#define N 11
void main()
{
int i,j,a;            /*数组为11行11列,0行0列不用*/
for(i=1;i<N;i++)
{
a=1;                  /*使第1列元素的值为1*/
a=1;                  /*使对角线元素的值为1*/
}
   for(i=3;i<N;i++)            /*从第3行开始处理*/
   for(j=2;j<=i-1;j++)
      a=a+a;
for(i=1;i<N;i++)            /*输出数组各元素的值*/
{for(j=1;j<=i;j++)
   printf(“%6d”,a);
    printf(“\n”);
}
printf(“\n”);
}
6.7略
转载请注明奥鹏作业答案网www.ap5u.com
页: [1]
查看完整版本: 中石油华东12春《C语言》作业答案