⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operation.cpp

📁 可进行大数的加减乘除、阶乘、三角函数、幂指数(指数只支持整数)的运算
💻 CPP
字号:
#include "Operation.h"

/*******************************************************\
函数: CompareSign
功能: 判断算术符号优先级
输入: 2个符号
输出: 无
返回: > <
备注: c1是栈的, c2是从字符串中读取的
\*******************************************************/
char Operation::CompareSign(char c1, char c2)
{
	if(c1 == '!')
		return '>';
	if(c2 == '!')
		return '<';
	if(c1 == '(' && c2 == ')')
		return '=';
	if(c1 == '(')
		return '<';
	if(c2 == '(')
		return '<';
	if(c1 == 's' || c1 == 'c' || c1 == 't')
		return '>';
	if(c2 == 's' || c2 == 'c' || c2 == 't')
		return '<';
	if(c2 == ')')
		return '>';
	if(c2 == '^')
		return '<';
//	if(c2 == '!')//c1 不会是 '('
//		return '>';
	if(c1 == '^')
		return '>';
	if(c1 == '/' || c1 == '*' || c1 == '%')
		return '>';
	if((c1 == '+' || c1 == '-') && (c2 == '/' || c2 == '*' || c2 == '%'))
		return '<';
	return '>';
}

/*******************************************************\
函数: IsNumber
功能: 判断是否为数字
输入: 1字符
输出: 无
返回: bool
备注: 无
\*******************************************************/
bool Operation::IsNumber(char c)
{
	return ((c >= '0' && c <= '9') || c == '.') ? 1 : 0;
}

/*******************************************************\
函数: Operations构造函数
功能: 初始化
输入: 无
输出: 无
返回: 无
备注: 无
\*******************************************************/
Operation::Operation(std::string NumLine)
{
	this->m_NumLine = NumLine;
}

Operation::Operation(char* NumLine)
{
	m_NumLine = NumLine;
}
/*******************************************************\
函数: Operations
功能: 数学表达式运算
输入: 表达式
输出: 无
返回: 表达式结果
备注: 借助类Arithmetic进行运算
\*******************************************************/
Arithmetic Operation::Operate(Arithmetic& a, char c, Arithmetic& b)
{
	Arithmetic result;
	switch(c)
	{
	case '+':
		result = a + b;
		break;
	case '-':
		result = a - b;
		break;
	case '*':
		result = a * b;
		break;
	case '/':
		result = a / b;
		break;
	case '^':
		result = a.RootInteger(a.GetData(), b.GetData());
		break;
	case '%':
		result = a % b;
		break;
	default:
		break;
	}
	return result;
}

/*******************************************************\
函数: Operations
功能: 数学表达式运算
输入: 表达式
输出: 无
返回: 表达式结果
备注: 借助类Arithmetic进行运算
\*******************************************************/
std::string Operation::Operations()
{
	Arithmetic result = "", a = "", b = "";
	std::stack< Arithmetic > numbers;
	std::stack< char > signs;
	signs.push('(');
	m_NumLine += ")";
	std::string tn = "";
	int state = 0, i = 0;
	char c, l;
	while(i < m_NumLine.length())
	{
		char h = m_NumLine[i];
		if(m_NumLine[i] == 32)
			++i;
		if(i >= m_NumLine.length())
			break;
		if(IsNumber(m_NumLine[i]))
		{
			state = 1;
			tn += m_NumLine[i++];
		}
		else
		{
			if(state == 1)
			{
				Arithmetic t = tn;
				numbers.push(t);
				state = 0;
				tn = "";
			}
			switch(CompareSign(signs.top(), m_NumLine[i]))
			{
			case '<':
				signs.push(m_NumLine[i++]);
				break;
			case '=':
				signs.pop();
				++i;
				break;
			case '>':
				c = signs.top();
				signs.pop();
				if(c == '!')
				{
					a = numbers.top();
					numbers.pop();
					result = a.Factorial();
					numbers.push(result);
				}
				if(c == 's')
				{
					a = numbers.top();
					numbers.pop();
					result = a.Sin();
					numbers.push(result);
				}
				if(c == 't')
				{
					a = numbers.top();
					numbers.pop();
					result = a.Tan();
					numbers.push(result);
				}
				if(c == 'c')
				{
					a = numbers.top();
					numbers.pop();
					result = a.Cos();
					numbers.push(result);
				}
				if(c != 'c' && c != 's' && c != 't' && c != '!')
				{
					a = numbers.top();
					numbers.pop();
					b = numbers.top();
					numbers.pop();
					result = Operate(b, c, a);
					numbers.push(result);
				}
				break;
			default:
				break;
			}
		}
//		l = signs.top();
	}
	result = numbers.top();
	return result.GetData();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -