求助编写计算器的问题
请问下各位大虾们,我现在在编一个计算器的作业交上去,
请问下,* - + = 这些符号怎么实现他的功能,
需要用到哪些类,接口
[解决办法]
直接用好了
int a,b,c;
c=a+b;
c=a-b;
c=a*b;
c=a/b;
是不是被概念忽悠了...张口就类和接口...
这些基本的...系统默认类里面都带上了
[解决办法]
我先昏一下~
[解决办法]
多写几个方法就可以了啊!每个方法就相当于一个计算就可以了
[解决办法]
估计以前lz是搞汇编是的吧,呵呵
[解决办法]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyCalculater extends JFrame implements ActionListener{
JTextField tf=new JTextField();
String num= " ";
Double number;
int pointCount=0;
char operation;
JButton b1=new JButton( "1 ");
JButton b2=new JButton( "2 ");
JButton b3=new JButton( "3 ");
JButton b4=new JButton( "4 ");
JButton b5=new JButton( "5 ");
JButton b6=new JButton( "6 ");
JButton b7=new JButton( "7 ");
JButton b8=new JButton( "8 ");
JButton b9=new JButton( "9 ");
JButton b0=new JButton( "0 ");
JButton b10=new JButton( "+ ");
JButton b11=new JButton( "- ");
JButton b12=new JButton( "* ");
JButton b13=new JButton( "/ ");
JButton b14=new JButton( "= ");
JButton b15=new JButton( ". ");
JButton b16=new JButton( " <- ");
JButton b17=new JButton( "S ");
JButton b18=new JButton( "C ");
JButton b19=new JButton( "+/- ");
void buildConstraints(GridBagConstraints gbc,int gx,int gy,
int gw,int gh,int wx,int wy){
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
}
public MyCalculater(){
super( "MyCaculater ");
setBounds(200,200,280,215);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
GridBagLayout exlayout=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
JPanel expane=new JPanel();
expane.setLayout(exlayout);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
GridLayout inlayout=new GridLayout(4,5,5,5);
JPanel inpane2=new JPanel();
inpane2.setLayout(inlayout);
inpane2.add(b1);
inpane2.add(b2);
inpane2.add(b3);
inpane2.add(b10);
inpane2.add(b18);
inpane2.add(b4);
inpane2.add(b5);
inpane2.add(b6);
inpane2.add(b11);
inpane2.add(b16);
inpane2.add(b7);
inpane2.add(b8);
inpane2.add(b9);
inpane2.add(b12);
inpane2.add(b19);
inpane2.add(b0);
inpane2.add(b15);
inpane2.add(b13);
inpane2.add(b17);
inpane2.add(b14);
tf.setEditable(false);
buildConstraints(constraints,0,0,1,1,100,20);
constraints.fill=GridBagConstraints.HORIZONTAL;
exlayout.setConstraints(tf,constraints);
expane.add(tf);
buildConstraints(constraints,0,1,1,1,100,80);
exlayout.setConstraints(inpane2,constraints);
expane.add(inpane2);
setContentPane(expane);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==b10){
if(num!= " "){// if num have not exact digital,then press +,-*/ button
operation= '+ ';
number=new Double(num);
num= " ";
if(pointCount!=0)
pointCount=0;
}
}
if(event.getSource()==b11){
if(num!= " "){
operation= '- ';
number=new Double(num);
num= " ";
if(pointCount!=0)
pointCount=0;
}
}
if(event.getSource()==b12){
if(num!= " "){
operation= '* ';
number=new Double(num);
num= " ";
if(pointCount!=0)
pointCount=0;
}
}
if(event.getSource()==b13){
if(num!= " "){
operation= '/ ';
number=new Double(num);
num= " ";
if(pointCount!=0)
pointCount=0;
}
}
//number control
if(event.getSource()==b1){
num=num+ '1 ';
tf.setText(num);
}
if(event.getSource()==b2){
num=num+ '2 ';
tf.setText(num);
}
if(event.getSource()==b3){
num=num+ '3 ';
tf.setText(num);
}
if(event.getSource()==b4){
num=num+ '4 ';
tf.setText(num);
}
if(event.getSource()==b5){
num=num+ '5 ';
tf.setText(num);
}
if(event.getSource()==b6){
num=num+ '6 ';
tf.setText(num);
}
if(event.getSource()==b7){
num=num+ '7 ';
tf.setText(num);
}
if(event.getSource()==b8){
num=num+ '8 ';
tf.setText(num);
}
if(event.getSource()==b9){
num=num+ '9 ';
tf.setText(num);
}
if(event.getSource()==b0){
num=num+ '0 ';
tf.setText(num);
}
if(event.getSource()==b15){
if(pointCount==0){
num=num+ '. ';
pointCount++;
}
tf.setText(num);
}
if(event.getSource()==b16){
if(num.length()==1){
num= " ";
tf.setText(num);
char n[]=num.toCharArray();
boolean pointExist=false;
for(int i=0;i <=(num.length()-1);i++)
{
if(n[i]== '. ')
pointExist=true;
break;
}
if(pointExist==false)
pointCount=0;
}///////////////////
if(num.length()> 1){
num=num.substring(0,num.length()-1);
tf.setText(num);
char n[]=num.toCharArray();
boolean pointExist=false;
for(int i=0;i <=(num.length()-1);i++)
{
if(n[i]== '. ')
pointExist=true;
break;
}
if(pointExist==false)
pointCount=0;
}///////////////////////////////
}
if(event.getSource()==b17){
if(num!= " "){
Double n=new Double(num);
if(n.doubleValue()> =0){
double display=Math.sqrt(n.doubleValue());
num= " "+display;
tf.setText(num);
}
else
tf.setText( "input error ");
}
}
if(event.getSource()==b18){
num= "0 ";
pointCount=0;
tf.setText(num);
}
if(event.getSource()==b19){
if(num!= " "){
Double n=new Double(num);
if(n.floatValue()> 0){
num= "- "+n.doubleValue();
tf.setText(num);
}
else {
num= " "+(-n.doubleValue());
tf.setText(num);
}
}
}
if(event.getSource()==b14){
double result;
pointCount=1;//result is a float number with '. '
switch (operation){
case '+ ':if(num!= " "){
Double n1=new Double(num);
result=n1.doubleValue()+number.doubleValue();
num= " "+result;
tf.setText( " "+result);
operation= '\0 ';
number=new Double( "0 ");
n1=null;
}
break;
case '- ':if(num!= " "){
Double n2=new Double(num);
result=number.doubleValue()-n2.doubleValue();
num= " "+result;
tf.setText( " "+result);
operation= '\0 ';
number=new Double( "0 ");
n2=null;
}
break;
case '* ':if(num!= " "){
Double n3=new Double(num);
result=number.doubleValue()*n3.doubleValue();
num= " "+result;
tf.setText( " "+result);
operation= '\0 ';
number=new Double( "0 ");
n3=null;
}
break;
case '/ ':if(num!= " "){
Double n4=new Double(num);
if(n4.doubleValue()!=0){
result=number.doubleValue()/n4.doubleValue();
num= " "+result;
tf.setText( " "+result);
number=new Double( "0 ");
operation= '\0 ';
n4=null;
}
else{
tf.setText( "input error ");
n4=null;
}
}
break;
default : tf.setText( " "+num);
}
}
}// end actionPerformed()
public static void main(String[] s){
MyCalculater calculater=new MyCalculater();
}
}
[解决办法]
上面的计算器只能按先后顺序计算吧,好像不可以按算术符号的优先级来进行计算。
[解决办法]
晕 有必要搞那么长吗
[解决办法]
不简洁
使用工厂方法!
------解决方案--------------------
我想知道怎么按算术符号的优先级来进行计算
[解决办法]
http://dreamerhx.ys168.com/
myprogramming 我的程序
csdn_Express.rar 11KB CSDN帖子部分代码
我以前讨论的表达式计算,希望能给大家帮助
[解决办法]
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class myCount extends JFrame implements ActionListener
{
JButton clear;
JTextField scanner;
mainBlock main;
frmdia dialog;
float floFont,floLast,floNum;
String strFont= " ";
String strLast= " ";
String strNum= " ";
String strSign= " ";
JPanel pane;
myCount()
{
super( "计 算 器 ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
dialog=new frmdia(this, "提示 ",true);
pane=new JPanel();
JPanel smlpane=new JPanel();
smlpane.setLayout(new GridLayout(2,1,20,20));
pane.setLayout(new GridLayout(2,1,0,10));
scanner=new JTextField( "0 ",20);
scanner.setEditable(false);
clear=new JButton( "清 零 ");
clear.addActionListener(this);
main=new mainBlock(this);
smlpane.add(scanner);
smlpane.add(clear);
pane.add(smlpane);
pane.add(main);
setContentPane(pane);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==clear)
{
strSign= " ";
strLast= " ";strFont= " ";
scanner.setText( "0 ");
floFont=0;
floLast=0;
repaint();
}
}
public static void main(String args[])
{
myCount mycount=new myCount();
mycount.pack();
}
public Insets getInsets()
{
return new Insets(40,15,25,15);
}
void others()
{
floFont=Float.parseFloat(strLast);
if(strSign== "+ ")
floNum=floLast+floFont;
else if(strSign== "- ")
floNum=floLast-floFont;
else if(strSign== "* ")
floNum=floLast*floFont;
else if(strSign== "/ ")
{
if(floFont==0)
{
dialog.setVisible(true);
strSign= " ";strNum= " ";
strLast= " ";strFont= " ";
scanner.setText( "0 ");
floFont=0;
floLast=0;
floNum=0;
repaint();
}else{
floNum=floLast/floFont;
}
}
strLast= " "+floNum;
strSign= " ";
}
void countNumber(mainBlock mblock,String str)
{
strNum=str;
if(strNum== ". ")
strNum= "10 ";
else if(strNum== "= ")
strNum= "11 ";
else if(strNum== "+ ")
strNum= "12 ";
else if(strNum== "- ")
strNum= "13 ";
else if(strNum== "* ")
strNum= "14 ";
else if(strNum== "/ ")
strNum= "15 ";
int i=Integer.parseInt(strNum);
switch(i)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strLast+=(i);
break;
case 10:
strLast+= ". ";
break;
case 11:
if(strLast!= " ")
{
others();
}
break;
case 12:
if((strLast!= " ") && (strLast!= "- "))
{
if(strSign!= " ")
{
others();
}
strSign= "+ ";
floLast=Float.parseFloat(strLast);
strLast= " ";
}
break;
case 13:
if(strSign!= " ")
{
others();
}
strSign= "- ";
floLast=Float.parseFloat(strLast);
strLast= " ";
break;
case 14:
if((strLast!= " ") && (strLast!= "- "))
{
if(strSign!= " ")
{
others();
}
strSign= "* ";
floLast=Float.parseFloat(strLast);
strLast= " ";
}
break;
case 15:
if((strLast!= " ") && (strLast!= "- "))
{
if(strSign!= " ")
{
others();
}
strSign= "/ ";
floLast=Float.parseFloat(strLast);
strLast= " ";
}
break;
}
scanner.setText(strLast);
pane.repaint();
}
}
class mainBlock extends JPanel implements ActionListener
{
myCount frame;
JButton[] btn=new JButton[16];
mainBlock(myCount parent)
{
frame=parent;
setLayout(new GridLayout(4,4,5,5));
for(int i=0;i <10;i++)
btn[i]=new JButton( " "+i);
btn[10]=new JButton( ". ");
btn[11]=new JButton( "= ");
btn[12]=new JButton( "+ ");
btn[13]=new JButton( "- ");
btn[14]=new JButton( "* ");
btn[15]=new JButton( "/ ");
for(int i=0;i <16;i++)
{btn[i].addActionListener(this);}
add(btn[12]);
add(btn[13]);
add(btn[14]);
add(btn[15]);
for(int i=1;i <10;i++)
add(btn[i]);
add(btn[0]);
add(btn[10]);
add(btn[11]);
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
frame.countNumber(this,str);
}
}
class frmdia extends Dialog implements ActionListener
{
Button btn;
Label lg;
frmdia(JFrame frm,String str,boolean b1)
{
super(frm,str,b1);
setBounds(200,250,95,110);
setVisible(false);
setLayout(null);
btn=new Button( "确定 ");
lg=new Label( " 错误参数 ");
add(lg);add(btn);
lg.setBounds(10,40,95,30);
btn.setBounds(30,70,40,25);
btn.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
setVisible(false);
}
}
}
[解决办法]
+,-,*,/这些功能是自己写函数的,其实不难,自己多想想就行了,我编计算器用了两个晚上呢!