import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.lang.Math;
public class jisuanqi { public static void main(String[] args) {
UI ui= new UI();
ui.ui0(); }}
class UI
{
private int i=0;
private int j=0,k=0;
double sum=0;
int radix[]=new int[20]; //为1则该位置有小数点
String sign[]=new String[10];//存储输入的符号
double arr[]=new double[20]; //存储输入的数值,
TextField t=new TextField(30);
void ui0()
{
Arrays.fill(arr,10);
Frame frame = new Frame("计算器");
frame.setBounds(800,300,270,200);
Panel panel = new Panel();
Button but[] =new Button[20];
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent a) {
System.exit(0);
}
} );
panel.add(t); //插入文本域
frame.add(panel,BorderLayout.NORTH);//设置文本域位置
Panel pan =new Panel();
pan.setLayout(new GridLayout(5,4,3,3)); //插入网状结构设置按键
String name[]= {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=",".","c","","",""};
for(int i=0;i<name.length;i++)//对按键赋值
{
but[i]=new Button(name[i]);
pan.add(new Button(name[i]));
}
frame.add(pan,BorderLayout.CENTER);//网状结构位置
//自动设定界面大小frame.pack();
frame.setVisible(true);//界面可见
for(i=0;i<name.length;i++) {
if(i<10)
{
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) { /////////按钮未具有效应??!!
t.setText(t.getText()+i);
arr[j]=Double.parseDouble(name[i]); //将按钮输入的值存于数组中用于以后的计算
j++;
}
} );}
else if(i>=10 && i<14)
{
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
t.setText(t.getText()+name[i]);
sign[k]=name[i]; //将按钮输入的符号存于数组中用于以后的计算
k++;
j++;
}
} );}
else if(i==14) {
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int tab,s=0;
double su[]=new double[10]; //存储整合后的数据;
for(tab=0;tab<arr.length;tab++) {
if(arr[tab]!=10) {
s+=arr[tab]*Math.pow(10,tab);
}//else if(radix[tab]=1){}
else {
for(int q=0;q<10;q++) {
if(su[q]==0) {
su[q]=s;
s=0;
}
}
}
}
sum=su[0];
for(int w=0;w<10;w++) {
switch(sign[w]) {
case "+":sum+=su[w+1];break;
case "-":sum-=su[w+1];break;
case "*":sum*=su[w+1];break;
case "/":sum/=su[w+1];break;
default:break;
}
}
t.setText(String.valueOf(sum) );
}
} );}
if(i==15) {
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
radix[j]=1;
j++;
}
} );}
else if(i==16){
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ //c,全部置零,重新输入;
Arrays.fill(arr,10);
Arrays.fill(sign,null);
j=0;
t.setText("");
}
} );}
}//for循环外
}//ui函数外
}
import java.awt.event.*;
import java.util.Arrays;
import java.lang.Math;
public class jisuanqi { public static void main(String[] args) {
UI ui= new UI();
ui.ui0(); }}
class UI
{
private int i=0;
private int j=0,k=0;
double sum=0;
int radix[]=new int[20]; //为1则该位置有小数点
String sign[]=new String[10];//存储输入的符号
double arr[]=new double[20]; //存储输入的数值,
TextField t=new TextField(30);
void ui0()
{
Arrays.fill(arr,10);
Frame frame = new Frame("计算器");
frame.setBounds(800,300,270,200);
Panel panel = new Panel();
Button but[] =new Button[20];
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent a) {
System.exit(0);
}
} );
panel.add(t); //插入文本域
frame.add(panel,BorderLayout.NORTH);//设置文本域位置
Panel pan =new Panel();
pan.setLayout(new GridLayout(5,4,3,3)); //插入网状结构设置按键
String name[]= {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=",".","c","","",""};
for(int i=0;i<name.length;i++)//对按键赋值
{
but[i]=new Button(name[i]);
pan.add(new Button(name[i]));
}
frame.add(pan,BorderLayout.CENTER);//网状结构位置
//自动设定界面大小frame.pack();
frame.setVisible(true);//界面可见
for(i=0;i<name.length;i++) {
if(i<10)
{
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) { /////////按钮未具有效应??!!
t.setText(t.getText()+i);
arr[j]=Double.parseDouble(name[i]); //将按钮输入的值存于数组中用于以后的计算
j++;
}
} );}
else if(i>=10 && i<14)
{
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
t.setText(t.getText()+name[i]);
sign[k]=name[i]; //将按钮输入的符号存于数组中用于以后的计算
k++;
j++;
}
} );}
else if(i==14) {
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int tab,s=0;
double su[]=new double[10]; //存储整合后的数据;
for(tab=0;tab<arr.length;tab++) {
if(arr[tab]!=10) {
s+=arr[tab]*Math.pow(10,tab);
}//else if(radix[tab]=1){}
else {
for(int q=0;q<10;q++) {
if(su[q]==0) {
su[q]=s;
s=0;
}
}
}
}
sum=su[0];
for(int w=0;w<10;w++) {
switch(sign[w]) {
case "+":sum+=su[w+1];break;
case "-":sum-=su[w+1];break;
case "*":sum*=su[w+1];break;
case "/":sum/=su[w+1];break;
default:break;
}
}
t.setText(String.valueOf(sum) );
}
} );}
if(i==15) {
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
radix[j]=1;
j++;
}
} );}
else if(i==16){
but[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ //c,全部置零,重新输入;
Arrays.fill(arr,10);
Arrays.fill(sign,null);
j=0;
t.setText("");
}
} );}
}//for循环外
}//ui函数外
}