栈的应用 平衡符号 后缀表达式 中缀到后缀的转换

数据结构与算法分析——c语言描述 第三章 平衡符号


平衡符号

#include"stack.h"
#include<stdio.h>

int isCorrespond(char c1, char c2) {
	if (c1 == '('&&c2 == ')')
		return 1;
	else if (c1 == '['&&c2 == ']')
		return 1;
	else
		return 0;
}

int main() {
	FILE *fin;
	Stack s = CreatStack(100);
	char c,c2;
	if ((fin = fopen("test.txt", "rt")) == NULL){
		fprintf(stderr, "cannot open output file.\n");
		return 1;
	}
	while ((c = getc(fin)) != EOF) {
		if (c == '(' || c == '[')
			Push(c,s);
		else if (c == ')' || c == ']') {
			c2=TopAndPop(s);
			if (!isCorrespond(c2, c)) {
				printf("has error%c %c\n",c2,c);
			}
		}
	}
}


后缀表达式 中缀到后缀的转换,这两节的应用我自己合起来写了输入一个表达式计算。一开始先不管括号,很快写完了。后来实现括号功能的就想到使用递归的。但我硬是要用循环来写,结果写了一个晚上和一个早上也没有写出来。最后还是使用了递归了。。。。对了用了c++来写,c来写的话很麻烦,自己实现的栈只能使用保存一种类型,还是用c++写了。

#include<stack>
#include<stdio.h>
#include<ctype.h>
#include<string>
#include<map>
using namespace std;
#define MAXN 100

char expression[MAXN];//输入的表达式
string postfix[MAXN];//转换成的逆序表达式

int i = 0, j = 0;//i是expression的游标,postfix是逆序表达式的游标
map<char, int(*)(int, int)> operaFunc;
int _add(int x, int y) { return x + y; }
int _sub(int x, int y) { return x - y; }
int _mul(int x, int y) { return x * y; }
int _div(int x, int y) { return x / y; }

int operatorCmp(char c1, char c2);//c1为将要插入栈的运算符,c2为栈顶运算符,1表示c1优先级大于c2,0为相等,-1为小于
#define putNum()  postfix[j++] = num,num.clear();//把数字输出到逆序表达式
#define putOperator() oper= operator_stack.top(),operator_stack.pop(),postfix[j++] = oper;//把符号输出到逆序表达式

void midToPostfix() {
	stack<char>operator_stack;
	string num;
	char c, oper;
	for (; expression[i] != '\0'; i++) {
		c = expression[i];
		if (c == '(') {
			i++;
			midToPostfix();
			continue;
		}
		if (c == ')') {
			break;
		}
		if (isdigit(c))
			num += c;
		else {
			if (!num.empty())
				putNum();
			if (operator_stack.empty())
				operator_stack.push(c);
			else if (operatorCmp(c, operator_stack.top()) == 1) {
				operator_stack.push(c);
			}
			else {
				while (1) {
					putOperator();
					if (operatorCmp(c, oper) == 1 || operator_stack.empty()) {
						operator_stack.push(c);
						break;
					}
				}
			}
		}
	}
	if (!num.empty())
		postfix[j++] = num;
	while (!operator_stack.empty()) {
		postfix[j++] = operator_stack.top();
		operator_stack.pop();
	}
}

int postfixToInt() {
	stack<int>num_stack;
	int x, y, ans;
	operaFunc['+'] = _add;
	operaFunc['-'] = _sub;
	operaFunc['*'] = _mul;
	operaFunc['/'] = _div;
	for (i = 0; i < j; i++) {
		if (isdigit(postfix[i][0]))
			num_stack.push(stoi(postfix[i]));
		else {
			y = num_stack.top();
			num_stack.pop();
			x = num_stack.top();
			num_stack.pop();
			ans = operaFunc[postfix[i][0]](x, y);
			num_stack.push(ans);
		}
	}
	return  num_stack.top();
}



int main() {
	scanf("%s", expression);
	midToPostfix();
	for (int i = 0; i < j; i++) {
		printf("%s", postfix[i].c_str());
	}
	printf("\n");
	printf("%d", postfixToInt());
}

int operatorCmp(char c1, char c2) {
	int t1, t2;
	if (c1 == '*' || c1 == '/')
		t1 = 2;
	else if (c1 == '+' || c1 == '-')
		t1 = 1;


	if (c2 == '*' || c2 == '/')
		t2 = 2;
	else if (c2 == '+' || c2 == '-')
		t2 = 1;

	return t1 - t2;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值