方法一:函数调用
#include <stdio.h>
int max(int x,int y){
return x > y ? x : y;
}
int min(int x,int y){
return x < y ? x : y;
}
void main(){
int a,b;
scanf("%d,%d",&a,&b);
printf("max=%d\n",max(a,b));
printf("min=%d\n",min(a,b));
}
方法二:指针实现
#include <stdio.h>
int main(){
int *p1,*p2,*p,a,b;
scanf("%d,%d",&a,&b);
p1=&a;
p2=&b;
if(a<b){
p=p1;
p1=p2;
p2=p;
}
printf("max=%d\n",*p1);
printf("min=%d\n",*p2);
return 0;
}
方法三:指针函数调用
#include <stdio.h>
void swap(int *p1,int *p2){
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int main(){
int a,b;
int *pointer_1,*pointer_2;
scanf("%d,%d",&a,&b);
pointer_1=&a;
pointer_2=&b;
if(a<b)
swap(pointer_1,pointer_2);
printf("max=%d\n",a);
printf("min=%d\n",b);
return 0;
}
方法四:考察“指向函数的指针变量”
#include <stdio.h>
int max(int x,int y){
int z;
if(x>y)
z=x;
else
z=y;
return z;
}
int min(int x,int y){
int z;
if(x<y)
z=x;
else
z=y;
return z;
}
int main(){
int (*p)(int,int); //定义指向函数的指针变量
int a,b,n1,n2;
scanf("%d,%d",&a,&b);
p=max;
n1=(*p)(a,b);
printf("max=%d\n",n1);
p=min;
n2=(*p)(a,b);
printf("min=%d",n2);
return 0;
}