java算术混合运算代码,计算器如何实现混合运算算法
在java中一个混合运算10+'a'+1.5*3,它是怎么运算的结果是111.5
你注意一下Java在运算时候的一些规则即可:
我们提供的服务有:成都做网站、成都网站设计、微信公众号开发、网站优化、网站认证、邯郸ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的邯郸网站制作公司
1)在优先级相同的情况下,Java是从左到右计算的;
2)在不同类型之间进行计算时,Java一般会将内部宽度比较小的将转换成大的,以避免溢出。
比如10是int,内部32位,'a'是char类型,内部16位,
‘a'是一个char类型,其ascii码是97,在运算时就是用的是这个值;
将其转换成32位的int的97
3) 运算符是有优先级的。乘法的优先级高于加法,这和你学过的数学知识是一致的。最后遇到的1.5*3会得到double类型的4.5
因此最后结果就是 10 + 97 + 4.5 = 111.5,
但是,如果你是用System.out.println打印的,java转换成最高精度,也就是double并显示;
如果你把这个值赋给了int,那么结果是111,会舍去小数(而不是四舍五入)。
怎么用JAVA做加减混合运算?
import java.util.Scanner;
public class HybridOperation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入一个表达式:");
String s = input.nextLine().trim();
int n = s.length();
int sum = 0;
int p = -1;
char a = '+';
for (int i = 1; i n; i++) {
char ch = s.charAt(i);
if (ch == '+' || ch == '-') {
if (a == '+') {
sum += Integer.parseInt(s.substring(p + 1, i));
a = ch;
p = i;
} else if (a == '-') {
sum -= Integer.parseInt(s.substring(p + 1, i));
a = ch;
p = i;
}
}
}
if (a == '+') {
sum += Integer.parseInt(s.substring(p + 1));
}
if (a == '-') {
sum -= Integer.parseInt(s.substring(p + 1));
}
System.out.println(s+" = "+sum);
}
}
可以实现任意次数的加减,
如:1+2+3-2
正整数加减乘除四则混合运算求值方法 java
您好:
首先在类中定义 加减乘除 四个函数(因整数除法会有小数,因此类型定义为double),具体如下:
根据自己的混合运算要求,组合以上方法即可,例如:((3+4)*2-6)/4 = 2
希望对你有帮助!
用JAVA实现复数的混合运算的源程序
public class Complex
{
private double realPart;
private double imaginaryPart;
public Complex(double a, double b)
{
this.realPart = a;
this.imaginaryPart = b;
}
public Complex add(Complex a)
{
Complex result = new Complex(this.realPart + a.realPart, this.imaginaryPart + a.imaginaryPart);//(why?)
return result;
}
public Complex decrease(Complex a)
{
Complex result = new Complex(this.realPart - a.realPart, this.imaginaryPart - a.imaginaryPart);//(why?)
return result;
}
public Complex multiply(Complex a)
{
double newReal = this.realPart*a.realPart - this.imaginaryPart * a.imaginaryPart;
double newImaginary = this.realPart*a.imaginaryPart + this.imaginaryPart * a.realPart;
Complex result = new Complex(newReal, newImaginary);
return result;
}
public Complex divide(Complex a)
{
Complex conjugate = new Complex(this.realPart, -this.imaginaryPart);
Complex multiplication = conjugate.multiply(a);
multiplication.realPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;
multiplication.imaginaryPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;
return multiplication;
}
public String toString()
{
String show = this.realPart + " + " + this.imaginaryPart + "i";
return show;
}
public static void main(String [] args)
{
Complex a = new Complex (2, 3);
Complex b = new Complex (1,1);
System.out.println((a.add(b)).toString());
System.out.println((a.decrease(b)).toString());
System.out.println((a.multiply(b)).toString());
System.out.println((a.divide(b)).toString());
}
}
java反射来做一个四则混合运算
我这给你个简单的只算加法的例子吧
先编写一个用于进行加法运算的类
CalcPlus.java
package com.test.reflact;
public class CalcPlus
{
public double plus(double num1,double num2)
{
return num1+num2;
}
}
然后编写新的测试类用于调用CalcPlus的plus进行计算
ReflectDemo.java
package com.test.reflact;
public class ReflectDemo
{
public static void main(String[] args)
{
try
{
//通过反射加载加法计算类
Class plusClass = Class.forName("com.test.reflact.CalcPlus");
//创建加法计算类的实例
CalcPlus plusInstance = (CalcPlus) plusClass.newInstance();
double num1 = 5.5;
double num2 = 8.6;
//进行计算并输出结果
System.out.println(plusInstance.plus(num1, num2));
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
求一个带四则混合运算的JAVA计算器,简单点的。谢谢。十分紧急。
class Stack_Float
{
float nums[];
int top;
Stack_Float()
{
nums = new float[50];
top = -1;
}
boolean IsEmpty()
{
if(top == -1)
return true;
else
return false;
}
float Pop_Stack()
{
if(top==-1)
{
return 0;
}
top--;
return nums[top + 1];
}
float GetTop()
{
return nums[top];
}
void Push_Stack(float num)
{
if(top == 49)
return;
top++;
nums[top] = num;
}
}
/*****************************************************************************/
class Stack_Char
{
char str[];
int top;
Stack_Char()
{
str = new char[50];
top = -1;
}
boolean IsEmpty()
{
if(top==-1)
return true;
else
return false;
}
void Push_Stack(char ch)
{
if(top == 49)
return;
top++;
str[top] = ch;
}
char Pop_Stack()
{
if(top == -1)
return '\0';
top--;
return str[top + 1];
}
char GetTop()
{
if(top == -1)
{
System.out.print("error");
System.exit(0);
}
return str[top];
}
}
/*****************************************************************************/
public class jisuanqi extends javax.swing.JFrame
{
String show = "";
public jisuanqi()
{
initComponents();
}
char[] TranSmit(char str[])
{
char houzhui[] = new char[50]; //存放后缀表达式的字符串
int i = 0,j = 0;
char c = str[i];
Stack_Char s = new Stack_Char(); //存放运算符的栈
while(c != '=') //对算术表达式扫描未结束时
{
if(c = '0' c = '9')
{
while(c = '0' c = '9')//数字直接入栈
{
houzhui[j]=c;
j++;
i++;
c=str[i];
}
houzhui[j]='#';//用#隔开数字
j++;
}
switch(c) //扫描到运算符时
{
case '+':
case '-':
case '*':
case '/':
if(s.IsEmpty() == true) //栈空,直接入栈
{
s.Push_Stack(c);
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == -1)
{
s.Push_Stack(c); //入栈
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == 1)
{
houzhui[j]=s.Pop_Stack();//出栈元素存入后缀表达式
j++;
break;
}
}
}
while(s.IsEmpty() != true)//把剩余的运算符直接出栈
{
houzhui[j]=s.Pop_Stack();
j++;
}
houzhui[j] = '=';//后缀表达式后面加 =
j++;
houzhui[j] = '\0';
j++;
return houzhui;
}
float Count(char str[])
{
Stack_Float s = new Stack_Float();//定义存放数字的栈
char c = str[0];
int i = 0,j = 0;
float result = 0,temp,left,right;
while(c != '=') //未扫描到 = 时
{
if(c = '0' c = '9')//扫描到数字
{
temp = 0;
while(c != '#')//未读到分隔符时
{
temp = temp * 10 + c - '0';
i++;
c = str[i];
}
s.Push_Stack(temp);//进栈
}
switch(c)//扫描到运算符时
{
case '+':
{
result = s.Pop_Stack() + s.Pop_Stack();//2个数字出栈相加
s.Push_Stack(result);//最后得数进栈
break;
}
case '-':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left - right;
s.Push_Stack(result);
break;
}
case '*':
{
result = s.Pop_Stack() * s.Pop_Stack();//2个数字出栈相乘
s.Push_Stack(result);
break;
}
case '/':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left / right;
s.Push_Stack(result);
break;
}
}
i++;
c = str[i];
}
return result;
}
int ComPare(char a,char b) //判断运算符的优先级函数
{
int s[][] =
{// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1
{1,1,-1,-1},
{1,1,-1,-1},
{1,1,1,1},
{1,1,1,1},
};
char x1[]={'+','-','*','/'};//栈顶元素
char x2[]={'+','-','*','/'};//算术表达式中的元素
int k=0,m,n = 0;
for(m=0;m4;m++) //查找2个进行比较的运算符在表中的位置,并返回比较结果
{
for(n=0;n4;n++)
{
if(x1[m]==ax2[n]==b)
{
k=1;break; //找到比较结果后,跳出循环
}
}
if(k==1)
break;
}
return s[m][n];//返回比较结果
}
/*****************************************************************************/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"//GEN-BEGIN:initComponents
private void initComponents() {
text = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().add(text);
text.setBounds(10, 10, 270, 30);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(10, 50, 60, 25);
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(80, 50, 60, 25);
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(150, 50, 60, 25);
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(220, 50, 60, 25);
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(10, 80, 60, 25);
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6);
jButton6.setBounds(80, 80, 60, 25);
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7);
jButton7.setBounds(150, 80, 60, 25);
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
getContentPane().add(jButton8);
jButton8.setBounds(220, 80, 60, 25);
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
getContentPane().add(jButton9);
jButton9.setBounds(10, 110, 60, 25);
jButton10.setText("0");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
getContentPane().add(jButton10);
jButton10.setBounds(80, 110, 60, 25);
jButton11.setText("+");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
getContentPane().add(jButton11);
jButton11.setBounds(150, 110, 60, 25);
jButton12.setText("-");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
getContentPane().add(jButton12);
jButton12.setBounds(220, 110, 60, 25);
jButton13.setText("*");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
getContentPane().add(jButton13);
jButton13.setBounds(10, 140, 60, 25);
jButton14.setText("/");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
getContentPane().add(jButton14);
jButton14.setBounds(80, 140, 60, 25);
jButton21.setText("CE");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(evt);
}
});
getContentPane().add(jButton21);
jButton21.setBounds(150, 140, 60, 25);
jButton22.setText("=");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
getContentPane().add(jButton22);
jButton22.setBounds(220, 140, 60, 25);
pack();
}// /editor-fold//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
show += "1";
text.setText(show);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
show += "2";
text.setText(show);
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
show += "3";
text.setText(show);
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
show += "4";
text.setText(show);
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
show += "5";
text.setText(show);
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
show += "6";
text.setText(show);
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
show += "7";
text.setText(show);
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
show += "8";
text.setText(show);
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
show += "9";
text.setText(show);
}//GEN-LAST:event_jButton9ActionPerformed
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
show += "0";
text.setText(show);
}//GEN-LAST:event_jButton10ActionPerformed
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
show += "*";
text.setText(show);
}//GEN-LAST:event_jButton13ActionPerformed
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
show += "/";
text.setText(show);
}//GEN-LAST:event_jButton14ActionPerformed
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed
show = "";
text.setText("");
}//GEN-LAST:event_jButton21ActionPerformed
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed
show += "=";
text.setText(show);
char str1[] = new char[50];
char str2[] = new char[50];
float result = 0;
str1 = show.toCharArray();
str2 = TranSmit(str1);
result = Count(str2);
text.setText("" + result);
show = "";
}//GEN-LAST:event_jButton22ActionPerformed
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
show += "-";
text.setText(show);
}//GEN-LAST:event_jButton12ActionPerformed
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
show += "+";
text.setText(show);
}//GEN-LAST:event_jButton11ActionPerformed
public static void main(String args[])
{
jisuanqi j = new jisuanqi();
j.setBounds(300, 300, 300, 195);
j.setVisible(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JTextField text;
// End of variables declaration//GEN-END:variables
}
当前标题:java算术混合运算代码,计算器如何实现混合运算算法
转载来于:http://ybzwz.com/article/dsepgep.html