葛成功之路 2020-03-23 21:33 采纳率: 87.5%
浏览 191
已采纳

请问一下为什么我的双精度数无法比较大小?

输出结果有四个不说,还一个都不对

#include<iostream> 
#include<math.h>
using namespace std;
int max(int a,int b) {
    if (a>=b)
    return a;
else if (a<b)
    return b; 
}
int max(int a,int b,int c){
    int d;{
    if(a>=b)
    d=a;
    else if(a<b)
    d=b;}
    if(d>=c)
    return d;
    else if(d<c) 
    return c;
    } 
    double max(double a,double b){ 
    if (fabs(a-b)>=0.00001)
    return a;
    else if (fabs(a-b)<0.00001)
    return b; 
    } 
double max(double a,double b,double c){
    double d;{
    if(fabs(a-b)>=0.00001)
    d=a;
    else if(fabs(a-b)<0.00001)
    d=b;}
    if(fabs(d-c)>=0.00001)
    return d;
    else if(fabs(d-c)<0.00001) 
    return c;
}
int main(){
    int m,n;
    cin>>m>>n;
    cout<<"the max is "<<max(m,n)<<endl;
    int e,r,t; 
    cin>>e>>r>>t;
    cout<<"the max is "<<max(e,r,t)<<endl;
    double y,u;
    cin>>y>>u;
    cout<<"the max is "<<max(y,u)<<endl;
    double i,o,p;
    cin>>i>>o>>p;
    cout<<"the max is "<<max(i,o,p)<<endl;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-23 23:44
    关注

    abs(a-b)>=0.00001
    这个是判断ab是否(近似)相等,相等<=0.00001,不相等>=0.00001
    fabs是绝对值的意思。
    这个不能判断大小,正确的程序我给你了。

    你按照我的回答,这么写,并没有任何问题

    #include<iostream> 
    #include<math.h>
    using namespace std;
    int max(int a,int b) {
        if (a>=b)
            return a;
        else if (a<b)
            return b; 
    }
    int max(int a,int b,int c){
        int d;{
            if(a>=b)
                d=a;
            else if(a<b)
                d=b;}
        if(d>=c)
            return d;
        else if(d<c) 
            return c;
    } 
    double max(double a,double b){ 
        if (a>b)
            return a;
        else if (a<=b)
            return b; 
    } 
    double max(double a,double b,double c){
        double d;{
            if(a>b)
                d=a;
            else if(a<=b)
                d=b;}
        if(d>c)
            return d;
        else if(d<=c) 
            return c;
    }
    int main(){
        int m,n;
        cin>>m>>n;
        cout<<"the max is "<<max(m,n)<<endl;
        int e,r,t; 
        cin>>e>>r>>t;
        cout<<"the max is "<<max(e,r,t)<<endl;
        double y,u;
        cin>>y>>u;
        cout<<"the max is "<<max(y,u)<<endl;
        double i,o,p;
        cin>>i>>o>>p;
        cout<<"the max is "<<max(i,o,p)<<endl;
        return 0;
    }
    
    

    1
    2
    the max is 2
    3
    6
    2
    the max is 6
    1.8
    1.4
    the max is 1.8
    2.7
    2.9
    1.6
    the max is 2.9
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题