下面是公式计算的代码,尝试在理解的前提下改造成中缀表达式转换后缀表达式的代码。

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "math.h"

#pragma region 使用粘贴符定义复用栈代码

#define DelcareStack(type)                                              \

    typedef struct type##_Node                                          \

    {                                                                   \

        type data;                                                      \

        struct type##_Node *next;                                       \

    } type##Stack;                                                      \

    int type##StackEmpty(type##Stack *stack)                            \

    {                                                                   \

        return stack == NULL;                                           \

    }                                                                   \

    void type##StackPush(type##Stack **stack, type c)                   \

    {                                                                   \

        type##Stack *node = (type##Stack *)malloc(sizeof(type##Stack)); \

        node->data = c;                                                 \

        node->next = *stack;                                            \

        *stack = node;                                                  \

    }                                                                   \

    int type##StackPop(type##Stack **stack, type *data)                 \

    {                                                                   \

        if (*stack == NULL)                                             \

            return 0;                                                   \

        else                                                            \

        {                                                               \

            *data = (*stack)->data;                                     \

            *stack = (*stack)->next;                                    \

            return 1;                                                   \

        }                                                               \

    }                                                                   \

    int type##StackTop(type##Stack *stack, type *data)                  \

    {                                                                   \

        if (stack == NULL)                                              \

            return 0;                                                   \

        else                                                            \

        {                                                               \

            *data = stack->data;                                        \

            return 1;                                                   \

        }                                                               \

    }

#pragma endregion 复用栈

DelcareStack(char)

DelcareStack(float)

int higherThan(char opInExp, char opInStack)

{

    char *ops = "+-*/\%^";

    int rate[6] = {0, 0, 1, 1, 1, 2};

    int expRate, stackRate;

    if (opInExp == ')')

        return 0;

    if (opInExp == '(')

        return 1;

    if (opInStack == '(')

        return 1;



    expRate = rate[(char *)strchr(ops, opInExp) - ops];

    stackRate = rate[(char *)strchr(ops, opInStack) - ops];

    return expRate - stackRate;

}

float calc(float a, float b, char op)

{

    switch (op)

    {

    case '+':

        return a + b;

    case '-':

        return a - b;

    case '*':

        return a * b;

    case '/':

        return a / b;

    case '%':

        return (int)a % (int)b;

    case '^':

        return pow(a, b);

    }

}

float calcExp(char *expression)

{

    charStack *op=NULL;  //运算符栈

    floatStack *ob=NULL; //对象栈

    char *p = expression, opInStack;

    float a, b;

    while (*p)

    {

        if (strchr("0123456789", *p))

        {

            floatStackPush(&ob, *p - 48);

            p++;

        }

        else

        {

            if (charStackEmpty(op))

            {

                charStackPush(&op, *p);

                p++;

            }

            else

            {

                charStackTop(op, &opInStack);

                if (higherThan(*p, opInStack) > 0)

                {

                    charStackPush(&op, *p);

                    p++;

                }

                else

                {

                    charStackPop(&op, &opInStack);

                    if (opInStack != '(')

                    {

                        floatStackPop(&ob, &b);

                        floatStackPop(&ob, &a);

                        printf("calculating %f%c%f\n", a, opInStack, b);



                        floatStackPush(&ob, calc(a, b, opInStack));

                    }

                    else

                        p++;

                }

            }

        }

    }

    while (!charStackEmpty(op))

    {

        floatStackPop(&ob, &b);

        floatStackPop(&ob, &a);

        charStackPop(&op, &opInStack);

        printf("calculating %f%c%f\n", a, opInStack, b);

        floatStackPush(&ob, calc(a, b, opInStack));

    }

    floatStackPop(&ob, &a);

    return a;

}

int main()

{

    printf("%.2f", calcExp("3*2^(4+2*2-1*3)-5"));

    getchar();

}

我的代码:

/*
因为没完全看懂##那里的代码,所以我就用了c++的stack
还有判断优先级的函数也没看懂
*/
#include<iostream>
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include<stack>
using namespace std;

//这个函数没搞明白是咋运行的
int higherThan(char opInExp, char opInStack)
{
	const char* ops = "+-*/\%^";         //这里不明白
	int rate[6] = { 0, 0, 1, 1, 1, 2 };  //这里不明白
	int expRate, stackRate;
	if (opInExp == ')')
		return 0;
	if (opInExp == '(')
		return 1;
	if (opInStack == '(')
		return 1;

	expRate = rate[(char*)strchr(ops, opInExp) - ops];      //这里不明白
	stackRate = rate[(char*)strchr(ops, opInStack) - ops];   //这里不明白
	return expRate - stackRate;
}

void calcExp(char* expression) {
	stack<char> st;

	char* p = expression;  //指针p指向的是我们输入的表达式的第一个字符
	char topchar, temp;

	while (*p) {
		if (strchr("0123456789", *p)) {
			//如果这个字符是运算数,那么输出
			printf("%c", *p);
			p++;
		}
		else {
			if (st.empty()) {  //如果栈为空,那么将运算符入栈
				st.push(*p);
				p++;
			}
			else {       //如果栈不空的话
				topchar = st.top();   //获取栈顶元素
				if (higherThan(*p, topchar) > 0)   //如果读取到的字符的优先级大于栈顶元素,入栈
				{
					st.push(*p);
					p++;
				}
				else {        //如果读取到的字符的优先级小于栈顶元素,那么就要输出了
					temp = st.top();
					st.pop();
					if (temp != '(') {
						printf("%c", temp);
					}
					else {
						p++;
					}

				}
			}
		}
	}

	while (!st.empty()) {
		printf("%c", st.top());
		st.pop();
	}

}

int main() {
	char s[100];
	scanf("%s", s);
	calcExp(s);
	getchar();
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值