📄 calculator_app.java
字号:
if(str.compareTo("0")==0||op!="")
str=e.getActionCommand();
else str=str+e.getActionCommand();
str3=str;
textf.setText(str);
showStatus(e.getActionCommand());
}
else if ((e.getSource()==button[37])|| (e.getSource()==button[35])||(e.getSource()==button[33])||(e.getSource()==button[31])||(e.getSource()==button[25]))
{ // "+" "+" "-" "*" "/" Buttons
cond5=true;
String tempStr="";
op=e.getActionCommand();
str1=str;
str3=str;
tempStr = str;
str="0";
textf.setText(tempStr);
}
else if ((e.getSource()==button[18])) // "=" Button
{ calculate();
//f.CreateFile();
}
else if (e.getSource()==button[5]) // "BackSpace" Button
{
if(str.length() != 0)
{
str=str.substring(0,str.length()-1);
textf.setText(str);
}
else
JOptionPane.showMessageDialog(null, "Do not use backspace now.");
}
else if (e.getSource()==button[3]) // "C" Button
{
op="";
str=str3;
textf.setText(str);
}
else if (e.getSource()==button[4]) //"CE" Button
{
textf.setText("0");
cond5=true;
str="0";
str1="";
str2="";
}
else if (e.getSource()==button[27]) // "1/X" button
{
pressed=true;
float g=0,p=0;
p=Float.valueOf(str).floatValue();
if(p==0)
{
str="0";
textf.setText("Infinite");
}
else
{
g=(1/p);
str=""+g;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("1/"+p+"="+str);
}
}
else if (e.getSource()==button[28]) // "+/-" Button
{
if (!(str.compareTo("0")==0))
{
if(str.charAt(0)=='-')
str=str.substring(1,str.length());
else
str="-"+str;
textf.setText (str);
}
}
else if(e.getSource()==button[34]) // "Me" button
{
pressed=true;
memoryTagLabel.setText("M");
mem = Double.valueOf(str).doubleValue();
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[36]) //"M+" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem+(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[38]) //"M-" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem-(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[39]) //"MR" Button
{
str=""+mem;
str3=str;
textf.setText(str3);
}
else if(e.getSource()==button[23])
{
pressed=true;
double y=0;
double x=Double.valueOf(str).doubleValue();
y=Math_class.sqr_func(x);
textf.setText(""+y);
str=""+y;
str3=str;
f.CreateFile();
f.addTofile("sqr("+x+")="+str);
}
else if(e.getSource()==button[24])
{
pressed=true;
int x=Integer.valueOf (str).intValue();
if(x>39)
textf.setText("Infinite");
else
{
str=""+Math_class.fact_func(x);
textf.setText(""+str);
str3=str;
f.CreateFile();
f.addTofile(""+x+"!="+str);
}
}
else if(e.getSource()==button[2]) //"Shift" Button
{
if(!cond3)
{
button[20].setLabel("arcSin");
button[21].setLabel("arcCos");
cond3=true;
}
else
{
cond3=false;
button[20].setLabel ("Sin");
button[21].setLabel ("Cos");
}
}
else if(e.getSource()==button[40]) //" R" Button
{
textf.setText("0");
mem=0;
memoryTagLabel.setText("");
}
else if(e.getSource()==button[26])
{
pressed=true;
float p,r;
p=Float.valueOf(str).floatValue();
r=p/100;
str=""+r;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile(""+p+"%="+str);
}
else if(e.getSource()==button[22])
{
pressed=true;
double l=0;
m=Double.valueOf(str).doubleValue();
l=Math.sqrt(m);
str=""+l;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("sqrt("+m+") ="+str);
}
else if (e.getSource()==button[19])
{
pressed=true;
textf.setText(str3);
str=str3;
showStatus(str3);
}
else if (e.getSource()==button[17])
{
pressed=true;
textf.setText(""+3.1415926);
str=""+3.1415926;
}
}//Method ends action performed.
public void calculate()
{
double l=0;
int o=0;
String opp="";
str2=str;
str="0";
m=Double.valueOf(str1).doubleValue();
n=Double.valueOf(str2).doubleValue();
opp=op;
if (op.compareTo("+")==0)
{
l=m+n;
op="";
}
else if (op.compareTo("-")==0)
{
l=m-n;
op="";
}
else if (op.compareTo("*")==0)
{
l=m*n;
op="";
}
else if (op.compareTo("/")==0)
{
l=m/n;
op="";
}
else if (op.compareTo("")==0)
{
l=Double.valueOf(str2).doubleValue();
}
else if (op.compareTo("Pow")==0)
{
o=Integer.valueOf(str2).intValue();
l=Math_class.pow_func(m,o);
op="";
}
cond5=true;
pressed=true;//pressed=true specify the
//End user has pressed a symbol key such as, +, -, /.
str=""+l;
textf.setText(str);
f.CreateFile();
f.addTofile(str1+opp+str2+"="+l);
}//End of method declaration "calculate".
class File_class
{
DataOutputStream Of;
public void WriteFileOnTextArea()// Method to write in the file.
{
String str;
char st;
str="";
try
{
DataInputStream If=new DataInputStream(new FileInputStream("FileOut.txt"));
while(If.readLine()!=null)
{
if ((st=If.readChar())!='E')
{
str=st+str;
}
if (st=='E')
{
textarea1.setText("");
textarea1.append(str);
textarea1.append("\n");
str="";
}
}//Ed while.
If.close();
}//End try.
catch(FileNotFoundException e){} //Catches the error if the file is not found.
catch(IOException e) {}
}// End of method WriteFileOnTextArea().
public void CreateFile()
{
try
{
Of = new DataOutputStream(new FileOutputStream("FileOut.txt"));
Of.writeChars("Begin\n");
}
catch(IOException e)
{
System.err.println("File not opened properly\n"+e.toString());
System.exit(1);
}
}
public void endStore()
{
try
{
Of.close();
}
catch(IOException e)
{
System.err.println("File not closed properly\n"+e.toString());
System.exit(1);
}
}
public void addTofile(String str)
{
try
{
for(int i=str.length()-1;i>=0;i--)
{
Of.writeChar(str.charAt(i));
Of.writeChars("\n");
}
Of.writeChar('E');
Of.writeChars("\n");
}
catch (IOException io){}
}
public void delFile()
{
CreateFile();
try
{
Of.close();
}
catch (IOException io){}
}
} //End of File_class
} // End of class Calculate_class
class Math_class // MATH CLASS
{
public static double sin_func(double x)
{
if (x<=0.0005)
x=x+((x*x*x)/6);
else
x=2*sin_func(x/2)*cos_func(x/2);
return x;
}
public static double cos_func (double x)
{
if (x<=0.0005)
x=1-((x*x*x)/2);
else
x=(cos_func(x/2)*cos_func(x/2))-(sin_func(x/2)*sin_func(x/2));
return x;
}// End of cos_func() method.
public static double convert_rad_deg (double x)
{
//converting from radian to degree
double y;
y=(180*x)/Math.PI;
return y;
}
public static double convert_deg_rad (double x)
{
//Converting from degree to radian.
double y;
y=(Math.PI*x)/180;
return y;
}
public static double pow_func(double x,int y)
{
double s;
s=1;
for(int i=1;i<=y;i++)
s=s*x;
return s;
}
public static double sqr_func (double x)
{
double s=x*x;
return s;
}
public static long fact_func (int x)
//Factorial method.
{
long t=1;
for(int i=x;i>0;i--)
t=i*t;
return t;
}
} // END of class Math_class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -