在我的编程课上,我必须编写一个计算器,它可以用堆栈进行运算。
这是我为堆栈本身编写的代码:

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

#define MAXSIZE 10

double stk[MAXSIZE]; //Stack array
int top=-1; //Top position in stack

void push(double n);
double pop(void);
void display(void);

/* Add an element to the stack */
void push(double n) {

    if (top == (MAXSIZE - 1)) {
        printf ("Stack is full\n");
    }
    else {
        //s.top++;
        //stk = (double *) malloc(MAXSIZE*sizeof(double));
        stk[++top] = n;
    }

    return;
}

/* Remove and return the top element from the stack */
double pop() {

    double num;

    if (top == -1) {
        printf ("Stack is empty\n");
        return (top);
    }
    else {
        num = stk[top--];
        printf ("Pop:%f\n", num); //Debugging line
        return (num);
    }
}

/* Prints all elements in the stack */
void display() {

    int i;

    if (top == -1) {
        printf ("Stack is empty\n");
        return;
    }
    else {
        for (i = top; i >= 0; i--) {
            printf ("%f\n", stk[i]);
        }
    }
}

这是计算器(在另一个文件中,我使用makefile编译):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

int isNumber(const char *s);
void insert(double num);
void sum(void);

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

    int loop = 1;
    char input[10];

    /* Main Loop */
    while (loop == 1) {
        printf("> ");
        scanf(" %[^\n]", input);

        if (isNumber(input)) {
            double nu = atof(input);
            insert(nu);
        }

        else if (strcmp(input, "+") == 0)
            sum();
        else if (strcmp(input, "l") == 0)
            list();
        else if (strcmp(input, "exit") == 0) //exit
            loop = 0;
    } //end while

} //end main

int isNumber(const char *s) {
    while (*s) {
        if((*s<'0' || *s>'9') && *s!='-' && *s!='.')
            return 0;
        s++;
    }

    return 1;
}

void insert(double num) {
    push(num);
}

/* This function is called when the user enters a '+' instead of a number into the command line. It takes the top two numbers from the stack and adds them together */
void sum() {
    double num1, num2, res;

    num1 = pop();
    num2 = pop();
    res = num1+num2;

    printf("num1:%f num2:%f sum:%f\n", num1, num2, res); //Debug
}

int list() {
    display();
}

这个程序编译得很好。当我运行它时,我通过输入5,然后输入6,再输入+来测试它,然后得到这个输出:
Pop:6.000000
Pop:4.000000
num1:13.000000 num2:13.000000 sum:26.000000

显然pop()函数返回的数字是正确的,但是当将其分配给计算器函数中的变量时,由于某种原因,它将其改为13。它并不总是13,对于较大的数字,它更高;输入500返回14,1000返回15,10000返回16,依此类推。
我最初用一个int数组来创建堆栈,它实际上工作得很好(如果我把所有的double都改成int,它仍然可以工作)。此外,堆栈本身似乎工作正常,因为display()函数正确地打印了用户输入的所有值)。
我真的很困惑,错误是从哪里来的,我实际上正在考虑重写整个堆栈作为一个链表,而不是,但我想给这最后一次机会。
提前谢谢你的帮助。
编辑:我在我的计算器文件中添加了一个#include“Stack.h”(将Stack.c改为Stack.h),并释放了makefile,它现在可以工作了。我不知道最初发生了什么,但我很高兴它起作用了。

最佳答案

我把你所有的逻辑放在一个文件中,并添加了简单的main:

int main()
{
    push(4.0);
    push(3.8);
    sum();
    return 0;
}

然后编译为gcc main.c。而且效果很好:
人口:380万
流行语:4.000000
num1:3.800000 num2:4.000000和:7.800000
你确定你真的经常链接你的项目(并打破你的代码到一些模块)?你能提供更多关于如何编译你的版本的信息吗?
你的程序逻辑很好
更新
您需要添加具有相同文本的Stack.h文件:
#include <stdio.h>
#include <stdlib.h>

void push(double n);
double pop(void);
void display(void);

Stack.c中删除此代码并在第一行中添加#include "Stack.h"
Main.c中,仅在6行中添加#include "Stack.h"(在系统#include指令之后)。
不要更改生成文件。这不是必需的。
关于问候,
阿吉奥

07-24 09:51
查看更多