首页 > 代码库 > 作业九 指针+总结

作业九 指针+总结

计算两数的和与差

#include<stdio.h>
void sum_diff(float op1,float op2,float *psum,float *pdiff);

int main(void)
{
    float op1,op2,sum,diff;
    printf("input op1 and op2:");
    scanf("%f%f",&op1,&op2);
    sum_diff(op1,op2,&sum,&diff);
    printf("%f+%f=%f,%f-%f=%f\n",op1,op2,sum,op1,op2,diff);
    
    return 0;
} 
void sum_diff(float op1,float op2,float *psum,float *pdiff)
{
    *psum=op1+op2;
    *pdiff=op1-op2;
}

技术分享

 

利用指针找最大值

#include<stdio.h>
void findmax(int *px,int *py,int *pmax);

int main()
{
    int x,y,max;
    printf("input x and y:");
    scanf("%d%d",&x,&y);
    findmax(&x,&y,&max);
    printf("%d\n",max);
    
    return 0;
}
void findmax(int *px,int *py,int *pmax)
{
    if(*px<*py)
        *pmax=*py;
    else
        *pmax=*px;
}

技术分享

 

拆分实数的整数与小数部分

#include<stdio.h>
void splitfloat(float x,int *intpart,float *fracpart);

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;
}
void splitfloat(float x,int *intpart,float *fracpart)
{
    if(x<0)
        x=-x;
    *intpart=(int)x;
    *fracpart=x-*intpart;
}

技术分享

 

 

一、知识点

1.

 

作业九 指针+总结