一、 选择题
1. 设已定义“int a, * p”,下列赋值表达式中正确的是 :C)p=&a
2. 设已定义“int x,*p=&x;”,则下列表达式中错误的是:B)&*x
3. 若已定义“int a=1 ,*b=&a;”,则“printf(“%d \n”,*b);”的输出结果为:A)
a的值。
4. 设已定义“int x,*p,*pl=&x,*p2=&x;”,则下列表达式中错误的是:C)p=p1+p2.
5. 设有函数定义“void p(int *x){printf(“%d\n”,*x);}”和变量定义“int a=3;”,则正确的函数调用是:C)p(&a)
6. 函数“int fun(char * x){char * y=x; while(*y)y++;return(y-x); }”的功能是A)求字符串的长度。
7. 运行一下程序,输出结果为:B)5 6
int fun (int a,int *b)
{
a++;(*b)++;
return a+*b;
}
void main()
{int x=1,y=2;
Printf(“%d”,fun(x,&y));
Printf(“%d”,fun(x,&y));
}
8. 运行以下程序,输出结果为:C)58
#include<stdio.h>
Int * fun(int a ,int *b)
{a++;(*b)++;
*b=a+*b;
return b;
}
Void main()
{
Int x=1,y=2,*z;
Z=fun(x,&y);
Printf(“%d”,*z);
Z=fun(x,&y);
Printf(“%d”,*z);
}
9. 若已定义“int a[]={1,2 ,3,4},*p=a;”,则下面表达式中值不等于2的是C)*(++a)
10. 若已定义“int a[]={1,2 ,3,4},*p=a+1;”,则p[2]的值为C)4
11. 设已定义“int x[4][10],*p=x[0];”,则下列表达式中的值为整形的是B)*(p+1)
12. 设已定义“char s[]=”ABCD”;”,”printf(“%s”,s+1)”的值为C)BCD
13. 设已定义“char str[]=”abcd”,*ptr=str;”,则*(prt+4)的值为B)0
14. 下面对字符串变量的初始化或赋值操作中,错误的是C)char a[10];a=”OK”;
15. 设已定义“char *ps[2]={“abc”,”1234”};”,则以下叙述中错误的是A)ps为指针变量,它指向一个长度为2的字符串数组
16. 设已定义“struct {int a,b;} s,*ps=&s;”,则错误的结构体成员引用是C)*ps.a
17. 设已有以下定义,则表达式的值为2的是A)k=++p->data
struct st {
int data;
st *link;
} a[3]={1,a+1,3,a+2,5,0},*p=a;
二、 编程题
c语言程序设计教材答案1. 输入3个字符串,输出其中最大的字符串(用字符指针)
#include <stdio.h>
#include <stdlib.h>
#define str_count 3
#define str_length 100
int main(int argc, char *argv[])
{
char a[str_count][str_length],*p;
printf("请输入3个字符串:");
int i;
for(i=0;i<str_count;i++)
{
scanf("%s",a[i]);
}
p=a[0];
for(i=1;i<str_count;i++)
{
if(strcmp(p,a[i])<0)
{
p=a[i];
}
}
printf("最大的字符串为:%s",p);
system("PAUSE");
return 0;
}
2. 定义一个函数,函数的功能是求已知半径的圆的周长和面积。要求把半径、周长和面积设置成函数参数。
#include <stdio.h>
#include <stdlib.h>
#define pi 3.1415926
int main(int argc, char *argv[])
{
double r;
printf("请输入半径:");
scanf("%lf",&r);
double c,s;
void circle(double r, double *,double *);
circle(r,&c,&s);
printf("圆的周长和半径分别为:%lf,%lf",c,s);
system("PAUSE");
return 0;
}
void circle(double r,double *c,double *s)
{
*c=2*pi*r;
*s=pi*r*r;
}
3. 定义函数max,函数参数为3个字符串,函数返回值最大的字符串。
#include <stdio.h>
#include <stdlib.h>
#define str_length 100
#define str_count 3
int main(int argc, char *argv[])
{
char a[str_count][str_length];
printf("请输入三个字符串:\n");
int i;
for(i=0;i<str_count;i++)
{
scanf("%s",a[i]);
}
char * max(char * a1,char *a2,char *a3);
printf("最大的字符串是:%s",max(a[0],a[1],a[2]));
system("PAUSE");
return 0;
}
char * max(char * a1,char *a2,char *a3)
{
char * p;
p=a1;
if(strcmp(p,a2)<0)
{
p=a2;
}
if(strcmp(p,a3)<0)
{
p=a3;
}
return p;
}
4. 自己定义字符串复制函数,然后调用之
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *s="test";
char *o;
char * str_copy_self(char *s);
o=str_copy_self(s);
printf("%s",o);
system("PAUSE");
return 0;
}
char * str_copy_self(char *s)
{
int str_length=strlen(s);
char *o=(char *)malloc((str_length+1)*sizeof(char));
int i;
for(i=0;i<str_length;i++)
{
o[i]=s[i];
}
o[i]='\0';
return o;
}
5. 定义一个函数,函数参数为一维数组(用指针表示),函数返回数组元素的平均值。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double arr[]={1,2,3,4,5,6,7,8,9,10};
double ave(double arr[],int arr_len);
printf("%lf",ave(arr,10));
system("PAUSE");
return 0;
}
double ave(double arr[],int arr_len)
{
int i;
double res=0;
for(i=0;i<arr_len;i++)
{
res+=arr[i];
}
res=res/arr_len;
return res;
}
6. 定义一个函数,删除字符串中第k个字符开始的m个字符,例如删除字符串abcde第2个字符开始的3个字符,则删除后结果为ae;又如删除字符串abcde第4个字符开始的5个字符,则删除后结果为abc;
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论