C 语言程序设计实验教学( 8)【实验目的】 指针是 C 语言中非常重要的一章内容。通过实验让学生掌握各类指针的定义与引用、指针作为函数参数的应用、字符指针的应用、函数指针的应用。【实验要求】 使用指针来完成变量的引用,编写程序将指针作为参数进行传递,理解指针的含义。【实验课时】 10.0【实验内容】1. 上机编程实现用函数来将从键盘上输入的三个数按由小到大的顺序输出。要求编写自定义函数swap() 用于交换两个变量的值; 且函数原型为 :void swap(int *p1,int *p2);;并在 main 函数中 用指针变量作为实际参数调用函数 swap() ;最后输出排序后的结果。#include main() {void swap(int *p1,int *p2); int n1,n2,n3; int *p1,*p2,*p3; printf("Input three integers 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;} 运行结果如下:Input three integers n1,n2,n3: 34,21,25↙Now, the order is: 21,25,34 2. 上机编程实现用函数来将从键盘上输入的三个字符串按由小到大的顺序输出。要求编写自定义函数swap() 用于交换两个变量的值;且函数原型为:void swap(char *p1,char *p2);;并在 main 函数中 用字符数组名作为实际参数 调用函数 swap() ;最后输出排序后的结果。#include #include main() {void swap (char *p1,char*p2); char str1[20],str2[20],str3[20]; printf("Input three strings:\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[20]; strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p);} 运行结果如下:Input three lines: I study very hard. ↙C language is very interesting.↙He is a professor. ↙Now, the order is: C language is very interesting. ...