模拟简单计算器
public class CalSuffix {
private MyIntStack stack;
private String input;
public CalSuffix(String input){
this.input=input;
int length=input.length();
stack=new MyIntStack(length);
}
public int doCalc(){
int num1,num2,result;
for (int i = 0; i <input.length() ; i++) {
char c=input.charAt(i);
if (c>='0'&&c<='9'){
stack.push(c-'0'); //如果是数字 直接压入栈中
}else {
num2=stack.pop(); // 先弹出第二个操作数
num1=stack.pop(); // 第一个操作数
switch (c){
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
if (num2==0) {
result=0;
break;}
result=num1 / num2;
break;
default:
result=0;
break;
}//end switch
stack.push(result);
}// end else
} // end for
result = stack.pop();
return result;
}
public static void main(String[] args) {
InfixToSuffix in=new InfixToSuffix("1*(2+3)-5/(2+3)");
MyCharStack my=in.doTransfer();
my.displayStack();
String s="";
while (!my.isEmpty()){
s+=my.pop();
}
CalSuffix cs=new CalSuffix(s); //"123+*523+/-"
System.out.println("结果");
System.out.println(cs.doCalc());
}
}
// 前缀表达式转换为后缀表达式
public class InfixToSuffix {
private MyCharStack s1; //定义运算符栈
private MyCharStack s2; //定义存储结果栈
private String input;
//默认构造方法,参数为输入的中缀表达式
public InfixToSuffix(String in){
input=in;
s1=new MyCharStack(input.length());
s2=new MyCharStack(input.length());
}
//中缀表达式转换为后缀表达式,将结果存储在栈中返回,逆序显示后缀表达式
public MyCharStack doTransfer(){
for (int i = 0; i <input.length() ; i++) {
System.out.print("s1栈元素为:");
s1.displayStack();
System.out.print("s2栈元素为:");
s2.displayStack();
char ch=input.charAt(i);
System.out.println("当前解析字符:"+ch);
switch (ch){
case '+':
case '-':
gotOper(ch,1);
break;
case '*':
case '/':
gotOper(ch,2);
break;
case '(':
s1.push(ch); //如果当前字符是‘(’,则将其入栈
break;
case ')':
gotParen(ch);
break;
default: // 如果是操作数,则直接压入s2
s2.push(ch);
break;
} //end switch
} // end for
while (!s1.isEmpty()){
s2.push(s1.pop());
}
return s2;
}
private void gotOper(char ch, int i) {
while(!s1.isEmpty()){
char optop=s1.pop();
if (optop=='('){// 如果栈顶是(,直接将操作符压入栈s1
s1.push(optop);
break;
}else {
int j;
if (optop=='+'||optop=='-'){
j = 1;
}else {
j =2;
}
if (j<i){ //如果当前运算符比S1栈顶运算符优先级高,则将运算符压入s1
s1.push(optop);
break;
}else {//如果当前运算符与栈顶运算符相同或者小于优先级别,那么僵s1栈顶元素的运算符弹出并压入到s2中
//并且要再次转到while循环中与s1中新的栈顶元素相比较
s2.push(optop);
}
}// else
} // end while
//如果s1为空,将当前解析的运算符直接压入s1
s1.push(ch);
}
//如果字符是‘)’时,如果栈顶‘(’,则将这一对括号丢弃,否则依次弹出s1栈顶的字符,压入s2,知道遇到‘(’
public void gotParen(char ch){
while (!s1.isEmpty()){
char chx=s1.pop();
if (chx=='('){
break;
}else {
s2.push(chx);
}
}
}
}
//定义一个栈
public class MyCharStack {
private char[] array;
private int maxSize;
private int top;
public MyCharStack() {
}
public MyCharStack(int size) {
array = new char[size];
this.maxSize = size;
top =-1 ;
}
//压入数据
public void push(char value){
if (top<maxSize-1){
array[++top]=value;
}
}
//弹出栈顶数据
public char pop(){
return array[top--];
}
//访问栈顶数据
public char peek(){
return array[top];
}
//查看指定位置的元素
public char peekN(int n){
return array[n];
}
// 为了便于后面分解展示栈中的内容,我们增加了一个遍历栈的方法(实际上栈只能访问栈顶元素)
public void displayStack(){
System.out.println("Stack(bottom-->top):");
for (int i = 0; i <top+1 ; i++) {
System.out.print(peekN(i));
System.out.print(" ");
}
System.out.println();
}
//判断栈是否为空
public boolean isEmpty(){
return top==-1;
}
//判断栈是否满了
public boolean isFull(){
return top==(maxSize-1);
}
}
public class MyIntStack {
private int[] array;
private int maxSize;
private int top;
public MyIntStack() {
}
public MyIntStack(int size) {
array = new int[size];
maxSize = size;
top =-1 ;
}
//压入数据
public void push(int value){
if (top<maxSize-1){
array[++top]=value;
}
}
//弹出栈顶数据
public int pop(){
if (top==-1) return 0;
return array[top--];
}
//访问栈顶数据
public int peek(){
return array[top];
}
//查看指定位置的元素
public int peekN(int n){
return array[n];
}
// 为了便于后面分解展示栈中的内容,我们增加了一个遍历栈的方法(实际上栈只能访问栈顶元素)
public void displayStack(){
System.out.println("Stack(bottom-->top):");
for (int i = 0; i <top+1 ; i++) {
System.out.print(peekN(i));
System.out.print(" ");
}
System.out.println();
}
//判断栈是否为空
public boolean isEmpty(){
return top==-1;
}
//判断栈是否满了
public boolean isFull(){
return top==(maxSize-1);
}
}
尾递归
int f(int n){
if (n<=1) return n;
else
return f(n-1)+f(n-2);
}
int f2(int n){
int pre=0;
int now=1,next=0,j=2;
if (n<=1) return n;
else {
for (j = 2; j <=n ;j ++) {
next= pre+now;
pre=now;
now=next;
}
return next;
}
}