C++实现单位数字公式运算器,栈的使用

1、声明栈

class Sqstack {
public:
	void initstack();
	bool push(elemtype in);
	bool pop();
	elemtype gettop();
	bool nul();
private:
    // 栈底
	elemtype* base;
	// 栈顶
	elemtype* top;
	// 栈容量
	int stacksize;
};

2、初始化栈

void Sqstack::initstack()//初始化栈
{
	this->base = new elemtype[maxsize];
	if (this->base == NULL)
		exit(0);
	this->top = this->base;
	this->stacksize = 0;
}

3、压入栈

bool Sqstack::push(elemtype in) //压入元素
{
	if (this->top - this->base < maxsize)
	{
		*(this->top) = in;
		this->top++;
		this->stacksize++;
		return 1;
	}
	else
		return 0;
}

4、弹出栈

bool Sqstack::pop()//弹出元素
{
	if (this->top - this->base)
	{
		this->top--;
		*(this->top) = 0;
		this->stacksize--;
		return 1;
	}
	return 0;
}

5、取栈顶元素值

elemtype Sqstack::gettop()//取栈顶元素
{
	if (this->top - this->base)
		return *(this->top - 1);
}
bool Sqstack::nul()
{
	if (this->stacksize == 0)
		return 1;
	return 0;
}

6、实现a1,a2运算,a表示运算符

int op(int a1, int a2, char a)
{
	if (a == '+')
		return a1 + a2;
	if (a == '-')
		return a1 - a2;
	if (a == '*')
		return a1 * a2;
	if (a == '/')
		return a1 / a2;
}

7、主函数

int main()
{
	map<char, int> m;		// m用于存储运算符的优先级
	m['+'] = 1;
	m['-'] = 1;
	m['*'] = 2;
	m['/'] = 2;
	m['('] = 3;
	Sqstack s1, s2;
	char a[100];
	cin >> a;
	int a1, a2;
	s1.initstack(); s2.initstack();//s1为算符栈  s2为算数栈
	for (auto e : a)
	{
		if (e == '\0' || e == '\n') break;
		if (e >= '0' && e <= '9')
		{
			s2.push(atoi(&e));
		}
		else if (s1.nul() || m[s1.gettop()] < m[e])
		{
			s1.push(e);
		}
		else if (e == ')')
		{
			while (s1.gettop() != '(')
			{
				a1 = s2.gettop(); s2.pop();
				a2 = s2.gettop(); s2.pop();
				s2.push(op(a2, a1, s1.gettop()));
				s1.pop();
			}
			s1.pop();
		}
		else
		{
			s1.push(e);
		}
	}
	while (!(s1.nul()))
	{
		a1 = s2.gettop(); s2.pop();
		a2 = s2.gettop(); s2.pop();
		s2.push(op(a2, a1, s1.gettop()));
		s1.pop();
	}
	cout << s2.gettop();
}

全部代码:

#include
#include
using namespace std;
#define maxsize 1000
typedef int elemtype;
class Sqstack {
public:
	void initstack();
	bool push(elemtype in);
	bool pop();
	elemtype gettop();
	bool nul();
private:
	elemtype* base;//栈底
	elemtype* top;//栈顶
	int stacksize;//栈容量
};
void Sqstack::initstack()//初始化栈
{
	this->base = new elemtype[maxsize];
	if (this->base == NULL)
		exit(0);
	this->top = this->base;
	this->stacksize = 0;
}
bool Sqstack::push(elemtype in) //压入元素
{
	if (this->top - this->base < maxsize)
	{
		*(this->top) = in;
		this->top++;
		this->stacksize++;
		return 1;
	}
	else
		return 0;
}
bool Sqstack::pop()//弹出元素
{
	if (this->top - this->base)
	{
		this->top--;
		*(this->top) = 0;
		this->stacksize--;
		return 1;
	}
	return 0;
}
elemtype Sqstack::gettop()//取栈顶元素
{
	if (this->top - this->base)
		return *(this->top - 1);
}
bool Sqstack::nul()
{
	if (this->stacksize == 0)
		return 1;
	return 0;
}
int op(int a1, int a2, char a);
int op(int a1, int a2, char a)
{
	if (a == '+')
		return a1 + a2;
	if (a == '-')
		return a1 - a2;
	if (a == '*')
		return a1 * a2;
	if (a == '/')
		return a1 / a2;
}

void main_func() {
	map<char, int> m;
	m['+'] = 1;
	m['-'] = 1;
	m['*'] = 2;
	m['/'] = 2;
	m['('] = 3;
	Sqstack s1, s2;
	cout << "输入运算的式子: ";
	char a[100];
	cin >> a;
	int a1, a2;
	s1.initstack(); s2.initstack();//s1为算符栈  s2为算数栈
	for (auto e : a)
	{
		if (e == '\0' || e == '\n') break;
		if (e >= '0' && e <= '9')
		{
			s2.push(atoi(&e));
		}
		else if (s1.nul() || m[s1.gettop()] < m[e])
		{
			s1.push(e);
		}
		else if (e == ')')
		{
			while (s1.gettop() != '(')
			{
				a1 = s2.gettop(); s2.pop();
				a2 = s2.gettop(); s2.pop();
				s2.push(op(a2, a1, s1.gettop()));
				s1.pop();
			}
			s1.pop();
		}
		else
		{
			s1.push(e);
		}
	}
	while (!(s1.nul()))
	{
		a1 = s2.gettop(); s2.pop();
		a2 = s2.gettop(); s2.pop();
		s2.push(op(a2, a1, s1.gettop()));
		s1.pop();
	}
	cout << "运算结果: " << endl << s2.gettop() << endl;
}

int main()
{
	system("title 单位数字运算器");
	while (true) {
		main_func();
	}
	
}

测试:
C++实现单位数字公式运算器,栈的使用_第1张图片

你可能感兴趣的:(c++,开发语言)