ja 用大写中文数字计算加减法

Button four = new Button("4");

public class MapTest {

integer加减(integer加1)integer加减(integer加1)


integer加减(integer加1)


for(int i=o.size()-1; i>=0; i--){

public static void main(String[] args) {

if(but==null){

Map map = new HashMap();

map.put("壹", 1);

map.put("贰", 2);

map.put("叁", 3);

map.put("肆", 4);

map.put("伍", 5);

map.put("陆", 6);

map.put("柒", 7);

map.put("捌", 8);

map.put("玖", 9);

String str = "肆+伍";// 我们就设是这个样式的

int a = map.get(str.substring(0, 1));

char b = str.charAt(1);

int c = map.get(str.substring(2, 3));

switch (b) {

case '+':

System.out.println(a + c);

break;

case '-':

System.out.println(a - c);

break;

case '':

System.out.println(a c);

break;

case '/':

System.out.println(a / c);

break;

default:

System.out.println("未定义的符号");

}}

}这是一个小小的例子,给你一条路走,具体的逻辑需要你自己去修正

vb时间的加减

}exists = jtexternal.getText();

计算日期的间隔,应该使用DateDiff 函数。计算某日期之前或者之后的某一时printf "%.0fn" $var #这个与上面不同,是四舍五入的间,已知相的时间,可以使用DateADD函数。

String str = "83-32+8332/4+3";

时间不能直接加减,只有使用DateAdd函数。

vb 加减法运算6进制问题

fourPanel.setLayout(new GridLayout(1, 2,5,0));

第2个容易,加起来就是01110011,符号位是0,是正数

YSF = 4

有不懂的再问我把

可以将六进制转换成十进制进行计算,计算结果再转换成六进制。

下面给出的就是六进制加法运算,减法运算你参考着编吧

Private Function Jf6(Js1 As String, Js2 As String) As String

Dim Js11 As String, Js22 As String, JsTemp As String

Dim Ajs As Integer, Bjs As Integer, Jws As Integer

Dim I As Integer, J As Integer, K As Integer, HTemp As Integer, JwTemp As Integer

If Len(Js1) > Len(Js2) Then

Js11 = Js2

Js22 = Js1

Else

Js11 = Js1

Js22 = Js2

End If

K = Len(Js22)

Jws = 0

For I = 0 To J - 1

Ajs = Mid(Js11, J - I, 1)

Bjs = Mid(Js22, K - I, 1)

HTemp = Ajs + Bjs + Jws

Jws = 0

Do While HTemp >= 6

Jws = Jws + 1

Loop

Jf6 = HTemp & Jf6

Next I

Do While I < K

Bjs = Mid(Js22, K - I, 1)

HTemp = Bjs + Jws

Jws = 0

Do While HTemp >= 6

Jws = Jws + 1

Loop

Jf6 = HTemp & Jf6

I = I + 1

Loop

JwTemp = 0

Do While Jws >= 6

Jws = Jws - 6

JwTemp = JwTemp + 1

Loop

If JwTemp > 0 Then Jf6 = JwTemp & Jws & Jf6 Else Jf6 = Jws & Jf6

End Function

Private Sub Command1_Click()

MsgBocase OStack.PLUS:return a+b;x Jf6(Text1, Text2)

End Sub

写了个函数,自己体会吧

Dim liu1 As Integer

Private Function Liu(x As Integer) As Long '10进制转六进制函数

liu1 = 10 (x 6) + x Mod 6

For i = Len(liu1) To 1 Step -1

If Mid(liu1, i, 1) > 5 Then

liu2 = Val(Replace(liu1, Mid(liu1, i, 1), Str(Val(Mid(liu1, i, 1) - 6))))

liu1 = liu2 + Int(10 ^ i)

End If

Next i

Liu = liu1

End Function

Private Sub Command1_Click()

Text3 = ""

s = Liu(Val(Text1) + Val(Text2)) '减法也是一个道理

Text3 = s '6进制

End Sub

运算结果除6取余就可,c=A mod 6

5+3=12?

自己写函数实现吧~ 计算结果转6进制~~

shell 脚本里面从一个文本里面读出一个数字,如何转换成整数?我需要用这个数字进行加减乘除

J = Len(Js11)

方法有很多:

String one = oneTextField.getText();

var=4.57

法一:

echo ${var%.} #这个是直接去除小数点及后面所有内容,只用于bash

法二:

echo $var | awk -F. '{print $1}' #以小数点为分隔符取个字段

法三:

echo $var | awk '{print int($0)}' public void mouseClicked(MouseEvent e) {#awk中可直接使用C函数取整

法四:

echo "scale=0;$var/1"|bc -l #用bc计算

法五:

你可以找下函数 有公式取整数,例如:

Basic函数原型INT(number)

类别数学函数

返回值Integer 类型

作用求不大于number

的整数,Int(3.8)=3,Int(-3.8)=-4。

注意:int不能取整!取整要用fix!

BASIC INT(number)取比number小的的整数。INT(1.1)=1

INT(-1.1)=-2。

ja加减乘除计算器界面编程?

Label1.Caption = Label1.Caption & i

以前学习的时候写过,听那些在工作岗位上的人说JFrame框架在企业中基本不用,所以也就没怎么深入研究了,你要的话我可以给你,能完善一下,完善不了的话就以后再说吧

HTemp = HTemp - 6

package test;

import ja.awt.;

import ja.awt.nt.;

import jax.swing.;

public class Z extends JFrame implements ActionListener, FocusListener {

private String[] total = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "", "0", ".", "=", "/" }; // 总数组

private String[] other = { "clear", "delete" }; // 其它功能数组

private String[] symbol = { "+", "-", "", "/" }; // 符号数组

private String multinumber = "0", operator1 = "", exists = "", operator2 = null, s = "";

// multinumber(多位数) operator1(运算符) exists(存在的)

private double d1, d2 = 0;

private int Count = 0;

JPanel jptotal = new JPanel(); // 总面板

JTextField jtexternal = new JTextField("0"); // 外部文本框

JTextField jtinside = new JTextField(); // 内部文本框

JPanel jptop1 = new JPanel(); // 顶部面板1

JPanel jptop2 = new JPanel(); // 顶部面板2

JPanel jpmiddle = new JPanel(); // 中部面板

public Z() {

jptotal.setLayout(new BorderLayout());

// 使用边界布局管理器

jptop1.setLayout(new GridLayout(2, 0));

jptop1.add(jtexternal);

jptop2.setLayout(new GridLayout(0, 2));

for (int i = 0; i < other.length; i++) {

JButton jbi = new JButton("" + other[i]);

jbi.setSize(100, 10);

jptop2.add(jbi);

jbi.addActionListener(this);

}jpmiddle.setLayout(new GridLayout(4, 0));

for (int i = 0; i < total.length; i++) {

JButton jbi = new JButton("" + total[i]);

jpmiddle.add(jbi);

jbi.addActionListener(this);

}jtexternal.addFocusListener(this);

jtexternal.add(jtinside);

jtinside.addFocusListener(this);

// 上面三行代码的作用是让文本框不可被选中

jptop1.add(jptop2);

jptotal.add(jptop1, "North");

// 以jp1为一个整体,让jp1里面的内容从最上面开始填充

jptotal.add(jpmiddle);

add(jptotal);

setVisible(true);

setSize(500, 500);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}// ctrl+/ :注释

public void actionPerformed(ActionEvent e) throws ArithmeticException {

try {

String inputing = e.getActionCommand();

// 表示获取当前输入的内容

if (inputing.equals(".")) {

Count++;

if (operator2 != null) {

int k = 0;

for (int i = 0; i < symbol.length; i++) {

if (operator2.equals(symbol[i])) {

k = k + 1;

}if (inputing.equals(symbol[i])) {

k = k + 1;

}if (k == 2) {

jtexternal.setText(exists.substring(0, exists.length() - 1) + inputing);

operator1 = inputing;

return;

}// 实现四则运算符不可同时存在,新输入的替换旧的

if (operator2.equals(symbol[i]) && inputing.equals(".")) {

return;

}// 实现四则运算符和点不可挨着出现(点后面可以出现四则运算符,点前面不可出现)

}}

if (exists.equals("0")) {

if (inputing.equals(".") || (inputing.equals("+")) || (inputing.equals("-")) || (inputing.equals(""))

jtexternal.setText("0" + inputing);

} else {

jtexternal.setText(inputing);

}} else {

jtexternal.setText(exists + inputing);

}// 实现如果0前面没有非0数字的话,那么无论输入多少次0,都只显示一个0,如果输入其它数字,0将会被替换

if ((d1 == 0.0 || d2 == 0.0) && inputing.equals("=") && !exists.substring(0, 1).equals("0")) {

return;

}// 实现如果文本框内存在数字,那么输入等号,将打印其本身

if (inputing.equals("clear")) {

multinumber = "0";

exists = "";

operator1 = "";

operator2 = null;

d2 = 0;

s = "";

d1 = 0.0;

// 实现,当点击clear时,文本框内将被清空至0,所有数据都将还原至初始状态

} else if (inputing.equals("delete")) {

if (exists.length() <= 1 || s.equals("=")) {

multinumber = "0";

exists = "";

operator1 = "";

operator2 = null;

d2 = 0;

s = "";

d1 = 0;

// 实现,按delete删除刚输入的内容,如果将一个数字删除,那么效果同按clear

} else {

if (inputing.equals("+") || inputing.equals("-") || inputing.equals("") || inputing.equals("/")) {

Count = 1;

}jtexternal.setText(exists.substring(0, exists.length() - 1));

// 将operator2存储上一次值

operator2 = exists.substring(exists.length() - 2, exists.length() - 1);

/

实现如果四则运算符后面有数字的话

那么如果此时按删除键

删除之后如果新输入的还是四则运算符话

那么将会将原来的四则运算符替换成新输入的

/

}} else if (!inputing.equals("+") && !inputing.equals("-") && !inputing.equals("") && !inputing.equals("/")

&& !inputing.equals("=")) {

// 判断输入是否为数字

if (s.equals("=")) {

if (exists.equals("除数不能为0") || new Double(exists) == d1) {

if (inputing.equals(".")) {

jtexternal.setText("0" + inputing);

} else {

jtexternal.setText(inputing);

}}

s = "";

}// 实现一次计算完成之后,可以进行新的运算

if (multinumber == null) { // 多位数计算

multinumber = inputing;

} else {

multinumber += inputing;

}d1 = new Double(multinumber);

if (operator1.equals("+")) { // 进行四则运算

if (d2 == 0) {

if (operator1.equals("") || operator1.equals("/")) {

d2 = 1;

}}

// 实现,当四则运算符后面没有数字时,那么输入等号后结果将为符号前面的结果,如果符号前面只有一个数字,那么将打印自身

d1 = d1 + d2;

} else if (operator1.equals("-")) {

d1 = d2 - d1;

} else if (operator1.equals("")) {

d1 = d1 d2;

} else if (operator1.equals("/")) {

d1 = d2 / d1;

}// 利用临时变量f2,实现四则运算

operator2 = inputing;

} else if (!inputing.equals("=")) { // [四则运算符] 输入等号后继续进行计算

multinumber = "0";

operator1 = inputing;

d2 = d1;

operator2 = inputing;

s = "";

// 实现小数运算

} else { // 就是 [=] 得出最终结果

jtexternal.setText("" + d1);

s = "=";

multinumber = "0";

if (operator2.equals("0") && "/".equals(operator1)) {

operator2 = null;

d1 = 0;// 将非法运算值替换成正确值

throw new ArithmeticException();// 指定抛出异常的条件

//赋值计算不抛算数异常,如果想让其抛出,必须指定条件重新抛出异常

} else {

operator1 = inputing;

operator2 = null;

d2 = 0;

} // 计算结果

if (inputing.equals("=") && (d1 == 0.0 || d2 == 0.0)) {

return;

}// 实现等号不可重复输入

}} catch (ArithmeticException e1) {

jtexternal.setText("除数不能为0");

// 指定抛出异常内容

}}

public static void main(String[] args) {

new Z();

}public void focusGained(FocusEvent e) {

jtinside.requestFocus();

// 让焦点聚焦到内部文本框,实现外部文本框的不可聚焦功能

}public void focusLost(FocusEvent e) {

}}

/

目前delete删除输入值之后让其它值还原功能未实现,等以后再进行改进 举个例子:比如我要计算1+1,但是在输入等号的时候,我想换成别的

比如我想换成1-1,我按delete将1和加号删除,换成1-1 但是结果却不是0 又或者我换成11+1,但是结果却不是12

又或者当我输入11时,我不小心多输入了一个数字,这时候再进行运算,结果就不是我想要的了 ...

/

简单练习,仅供参考。

package com.hello.swing;

import jax.swing.;

import ja.awt.;

public class Calculator {

public static void main(String[] args) {

create();

} public static void create(){

new CalculatorFrame();

}static class CalculatorFrame extends JFrame{

JTextField oneTextField;

JTextField twoTextField;

JTextField resultTextField;

public CalculatorFrame(){

Container container = getContentPane();

setLayout(new GridLayout(5, 1, 0, 5));

// 行显示标题:简单计算器

JLabel titleLabel = new JLabel("简单计算器");

titleLabel.setHorizontalAlignment(JLabel.CENTER); // 水平居中

titleLabel.setFont(new Font("宋体",Font.BOLD,20));

container.add(titleLabel);

// 第二行显示:数 1

JPanel onePanel = new JPanel();

onePanel.setLayout(new GridLayout(1, 2, 5, 0));

JLabel oneLabel = new JLabel("数1:");

oneprivate static float cheng(float x,float y) {Label.setHorizontalAlignment(JLabel.RIGHT); // 右对齐

onePanel.add(oneLabel);

oneTextField = new JTextField();

onePanel.add(oneTextField);

container.add(onePanel);

// 第三行显示:数 2

JPanel twoPanel = new JPanel();

twoPanel.setLayout(new GridLayout(1, 2,5,0));

JLabel twoLabel = new JLabel("数2:");

twoLabel.setHorizontalAlignment(JLabel.RIGHT); // 右对齐

twoPanel.add(twoLabel);

twoTextField = new JTextField();

twoPanel.add(twoTextField);

container.add(twoPanel);

// 第四行显示加减乘除按钮

JPanel threePanel = new JPanel();

threePanel.setLayout(new GridLayout(1, 4,5,0));

JButton addButton = new JButton("加");

addButton.addActionListener(e -> {

String two = twoTextField.getText();

resultTextField.setText(String.valueOf(Integer.parseInt(one) + Integer.parseInt(two)));

});

JButton subtractButton = new JButton("减");

subtractButton.addActionListener(e -> {

String two = twoTextField.getText();

resultTextField.setText(String.valueOf(Integer.parseInt(one) - Integer.parseInt(two)));

});

JButton multiplyButton = new JButton("乘");

multiplyButton.addActionListener(e -> {

String two = twoTextField.getText();

resultTextField.setText(String.valueOf(Integer.parseInt(one) Integer.parseInt(two)));

});

JButton divideButton = new JButton("除");

divideButton.addActionListener(e -> {

String two = twoTextField.getText();

resultTextField.setText(String.valueOf(Integer.parseInt(one) / Integer.parseInt(two)));

});

threePanel.add(addButton);

threePanel.add(multiplyButton);

container.add(threePanel);

// 第五行显示结果

JPanel fourPanel = new JPanel();

JLabel resultLabel = new JLabel("计算结果:");

resultLabel.setHorizontalAlignment(JLabel.RIGHT); // 右对齐

fourPanel.add(resultLabel);

resultTextField = new JTextField();

fourPanel.add(resultTextField);

container.add(fourPanel);

// Frame属性设置

setTitle("简单计算器"); // 窗口标题

setVisible(true); // 窗口可见

setSize(400, ); // 窗口大小

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 窗口关闭

}}

}

本例用到了控件在容器中的网格布局,该布局使得运行界面在放大时控件的大小和控件之间的间距也会跟着放大,使得界面看着更美观。网格布局的参数:

一、代码如下

二、运行结果

计算都是以double类型进行的。

运行画面68的结果

00:11

如何用VB编写一个加减乘除计算器?只要代码!

2、在新建的这是根据你自己编的改正的代码,自己觉得比用下拉列表的更好,而且对你来说,这个更好理解消化。不过要是要你题目中所说的代码或是有看不懂的地方,直接HI我,或留言,我再给你。另外,初学JAVA,给你一本书《JAVA开发技术大全》,我就是从这本书起步的。类中,添加4个运算类;

Option Explicit Dim Op1, Op2 Dim DecimalFlag As Integer Dim NumOps As Integer Dim LastInput Dim OpFlag Dim TempReadout Private Sub Cancel_Click() Readout = Format(0, "0.") Op1 = 0 Op2 = 0 Form_Load End Sub Private Sub CancelEntry_Click() Readout = Format(0, "0.") DecimalFlag = False LastInput = "CE" End Sub Private Sub Decimal_Click() If LastInput = "NEG" Then Readout = Format(0, "-0.") ElseIf LastInput <> "NUMS" Then Readout = Format(0, "0.") End If DecimalFlag = True LastInput = "NUMS" End Sub Private Sub Form_Load() DecimalFlag = False NumOps = 0 LastInput = "NONE" OpFlag = " " Readout = Format(0, "0.") 'Decimal.Caption = Format(0, ".") End Sub Private Sub Number_Click(Index As Integer) If LastInput <> "NUMS" Then Readout = Format(0, ".") DecimalFlag = False End If If DecimalFlag Then Readout = Readout + Number(Index).Caption Else Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".") End If If LastInput = "NEG" Then Readout = "-" & Readout LastInput = "NUMS" End Sub Private Sub Operator_Click(Index As Integer) TempReadout = Readout If LastInput = "NUMS" Then NumOps = NumOps + 1 End If Select Case NumOps Case 0 If Operator(Index).Caption = "-" And LastInput <> "NEG" Then Readout = "-" & Readout LastInput = "NEG" End If Case 1 Op1 = Readout If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then Readout = "-" LastInput = "NEG" End If Case 2 Op2 = TempReadout Select Case OpFlag Case "+" Op1 = CDbl(Op1) + CDbl(Op2) Case "-" Op1 = CDbl(Op1) - CDbl(Op2) Case "X" Op1 = CDbl(Op1) CDbl(Op2) Case "/" If Op2 = 0 Then MsgBox "除数不能为 0", 48, "计算器" Else Op1 = CDbl(Op1) / CDbl(Op2) End If Case "=" Op1 = CDbl(Op2) Case "%" Op1 = CDbl(Op1) CDbl(Op2) End Select Readout = Op1 NumOps = 1 End Select If LastInput <> "NEG" Then LastInput = "OPS" OpFlag = Operator(Index).Caption End If End Sub Private Sub Percent_Click() Readout = Readout / 100 LastInput = "Ops" OpFlag = "%" NumOps = NumOps + 1 DecimalFlag = True End Sub 窗体代码 VERSION 5.00 Begin VB.Form Calculator BorderStyle = 1 'Fixed Single Caption = "计算器" Height = 2970 Left = 2580 Top = 1485 Width = 3270 ClipControls = 0 'False BeginProperty Font Name = "System" Size = 9.75 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikrough = 0 'False EndProperty Icon = "CALC.frx":0000 LinkMode = 1 'Source LinkTopic = "Form1" MaxButton = 0 'False PaletteMode = 1 'UseZOrder ScaleHeight = 2970 ScaleWidth = 3270 WhatsThisHelp = -1 'True Begin VB.CommandButton Number Caption = "7" Height = 480 Index = 7 Left = 120 TabIndex = 7 Top = 600 Width = 480 End Begin VB.CommandButton Number Caption = "8" Height = 480 Index = 8 Left = 720 TabIndex = 8 Top = 600 Width = 480 End Begin VB.CommandButton Number Caption = "9" Height = 480 Index = 9 Left = 1320 TabIndex = 9 Top = 600 Width = 480 End Begin VB.CommandButton Cancel Caption = "C" Height = 480 Left = 2040 TabIndex = 10 Top = 600 Width = 480 End Begin VB.CommandButton CancelEntry Caption = "CE" Height = 480 Left = 2640 TabIndex = 11 Top = 600 Width = 480 End Begin VB.CommandButton Number Caption = "4" Height = 480 Index = 4 Left = 120 TabIndex = 4 Top = 1200 Width = 480 End Begin VB.CommandButton Number Caption = "5" Height = 480 Index = 5 Left = 720 TabIndex = 5 Top = 1200 Width = 480 End Begin VB.CommandButton Number Caption = "6" Height = 480 Index = 6 Left = 1320 TabIndex = 6 Top = 1200 Width = 480 End Begin VB.CommandButton Operator Caption = "+" Height = 480 Index = 1 Left = 2040 TabIndex = 12 Top = 1200 Width = 480 End Begin VB.CommandButton Operator Caption = "-" Height = 480 Index = 3 Left = 2640 TabIndex = 13 Top = 1200 Width = 480 End Begin VB.CommandButton Number Caption = "1" Height = 480 Index = 1 Left = 120 TabIndex = 1 Top = 1800 Width = 480 End Begin VB.CommandButton Number Caption = "2" Height = 480 Index = 2 Left = 720 TabIndex = 2 Top = 1800 Width = 480 End Begin VB.CommandButton Number Caption = "3" Height = 480 Index = 3 Left = 1320 TabIndex = 3 Top = 1800 Width = 480 End Begin VB.CommandButton Operator Caption = "X" Height = 480 Index = 2 Left = 2040 TabIndex = 14 Top = 1800 Width = 480 End Begin VB.CommandButton Operator Caption = "/" Height = 480 Index = 0 Left = 2640 TabIndex = 15 Top = 1800 Width = 480 End Begin VB.CommandButton Number Caption = "0" Height = 480 Index = 0 Left = 120 TabIndex = 0 Top = 2400 Width = 1080 End Begin VB.CommandButton Decimal Caption = "." Height = 480 Left = 1320 TabIndex = 18 Top = 2400 Width = 480 End Begin VB.CommandButton Operator Caption = "=" Height = 480 Index = 4 Left = 2040 TabIndex = 16 Top = 2400 Width = 480 End Begin VB.CommandButton Percent Caption = "%" Height = 480 Left = 2640 TabIndex = 17 Top = 2400 Width = 480 End Begin VB.Label Readout Alignment = 1 'Right Justify BackColor = &H0000FFFF& BorderStyle = 1 'Fixed Single Caption = "0." BeginProperty Font Name = "MS Sans Serif" Size = 12 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikrough = 0 'False EndProperty ForeColor = &H00000000& Height = 375 Left = 120 TabIndex = 19 Top = 105 Width = 3000 End End Attribute VB_Name = "Calculator" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exed = False

case '%':

加减乘除运算(Ja)

Integer 是一个类,它是对int进行了封装,是整数据类型,默认值为null(int是原始数据类型,默认值为0)。

//MathX.ja

//表达式计算程序,原创,

//与网上多数介绍的方法思路有点不同,

//与采用直接调用js的方法有根本的不同,

//支持 加 减 乘 除 幂 及 开方 运算,支持多级括号,

//本程序在 JDK1.6_11下开发,低版本JDK兼容性未测试,

import ja.text.DecimalFormat;

public class MathX {

public static void main(String[] args) {

double d = Calc.doCalc(str).getValue();

System.out.println(Calc.getStr()+"="+d);

}// public static double calc(String s){

// return Calc.doCalc(s).getValue();

// }

// public static String getStr(){

// return Calc.getStr();

// }

// public static String getFormatted() {

// return Calc.getFormatted();

// }

}class Calc{

private static final DecimalFormat df = new DecimalFormat("0.000000");

private static CalcCore co;

private static NumberWrapper result;

public static NumberWrapper doCalc(String exp){

co = new CalcCore(exp);

result = co.getResult();

return result;

}public static String getFormatted() {

return df.format(result.getValue());

}public static String getStr(){return co.toString();}

}//数据外覆类

class NumberWrapper{

public static final int IaN = 0;

public static final int NaN = 1;

public static final NumberWrapper NOTHING = new NumberWrapper(Double.NaN,NumberWrapper.NaN);

private double value;

private int id;

public NumberWrapper(double v){

this(v,NumberWrapper.IaN);

}public double getValue(){

return id==NumberWrapper.IaN?value:Double.NaN;

}public NumberWrapper(double v,int id){

this.value=v;

this.id=id;

}// +-}public void push(char c){/^~

public NumberWrapper calc(NumberWrapper x,char o){

if(this.NaN()||x.NaN(default :))

return NumberWrapper.NOTHING;

return new NumberWrapper(calc(this.getValue(),x.getValue(),o));

}private double calc(double a,double b,char o){

try{

switch(o){

case OStack.SUBTRACT:return a-b;

case OStack.MULTIPLY:return ab;

case OStack.DIVIDE:return a/b;

case OStack.POWER:return Math.pow(a, b);

case OStack.EVOLUTION:return Math.pow(a, 1d/b);

default:return Double.NaN;

}}catch(Exception e){}

return Double.NaN;

public boolean NaN(){return id==NaN;}

}class CalcCore{

private NStack n;

private OStack o;

private NumberWrapper result;

private String src;

public CalcCore(String src){

this.src="/uploads/b679952b17ff7ce7ad207fc320bbc131.png" data-original=src;

rebuildString();

this.n=new NStack();

this.o=new OStack();

this.split();

this.calc();

}public String toString(){

return src;

}private void rebuildString() {

// (...)(...) --> (...)(...)

src="/uploads/b679952b17ff7ce7ad207fc320bbc131.png" data-original=src.replaceAll(")(",")"+OStack.MULTIPLY+"(");

// 1234(...) --> 1234(...)

// (...)1234 --> (...)1234

int i=0;

while(i

if(hasNext(i+1)&&this.isNumber(i)&&src.charAt(i+1)==OStack.LB||

hasNext(i+1)&&this.isNumber(i+1)&&src.charAt(i)==OStack.RB){

src="/uploads/b679952b17ff7ce7ad207fc320bbc131.png" data-original=src.substring(0,i+1)+OStack.MULTIPLY+src.substring(i+1);

}++i;

}//~1234 -->2~1234

//(~1234) -->(2~1234)

i=0;

while(i

if(src.charAt(i)==OStack.EVOLUTION && pio(i)){

src="/uploads/b679952b17ff7ce7ad207fc320bbc131.png" data-original=src.substring(0,i)+"2"+src.substring(i);

}++i;

}}

private void calc() {

for(int i=0; i

char ch = o.get(i);

if(ch==OStack.EVOLUTION||ch==OStack.POWER){

NumberWrapper n0 = n.remove(i);

NumberWrapper n1 = n.remove(i);

ch = o.remove(i);

NumberWrapper rs;

if(ch==OStack.EVOLUTION)

rs = n0.calc(n1, ch);

else

rs = n1.calc(n0, ch);

n.insert(i, rs);

--i;

}}

char ch = o.get(i);

if(ch==OStack.MULTIPLY||ch==OStack.DIVIDE){

NumberWrapper n0 = n.remove(i+1);

NumberWrapper n1 = n.remove(i);

ch = o.remove(i);

NumberWrapper rs = n0.calc(n1, ch);

n.insert(i, rs);

}}

char ch = o.get(i);

NumberWrapper n0 = n.remove(i+1);

NumberWrapper n1 = n.remove(i);

ch = o.remove(i);

NumberWrapper rs = n0.calc(n1, ch);

n.insert(i, rs);

}if(n.isEmpty()||n.size()>1)

result = NumberWrapper.NOTHING;

else

result = n.pop();

}public NumberWrapper getResult(){

return result;

}private void split(){

int cont;

for(int i=0; i

char c = src.charAt(i);

switch(c){

case '(':

int pair = nextPair(src,i+1);

String sub = substring(i+1,pair);

n.push(Calc.doCalc(sub));

i=pair;

break;

case '-':

case '+':

boolean iso = pio(i);

cont = continuous(i+1);

if(iso){

n.push(new NumberWrapper(parse(substring(i,cont))));

i=cont-1;

break;

}case '':

case '/':

case '^':

case '~':o.push(c);break;

cont = continuous(i+1);

n.push(new NumberWrapper(parse(substring(i,cont))));

i=cont-1;

}}

}private double parse(String s){

try{

return Double.parseDouble(s);

return Double.NaN;

}private String substring(int i, int cont) {

return src.substring(i,cont);

}private boolean hasNext(int i){

return src.length()>i;

}private int continuous(int i) {

while(hasNext(i) && isNumber(i))

++i;

return i;

}private boolean pio(int i){

return i<1?true:OStack.iso(src.charAt(i-1));

char c = src.charAt();

}public int nextPair(String src,int ){

int inner = 0;

int len = src.length();

for(int i=; i

char c = src.charAt(i);

if(c==')')

if(inner==0)return i;

else --inner;

else if(c=='(')++inner;

}return -1;

}}

//作符栈(半角符号)

class OStack{

public static final char PLUS = '+';

public static final char SUBTRACT = '-';

public static final char MULTIPLY = '';

public static final char DIVIDE = '/';

public static final char POWER = '^';

public static final char EVOLUTION = '~';

public static final char LB = '(';

public static final char RB = ')';

public static boolean iso(char c) {

switch(c){

case PLUS:

case SUBTRACT:

case MULTIPLY:

case DIVIDE:

case POWER:

case EVOLUTION:

case LB:

// case RB:

return true;

}return false;

}public boolean hasNext(int i) {

return this.length>i+1;

}private char[] stack;

private int length;

public OStack(){

this.stack=new char[0];

this.length=0;

}public boolean isEmpty(){

return length==0;

char[] tmp = new char[this.length+1];

tmp[0]=c;

System.arraycopy(stack, 0, tmp, 1, length);

++length;

this.stack=tmp;

}public char pop(){

if(this.isEmpty())throw new StackOverflowError();

char c = stack[0];

char[] tmp = new char[length-1];

System.arraycopy(stack, 1, tmp, 0, tmp.length);

--length;

this.stack=tmp;

return c;

}public char remove(int i){

if(i<0||i>=length)throw new StackOverflowError();

char[] tmp = new char[length-1];

System.arraycopy(stack,0,tmp,0,i);

System.arraycopy(stack,i+1,tmp,i,tmp.length-i);

this.length--;

char n=stack[i];

this.stack=tmp;

return n;

}public void insert(int i,char n){

if(i<0||i>length)throw new StackOverflowError();

char[] tmp = new char[length+1];

System.arraycopy(stack,0,tmp,0,i);

System.arraycopy(stack,i,tmp,i+1,this.length-i);

tmp[i]=n;

this.length++;

this.stack=tmp;

}public char get(int i){

return this.stack[i];

}public int size(){return this.length;}

}//数据栈

class NStack{

private NumberWrapper[] stack;

private int length;

public NStack(){

this.stack=new NumberWrapper[0];

this.length=0;

}public NStack(NStack n){

this.stack=new NumberWrapper[n.length];

this.length=n.length;

for(int i=0; i

this.stack[i]=n.stack[i];

}}

public void push(NumberWrapper d){

NumberWrapper[] tmp = new NumberWrapper[this.length+1];

System.arraycopy(stack, 0, tmp, 1, this.length);

tmp[0] = d;

this.stack=tmp;

this.length++;

}public NumberWrapper pop() throws StackOverflowError{

if(this.isEmpty())throw new StackOverflowError();

NumberWrapper[] tmp = new NumberWrapper[this.length-1];

System.arraycopy(stack, 1, tmp, 0, tmp.length);

NumberWrapper d = stack[0];

this.stack=tmp;

this.length--;

return d;

}public NumberWrapper get(int i){

return this.stack[i];

}public NumberWrapper remove(int i){

if(i<0||i>=length)throw new StackOverflowError();

NumberWrapper[] tmp = new NumberWrapper[length-1];

System.arraycopy(stack,0,tmp,0,i);

System.arraycopy(stack,i+1,tmp,i,tmp.length-i);

this.length--;

NumberWrapper n=stack[i];

this.stack=tmp;

return n;

}public void insert(int i,NumberWrapper n){

if(i<0||i>length)throw new StackOverflowError();

NumberWrapper[] tmp = new NumberWrapper[length+1];

System.arraycopy(stack,0,tmp,0,i);

System.arraycopy(stack,i,tmp,i+1,this.length-i);

tmp[i]=n;

this.length++;

this.stack=tmp;

}public boolean isEmpty(){

return this.length==0;

}public int size(){return this.length;}

}

大体就跟 fantasy316 写的大同小异,使用堆栈做。一个一个字符读,作符入栈,作数直接写出,然后根据运算优先级写出符号,建立后缀表达式;再根据后缀表达式利用栈结构算出结果。

应该是JS的吧 不与WEB交互计算的话估计取值有点麻烦

因为不能判断你的值是多少位 运算符号在什么地方

当然了要真不是WEB的截面的话 可以把输入的转化成ASCII进行比较

分解出是值还是运算符 然后进行作

不过这个判断是有的写了

class test3{

public static void main(String[] args)throws Exception{

try{

System.out.println(myMath("(12+15)56+45681"));

}catch(jax.script.ScriptException se){

}}

public static double myMath(String s) throws jax.script.ScriptException{

Object o = new jax.script.ScriptEngineMar().getEngineByName("JaScript").eval(s);

return (Double)o;

}}

用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现

public class Addition extends Applet implements ActionListener {

import ja.awt.;

import ja.awt.nt.;

import ja.applet.Applet;

Label label1=new Label();

Label label2=new Label("=");

TextField field1=new TextField(6);

TextField field2=new TextField(6);

TextField field3=new TextField(6);

Button button1=new Button("相加");

Button button2=new Button("相减");

Button button3=new Button("相乘");

Button button4=new Button("相除");

public void init() { // 初始化

add(field1); add(label1);

add(field2); add(label2);

add(field3); add(button1);

add(button2); add(button3);

add(button4);

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

button4.addActionListener(this);

}//加

public void dobutton1() { // 处理按钮

int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());

field3.setText(Integer.toString(x)); // 数值转换为字符串

label1.setText("+");

}//减

public void dobutton2() { // 处理按钮

int x=Integer.parseInt(field1.getText())-Integer.parseInt(field2.getText());

field3.setText(Integer.toString(x)); // 数值转换为字符串

}//乘

public void dobutton3(){ // 处理按钮

int x=Integer.parseInt(field1.getText())Integer.parseInt(field2.getText());

field3.setText(Integer.toString(x)); // 数值转换为字符串

label1.setText("");

}//除

public void dobutton4() { // 处理按钮

int x=Integer.parseInt(field1.getText())/Integer.parseInt(field2.getText());

field3.setText(Integer.toString(x)); // 数值转换为字符串

label1.setText("/");

}//源处理

public void actionPerformed(ActionEvent e){

Object obj;

obj=e.getSource();

if(obj==button1){

dobutton1();

}else if(obj==button2){

dobutton2();

}else if(obj==button3){

dobutton3();

}else if(obj==button4){

dobutton4();

}}

}

一看就是学校的作业题,可怜啊,现在用applet的基本上非常少了,可是学校还在教这些无用的东西。

package com.inter.test;

import ja.applet.Applet;

import ja.awt.Rectangle;

import ja.awt.nt.ActionEvent;

import ja.awt.nt.ActionListener;

import jax.swing.JButton;

import jax.swing.JComboBox;

import jax.swing.JTextField;

@SuppressWarnings("serial")

public class TestApplet extends Applet{

private JTextField test1;

private JTextField test2;

private JComboBox comBox;

private String[] list={"+","-","","/"};

private JButton but;

private JTextField test3;

private JTextField getTest3(){

if(test3==null){

test3=new JTextField();

test3.setBounds(new Rectangle(120,50,50,20));

}return test3;

}private JButton getButton(){

but.setBounds(new Rectangle(10,50,60,20));

but.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String one=test1.getText();

String two=test2.getText();

char c=((String)comBox.getSelectedItem()).charAt(0);

switch(c){

case '+':

test3.setText(String.valueOf(Integer.parseInt(one)+Integer.parseInt(two)));

break;

case '-':

test3.setText(String.valueOf(Integer.parseInt(one)-Integer.parseInt(two)));

break;

case '':

test3.setText(String.valueOf(Integer.parseInt(one)Integer.parseInt(two)));

break;

case '/':

test3.setText(String.valueOf(Integer.parseInt(one)/Integer.parseInt(two)));

break;

}}

});

}return but;

}private JTextField getTest1(){

if(test1==null){

test1=new JTextField();

test1.setBounds(new Rectangle(10,10,50,20));

}return test1;

}privathreePanel.add(subtractButton);te JComboBox getCombobox(){

if(comBox==null){

comBox=new JComboBox(list);

comBox.setBounds(new Rectangle(65,10,40,20));

}return comBox;

}public TestApplet() {

this.setLayout(null);

this.for(int i=o.size()-1; i>=0; i--){add(getButton());

this.add(getTest1());

this.add(getCombobox());

this.add(getTest2());

this.add(getTest3());

this.setVisible(true);

}private JTextField getTest2() {

if(test2==null){

test2=new JTextField();

test2.setBounds(new Rectangle(110,10,50,20));

}return test2;

}public static void main(String[] args) {

new TestApplet();

}}

