一个函数一个宏,应该怎么改

问下各位这个要怎么改才行呢
问题是:分别用函数和带参的宏,从三个数中找出最大的数。最大的数,输出两遍,先用函数,再用宏。函数部分保留3位小数,宏保留两位小数。

img

首先,去除362行宏定义里面,多余的一个冒号和一个分号;

然后,把MAX函数的返回值改为float类型;

接着,把利用宏来寻找三个数的最大值那里,用两次宏来寻找三个数的最大值;

最后,再把利用宏寻找三个数最大值赋值给一个float变量,再来打印保留三位小数的结果。

修改如下:


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
#define bidaxiao(a,b) (a>b?a:b)  // 去除多余的一个冒号和分号

// 函数返回类型改为float类型
float MAX(float a,float b,float c){
    
    return  (float) max(max(a,b),c);
} 

int main(void){
    
    float x,y,z,m;
    cin>>x>>y>>z;
    
    m=MAX(x,y,z);
    cout<<fixed<<setprecision(3)<<m<<endl;
    
    // 这里使用宏寻找三个数的最大值,
    //可以先寻找两个数的最大值, 然后再拿这个结果与第三个数比较最大值 
    m=bidaxiao(bidaxiao(x,y),z);
    cout<<fixed<<setprecision(2)<<m<<endl;
    
    return 0;
}
 

img

双冒号 改成 冒号

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7546291
  • 你也可以参考下这篇文章:利用顺序表的操作,实现以下函数: 1)从顺序表中删除具有最小值的元素并由函数返回被删除元素的值。空出的位置由最后一个元素填补,若顺序表为空则显示出错信息并退出运行。
  • 除此之外, 这篇博客: 用二叉树孩子兄弟链式表示法表示树,求树深度(非递归算法)中的 该算法的关键是用队列存储每一层的结点,在存储下一层时要把上一层的全部出队,难度在于,如何判断上一层全部出队。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 先上代码

    Status GetDepth(CSTree T){
        if(!T) return ERROR;
        if(!T->firstchild) return 1;
        int Depth=0;
        CSTree p=T;
        LinkQueue Q; InitQueue(Q);int i=0,j=1;
        EnQueue(Q,p);
        while(!EmptyQueue(Q)){
        for(i=0;j>0;j--){//j记录该层的结点个数i,将该层所有的结点都出队,j层所有结点的下一层都入队。
            DeQueue(Q,p);
            if(p->firstchild) {p=p->firstchild;EnQueue(Q,p);i++;
            while(p->nextsibling){
                p=p->nextsibling;
                EnQueue(Q,p);
                i++;
              }
            }
          }Depth++;j=i;
        }
        return Depth;
    }
    

    算法的关键是在while(!EmptyQueue)中使用了一个for循环,i是每一层的结点个数,将i赋值给j 。每进行一次while 都将i初始化为0,然后通过j的自减将每一个结点的孩子存放到队列里,并将该层所有结点出队。每完成一层操作,depth+1。
    下面是完整代码:

    #include <stdio.h>
    #include <stdlib.h>
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -2
    #define TRUE 1
    #define FLASE 0
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    typedef int Status;
    typedef char ElemType;
    typedef struct CSNode{
        ElemType data;
        struct CSNode *firstchild,*nextsibling;
    }CSNode,*CSTree;
    typedef struct{
        CSTree *base;
        CSTree *top;
        int stacksize;
    }SqStack;
    Status InitStack(SqStack &S){
        S.base=(CSTree *)malloc(sizeof(CSNode));
        if(!S.base) exit(OVERFLOW);
        S.top=S.base;
        S.stacksize=STACK_INIT_SIZE;
        return OK;
    }
    Status Push(SqStack &S,CSTree &e){
        if(S.top-S.base>=S.stacksize){
            S.base=(CSTree *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(CSTree));
            if(!S.base) exit(OVERFLOW);
            S.top=S.base+S.stacksize;
            S.stacksize+=STACKINCREMENT;
        }
        *S.top++=e;
        return OK;
    }
    Status Pop(SqStack &S,CSTree &e){
        if(S.base==S.top) return ERROR;
        e=*--S.top;
        return OK;
    }
    Status StackEmpty(SqStack S){
        if(S.base==S.top) return OK;
        else
            return ERROR;
    }
    typedef struct QNode{
        CSTree data;
        struct QNode *next;
    }QNode,*QueuePtr;
    typedef struct{
        QueuePtr front;
        QueuePtr rear;
    }LinkQueue;
    Status InitQueue(LinkQueue &Q){
        Q.front = Q.rear =(QueuePtr)malloc(sizeof(QNode));
        if(!Q.front) exit(OVERFLOW);
        Q.front->next=NULL;
        return OK;
    }
    Status EnQueue (LinkQueue &Q,CSTree e){
        QueuePtr p;
        p=(QueuePtr)malloc(sizeof(QNode));
        if(!p) exit(OVERFLOW);
        p->data=e;
        p->next=NULL;
        Q.rear->next=p;
        Q.rear=p;
        return OK;
    }
    Status DeQueue (LinkQueue &Q,CSTree &e){
        QueuePtr p;
        if(Q.front == Q.rear) return ERROR;
        p=Q.front->next;
        e=p->data;
        Q.front->next=p->next;
        if(Q.rear == p) Q.rear=Q.front;
        free(p);
        return OK;
    }
    Status EmptyQueue(LinkQueue Q){
        if(Q.front == Q.rear) return 1;
        else
            return 0;
    }
    Status CreateCSTree(CSTree &T,SqStack &S){
        ElemType ch;
        T=(CSNode *)malloc(sizeof(CSNode));
        if(!T) exit(OVERFLOW);
        scanf("%c",&ch);
        T->data=ch;
        Push(S,T);
        while(!StackEmpty(S)){
            scanf("%c",&ch);
            if(ch!='#'){
                T->firstchild=(CSNode *)malloc(sizeof(CSNode));
                T->firstchild->data=ch;
                Push(S,T->firstchild);
            }
            else
                T->firstchild=NULL;
            scanf("%c",&ch);
            if(ch!='#'){
                T->nextsibling=(CSNode *)malloc(sizeof(CSNode));
                T->nextsibling->data=ch;
                Push(S,T->nextsibling);
            }
            else
                T->nextsibling=NULL;
            Pop(S,T);
        }
        return OK;
    }
    Status GetDepth(CSTree T){
        if(!T) return ERROR;
        if(!T->firstchild) return 1;
        int Depth=0;
        CSTree p=T;
        LinkQueue Q; InitQueue(Q);int i=0,j=1;
        EnQueue(Q,p);
        while(!EmptyQueue(Q)){
        for(i=0;j>0;j--){//j记录该层的结点个数i,将该层所有的结点都出队,j层所有结点的下一层都入队。
            DeQueue(Q,p);
            if(p->firstchild) {p=p->firstchild;EnQueue(Q,p);i++;
            while(p->nextsibling){
                p=p->nextsibling;
                EnQueue(Q,p);
                i++;
              }
            }
          }Depth++;j=i;
        }
        return Depth;
    }
    int main(){
        CSTree T;
        SqStack S;
        InitStack(S);
        printf("Create a Tree :\n");
        CreateCSTree(T,S);
        printf("Get the depth of the Tree!\n");
        printf("The depth of the tree is: %d\n",GetDepth(T));
    }
    
  • 您还可以看一下 CSDN讲师老师的异构大数据平台,让多源异构数据融合贯通!课程中的 异构大数据平台,让多源异构数据融合贯通-下小节, 巩固相关知识点