1.计算两数的和与差。要求自定义一个函数
#include<Stdio.h> void sum_diff(float op1,float op2,float *psum,float *pdiff) { *psum=op1+op2; *pdiff=op1-op2; } int main() { float op1,op2,*psum,*pdiff,sum,diff; printf("请输入两个实数:"); scanf("%f%f",&op1,&op2); sum_diff(op1,op2,&sum,&diff); printf("sum=%.2f diff=%.2f",sum,diff); return 0; }
测试截图:
2.利用指针找最大值:要求自定义一个函数
#include<stdio.h> void findmax( int *px,int *py,int *pmax) { if(*px>*py) *pmax=*px; if(*py>*px) *pmax=*py; } int main() { int *px,*py,a,b,max; printf("请输入两个数:"); scanf("%d%d",&a,&b); px=&a; py=&b; findmax(px,py,&max); printf("max=%d",max); return 0; }
测试截图:
3.拆分实数的整数与小数部分:要求自定义一个函数
#include<Stdio.h> void splitfloat (float x, int *intpart,float *fracpart) { int y; float z; y=x/1; z=x-y; *intpart=y; *fracpart=z; } int main() { float xs,x,z,*intpart,*fracpart; int y,zs; printf("请输入 x:"); scanf("%f",&x); splitfloat(x,&zs,&xs); printf("intpart=%d fracpart=%.2f",zs,xs); return 0; }
测试截图:
时间: 2024-10-16 22:50:15