简体   繁体   English

反向功能一直给我错误

[英]reverse function keeps giving me errors

This is a function that calls to arrays in a stack of chararrays and integers. 这是一个调用chararrays和整数堆栈中的数组的函数。 It is supposed to reverse the values, but first i have to figure out which is a string and which is an integer before i can switch them. 它应该反转这些值,但首先我要弄清楚哪个是字符串,哪个是整数才能切换它们。 but i keep getting errors. 但我不断收到错误。 Does anything look ridiculosuly off on this? 有什么看起来有点荒谬吗?

void reverse(Stack *S)
// NOTE:  Called w/ user input 'r'   
// PRE:   Stack S is initialized  
// POST:  The first two values of the stack are reversed on the stack 
{
    int valone;
    int valtwo;
    char stringone[50];
    char stringtwo[50];

    if (S->size < 1)
    {
        printf("Error: There are less than 2 values on the stack \n");
    }  
    else
    { 
        valone = (float)topInt(S);
        strcpy(stringone, topString(S));
        pop(S);
        valtwo = (float)topInt(S);
        strcpy(stringone, topString(S));
        pop(S);

        if(stringone[0] == '\n')
        {
            pushInt(S, valone);
        }
        else if(valone == '\n')
        {
            pushString(S, stringone);
        }
        else if(stringtwo[0] == '\n')
        {
            pushInt(S, valtwo);
        }
        else if(valtwo == '\n')
        {
            pushString(S, stringtwo);
        }
    }
}

You are popping two values off the stack, but only pushing one value back onto it. 您正在从堆栈中弹出两个值,但只将一个值推回到它上面。 You need to change one of the else if s to if . else if是, if需要将其中一个更改为if

    if(stringone[0] == '\n')
    {
        pushInt(S, valone);
    }
    else if(valone == '\n')
    {
        pushString(S, stringone);
    }

    if(stringtwo[0] == '\n')
    {
        pushInt(S, valtwo);
    }
    else if(valtwo == '\n')
    {
        pushString(S, stringtwo);
    }

I don't know if this will solve your problem. 我不知道这是否能解决你的问题。 What error are you getting? 你得到什么错误 Please post that. 请发布。

Also, why are you using \\n as some special value here? 另外,为什么你在这里使用\\n作为特殊值? You will run into strange issues if valone or valtwo end up equivalent to the integral value of \\n . 你会遇到奇怪的问题,如果valonevaltwo最终相当于积分值\\n

If I were you, I would change the approach to something like... 如果我是你,我会改变方法......

void reverse(Stack **S)
{
    Stack* newS = allocateEmptyStack();

    while (!isEmpty(*S))
    {
        StackItem* item = top(*S);
        pop(*S);
        push(newS, item);
    }

    freeStack(*S);
    *S = newS;

 }

Some potential definitions... 一些潜在的定义......

typedef enum ItemType
{
    STACK_STRING,
    STACK_INT,
    STACK_FLOAT
} ItemType;

typedef struct StackItem
{
    ItemType type;
    void* data;
    StackItem* next;
} StackItem;

typedef struct Stack
{
    StackItem* top;
} Stack;

Stack* allocateEmptyStack()
{
    Stack* S = malloc(sizeof(Stack));

    S->top = NULL;

    return S;
}

int isEmpty(Stack* S)
{
    if (S->top == NULL)
        return 1;

    return 0;
}

void freeStack(Stack* S)
{
    while (!isEmpty(S))
    {
        StackItem* item = top(S);
        pop(S);
        freeStackItem(item);
    }

    free(S);
}

StackItem* top(Stack* S)
{
    return S->top;
}

void pop(Stack* S)
{
    StackItem* topItem = top(S);

    if (topItem != NULL)
    {
        s->top = topItem->next;
    }
}

void push(Stack* S, StackItem* item)
{
    item->next = top(S);

     s->top = item;
}

StackItem* allocateStackItem(ItemType type, int dataSize)
{
    StackItem* item = malloc(sizeof(StackItem));

    item->data = malloc(dataSize);
    item->type = type;
    item->next = NULL;

    return item;
} 

void freeStackItem(StackItem* item)
{
    if (item->data != NULL)
        free(item->data);

    free(item);
}

An example of initializing a Stack ... 初始化Stack的示例...

Stack* S = allocateEmptyStack();

StackItem* item = allocateStackItem(STACK_INT, sizeof(int));
int* int_ptr = (int*)(item->data);
*int_ptr = 1234;

push(S, item);

const char* str = "this is a string";

item = allocateStackItem(STACK_STRING, strlen(str) + 1);
char* char_ptr = (char*)(item->data);
strcpy(char_ptr, str);

push(S, item);

Not to be too harsh, but this code is tantamount to completely incoherent. 不要太苛刻,但这段代码无异于完全不连贯。 I can't imagine what valone = (float)topInt(S); 我无法想象valone =(float)topInt(S); is intended to do, since valone is an int. 意图是这样做的,因为valone是一个int。 You also seem to be assigning both an integer identity and a string identity to the top element of the stack. 您似乎也将整数标识和字符串标识分配给堆栈的顶部元素。 You pop two items off the stack and push at most one. 你从堆栈中弹出两个项目并最多推送一个。 You strcpy to a fixed lenngth buffer without checking the size of the string you are copying, and finally, if you do push a string on the stack, you're pushing the local variable address which will be invalid when the function exits. 你可以在不检查正在复制的字符串大小的情况下strcpy到一个固定的lenngth缓冲区,最后,如果你在堆栈上推送一个字符串,你就会推送当函数退出时无效的本地变量地址。

Hard to understand without more detail,but it looks like you are doing two pops and only one push. 没有更多细节很难理解,但看起来你正在做两次弹出而只有一次推动。 You at least need to tale out your 2nd else. 你至少需要记下你的第二个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM