栈实现计算器(利用的是中缀表达式)

package stack;

public class Calculator
{
	public static void main(String[] args) throws Exception
	{
		String caculation="1-5+54645-1*2";
		//一个是符号栈,一个是数的栈
		Mystack1 nstack=new Mystack1(20);
		Mystack1 cstack=new Mystack1(20);
		char[] charArray = caculation.toCharArray();
		
		String temp="";//这个temp是处理数字是多位的时候进行拼接用的
		for (int i = 0; i < charArray.length; i++)
		{
			//如果是运算符
			if(cstack.isoperator(charArray[i]))
			{
				//先判断当前的符号栈是否是空的
				if(cstack.isempty())
				{
					cstack.push(charArray[i]);
				}
				else
				{
					//不是空的话就要和栈里面存在的运算符进行比较,决定是要运算还是加入
					if(cstack.priority(charArray[i])<=cstack.priority(cstack.peek()))
					{
						//栈里面的运算符大,就要进行运算,并且将结果 加入数栈
						nstack.push(nstack.count(nstack.pop(), nstack.pop(), cstack.pop()));
						//然后再将当前的符号入栈
						cstack.push(charArray[i]);
					}
					else
					{
						//栈的运算符小就直接入栈
						cstack.push(charArray[i]);
					}
				}
			}
			else
			{
				//是数的话,这里要看下一位是不是数了,
				//如果是数就不要进栈,如果是符号就进栈
				temp=temp+charArray[i];
				//检查后发现会报错,原来少了一个条件,最后一个是数字,
				//但是下面还要看最后的下一个是不是数字,这里就会超出数组的边界
				//所以如果是最后一个数就不用再继续查看下一个数了,直接加进去
				if(i==charArray.length-1)
				{
					nstack.push(Integer.valueOf(temp));
				}
				//如果下一个是符号,就将temp转换为数字进数栈,还要将temp重置
				else if(cstack.isoperator(charArray[i+1]))
				{
					nstack.push(Integer.valueOf(temp));
					temp="";
				}
			}
		}
		//当遍历完后,这条式子也计算得差不多了,
		//但是符号栈里面应该还有应该或者两个符号还没计算的
		while(true)
		{
			if(cstack.isempty())
			{
				break;
			}
			nstack.push(nstack.count(nstack.pop(), nstack.pop(), cstack.pop()));
		}
		System.out.printf("%s的结果是%s",caculation,nstack.pop());
	}
}
class Mystack1
{
	//下面三个分别表示栈的大小,栈顶,栈的存储内容的地方
	private int size;
	private int top;
	private int[] warehouse;
	public Mystack1(int size)
	{
		this.size=size;
		this.top=-1;		//先赋值top是-1
		warehouse=new int[this.size];
	}
	public boolean isfull()
	{
		if(top==this.size-1)
		{
			return true;
		}
		return false;
	}
	public boolean isempty()
	{
		return top==-1;
	}
	public void push(int element)
	{
		if(isfull())
		{
			System.out.println("满了");
			return;
		}
		top++;
		warehouse[top]=element;
	}
	public int pop() throws Exception
	{
		if(isempty())
		{
			throw new Exception("栈空了");
		}
		int temp=warehouse[top];
		top--;
		return temp;
	}
	public void show()
	{
		for(int x=top;x>=0;x--)
		{
			System.out.println(warehouse[x]);
		}
	}
	/*
	 * 下面是对应需要做计算机宽展的功能
	 */
	//返回当前栈顶的元素,但不是pop出来,只是看一下
	public int peek()
	{
		return warehouse[top];
	}
	//返回运算符的优先级,* / 是1, + -是0
	public int priority(int operator)
	{
		if(operator=='*'||operator=='/')
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	//判断是不是运算符
	public boolean isoperator(char temp)
	{
		return temp=='+'||temp=='-'||temp=='*'||temp=='/';
	}
	public int count(int num1,int num2,int operator)
	{
		int result=0;
		switch(operator)
		{
		case '+':
			result=num1+num2;
			break;
		case '-':
			result=num2-num1;
			break;
			//这里就得注意一下顺序了,因为num1先出来,证明它是后进去的,是减数
		case '*':
			result=num1*num2;
			break;
		case '/':
			result=num2/num1;
			break;
		}
		return result;
	}
}

下面不是计算器的代码,只是构思

package stack;
/*
 * 栈是一个先进后出的数据机构,它限制线性表中的元素只能在线性表的同一端进行
 * 可以利用数组,链表来模拟栈
 * 栈的应用也挺广泛的:
 * 1. 二叉树的深度遍历
 * 2. 图的深度优先查找
 * 3. 递归,会将储存下一个指令的地址,参数,区域变量等数据存入到栈堆中
 * 4. 子程序的调用
 */
public class Mystack
{
	//下面三个分别表示栈的大小,栈顶,栈的存储内容的地方
	private int size;
	private int top;
	private int[] warehouse;
	public Mystack(int size)
	{
		this.size=size;
		this.top=-1;		//先赋值top是-1
		warehouse=new int[this.size];
	}
	public boolean isfull()
	{
		if(top==this.size-1)
		{
			return true;
		}
		return false;
	}
	public boolean isempty()
	{
		return top==-1;
	}
	public void push(int element)
	{
		if(isfull())
		{
			System.out.println("满了");
			return;
		}
		top++;
		warehouse[top]=element;
	}
	public int pop() throws Exception
	{
		if(isempty())
		{
			throw new Exception("栈空了");
		}
		int temp=warehouse[top];
		top--;
		return temp;
	}
	public void show()
	{
		for(int x=top;x>=0;x--)
		{
			System.out.println(warehouse[x]);
		}
	}
	public static void main(String[] args)
	{
		System.out.println("经测试没问题");
	}
}

发布了133 篇原创文章 · 获赞 37 · 访问量 4735

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/104326640