gpt4 book ai didi

c - 理解 C 中的指针和堆栈

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:12 26 4
gpt4 key购买 nike

我正在编写一个程序,使用堆栈将从命令行输入的反向波兰表示法转换为中缀表示法。它不解决 rpn 符号,它只是以中缀符号打印出输出。我一直对堆栈的行为有疑问。该程序不包含所有代码功能(为简单起见)。我省略了打印数据和进行输入错误检查的函数,但它们不会更改堆栈。因此,假设输入了正确的数据(例如 2 5 +),那么我们可以继续研究该程序的行为方式。该程序使用三个文件,即客户端、接口(interface)和实现文件。当从命令行输入输入时,程序循环 argv[] 数组并检查数据是否为数字,如果是则将数字数据压入创建的堆栈。如果一些数据不是数字那么这意味着我们有一个算术运算符字符。当发生这种情况时,两个值将从堆栈中弹出并与算术运算符函数合并在一起。然后将合并的结果插入堆栈。输入 4 5 + 形式的输入与正确的输出 4 + 5 以及 9 8 + 6 形式的输入以及输出 9 + 8 - 6 的工作正常。

当输入这个表格 3 4 + 5 8++ 时出现问题,我得到输出 5 + 8 + 5 + 8,但正确的输出应该是 3 + 4 + 5 + 8。这可能是由于到我传递给 push_stack 函数的参数,它是一个在传入之前合并的字符串。我是学习指针的新手,所以也许有人可以为我澄清堆栈中发生了什么。我将这三个文件包含在修改堆栈的函数和调用中。感谢任何帮助

//stackfunctions.c file
#include "stackfunctions.h"

struct stackelements{
SType items[STORAGE];
int top;
};

pointer_stack stack_new(){
pointer_stack stack = malloc(sizeof(struct stackelements));
stack->top = -1; // empty initially
return stack;
}

int stack_push(pointer_stack stack, Stype stack_value){
if(stack->top == STORAGE - 1){
return 0;
}

stack->top++;
stack->items[stack->top] = stack_value;
return 1;
}

Stype stack_pop(pointer_stack stack){
if(stack->top == -1)
abort();//
stack->top--;
return stack->items[stack->top+1];
}

void stack_print(pointer_stack stack){
int i;
printf("\n--------Top------------\n");

for(i = stack->top; i >= 0; i--) {
printf(" %s\n", stack->items[i]);
}
printf("---------Bottom----------\n");
}

stackfunctions.h 文件

//stackfunctions.h file
#include<stdio.h>
#include<stdlib.h>

#define STORAGE 128

typedef struct stackelements *pointer_stack;

typedef char *Stype;

extern pointer_stack stack_new();

extern int stack_push(pointer_stack stack, Stype stack_value);

extern Stype stack_pop(pointer_stack stack);

extern void stack_print(pointer_stack stack);

主程序文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stackfunctions.h"

int main(int argc, char *argv[]){

int i,j;
float x = 0;
char *operator[1], *point_b[1], *point_a[1];
char result[100], a[30], b[30], op[3];

pointer_stack stackP;

stackP = stack_new(); // Create new stack

// Loop through command line argument input

for(i=1; i < argc; i++){
// Check to see if input is numeric.
if(sscanf(argv[i], " %f ", &x)){
stack_push(stackP, argv[i]); // If input is numeric push them into stack.

else{ // Input is an operator, then we must

operator[0] = argv[i];

//Pop two items off stack

point_b[0] = stack_pop(stackP);

// create strings from pointer returned by atack_pop function

memcpy(b, point_b[0] , strlen(point_b[0])+1);

point_a[0] = stack_pop(stackP);

memcpy(a, point_a[0] , strlen(point_a[0])+1);

memcpy(op, operator[0] , strlen(operator[0])+1);

// The we have to merge the popped off values and push the new
// result back on stack.

sprintf(result, "%s %s %s", a, op, b);

stack_push(stackP, result);

stack_print(stackP); // Function that prints the stack, function
// not included in other files, but it only
// prints out the stackP pointer values. I use this
// to check the output.

}// close else

}// close for loop

free(stackP);

return 0;
}

最佳答案

看起来您正在破坏存储空间(result)。

您将结果存储到 result 中,然后从那里将其插入堆栈。美好的。但您只是保存了指向 result 的指针。在带有 3 4 + 的第一个参数之后,结果应该有 3 + 4。但是,当您打开下一个参数时,您会将 5 8 + 转换为结果。但是,因为您只有一个结果数组,所以您最终会覆盖已有的 3 + 4 结果。

相反,您可能希望在每次压入堆栈时分配空间并在弹出时释放该内存。

关于c - 理解 C 中的指针和堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20025289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com