c语言程序阅读题目(带解释).doc
阅读程序模拟题
1.下面程序执行结果是
#include
void swap(int *,int *);
void main()
{
int a=5, b=7, *ptr1, *ptr2;
ptr1=&a, ptr2=&b
swap (ptr1,ptr2);
printf(“*ptr1=%d,*ptr2=%d\n”,*ptr1,*ptr2);
printf(“a=%d,b=%d\n”,a,b);
}
void swap (int *p1, *p2)
{
int p;
p=*p1;
*p1=*p2;
*p2=p;
}
2.输入we are students ,下面程序运行结果3
#include *(求输入的单词数)
void main()
{ char string[81];
int i,num=0,word=0;
char c;
gets(string);
for(i=0;(c=string[i])!='\0';i++)
if(c==' ') word=0;
else if(word==0)
{
word=1;
num++;
}
printf(" %d\n",num);
}
3.下面程序的运行结果是 2 4 5 7 6 0 11 9 7 3
#include (求已输入数据的反置式子)void inv(int x[], int n);
void main()
{ int i,a[10]={3,7,9,11,0,6,7,5,4,2};
inv(a,10);
printf("The array has been reverted:\n");
for(i=0;i<10;i++)
printf("%d,",a[i]);
}
void inv(int x[], int n)
{ int t,i,j,m=(n-1)/2;
for(i=0;i<=m;i++)
{
j=n-1-i;
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
4.下面程序的运行结果是char *a=I am a teacher
char *b=I am a teacher #include
void copy_string(char *,char *);
void main()
{ char *a="I am a teacher.";
char *b="You are a student.";
copy_string(a,b);
printf("\nstring_a=%s\nstring_b=%s\n",a,b);
}
void copy_string(char *from,char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
}
5.下面程序的运行的功能是BASIC
Computer
FORTRAN
Follow me
Great Wall
(按从小到大排列单词)
#include
#include
void sort(char *name[],int );
void print(char *name[],int);
void main()
{
char *name[]={"Follow me","BASIC",
"Great Wall","FORTRAN","Computer "};
int n=5;
sort(name,n);
}
void sort(char *name[],int n)
{ char *temp;
int i,j,k;
for(i=0;i<n-1;i++)< p="">
{ k=i;
for(j=i+1;j<n;j++)< p="">
if(strcmp(name[k],name[j])>0(若换成<0则是按从大到小)) k=j;
if(k!=i)
{
temp=name[i];basic语言解释程序属于什么
name[i]=name[k];
name[k]=temp;}
}
}
void print(char *name[],int n)
{
int i;
for(i=0;i<n;i++)< p="">
puts(name[i]);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论