📄 stactmachine.java
字号:
package org.yancan;
public class stactMachine
{
String stactcode ="";
arrayStact tempstact = new arrayStact();
public stactMachine(String temp)
{
stactcode = temp;
}
public void outputstactcode()
{
if(stactcode=="")
System.out.println("expression is wrong, so you must correct it~~!Then go on~!\n");
else
{
for(int i=0;i<stactcode.length();i++)
System.out.print(stactcode.charAt(i));
System.out.println("\n\n");
}
}
public void outputcompute()
{
if(stactcode=="")
;
else
{
String[] part = stactcode.split("\n");
for(int j=0;j<part.length;j++)
{
if(part[j].charAt(0)=='P')
tempstact.push(Integer.parseInt( part[j].substring(5, part[j].length()) ) );
else
{
// int tempa,tempb,tempc;
float tempa,tempb,tempc;
tempb=tempstact.pop();
tempa=tempstact.pop();
if(part[j].charAt(0)=='A')
{
tempc=tempa+tempb;
System.out.println(""+ formatoutput(tempa) + "+" + formatoutput(tempb) +" = "+ tempc+"\n");
}
if(part[j].charAt(0)=='S')
{
tempc=tempa-tempb;
System.out.println(""+ formatoutput(tempa) + "-" + formatoutput(tempb) +" = "+ tempc+"\n");
}
if(part[j].charAt(0)=='M')
{
tempc=tempa*tempb;
System.out.println(""+ formatoutput(tempa) + "*" + formatoutput(tempb) +" = "+ tempc+"\n");
}
if(part[j].charAt(0)=='D')
{
tempc=tempa/tempb;
System.out.println(""+ formatoutput(tempa) + "/" + formatoutput(tempb) +" = "+ tempc+"\n");
}
}//end inner else
}// end outer else
}//end for
}
// private String formatoutput(int temp)
private String formatoutput(float temp)
{
if(temp<0)
return "("+temp+")";
else
return ""+temp+"";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -