java静态栈(含回文数和计算机代码展示)

目录

一:栈的思想

二:栈的操作

三:回文数

3.1 栈的定义

3.2 回文数的判断

四:计算机

4.1 栈的定义(数字栈、符号栈)

4.1.1 判断是否是一个运算符

4.1.2 判断运算符的优先级

4.2 计算机代码展示 

4.2.1 栈的定义

4.2.2 计算机的运算


一:栈的思想

后进先出

二:栈的操作

压栈

弹栈 

三:回文数

3.1 栈的定义

public class stackproject {
	//栈的大小
	private int max;
	//数组模拟静态栈
	private int[] array;
	//栈顶元素
	private int top = -1;
	
	//栈的大小初始化
	public stackproject(int maxstack) {
		this.max = maxstack;
		array = new int[max];
	}
	
	//压栈
	public void push(int val) {
		//
		if(isFull()) {
			throw new RuntimeException("栈已经满");
		}
		top++;
		array[top] = val;
	}
	//弹栈
	public int pop() {
		if(ifEmpty()) {
			throw new RuntimeException("栈已经空");
		}
		int value = array[top];
		top--;
		return value;
	}
	//是否是空栈
	public boolean ifEmpty() {
		return this.top == -1;
	}
	//是否是满栈
	public boolean isFull() {
		return this.top == max -1;
	}
	//查看栈中所有元素
	public void list() {
		if(ifEmpty()) {
			throw new RuntimeException("栈已经空");
		}
		for(int i = 0; i < top; i++) {
			System.out.print(array[i] + " ");
		}
	}
	//获取栈的长度
	public int stacklength() {
		return top+1;
	}
}

3.2 回文数的判断

public class TestApp {
	public static void main(String[] args) {
		System.out.print(decation("aba"));
	}
	public static boolean decation(String str) {
		staticstack stack = new staticstack(10);
		for(int i = 0; i < str.length(); i++) {
			stack.push(str.charAt(i));
		}
		String newval = "";
		int newlength = stack.length();
		for(int i = 0; i < newlength; i++) {
			char c = (char)stack.pop();
			newval += c;
		}
		if(newval.equals(str)) {
			return true;
		}
		return false;
	}
}

四:计算机

4.1 栈的定义(数字栈、符号栈)

一串表达式中分为数字和字符分别压入数字栈和字符栈,然后通过判断字符的优先级来操作表达式(两个栈)的运算

4.1.1 判断是否是一个运算符

//判断是否是字符
	public boolean isOper(char c) {
		return c == '+' || c == '-' || c == '*' || c== '/';
	}

4.1.2 判断运算符的优先级

public int priority(char c) {
		if(c == '*' || c == '/')
			return 1;
		else if(c == '+' || c == '-')
			return 0;
		else
			return -1;
	}

4.2 计算机代码展示 

4.2.1 栈的定义

package javastack;

public class stackproject {
	//栈的大小
	private int max;
	//数组模拟静态栈
	private int[] array;
	//栈顶元素
	private int top = -1;
	
	//栈的大小初始化
	public stackproject(int maxstack) {
		this.max = maxstack;
		array = new int[max];
	}
	
	//压栈
	public void push(int val) {
		//
		if(isFull()) {
			throw new RuntimeException("栈已经满");
		}
		top++;
		array[top] = val;
	}
	//弹栈
	public int pop() {
		if(ifEmpty()) {
			throw new RuntimeException("栈已经空");
		}
		int value = array[top];
		top--;
		return value;
	}
	//是否是空栈
	public boolean ifEmpty() {
		return this.top == -1;
	}
	//是否是满栈
	public boolean isFull() {
		return this.top == max -1;
	}
	//查看栈中所有元素
	public void list() {
		if(ifEmpty()) {
			throw new RuntimeException("栈已经空");
		}
		for(int i = 0; i < top; i++) {
			System.out.print(array[i] + " ");
		}
	}
	//获取栈的长度
	public int stacklength() {
		return top+1;
	}
	//判断是否是字符
	public boolean isOper(char c) {
		return c == '+' || c == '-' || c == '*' || c== '/';
	}
	//判断栈的容量的大小
	public int stacksize() {
		return this.array.length;
	}
	//获取栈顶元素
	public int peek() {
		return this.array[top];
	}
	//判断运算符的优先级
	public int priority(char c) {
		if(c == '*' || c == '/')
			return 1;
		else if(c == '+' || c == '-')
			return 0;
		else
			return -1;
	}
	//计算两个数的结果
	public int calculate(int num1, int num2, int oper) {
		int result = 0;
		switch(oper) {
			case '+':
				result = num1 + num2;
				break;
			case '-':
				result = num2 - num1;
				break;
			case '*':
				result = num1 * num2;
				break;
			case '/':
				result = num2 / num1;
				break;
			default:
				break;
		}
		return result;
	}
}

4.2.2 计算机的运算

package javastack;

public class TestApp {
	public static void main(String[] args) {
		String str = "4+3*2-1";
		stackproject numstack = new stackproject(10);
		stackproject symbolstack = new stackproject(10);
		int length = str.length();
		int temp1 = 0;
		int temp2 = 0;
		int symbolChar = 0;
		int result = 0;
		String values = "";
		for(int i = 0; i < length; i++) {
			char c = str.charAt(i);
			if(symbolstack.isOper(c)) {
				if(!symbolstack.ifEmpty()) {
					if(symbolstack.priority(c) <= symbolstack.priority((char)symbolstack.peek())) {
						temp1 = numstack.pop();
						temp2 = numstack.pop();
						symbolChar = symbolstack.pop();
						result = numstack.calculate(temp1, temp2, symbolChar);
						numstack.push(result);
						symbolstack.push(c);
					}
					else {
						symbolstack.push(c);
					}
				}
				else {
					symbolstack.push(c);
				}
			}
			else {
				//多位数字
				values += c;
				if(i == length -1) {
					numstack.push(Integer.parseInt(values));
				}else {
					char data = str.substring(i + 1, i + 2).charAt(0);
					if(symbolstack.isOper(data)) {
						numstack.push(Integer.parseInt(values));
					}
				}
				values = "";
			}
		}
		while(true) {
			if(symbolstack.ifEmpty()) {
				break;
			}
			temp1 = numstack.pop();
			temp2 = numstack.pop();
			symbolChar = symbolstack.pop();
			result = numstack.calculate(temp1, temp2, symbolChar);
			numstack.push(result);
		}
		int res = numstack.pop();
		System.out.print(res);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值