📄 bigcalculator.java
字号:
}
public void output2()
{
formulaLabel.setText(str1);
}
public String evaluate(String s1,String s3,String s2)
{
String st = "";
if(!s3.equals(new String("sin"))&&!s3.equals(new String("cos"))
&&!s3.equals(new String("tan"))&&!s3.equals(new String("cot")))
{
c1 = s1.charAt(0);c2 = s2.charAt(0);
int a3[] = new int[100];
int m = s1.length(), n = s2.length();
if(c1 != '-' && c2 != '-')
{
int a1[] = new int[m];
int a2[] = new int[n];
for(int i=0;i<m;i++)
{
String temp1 = String.valueOf(s1.charAt(i));
a1[i] = Integer.parseInt(temp1);
}
for(int i=0;i<n;i++)
{
String temp2 = String.valueOf(s2.charAt(i));
a2[i] = Integer.parseInt(temp2);
}
if(s3.equalsIgnoreCase("+"))
a3 = LongInteger.add(a1,a2);
else if(s3.equalsIgnoreCase("-"))
a3 = LongInteger.subtract(a1,a2);
else if(s3.equalsIgnoreCase("×"))
a3 = LongInteger.multiply(a1,a2);
else if(s3.equalsIgnoreCase("/"))
a3 = LongInteger.divide(a1,a2);
else if(s3.equalsIgnoreCase("^"))
a3 = LongInteger.pow(a1,a2);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else if(c1 == '-' && c2 != '-')
{
int a4[] = new int[m-1];
int a5[] = new int[n];
for(int i=1;i<m;i++)
{
String temp1 = String.valueOf(s1.charAt(i));
a4[i-1] = Integer.parseInt(temp1);
}
for(int i=0;i<n;i++)
{
String temp2 = String.valueOf(s2.charAt(i));
a5[i] = Integer.parseInt(temp2);
}
if(s3.equalsIgnoreCase("+"))
{
if(m-1>n)
{
a3 = LongInteger.subtract(a4,a5);
st = "-";
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else
{
a3 = LongInteger.subtract(a5,a4);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
}
else
{
if(s3.equalsIgnoreCase("-"))
a3 = LongInteger.add(a4,a5);
else if(s3.equalsIgnoreCase("×"))
a3 = LongInteger.multiply(a4,a5);
else if(s3.equalsIgnoreCase("/"))
a3 = LongInteger.divide(a4,a5);
st = "-";
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
}
else if(c1 == '-' && c2 == '-')
{
int a6[] = new int[m-1];
int a7[] = new int[n-1];
for(int i=1;i<m;i++)
{
String temp1 = String.valueOf(s1.charAt(i));
a6[i-1] = Integer.parseInt(temp1);
}
for(int i=1;i<n;i++)
{
String temp2 = String.valueOf(s2.charAt(i));
a7[i-1] = Integer.parseInt(temp2);
}
if(s3.equalsIgnoreCase("+"))
{
a3 = LongInteger.add(a6,a7);
st = "-";
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else if(s3.equalsIgnoreCase("-"))
{
if(m-1>n-1)
{
a3 = LongInteger.subtract(a6,a7);
st = "-";
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else
{
a3 = LongInteger.subtract(a7,a6);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
}
else
{
if(s3.equalsIgnoreCase("×"))
a3 = LongInteger.multiply(a6,a7);
else if(s3.equalsIgnoreCase("/"))
a3 = LongInteger.divide(a6,a7);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
}
else if(c1 != '-' && c2 == '-')
{
int a8[] = new int[m];
int a9[] = new int[n-1];
for(int i=0;i<m;i++)
{
String temp1 = String.valueOf(s1.charAt(i));
a8[i] = Integer.parseInt(temp1);
}
for(int i=1;i<n;i++)
{
String temp2 = String.valueOf(s2.charAt(i));
a9[i-1] = Integer.parseInt(temp2);
}
if(s3.equalsIgnoreCase("+"))
{
a3 = LongInteger.subtract(a8,a9);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else if(s3.equalsIgnoreCase("-"))
{
a3 = LongInteger.add(a8,a9);
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
else
{
if(s3.equalsIgnoreCase("×"))
a3 = LongInteger.multiply(a8,a9);
else if(s3.equalsIgnoreCase("/"))
a3 = LongInteger.divide(a8,a9);
st = "-";
for(int i=0;i<a3.length;i++)
st += String.valueOf(a3[i]);
}
}
}
else
{
double in = 0,result = 0;
in = Double.parseDouble(s1);
if(s3.equalsIgnoreCase("sin"))
result = Math.sin(in*((2*pi)/360));
else if(s3.equalsIgnoreCase("cos"))
result = Math.cos(in*((2*pi)/360));
else if(s3.equalsIgnoreCase("tan"))
result = Math.tan(in*((2*pi)/360));
else if(s3.equalsIgnoreCase("cot"))
result = 1/Math.tan(in*((2*pi)/360));
st = String.valueOf(result);
}
return(st);
}
public int fuzhi(String st)
{
int in2 = 10;
if(st.equalsIgnoreCase("+"))
in2 = 1;
else if(st.equalsIgnoreCase("-"))
in2 = 1;
else if(st.equalsIgnoreCase("×"))
in2 = 2;
else if(st.equalsIgnoreCase("/"))
in2 = 2;
else if(st.equalsIgnoreCase("^"))
in2 = 2;
else if(st.equalsIgnoreCase("sin"))
in2 = 3;
else if(st.equalsIgnoreCase("cos"))
in2 = 3;
else if(st.equalsIgnoreCase("tan"))
in2 = 3;
else if(st.equalsIgnoreCase("cot"))
in2 = 3;
else if(st.equalsIgnoreCase("("))
in2 = 0;
else if(st.equalsIgnoreCase(")"))
in2 = 4;
else if(st.equalsIgnoreCase("="))
in2 = 5;
return(in2);
}
public void backSpace()
{
try
{
if(fuzhi(str5) == 3)
{
myStack2.pop();
str1 = "";
output2();
}
else
{
if(str5.equalsIgnoreCase("0")||str5.equalsIgnoreCase("1")||str5.equalsIgnoreCase("2")||
str5.equalsIgnoreCase("3")||str5.equalsIgnoreCase("4")||str5.equalsIgnoreCase("5")||
str5.equalsIgnoreCase("6")||str5.equalsIgnoreCase("7")||str5.equalsIgnoreCase("8")||
str5.equalsIgnoreCase("9"))
{
if(str2.length() == 1)
str2 = "";
else
str2 = str2.substring(0,str2.length()-1);
}
else
{
copystack(myStack3,myStack1);
copystack(myStack4,myStack2);
}
str1 = str1.substring(0,str1.length()-1);
output1();
output2();
}
}
catch(Exception r){}
}
public void copystack(SeqStack stkaim,SeqStack stkto)
{
try
{
SeqStack myStack5 = new SeqStack();
SeqStack myStack6 = new SeqStack();
stkto.top = 0;
while(!stkaim.isEmpty())
{
myStack5.push(stkaim.getTop());
myStack6.push(stkaim.pop());
}
while(!myStack5.isEmpty() && !myStack6.isEmpty())
{
stkto.push(myStack5.pop());
stkaim.push(myStack6.pop());
}
}
catch(Exception q){}
}
public void keyPressed(KeyEvent e)
{
System.out.println("keyPressed:"+e.getKeyCode());
for(int i = 0;i < 10;i++)
{
if(e.getKeyCode() == i + 48)
{
System.out.println("keyPressed:"+e.getKeyCode());
input1(Integer.toString(i));
input2(Integer.toString(i));
output1();
output2();
}
}
if(e.getKeyCode() == 10)
{
input1("=");
output1();
str1 = "";
myStack1.top = 0;
}
}
public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -