第七章自测练习答案 清华大学出版社 C语言习题、实验指导及课程设计
时间:2025-04-20
时间:2025-04-20
第七章
一.选择题
二.填空题
三 .编程题 (1)
#include <stdio.h> void main()
{ int x,y,a,b;int *ptr1,*ptr2; x=12; y=4;
ptr1=&x;ptr2=&y;
a=*ptr1**ptr2-6;
b=(4*(-*ptr2))/(*ptr1)+10;
printf("x=%d,y=%d,a=%d,b=%d,ptr1=%u,ptr2=%uptr1=%d,ptr2=%d\n",x,y,a,b,ptr1,ptr2,*ptr1,*ptr2);
}
x=12,y=4,a=42,b=9,ptr1=1245052,ptr2=1245048,ptr1=12,ptr2=4 (2)
#include <stdio.h>
void exchange(int *x,int *y) { char t;
t=*x; *x=*y; *y=t; }
void main() { int i,j;
scanf("x=%d,y=%d",&i,&j); exchange(&i,&j); printf("x=%d,y=%d",i,j);
,
}
(3) 20, 求两个数的最大值 (4) void main() {
int a,b,c;
int *p1=&a,*p2=&b,*p3=&c; scanf("%d%d%d",&a,&b,&c); if(a>b) swap(p1,p2); if(a>c) swap(p1,p3); if(b>c) swap(p2,p3); printf("%d,%d,%d",a,b,c); }
(5)
#include <stdio.h>
int fun(char *p1,char *p2){ int i=0; while((*p1!='\0')&&(*p2!='\0')){ }
if (*p1==*p2){p1++;p2++;} else {i=*p1-*p2;break;}
}
return i;
void main() {
char s1[100],s2[100]; gets(s1); gets(s2);
printf("%d",fun(s1,s2)); }
(6)
#include <stdio.h>
float fun(float *tt,float *max,float *min){ float i,avg=*tt;
*max=*tt; *min=*tt; for(i=1;i<10;i++) if(*max<*tt) *max=*tt; if(*min>*tt) *min=*tt; tt++;
{avg+=*tt;
} {
float ff[10]={10,2,5,8,7,4,21,5,8,17}; float max,min; }
printf("max=%.2f,min=%.2f,avg=%.2f",max,min,fun(ff,&max,&min)); }
return avg/10;
void main()