2012-10-25 37 views
0

这是一个函数,它调用一堆栈和整数中的数组。它应该颠倒这些值,但首先我必须弄清楚哪些是字符串,哪些是整数,然后才能切换它们。但我不断收到错误。有什么看起来可笑吗?反向函数不断给我错误

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); 
     } 
    } 
} 
+0

你得到了什么样的错误? – asbumste

+0

其中是Stack类型的定义?为什么你使用一个强制转换来将该值存储在一个int中?为什么有时候你会推弦,有时整数?您的代码令人困惑... – vmp

回答

1

您从堆栈中弹出两个值,但只将一个值重新输入到堆栈中。您需要将其中一个else 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); 
    } 

我不知道这是否能解决您的问题。你得到什么错误?请张贴。

此外,为什么你在这里使用\n作为一些特殊的价值?如果valonevaltwo最终等于\n的整数值,则会遇到奇怪的问题。

如果我是你,我会的办法改为类似...

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

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

    freeStack(*S); 
    *S = newS; 

} 

一些潜在的定义...

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); 
} 

初始化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); 
0

很难理解没有更多的细节,但它看起来像你是doi两个流行音乐,只有一个推动。你至少需要说出你的第二个人。

1

不要太苛刻,但这段代码无异于完全不连贯。 我无法想象什么valone =(float)topInt(S);是有意做的,因为 valone是一个int。您似乎也将整数标识 和字符串标识分配给堆栈的顶层元素。你弹出两个项目 离开堆栈,并最多推一个。你不用检查你正在复制的字符串的大小,最后如果 你在栈上压入一个字符串,那么你推入的局部变量地址为 ,当函数退出时它将是无效的。

+0

+1,简而言之,此代码中* *错误*的数量多于*右*。 – WhozCraig

+1

我其实没有注意到任何事情。对堆栈中太少项目的测试似乎也是不正确的。哦,亲爱的... – ddyer

+0

同意。代码没有太多意义。这就是为什么我最终编写了一个示例堆栈接口。这是一个缓慢的工作日... –