大学作业,实现表达式中缀转后缀并求值

本文介绍了一种使用 C++ 编程语言将数学表达式从标准的中缀形式转换为后缀形式,并计算其结果的方法。该程序支持四则运算、指数运算和小数负数,适用于大学级别的计算机科学课程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大学作业,实现表达式中缀转后缀,并求值

话不多说,直接撸代码。
语言c++
在VS2017上实现
支持小数负数的四则运算和指数运算,支持小括号的使用
目前只能达到double运算的精度

#include "stdafx.h"
#include<iostream>
#include<cstring>
#include<stack>
#include<cmath>

using namespace std;

char n[6666];
int g_pos;

double trans(int &pos) {
	double zhengshu = 0.0;
	double remainder = 0.0;
	while (n[pos] >= '0'&&n[pos] <= '9')
	{
		zhengshu = zhengshu * 10;
		zhengshu += (n[pos] - '0');
		pos++;
	}

	if (n[pos] == '.') {
		pos++;
		int c = 1;
		while (n[pos] >= '0'&&n[pos] <= '9') {
			double t = n[pos] - '0';
			t = t * pow(0.1, c);
			c++;
			remainder += t;
			pos++;
		}
	}
	return zhengshu + remainder;
}

int level(char ch) {
	switch (ch)
	{
	case'+':
	case'-':
		return 1;
	case'*':
	case'/':
		return 2;
	case'^':
		return 3;
	case'(':
		return 0;
	case'#':
		return -1;
	};
}

double operate(double a, char op, double b) {
	switch (op) {
	case'+':
		return a + b;
	case'-':
		return a - b;
	case'*':
		return a * b;
	case'/':
		return a / b;
	case'^':
		return pow(a, b);
	}
}

double compute() {
	stack<char>ope;
	stack<double>num;

	ope.push('#');
	int len = strlen(n);
	bool is_minus = true;

	for (g_pos = 0; g_pos < len;) {
		if (n[g_pos] == '-'&&is_minus)//负号
		{
			num.push(0);
			ope.push('-');
			g_pos++;
		}
		else if (n[g_pos] == '(') {
			is_minus = true;
			ope.push(n[g_pos]);
			g_pos++;
		}
		else if (n[g_pos] == ')') {
			is_minus = false;
			g_pos++;
			while (ope.top() != '(')
			{
				double b = num.top();
				num.pop();
				double a = num.top();
				num.pop();
				char op = ope.top();
				ope.pop();
				double result = operate(a, op, b);
				num.push(result);
			}
			ope.pop();
		}
		else if (n[g_pos] >= '0'&&n[g_pos] <= '9') {//数字
			is_minus = false;
			num.push(trans(g_pos));
		}
		else
		{
			while (level(n[g_pos]) <= level(ope.top())) {
				double b = num.top();
				num.pop();
				double a = num.top();
				num.pop();
				char op = ope.top();
				ope.pop();

				double result = operate(a, op, b);
				num.push(result);
			}
			ope.push(n[g_pos]);
			g_pos++;
		}
	}
	while (ope.top() != '#')
	{
		double b = num.top();
		num.pop();
		double a = num.top();
		num.pop();
		char op = ope.top();
		ope.pop();

		double result = operate(a, op, b);
		num.push(result);
	}
	return num.top();
}

void in_to_post(char *post) {
	stack<char>S;
	int k = 0, i = 0;
	S.push('#');
	int len = strlen(n);
	bool is_minus = true;
	for (g_pos = 0; g_pos < len;) {
		if (n[g_pos] == '-'&&is_minus)//负号
		{
			post[k] = '-';
			k++;
			g_pos++;
		}
		else if (n[g_pos] == '(') {
			is_minus = true;
			S.push(n[g_pos]);
			g_pos++;
		}
		else if (n[g_pos] == ')') {
			is_minus = false;
			g_pos++;
			while (S.top() != '(')
			{
				post[k] = S.top();
				S.pop();
				k++;
			}
			S.pop();
		}
		else if (n[g_pos] >= '0'&&n[g_pos] <= '9') {//数字
			is_minus = false;
			post[k] = n[g_pos];
			g_pos++;
			k++;
		}
		else if (n[g_pos] == '.') {
			is_minus = false;
			post[k] = n[g_pos];
			g_pos++;
			k++;
		}
		else if (S.empty()) {
			S.push(n[g_pos]);
			g_pos++;
		}
		else
		{
			while (level(n[g_pos]) <= level(S.top())) {
				post[k] = S.top();
				S.pop();
				k++;
			}
			S.push(n[g_pos]);
			post[k] = ' ';
			k++;
			g_pos++;
		}
	}
	while (S.top() != '#')
	{
		post[k] = S.top();
		S.pop();
		k++;
	}
	for (int j = 0; j <k; j++) {
		cout << post[j];
	}
}


int main()
{
	while (cin >> n)
	{
		char *post = new char;
		in_to_post(post);
		cout << "结果为:" << compute() << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值