Demo entry 6686350

55

   

Submitted by anonymous on Dec 23, 2017 at 09:15
Language: C++. Code size: 5.8 kB.

#include<stack>
#include<iostream>
using namespace std;
bool judge(char ch)//判断是否是运算符,是的话返回true否则返回false
{
	char ops[] = "+-*/";
	for (int i = 0; i < sizeof(ops) / sizeof(char); i++)
	{
		if (ch == ops[i])
			return true;
	}
	return false;
}
int Precedence(char op1, char op2)// 比较两个操作符的优先级

{
	if (op1 == '(')
		return -1;
	if (op1 == '+' || op1 == '-')
	{
		if (op2 == '*' || op2 == '/')
			return -1;
		else
			return 0;
	}

	if (op1 == '*' || op1 == '/')
	{
		if (op2 == '+' || op2 == '-')
			return 1;
		else
			return 0;
	}
}

void change(char* inFix, char* postFix)// 中缀表达式转换成后缀表达式
{
	int j = 0, len, m, k = 0;
	char c;
	stack<double> st;
	len = strlen(inFix);
	for (int i = 0; i < len; i++)
	{
		m = j;
		c = inFix[i];
		if (c == '-')
		{
			int o = i;
			if ((inFix[--i] >'9')||('0'>inFix[--o]))
				postFix[j++] = '-';		
			i++;
			continue;			
		}

		if (c == '(')//遇到(,直接入栈
		{
			st.push(c);
		}
		else if (c == ')')//遇到)进行判断
		{
			while (st.top() != '(')
			{
				m = j;
				if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,就不要加空格了
				{

					postFix[j++] = st.top();//栈顶不是左括号,出栈,进入后缀表达式
					st.pop();
					m = j;

				}
				else
				{
					postFix[j++] = ' ';
					postFix[j++] = st.top();//栈顶不是左括号,出栈,进入后缀表达式
					st.pop();
				}
			}
			st.pop();
		}
		else
		{
			if (!judge(c))//如果c不是运算符不是括号,直接输出到后缀表达式
				postFix[j++] = c;//st.push(c).
			else//是运算符
			{
				m = j;
				if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,不加入空格

				{
					while (st.empty() == false && Precedence(st.top(), c) >= 0)//遇到运算符,栈非空,弹出所有优先级大于或者等于该运算符的栈顶元素
					{
							postFix[j++] = st.top();//先取值
							st.pop();//再删除
							m = j;
					}
				}
				else
				{
					postFix[j++] = ' ';
					while (st.empty() == false && Precedence(st.top(), c) >= 0)//遇到运算符,栈非空,弹出所有优先级大于或者等于该运算符的栈顶元素
					{
						postFix[j++] = st.top();//先取值
						st.pop();//再删除
					}
				}
				st.push(c);
			}
		}
	}
	while (st.empty() == false)//最后栈依旧不是空的,则全部出栈
	{
		m = j;
		if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,不加入空格
		{
			postFix[j++] = st.top();
			k++;
			st.pop();
		}
		else
		{
			postFix[j++] = ' ';
			postFix[j++] = st.top();
			k++;
			st.pop();
		}
	}
	postFix[j] = 0;//字符数组结束标志
}
void change1(char* inFix, char* postFix)
{
	int j = 0, len, m, k = 0;
	char c;
	stack<double> st;
	len = strlen(inFix);
	for (int i = 0; i < len; i++)
	{
		m = j;
		c = inFix[i];
		if (c == '(')//遇到(,直接入栈
			st.push(c);
		else if (c == ')')//遇到)进行判断
		{
			while (st.top() != '(')
			{
				m = j;
				if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,就不要加空格了
				{
					postFix[j++] = st.top();//栈顶不是左括号,出栈,进入后缀表达式
					st.pop();
					m = j;
				}
				else
				{
					postFix[j++] = ' ';
					postFix[j++] = st.top();//栈顶不是左括号,出栈,进入后缀表达式
					st.pop();
				}
			}
			st.pop();
		}
		else
		{
			if (!judge(c))//如果c不是运算符不是括号,直接输出到后缀表达式
				postFix[j++] = c;//st.push(c).
			else//是运算符
			{
				m = j;
				if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,不加入空格

				{
					while (st.empty() == false && Precedence(st.top(), c) >= 0)//遇到运算符,栈非空,弹出所有优先级大于或者等于该运算符的栈顶元素
					{
						postFix[j++] = st.top();//先取值
						st.pop();//再删除
						m = j;
					}
				}
				else
				{
					postFix[j++] = ' ';
					while (st.empty() == false && Precedence(st.top(), c) >= 0)//遇到运算符,栈非空,弹出所有优先级大于或者等于该运算符的栈顶元素
					{
						postFix[j++] = st.top();//先取值
						st.pop();//再删除
					}
				}
				st.push(c);
			}
		}
	}

	while (st.empty() == false)//最后栈依旧不是空的,则全部出栈
	{
		m = j;
		if (m >= 1 && (judge(postFix[--m]) == true))//前面是运算符,不加入空格
		{
			postFix[j++] = st.top();
			k++;
			st.pop();
		}
		else
		{
			postFix[j++] = ' ';
			postFix[j++] = st.top();
			k++;
			st.pop();
		}
	}
	postFix[j] = 0;//字符数组结束标志
}
double postFixEval(char* postFix, int* a, int* b)
{
	stack<double> st;
	int len = strlen(postFix), y = 0;
	char c;
	*a = 0;
	*b = 1;
	for (int i = 0; i < len; i++)
	{
		c = postFix[i];
		if (judge(c) == false)//不是运算符
		{
			if (c >= '0' && c <= '9')
			{
				double int_c = (double)(c - '0');//强制转换
				y = y * 10.0 + int_c;
			}
			else if (c == ' ')
			{
				st.push(y); y = 0;
			}
		}
		else
		{
			double op1, op2;
			double res = 0, z = 0;
			op1 = st.top();
			st.pop();
			op2 = st.top();
			st.pop();
			switch (c)
			{
			case '+':
				res = op1 + op2;
				break;
			case '-':
				res = op2 - op1;
				break;
			case '*':
				res = op1 * op2;
				break;
			case '/':

				if (op1 != 0)
					res = op2 / op1;
				else
				{
					int temp;
					temp = *b;
					*a = temp;
					goto second;
				}
			second: break;
			}
			st.push(res);
		}
	}
	return st.top();
}
int execute()//操作函数
{
	char inFix[100];
	char postFix[100];
	char postFix1[100];
	double val;
	int z = 0, z1 = 1;
re:cout<<"请输入一个中缀表达式"<<endl;
	while (1)
	{

		gets_s(inFix);
		if (strlen(inFix) == 0)
			continue;
		change(inFix, postFix1);
	cout<<"后缀表达式为"<<postFix1<<endl;
	change1(inFix, postFix);

		val = postFixEval(postFix, &z, &z1);
		if (z == 0)		
			cout << "结果是" << val << endl;	
		else
		{
			cout << "输入表达式不合法,除数不能为0,请重新输入,谢谢\n" << endl;
			goto re;
		} 
		return 0;
	}
}
int main()
{
	char inFix[100];
	int a;
	while (1)
	{
		cout<<"------------操作菜单------------"<<endl;
		cout<<"   1:执行操作      ";
		cout << "2:退出程序"<<endl;
		cout<<"--------------------------------"<<endl;
		printf("  按数字键选择要执行的操作: \n");
		cin>>a;
		cout << endl;
		if (a == 2)
			break;
		switch (a)//如果输入错误,带*/号就无限循环
		{
		case 1:execute();   break;
		default:
			cout << "输入的数字不正确,请重新输入"<<endl;
			break;
		}
	}
	return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).