您好,请问如何写一个函数,让该函数有参数有flag!

#include <stdio.h>
void main()
{

int fun1(int x,int y);
int fun2(int x,int y);

void fun(bool flag);
int flag;
int a,b,c;
printf("请输入两个整数:\n");
scanf("%d%d",&a,&b);

if (flag) 
 { 
(*fun1)(a,b); 
c=fun1(a,b);
printf("max=\n",c);

 else 
 { 
(*fun2)(a,b); 
c=fun2(a,b);
printf("a=%d,b=%d\n",c);

}
}
int fun1(int x,int y)
{
int max;
if(x>y) max=x; 
else max=y;
return (1);
}
int fun2(int x,int y)
{
int t;
t=x;
x=y;
y=t;
return 0;
}

慕容森
浏览 289回答 2
2回答

心有法竹

修改后:#include <stdio.h>int fun1(int x,int y);int fun2(int x,int y);int fun(bool flag,int a,int b);void main(){bool flag=false;int a,b,c;printf("请输入两个整数:\n");scanf("%d%d",&a,&b);c=fun(flag,a,b);printf("max=%d\n",c);flag = true;c=fun(flag,a,b);printf("max=%d\n",c);}int fun(bool flag,int a,int b){if(flag){return fun1(a,b);}else{return fun2(a,b);}}int fun1(int x,int y){int max;if(x>y) max=x;else max=y;return (1);}int fun2(int x,int y){int t;t=x;x=y;y=t;return 0;}

守着星空守着你

typedef int (*fun_ptr)(int,int);int fun1(int x, int y){...return 0;}int fun2(int x, int y){....return 0;}void fun(bool flag){fun_ptr ptr;ptr = flag ? fun1 : fun2;ptr(x,y);}
打开App,查看更多内容
随时随地看视频慕课网APP