C语言指针选习题和答案.docVIP

  • 14
  • 0
  • 约7.5千字
  • 约 13页
  • 2019-05-02 发布于浙江
  • 举报
一、用指针方法编写一个程序,输入3个整数,将它们按由小到大的顺序输出 #include stdio.h void swap(int *pa,int *pb) { int temp; temp = *pa; *pa = *pb; *pb = temp; } void main() { int a,b,c,temp; scanf(%d%d%d,a,b,c); if(ab) swap(a,b); if(bc) swap(b,c); if(ac) swap(a,c); printf(%d,%d,%d,a,b,c); } 二、C语言 用指针方法 输入3个字符串 按由小到大顺序输出 #include stdio.h #include string.h int main(int argc, char* argv[]) { char *t; char *p1=NULL,*p2=NULL,*p3=NULL; char ch1[20]={0},ch2[20]={0},ch3[20]={0}; p1=ch1; p2=ch2; p3=ch3; printf(No1:); scanf(%s,p1); fflush(stdin); printf(No2:); scanf(%s,p2); fflush(stdin); printf(No3:); scanf(%s,p3); fflush(stdin); if(strcmp(p1,p2)0) {t=p1;p1=p2;p2=t;} if(strcmp(p1,p3)0) {t=p1;p1=p3;p3=t;} if(strcmp(p2,p3)0) {t=p2;p2=p3;p3=t;} printf(%s\n%s\n%s\n,p1,p2,p3); return 0; } 9.4编程输入一行文字,找出其中的大写字母,小写字母,空格,数字,及其他字符的个数 #includestdio.h void main() { int a=0,b=0,c=0,d=0,e=0,i=0; char *p,s[20]; while((s[i]=getchar())!=\n)i++; p=s; while(*p!=10) { if(*p=A*p=Z) a++; else if(*p=a*p=z) b++; else if(*p== ) c++; else if(*p=0*p=9) d++; else e++; p++; } printf(大写字母 %d 小写字母 %d\n,a,b); printf(空格 %d 数字 %d 非字符 %d\n,c,d,e); } 9.5写一个函数,将3 3矩阵转置 #include stdio.h void Transpose(int (*matrix)[3]) { int temp; int i, j; for(i=1;i3;i++)/*转置*/ { for(j=0;ji;j++) { temp = *(*(matrix+j)+i); *(*(matrix+j)+i) = *(*(matrix+i)+j); *(*(matrix+i)+j) = temp; } } } void main() { int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; Transpose(a); for(int i = 0;i3;i++) { for(int j=0;j3;j++) { printf(%d ,a[i][j]); } printf(\n); } } } 9.6用指向一维数组的指针做函数参数 #includestdio.h #includestring.h int main() { ?void sort(char (*s)[6]);//一维数组的指针做函数参数 ?int i; ?char str[10][6]; ?char (*p)[6];//定义一维数组的指针做函数参数 ?printf(please input string:/n); ?for(i=0;i10;i++) ??scanf(%s,str[i]); ?p=str;//将str一维数组指针,赋值给p; ??? sort(p); ?printf(the output sequence :/n); ?for(i=0;i10;i++) ??pri

文档评论(0)

1亿VIP精品文档

相关文档