c - C++ 中的® (\256) 字符

标签 c

C 语言中的 ® 是什么意思,在此代码中由于 ® 给出了一个错误,即 [Error] 在打印部分的程序中偏离 '\256'。和我 想翻译 c++ 代码。与此同时,我采取了 [Error] 从 'void*' 到 'node*' 的无效转换 [-fpermissive]

#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    #define MAX 10
    #define EMPTY -1

    struct node
    {
        char kind;
        char op;
        int number;
        struct node *left,*right;
    };

    struct stack
    {
        struct node *data[MAX];
        int top;
    };

    int isempty(struct stack *s)
    {
        return (s->top == EMPTY) ? 1 : 0;
    }

    void emptystack(struct stack* s)
    {
        s->top=EMPTY;
    }

    void push(struct stack* s, struct node *item)
    {
        if(s->top == (MAX-1))
        {
            printf("\nSTACK FULL");
        }
        else
        {
            ++s->top;
            s->data[s->top]=item;

        }
    }

    struct node* pop(struct stack* s)
    {
        struct node *ret=NULL;
        if(!isempty(s))
        {
            ret= s->data[s->top];
            --s->top;
        }
        return ret;
    }

    void postfix2exptree(char* postfix, struct node **root)
    {
        struct stack X;
        struct node *newnode,*op1,*op2;
        char numberextract[5];
        char *p;

        emptystack(&X);
        p = &postfix[0];
        strcpy(numberextract,"");
        while(*p)
        {

            while(*p == ' ' || *p == '\t')
            {
                p++;
            }

            if(isdigit(*p))
            {
                while(isdigit(*p))
                {
                    strcat(numberextract,p);
                    p++;
                }

                newnode = malloc(sizeof(struct node));
                newnode->kind = 'N';
                newnode->number = atoi(numberextract);
                newnode->left = NULL;
                newnode->right = NULL;
                push(&X,newnode);
                strcpy(numberextract,"");
            }
            else
            {
                op1 = pop(&X);
                op2 = pop(&X);
                newnode = malloc(sizeof(struct node));
                newnode->kind = 'O';
                newnode->op = *p;
                newnode->left = op2;
                newnode->right = op1;
                push(&X,newnode);
            }
            p++;
        }
        *root = pop(&X);
    }

    int evaluatetree(struct node *x)
    {
       if( x->kind == 'O' )
        {
          int op1 = evaluatetree( x->left );
          int op2 = evaluatetree( x->right );
          switch ( x->op )
            {
             case '+':  return op1 + op2;
             case '-':  return op1 - op2;
             case '*':  return op1 * op2;
             case '/':  return op1 / op2;
             default:   return 0;
          }
       }
        else
           return (x->number);
    }

    void inorder(struct node *x)
    {
        if(x != NULL)
        {
            inorder(x->left);

            if(x->kind == 'O')
                printf("%c ",x->op);
            else
                printf("%d ",x->number);

            inorder(x->right);
        }
    }

    void preorder(struct node *x)
    {
        if(x != NULL)
        {
            if(x->kind == 'O')
                printf("%c ",x->op);
            else
                printf("%d ",x->number);

            preorder(x->left);
            preorder(x->right);
        }
    }

    void postorder(struct node *x)
    {
        if(x != NULL)
        {
            postorder(x->left);
            postorder(x->right);

            if(x->kind == 'O')
                printf("%c ",x->op);
            else
                printf("%d ",x->number);
        }
    }

    int main()
    {
        struct node *r;
        postfix2exptree("100 50 - 2 /",&r);
        printf("Inorder = ");
        *inorder®;
        printf("\nPreorder = ");
        preorder®;
        printf("\nPostprder = ");
        postorder®;
        printf("\nResult = %d\n",evaluatetree®);
        return 0;
    }

最佳答案

这是“智能”文本编辑器的产物。

代码应该是这样的:

inorder(r);
printf("\nPreorder = ");
preorder(r);

其中 r 是一个 struct node *

原作者贴了文字没检查,智能文字编辑把(r)变成了版权标志。

请注意,inorderpreorder 是代码中较早定义的函数。

关于c - C++ 中的® (\256) 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43080264/

相关文章:

c++ - 比较整数提升后的结果

c - 使用 C 返回数组

c - 在 C 中的 Windows 上中断 Sleep()

c - 将数组成员按升序排列的程序

c - 我使用 malloc(0) 作为指向 char 的指针,并且我的代码有效。这是为什么?

c - 使用结构在 C 中获取变量的错误值

c - 返回 C 中的局部变量混淆

c++ - 重新分配已存储在目标变量中的一个值是否会导致重写和更长的运行时间?

c - GCC 将字符串常量存储在哪里以及这些指针从何处映射?

c - 检测c中的字符