Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我如何打印这个计算结果-DS堆栈问题_C++_Stack_Postfix Mta - Fatal编程技术网

C++ 我如何打印这个计算结果-DS堆栈问题

C++ 我如何打印这个计算结果-DS堆栈问题,c++,stack,postfix-mta,C++,Stack,Postfix Mta,这是数据结构(堆栈)问题。在C++中。(不使用STL) 我想输入中缀公式文件(txt,如果没有文件,输入是用户的键入。) 并转换后缀公式,然后计算公式 如何在计算函数中输入后缀公式? 我尝试制作文件和读取文件,但不起作用。(我尝试更改端点位置(删除),但也不起作用。) ArrayStack.h #include <iostream> #include <cstdlib> #define _CRT_SECURE_NO_WARNINGS using namespace st

这是数据结构(堆栈)问题。在C++中。(不使用STL) 我想输入中缀公式文件(txt,如果没有文件,输入是用户的键入。) 并转换后缀公式,然后计算公式

如何在计算函数中输入后缀公式? 我尝试制作文件和读取文件,但不起作用。(我尝试更改端点位置(删除),但也不起作用。)

ArrayStack.h

#include <iostream>
#include <cstdlib>
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

inline void error(const char *message)

{

    cout << message << endl;

    exit(1);

}



const int MAX_STACK_SIZE = 20;



class ArrayStack

{

private:

    int top;

    int data[MAX_STACK_SIZE];

public:

    ArrayStack()

    {
        top = -1;
    }

    ~ArrayStack()
    {

    }

    bool isEmpty()
    {
        return top == -1;
    }

    bool isFull()
    {
        return top == MAX_STACK_SIZE - 1;
    }

    void push(int e)
    {
        if (isFull())
            error("스택 포화 에러");
        data[++top] = e;
    }

    int pop()
    {

        if (isEmpty())

            error("스택 공백 에러");

        return data[top--];

    }

    int peek()

    {

        if (isEmpty())

            error("스택 공백 에러");

        return data[top];

    }

    void display()

    {
        printf("[스택 항목의 수 = %2d] ==> ", top + 1);
        for (int i = 0; i < top; i++)
            printf("<%2d>", data[i]);
        printf("\n");

    }

};
#包括
#包括
#定义\u CRT\u安全\u无\u警告
使用名称空间std;
内联无效错误(常量字符*消息)
{

“不使用STL”的局限性几乎与你在C++中写的想法相矛盾。它似乎是人为限制的。我是说,你可以编写你自己的操作符重载,谢谢你看到我的问题。为什么没有STL,因为这个代码库是讲课教材,我学习数据结构,我的教授对我说这是学习DS的好方法,所以不要使用STL。(之后,可能转换STL版本是另一个家庭作业。)
#pragma warning(disable:4996)

#include "ArrayStack.h"
#include <string.h>
#include <fstream>


inline static int precedence(char op)
{
    switch (op) {
    case '(': case ')': return 0;
    case '+': case '-': return 1;
    case '*': case '/': return 2;
    }
    return -1;
}

void infixToPostfix(FILE *fp = stdin)
{
    char         c, op;
    double       val;
    ArrayStack stack;
    ofstream os;
    os.open("stack_Data.txt");

    while ((c = getc(fp)) != '\n')
    {
        if ('0' <= c && c <= '9')
        {
            ungetc(c, fp);
            fscanf_s(fp, "%lf", &val);
            printf("%4.1f ", val);
            os << val;

        }
        else if (c == '(')
            stack.push(c);
        else if (c == ')')
        {
            while (!stack.isEmpty())
            {
                op = stack.pop();
                if (op == '(') break;
                else
                    printf("%c ", op);
                os << op;
            }
        }
        else if (c == '+' || c == '-' || c == '*' || c == '/')
        {
            while (!stack.isEmpty())
            {
                op = stack.peek();
                if (precedence(c) <= precedence(op))
                {
                    printf("%c ", op);
                    os << op;
                    stack.pop();
                }
                else break;
            }
            stack.push(c);
        }
    }
    while (!stack.isEmpty()) {
        char c2 = stack.pop();
        os << c2;

    printf("%c ", c2);
        }os << endl;
os.close();
    printf("\n");

}

double calcPostfixExpr(const char *filename)
{
    FILE *fp = fopen(filename, "r");

    if (fp == NULL)

        error("Error: 파일 존재하지 않습니다.\n");

    char ch;
    ArrayStack stack;

    while ((ch = getc(fp)) != '\n')
    {
        if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
        {
            double right = stack.pop();
            double left = stack.pop();
            switch (ch) {
            case '+':
                stack.push(left + right);
                break;
            case '-':
                stack.push(left - right);
                break;
            case '*':
                stack.push(left * right);
                break;
            case '/':
                stack.push(left / right);
                break;
            }
        }
        else if ('0' <= ch && ch <= '9')
        {
            ungetc(ch, fp);
            double val;
            fscanf_s(fp, "%lf", &val);
            stack.push(val);
        }
    }
    fclose(fp);
    return stack.pop();
}


void main() {

    infixToPostfix();
    double res = calcPostfixExpr("stack_Data.txt");
    printf("%f", res);
    system("Pause");
}
#pragma warning(disable:4996)

#include "ArrayStack.h"
#include <string.h>
#include <fstream>


inline static int precedence(char op)
{
    switch (op) {
    case '(': case ')': return 0;
    case '+': case '-': return 1;
    case '*': case '/': return 2;
    }
    return -1;
}

void infixToPostfix(FILE *fp = stdin)
{
    char         c, op;
    double       val;
    ArrayStack stack;

    while ((c = getc(fp)) != '\n')
    {
        if ('0' <= c && c <= '9')
        {
            ungetc(c, fp);
            fscanf_s(fp, "%lf", &val);
            printf("%4.1f ", val);

        }
        else if (c == '(')
            stack.push(c);
        else if (c == ')')
        {
            while (!stack.isEmpty())
            {
                op = stack.pop();
                if (op == '(') break;
                else
                    printf("%c ", op);

            }
        }
        else if (c == '+' || c == '-' || c == '*' || c == '/')
        {
            while (!stack.isEmpty())
            {
                op = stack.peek();
                if (precedence(c) <= precedence(op))
                {
                    printf("%c ", op);
                    stack.pop();
                }
                else break;
            }
            stack.push(c);
        }
    }
    while (!stack.isEmpty()) {
        char c2 = stack.pop();

        printf("%c ", c2);
    }
    printf("\n");

}

double calcPostfixExpr(FILE* fp = stdin)
{

    char ch;
    ArrayStack stack;

    while ((ch = getc(fp)) != '\n')
    {
        if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
        {
            double right = stack.pop();
            double left = stack.pop();
            switch (ch) {
            case '+':
                stack.push(left + right);
                break;
            case '-':
                stack.push(left - right);
                break;
            case '*':
                stack.push(left * right);
                break;
            case '/':
                stack.push(left / right);
                break;
            }
        }
        else if ('0' <= ch && ch <= '9')
        {
            ungetc(ch, fp);
            double val;
            fscanf_s(fp, "%lf", &val);
            stack.push(val);
        }
    }
    return stack.pop();
}


void main() {

    infixToPostfix();
    system("Pause");
}