中国矿业大学上机实践.doc
《中国矿业大学上机实践.doc》由会员分享,可在线阅读,更多相关《中国矿业大学上机实践.doc(7页珍藏版)》请在装配图网上搜索。
实验九、指针 一、 实验目的 1. 通过实验进一步掌握指针的概念,会定义和使用指针变量; 2. 能正确使用数组的指针和指向数组的指针变量; 3. 能正确使用字符串的指针和指向字符串的指针变量; 4. 了解指向指针的指针的概念及其使用方法。 二、 实验内容: 1. 输入3个整数,按由小到大的顺序输出,然后将程序改为:输入3个字符串,按由小到大顺序输出。 2. 将一个3*3的矩阵转置,用以函数实现之。在主函数中用scanf函数输入以下矩阵元素: 1 3 7 7 9 11 13 15 19 将数组名作为函数实参,在执行函数的过程中实现矩阵转置,函数调用结束后在主函数中输出已转置的矩阵。 三、 源程序: 9_1_1 #include "stdio.h" void main() { void swap(int * p1,int * p2); int n1,n2,n3; int * p1,* p2,* p3; printf("input three integer n1,n2,n3:"); scanf("%d,%d,%d",&n1,&n2,&n3); p1=&n1; p2=&n2; p3=&n3; if (n1>n2) swap (p1,p2); if (n1>n3) swap (p1,p3); if (n2>n3) swap (p2,p3); printf("Now,the order is : %d,%d,%d\n",n1,n2,n3); } void swap(int * p1,int * p2) { int p; p=* p1;* p1=* p2; * p2=p; } 9_1_2 #include "stdio.h" #include "string.h" void main() { void swap(char * ,char *); char str1[50],str2[50],str3[50]; printf("input three line :\n"); gets(str1); gets(str2); gets(str3); if (strcmp(str1,str2)>0) swap(str1,str2); if (strcmp(str1,str3)>0) swap(str1,str3); if (strcmp(str2,str3)>0) swap(str2,str3); printf("Now,the order is :\n"); printf("%s\n%s\n%s\n",str1,str2,str3); } void swap(char * p1,char * p2) { char p[50]; strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p); } 9_2 #include "stdio.h" void main() { void move(int * pointer); int a[3][3],* p,i; printf("input matrix:\n"); for(i=0;i<3;i++) scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]); p=&a[0][0]; move(p); printf("Now,matrix:\n"); for (i=0;i<3;i++) printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]); } void move(int * pointer) { int i,j,t; for(i=0;i<3;i++) for(j=i;j<3;j++) { t= * (pointer+3 * i+j); * (pointer+3 * i+j)= * (pointer+3 * j+i); * (pointer+3 * j+i)=t; } } 四、 实验结果: 9_1_1 input three integer n1,n2,n3:1351,1505,1534 Now,the order is : 1351,1505,1534 Press any key to continue 9_1_2 input three line : I study very hard. C language is very interesting. He is a professfor. Now,the order is : C language is very interesting. He is a professfor. I study very hard. Press any key to continue 9_2 input matrix: 1 2 3 4 5 6 7 8 9 Now,matrix: 1 4 7 2 5 8 3 6 9 Press any key to continue 五、 实验体会: 通过本事本实验进一步掌握了指针的概念,学会了定义和使用指针变量,能够正确使用数组的指针和指向数组的指针变量,能正确使用字符串的指针和指向字符串的指针变量,受益匪浅!!!- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中国 矿业大学 上机 实践

链接地址:https://www.zhuangpeitu.com/p-7908020.html