首页 > 代码库 > 作业十

作业十

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("Input op1 and op2:");
    scanf("%f%f",&op1,&op2); 
    sum_diff(op1,op2,&sum,&diff); 
    printf("sum=%f,diff=%f",sum,diff);
    
    return 0;
} 

测试截图:

技术分享

2.利用指针找最大值:要求自定义一个函数

#include<stdio.h> 
void findmax(int *px,int *py,int *pmax)
{
    if(*px<*py)
        *pmax=*py;
    else
        *pmax=*px;
}


int main()
{
    int x,y,max;
    printf("Input x和y:");
    scanf("%d%d",&x,&y);
    findmax(&x,&y,&max);
    printf("%d\n",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 x,fracpart;
    int intpart;
    printf("Input x:");
    scanf("%f",&x);
    splitfloat(x,&intpart,&fracpart);
    printf("the intpart is:%d\n",intpart);
    printf("the fracaprt is:%f\n",fracpart);
 
    return 0;
} 

测试截图:

技术分享

 

作业十