新人发帖,求指教。
之前看过很多制作计算器的程序,但可惜的是功能方面都没有那么完美,于是自己想写一个,慢慢写起来才感觉到其中的复杂程度。我第一个写了一个不计算表达式的,还有些细节要改。水平有限,真心求各位指点。第二个正在做一个计算表达式的,我把其中遇到的一点问题说说。第一个[code=java]import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Stack;public class Calculatorlijian { private JFrame f;
private JPanel panel1, panel2, panel3;
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private JButton bPoint, bx, bAdd, bDec, bMul, bDiv, bSin, bCos, bTan, bEqu,
bSquareRoot, bSqu, bRec, bMod, bln, blog, bChengFang, bJieCheng,
bPai, be, bBack, bClear, bRational;
private JRadioButton buttonA, buttonB, buttonC, buttonD;
private ButtonGroup group;
private JTextField textField2, textField1; private String s = "";
private String myinput = "";
private String result = "0";
private int op = 0;
private int rationalop = 0;
private int valueop = 0;
private double x, y;
private boolean textFieldop = false; public Calculatorlijian() { f = new JFrame("Calculator");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel(); b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bPoint = new JButton(".");
bx = new JButton("x");
bAdd = new JButton("+");
bDec = new JButton("-");
bMul = new JButton("*");
bDiv = new JButton("/");
bSin = new JButton("sin");
bCos = new JButton("cos");
bTan = new JButton("tan");
bEqu = new JButton("=");
bSquareRoot = new JButton("√");
bSqu = new JButton("x^2");
bRec = new JButton("1/x");
bMod = new JButton("%");
bln = new JButton("ln");
blog = new JButton("log");
bChengFang = new JButton("x^y");
bJieCheng = new JButton("n!");
bPai = new JButton("π");
be = new JButton("e");
bBack = new JButton("BACK");
bClear = new JButton("CLEAR");
bRational = new JButton("∟"); buttonA = new JRadioButton("十进制");
buttonA.setSelected(true);
buttonB = new JRadioButton("二进制");
buttonC = new JRadioButton("八进制");
buttonD = new JRadioButton("十六进制"); group = new ButtonGroup(); textField2 = new JTextField(15);
textField1 = new JTextField(15);
textField2.setEditable(false); panel1.setLayout(new GridLayout(7, 5));
panel1.add(b7);
panel1.add(b8);
panel1.add(b9);
panel1.add(bDiv);
panel1.add(bSin);
panel1.add(b4);
panel1.add(b5);
panel1.add(b6);
panel1.add(bMul);
panel1.add(bCos);
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
panel1.add(bDec);
panel1.add(bTan);
panel1.add(b0);
panel1.add(bPoint);
panel1.add(bx);
panel1.add(bAdd);
panel1.add(bEqu);
panel1.add(bSquareRoot);
panel1.add(bSqu);
panel1.add(bRec);
panel1.add(bMod);
panel1.add(bPai);
panel1.add(bln);
panel1.add(blog);
panel1.add(bChengFang);
panel1.add(bJieCheng);
panel1.add(be);
panel1.add(bRational);
panel1.add(buttonA);
group.add(buttonA);
panel1.add(buttonB);
group.add(buttonB);
panel1.add(buttonC);
group.add(buttonC);
panel1.add(buttonD);
group.add(buttonD); panel2.add(bBack, BorderLayout.WEST);
panel2.add(bClear, BorderLayout.EAST);
panel3.add(textField1, BorderLayout.NORTH);
panel3.add(textField2, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.EAST); f.add(panel1, BorderLayout.SOUTH);
f.add(panel3, BorderLayout.CENTER); ActionListenerDisplay1 actionListenerDisplay1 = new ActionListenerDisplay1();
ActionListenerDisplay2 actionListenerDisplay2 = new ActionListenerDisplay2();
ActionListenerCalculator1 actionListenerCalculator1 = new ActionListenerCalculator1();
ActionListenerCalculator2 actionListenerCalculator2 = new ActionListenerCalculator2();
ActionListenerCalculator3 actionListenerCalculator3 = new ActionListenerCalculator3();
b0.addActionListener(actionListenerDisplay1);
b1.addActionListener(actionListenerDisplay1);
b2.addActionListener(actionListenerDisplay1);
b3.addActionListener(actionListenerDisplay1);
b4.addActionListener(actionListenerDisplay1);
b5.addActionListener(actionListenerDisplay1);
b6.addActionListener(actionListenerDisplay1);
b7.addActionListener(actionListenerDisplay1);
b8.addActionListener(actionListenerDisplay1);
b9.addActionListener(actionListenerDisplay1);
bPoint.addActionListener(actionListenerDisplay1);
bAdd.addActionListener(actionListenerDisplay1);
bAdd.addActionListener(actionListenerCalculator1);
bDec.addActionListener(actionListenerDisplay1);
bDec.addActionListener(actionListenerCalculator1);
bMul.addActionListener(actionListenerDisplay1);
bMul.addActionListener(actionListenerCalculator1);
bDiv.addActionListener(actionListenerDisplay1);
bDiv.addActionListener(actionListenerCalculator1);
bSin.addActionListener(actionListenerDisplay1);
bSin.addActionListener(actionListenerCalculator2);
bCos.addActionListener(actionListenerDisplay1);
bCos.addActionListener(actionListenerCalculator2);
bTan.addActionListener(actionListenerDisplay1);
bTan.addActionListener(actionListenerCalculator2);
bSquareRoot.addActionListener(actionListenerDisplay1);
bSquareRoot.addActionListener(actionListenerCalculator2);
bSqu.addActionListener(actionListenerDisplay2);
bSqu.addActionListener(actionListenerCalculator1);
bRec.addActionListener(actionListenerDisplay2);
bRec.addActionListener(actionListenerCalculator1);
bMod.addActionListener(actionListenerDisplay1);
bMod.addActionListener(actionListenerCalculator1);
bPai.addActionListener(actionListenerDisplay1);
bln.addActionListener(actionListenerDisplay1);
bln.addActionListener(actionListenerCalculator2);
blog.addActionListener(actionListenerDisplay1);
blog.addActionListener(actionListenerCalculator2);
bChengFang.addActionListener(actionListenerDisplay2);
bChengFang.addActionListener(actionListenerCalculator1);
bJieCheng.addActionListener(actionListenerDisplay2);
bJieCheng.addActionListener(actionListenerCalculator1);
be.addActionListener(actionListenerDisplay1);
bEqu.addActionListener(actionListenerCalculator1);
bBack.addActionListener(actionListenerDisplay1);
bClear.addActionListener(actionListenerDisplay1);
bRational.addActionListener(actionListenerCalculator2); buttonA.addActionListener(actionListenerCalculator3);
buttonB.addActionListener(actionListenerCalculator3);
buttonC.addActionListener(actionListenerCalculator3);
buttonD.addActionListener(actionListenerCalculator3); }

解决方案 »

  1.   

    public static void main(String[] args) { Calculatorlijian myCalc = new Calculatorlijian();
    myCalc.launchFrame(); } public void launchFrame() { f.setResizable(false);
    f.setVisible(true);
    f.addWindowListener(new MyWindowListener());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack(); } class MyWindowListener extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    } class ActionListenerDisplay1 implements ActionListener {
    public void actionPerformed(ActionEvent e) { JButton ja = (JButton) e.getSource();
    s = ja.getActionCommand(); if (s.equals("BACK")) {
    myinput = textField1.getText().substring(0,
    textField1.getText().length() - 1);
    textField1.setText(myinput);
    } else if (s.equals("CLEAR")) {
    textField1.setText("");
    textField2.setText("");
    myinput = "";
    rationalop = 0;
    valueop = 0;
    } else if (s.equals("+") || s.equals("-") || s.equals("*")
    || s.equals("/") || s.equals("%") || s.equals("sin")
    || s.equals("cos") || s.equals("tan") || s.equals("√")
    || s.equals("ln") || s.equals("log") ) {
    textField1.setText(s);
    myinput = "";
    } else if (s.equals("π")) {
    myinput = String.valueOf(Math.PI);
    textField1.setText("π");
    } else if (s.equals("e")) {
    myinput = String.valueOf(Math.E);
    textField1.setText("e");
    } else {
    if (textFieldop)
    myinput = "";
    myinput += s;
    textField1.setText(myinput);
    textFieldop = false;
    } }
    } class ActionListenerDisplay2 implements ActionListener {
    public void actionPerformed(ActionEvent e) { if (e.getSource() == bSqu) {
    s += "^2";
    textField1.setText(s);
    } else if (e.getSource() == bJieCheng) {
    s += "!";
    textField1.setText(s);
    } else if (e.getSource() == bRec)
    textField1.setText("D"); else if (e.getSource() == bChengFang){
    textField1.setText("^");
        myinput="";
    }
    } } class ActionListenerCalculator1 implements ActionListener { public void actionPerformed(ActionEvent e) { x = stringToDouble(myinput);
    y = stringToDouble(result);
    JButton tempB = (JButton) e.getSource();
    String jaa = tempB.getActionCommand(); if (jaa.equals("=")) {
    operate(x);
    op = 0;
    textField2.setText(displayResult(result));
    myinput = result;
    textFieldop = true;
    }
    if (jaa.equals("+")) {
    operate(x);
    op = 1;
    }
    if (jaa.equals("-")) {
    operate(x);
    op = 2;
    }
    if (jaa.equals("*")) {
    operate(x);
    op = 3;
    }
    if (jaa.equals("/")) {
    operate(x);
    op = 4;
    }
    if (jaa.equals("%")) {
    operate(x);
    op = 5;
    }
    if (jaa.equals("x^2")) {
    op = 12; }
    if (jaa.equals("1/x")) {
    op = 13; }
    if (jaa.equals("x^y")) {
    operate(x);
    op = 14; }
    if (jaa.equals("n!")) {
    op = 15; }
    } } class ActionListenerCalculator2 implements ActionListener { public void actionPerformed(ActionEvent e) {
    JButton tempC = (JButton) e.getSource();
    String jcc = tempC.getActionCommand(); if (jcc.equals("sin")) {
    op = 6;
    }
    if (jcc.equals("cos")) {
    op = 7;
    }
    if (jcc.equals("tan")) {
    op = 8;
    }
    if (jcc.equals("√")) {
    op = 9;
    }
    if (jcc.equals("ln")) {
    op = 10;
    }
    if (jcc.equals("log")) {
    op = 11;
    } if (jcc.equals("∟")) {
    if (rationalop == 0) {
    textField2.setText(getRational1(textField2.getText()));
    rationalop = 1;
    } else if (rationalop == 1) {
    textField2.setText(getRational2(textField2.getText()));
    rationalop = 2;
    } else {
    textField2.setText(getRational3(textField2.getText()));
    rationalop = 0;
    }
    }
    } } class ActionListenerCalculator3 implements ActionListener { public void actionPerformed(ActionEvent e) { if (buttonA.isSelected()) {
    if (valueop == 1)
    textField2.setText(binTo(textField2.getText()));
    else if (valueop == 2)
    textField2.setText(octTo(textField2.getText()));
    else if (valueop == 3)
    textField2.setText(hexTo(textField2.getText()));
    valueop = 0;
    } else if (buttonB.isSelected()) {
    if (valueop == 0)
    textField2.setText(toBin(textField2.getText()));
    else if (valueop == 2)
    textField2.setText(toBin(octTo(textField2.getText())));
    else if (valueop == 3)
    textField2.setText(toBin(hexTo(textField2.getText())));
    valueop = 1;
    } else if (buttonC.isSelected()) {
    if (valueop == 0)
    textField2.setText(toOct(textField2.getText()));
    else if (valueop == 1)
    textField2.setText(toOct(binTo(textField2.getText())));
    else if (valueop == 3)
    textField2.setText(toOct(hexTo(textField2.getText())));
    valueop = 2;
    } else if (buttonD.isSelected()) {
    if (valueop == 0)
    textField2.setText(toHex(textField2.getText()));
    else if (valueop == 1)
    textField2.setText(toHex(binTo(textField2.getText())));
    else if (valueop == 2)
    textField2.setText(toHex(octTo(textField2.getText())));
    valueop = 3;
    } }
    }
    public String operate(double num) { switch (op) {
    case 0:
    result = String.valueOf(num);
    break;
    case 1:
    result = String.valueOf(y + num);
    break;
    case 2:
    result = String.valueOf(y - num);
    break;
    case 3:
    result = String.valueOf(y * num);
    break;
    case 4:
    if (num != 0)
    result = String.valueOf(y / num);
    else
    result = "The divisor can't be zero!"; break;
    case 5:
    if (num != 0)
    result = String.valueOf(getMod(y, num));
    else
    result = "The divisor can't be zero!"; break;
    case 6:
    result = String.valueOf(Math.sin(num));
    break;
    case 7:
    result = String.valueOf(Math.cos(num));
    break;
    case 8:
    result = String.valueOf(Math.tan(num));
    break;
    case 9:
    result = String.valueOf(Math.pow(num, 0.5));
    break;
    case 10:
    result = String.valueOf(Math.log10(num) / Math.log10(Math.E));
    break;
    case 11:
    result = String.valueOf(Math.log10(num));
    break;
    case 12:
    result = String.valueOf(Math.pow(num, 2));
    break;
    case 13:
    result = String.valueOf(1 / num);
    break;
    case 14:
    result = String.valueOf(Math.pow(y, num));
    break;
    case 15:
    result = String.valueOf(jiecheng(num));
    break;
    }
    return result; } public String displayResult(String s) {
    double a = stringToDouble(s);
    if (Math.abs(a) < Math.pow(10, -8))
    return String.valueOf(0);
    else if (a == (int) a) {
    int c = (int) a;
    return String.valueOf(c);
    } else
    return String.valueOf(a); } public double stringToDouble(String x) {
    double y;
    if (x == "π")
    y = Math.PI;
    else if (x == "e")
    y = Math.E;
    else
    y = Double.parseDouble(x);
    return y;
    } public long stringToLong(String x) {
    return Long.parseLong(x);
    } public double getMod(double a, double b) {
    return a - ((int) (a / b)) * b; } public double jiecheng(double k) {
    if (k <= 1)
    return 1;
    else
    return k * jiecheng(k - 1); } public String getRational1(String s) {
    for (int i = 0; i < s.length(); i++)
    if (s.charAt(i) == 'E')
    s = transferString(s); int i = 0;
    String s1 = "";
    String s2 = "";
    while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    long a = stringToLong(s1);
    long b = stringToLong(s2);
    long c = (long) Math.pow(10, s2.length()); return a + "∟" + b / gcd(b, c) + "∟" + c / gcd(b, c); } public long gcd(long a, long b) {
    long x;
    while ((a % b) != 0) {
    x = a % b;
    a = b;
    b = x;
    }
    return b; } public String getRational2(String s) { int i = 0, j = 0;
    String s1 = "";
    String s2 = "";
    String s3 = "";
    String stemp = "";
    while (s.charAt(i) != '∟')
    i++;
    s1 = s.substring(0, i);
    stemp = s.substring(i + 1);
    while (stemp.charAt(j) != '∟')
    j++;
    s2 = stemp.substring(0, j);
    s3 = stemp.substring(j + 1);
    long a = stringToLong(s1);
    long b = stringToLong(s2);
    long c = stringToLong(s3); return a * c + b + "∟" + c;
    } public String getRational3(String s) {
    int i = 0; String s1 = "";
    String s2 = "";
    while (s.charAt(i) != '∟')
    i++;
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    double a = stringToDouble(s1);
    double b = stringToDouble(s2);
    return String.valueOf(a / b); }
      

  2.   

    public String toBin(String s) { for (int i = 0; i < s.length(); i++)
    if (s.charAt(i) == 'E')
    s = transferString(s); boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0, k, n = 0;
    double b;
    String s1 = "";
    String s2 = "";
    Stack<Long> stack = new Stack<Long>();
    while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    long a = stringToLong(s1);
    b = stringToDouble(s) - a;
    while (a / 2 != 0) {
    stack.push(a % 2);
    a /= 2;
    }
    stack.push(a);
    while (stack.size() > 0) {
    s2 += stack.pop();
    } s2 += ".";
    while (n <= 14) {
    n++;
    b *= 2;
    k = (int) b;
    s2 += k;
    if (b > k)
    b -= k;
    else
    break; } return s2;
    } else
    return Long.toBinaryString(stringToLong(s));
    } public String toOct(String s) { boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0, k, n = 0;
    double b;
    String s1 = "";
    String s2 = "";
    Stack<Long> stack = new Stack<Long>();
    while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    long a = stringToLong(s1);
    b = stringToDouble(s) - a;
    while (a / 8 != 0) {
    stack.push(a % 8);
    a /= 8;
    }
    stack.push(a);
    while (stack.size() > 0) {
    s2 += stack.pop();
    }
    s2 += ".";
    while (n <= 14) {
    n++;
    b *= 8;
    k = (int) b;
    s2 += k;
    if (b > k)
    b -= k;
    else
    break; } return s2;
    } else
    return Long.toOctalString(stringToLong(s));
    } public String toHex(String s) {
    boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0, k, n = 0;
    double b;
    String s1 = "";
    String s2 = "";
    while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    long a = stringToLong(s1);
    b = stringToDouble(s) - a;
    s2 += Long.toHexString(a);
    s2 += "."; while (n <= 14) {
    n++;
    b *= 16;
    k = (int) b;
    if (b > k) {
    if (k < 10)
    s2 += k;
    else if (k == 10)
    s2 += 'a';
    else if (k == 11)
    s2 += 'b';
    else if (k == 12)
    s2 += 'c';
    else if (k == 13)
    s2 += 'd';
    else if (k == 14)
    s2 += 'e';
    else if (k == 15)
    s2 += 'f'; b -= k;
    } else
    break;
    } return s2;
    } else
    return Long.toHexString(stringToLong(s));
    } public String binTo(String s) { boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0;
    double a = 0;
    String s1 = "";
    String s2 = ""; while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    a += Integer.valueOf(s1, 2);
    a += Integer.valueOf(s2, 2) * Math.pow(2, -s2.length()); return String.valueOf(a);
    } else
    return Integer.valueOf(s, 2).toString();
    } public String octTo(String s) { boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0;
    double a = 0;
    String s1 = "";
    String s2 = ""; while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    a += Integer.valueOf(s1, 8);
    a += Integer.valueOf(s2, 8) * Math.pow(8, -s2.length()); return String.valueOf(a);
    } else
    return Integer.valueOf(s, 8).toString();
    } public String hexTo(String s) { boolean isdouble = false;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '.') {
    isdouble = true;
    break;
    }
    }
    if (isdouble) {
    int i = 0;
    double a = 0;
    String s1 = "";
    String s2 = ""; while (s.charAt(i) != '.')
    i++;
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    a += Integer.valueOf(s1, 16);
    a += Integer.valueOf(s2, 16) * Math.pow(16, -s2.length());
    return String.valueOf(a);
    } else
    return Integer.valueOf(s, 16).toString();
    } public String transferString(String s) {
    String s1 = "";
    String s2 = "";
    String s11 = "";
    String s12 = "";
    Long n;
    int a = 1;
    char temp;
    char[] chars;
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == 'E') {
    s1 = s.substring(0, i);
    s2 = s.substring(i + 1);
    break;
    }
    }
    for (int i = 0; i < s1.length(); i++) {
    if (s1.charAt(i) == '.') {
    s11 = s1.substring(0, i);
    s12 = s1.substring(i + 1);
    a = i;
    break;
    }
    } n = stringToLong(s2); if (n > 0) {
    chars = s1.toCharArray();
    if (n < s12.length()) {
    while (n > 0) {
    temp = chars[a];
    chars[a] = chars[a + 1];
    chars[a + 1] = temp;
    a++;
    n--;
    }
    return String.valueOf(chars);
    } else if (n == s12.length()) {
    while (n > 0) {
    chars[a] = chars[a + 1];
    a++;
    n--; }
    return String.valueOf(chars).substring(0, s1.length() - 1);
    } else {
    while (a < s1.length() - 1) {
    chars[a] = chars[a + 1];
    a++; }
    String sss = String.valueOf(chars)
    .substring(0, s1.length() - 1);
    long k = n - s12.length();
    while (k > 0)
    sss += "0";
    return sss;
    } } else {
    String sss = "0.";
    while (Math.abs(n) > 1)
    sss += "0";
    sss += s11;
    sss += s12;
    return sss;
    } }
    }
    [/code]
      

  3.   

    拙计,怎么显示行数啊第二个:import java.util.Stack;public class calculator2 { public static void main(String[] args) {
    String a = "33+5"; System.out.println(String.valueOf(calcuString(a))); } public static double calcuString(String s) { s += "#";
    String numString = "";
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Character> stack2 = new Stack<Character>();
    stack2.push('('); for (int i = 0; i < s.length(); i++) {
    char temp = s.charAt(i); if (Character.isDigit(temp)) {
    numString += temp;
    } else { stack1.push(Integer.parseInt(numString)); numString = "";
    if (temp != ')'& (priority(temp) > priority(stack2.peek()) | temp == '('
    )) 

    stack2.push(temp);
    else { do {
    char cal = stack2.pop();
    if (cal == '+') {
    int a = stack1.pop();
    int b = stack1.pop();
    stack1.push(a + b); } else if (cal == '-') {
    int a = stack1.pop();
    int b = stack1.pop();
    stack1.push(b - a); } else if (cal == '*') {
    int a = stack1.pop();
    int b = stack1.pop();
    stack1.push(a * b); } else if (cal == '/') {
    int a = stack1.pop();
    int b = stack1.pop();
    stack1.push(b / a); }
    } while ( temp != '(' & temp != '#'& (priority(temp) <= priority(stack2.peek())
    | temp == ')' ));
    } } } return stack1.peek(); } public static int priority(char a) {
    switch (a) {
    case '#':
    case '(':
    return 0; case ')':
    return 1;
    case '+':
    case '-':
    return 2; case '*':
    case '/':
    return 3; default:
    return 8;
    } }
    }
      

  4.   

    在写计算表达式过程中,遇到了问题,没办法,写了这个程序慢慢调试。。最后找到问题,关于&&与&,||与|的区别还有&与|的优先级问题,汗,查了查书才知道&优先级比|高,比如计算1|0&1&0,基础不行啊。
      

  5.   

    你那个例子只能说明计算优先级是从右到左的,我怎么没听过&优先级比|高
      

  6.   

    如果优先级是从右到左的话 0&1&0|1应该是0吧,我算出来是1哦