栈
代码实现栈
class ArrayStack{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
this.stack = new int[this.maxSize];
}
public boolean isFull(){
return this.top == maxSize-1;
}
public boolean isEmpty(){
return this.top == -1;
}
public void push(int value){
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if(isEmpty()){
System.out.println("栈空");
return;
}
for(int i=top;i>=0;i--){
System.out.println(stack[i]);
}
}
}
计算器(中缀表达式)
public class Calculator{
public static void main(String[] args){
String expression = "3+2*6-2";
ArrayStack numStack = new ArrayStack(20);
ArrayStack operStack = new ArrayStack(10);
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = '';
String keepNum = "";
while(true){
ch = expression.chartAt(index);
if(operStack.isOper(ch)){
if(!operStack.isEmpty()){
if(operStack.priority(ch) <= operStack.priority(operStack.peek)){
num1 = numStack.pop();
num2 = nmuStack.pop();
oper = operStack.pop();
res = numStack.cal(num1,num2,oper);
numStack.push(res);
operStack.push(ch);
}else{
operStack.push(ch);
}
}else{
operStack.push(ch);
}
}else{
keepNum += ch;
if(index==expression.length()-1){
numStack.push(Integer.parseInt(keepNum));
}else{
if(operStack.isOper(expression.charAt(index+1))){
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
if(index>=expression.length()){
break;
}
}
while(true){
if(operStack.isEmpty()){
break;
}
num1 = numStak.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1,num2,oper);
numStack.push(res);
}
int res2 = numStack.pop();
System.outprintf("%s=%d",expression,res2);
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
this.stack = new int[this.maxSize];
}
public int peek(){
return stack[top];
}
public boolean isFull(){
return this.top == maxSize-1;
}
public boolean isEmpty(){
return this.top == -1;
}
public void push(int value){
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if(isEmpty()){
System.out.println("栈空");
return;
}
for(int i=top;i>=0;i--){
System.out.println(stack[i]);
}
}
public int priority(int oper){
if(oper == '*' || oper == '/'){
return 1;
}else if(oper =='+' || oper =='-'){
return 0;
}else{
return -1;
}
}
public booleam isOper(char val){
return val == '+' || val== '-' || val == '*' || val == '/';
}
public int cal(int num1,int num2,int oper){
int res = 0;
switch(oper){
case '+': res = num1+num2;break;
case '-': res = num1-num2;break;
case '*': res = num1*num2;break;
case '/': res = num1/num2;break;
default:break;
}
return res;
}
}
计算器(后缀表达式)
public class PolandNation{
public static void main(Styring[] args){
String suffixExpression = "3 4 + 5 * 6 -";
List<String> rpnList = getList(suffixExpression);
int res = calculate(list);
}
public static List<String> getListString(String suffixExpression){
String[] split = suffixExpression.split(" ");
List<String> lsit = new ArrayList<String>();
for(Stringe ele : split){
list.add(ele);
}
return list;
}
public static int calculate(List<String> ls){
Stack<String> stack = new Stack<String>();
for(String item : ls){
if(item.matches("\\d+")){
stack.push(item);
}else{
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if(item.equals("-")){
res = num1+num2;
}else if(item.equals("-")){
res num1 - num2;
}else if(item.equals(*)){
res = num1* num2;
}else if(item.equals("/")){
res = num1 / num2;
}else{
throw new RuntimeException("运算符有误");
}
stack.push(""+res)
}
}
return Integer.parseInt(stack.pop());
}
}
中缀转后缀
public class Demo{
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
public static List<String> parseSuffixExpressionList(List<String> ls){
Stack<String> s1 = new Stack<String>();
List<String> s2 = new List<String>();
for(String item : ls){
if(item.matches("\\d+")){
s2.add(item);
}else if(item.equals("(")){
s1.push(item);
}else if(item.equals(")")){、
while(!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();
}else{
while(s1.size()!=0 && getOperValue(s1.peek())>= getOperValue(item)){
s2.add(s1.pop());
}
s1.push(item);
}
}
while(s1.size()!=0){
s2.add(s1.pop());
}
return s2;
}
pulic static List<String> toInfixExpressionList(String s){
List<String> ls = new ArrayList<String>();
int i=0;
String str;
char c;
do{
if((c=s.charAt(i)))=<48 || (c=s.charAt(i)))>57){
ls.add(""+c);
i++
}else{
str = "";
while(i<s.length()&& (c=s.charAt(i))>=48 && (c=s.charAt(i))<=57){
str += c;
i++;
}
ls.add(str);
}
}while(i<s.length());
return ls;
}
public static int getOperValue(String operation){
int result = 0;
switch(operation){
case "+":result = ADD;break;
case "-":result = ADD;break;
case "*":result = ADD;break;
case "/":result = ADD;break;
default: System.out.println("不存在该运算符"); break;
}
return result;
}
}