这个只能实现整数的,你再改一点,就可以实现浮点数了!

ja数组元素如何进行加减乘除,请大侠咪赐教!!

YSF = 2

public class Test {

label1.setText("-");

statiElseIf M = 3 Thenc int sum1;

static int sum2;

static int sum3;

public static void main(String[] args) {

String[] a = {"1","2","3","4","5","6","7","8","9"};

String[] b = {"1","2","3","4","5","6","7","8","9"};

for (int i=0;i

sum1 += Integer.valueOf(a[i]);

}for (int i=0;i

sum2 += Integer.valueOf(b[i]);

}for (int i=0;i

sum3 += Integer.valueOf(a[i])Integer.valueOf(b[i])/Integer.valueOf(b[i]);//0不能做除数,我帮你把0去除了;

}System.out.println(sum1);

System.out.println(sum2);

System.out.println(sum1/sum2);

System.out.println(sum3);

}}

首先,你那是一维数据,如果是二维应该是:String [][] a=new String[10][10];这样的写法

你确定要用String类型的?

ja中这个是什么意思 private Integer number;

|| (inputing.equals("/"))) {

您好,提问者:

titleLabel.setVerticalAlignment(JLabel.CENTER); // 垂直居中

private是私有的修饰符,只能在本类中访问。

Integer是int的包装类,Integer的值可以为null,而int的值默认是0。

number是自己起的一个名字,用来存储变量的。

private 表示私有的 在类的内部进行访问。

number 就是自定义的一个变量名。

总的说,就是定义了一个 私有的Integer 的整数据类型 的变量 number。

给你个最简单的例子

private String str;

其实上面和这个一样就是一个类里面的私有变量,声明而已。

private Integer number; 就是声明一个 名字为number的数字。

nText4 = Val(Text1) - Val(Text3)umber为变量名。integer为变量的类型。private修饰变量的作用范围。

一个私有的Integer类型的变量number 变量若是全局的,则只能在类的内部使用。

private : 表示私有的

public : 表示公有的

(这些统称为 访问修饰符)

Integer 是一个类,它是对int进行了封装。

好了,我只能说这么多了 选不选看你了 呵呵!

声明私有整型变量名字叫 number