當前位置:首頁 » 語數英語 » java數學計算

java數學計算

發布時間: 2021-08-08 09:04:32

㈠ java編程數學表達式的計算!

採用棧的形式存取數據,根據二叉樹,進行數學運算。你要是學了數據結構就明白怎麼做了……

㈡ java計算簡單的數學公式.

publicclassDemo2{
publicstaticvoidmain(String[]args){
doubleprice=100.0;//單價
intnums=200;//數量
doubletotal;//總價
total=price*nums;//計算總價

doubleprofit;//利潤
doublecost=12000;//成本
doubletax=0.17;//稅率
profit=(total-cost)*(1-tax);//計算利潤
System.out.println("利潤:"+profit+"元");//輸出利潤
}
}

運行測試

利潤:6640.0元

㈢ java定義一個實現常用數學運算的類MyMath

public class MyMath{
public static int max(int p1, int p2, int p3) {
// 用排序
}
// 剩下還是你自己寫,這是鍛煉自己的機會,剛開始學是很有成就感的,不要被別人搶了哈
}

㈣ 數學指數運算(java編程)

//當a大於0,a不等於1時,a的X次方=N等價於log(N)a=x
//logx(y)=loge(x)/loge(y),換底公式
publicclassYugi{
publicstaticvoidmain(String[]args){
System.out.println(Math.log(50)/Math.log(10));
}
}

㈤ Java實現數字運算

說個思路吧:
1、double 數組排序,

2、去到大於80的數組元素下標,根據這個下標x 做出一個long y型數據,這個數據的二進製表示全是11111111111111111111111....這個方式
3,y不停減1,long型數據就會有位出現0了,當有35個位置為1時,就可以分別取得相應的位數和數組元素分別相乘累加,如果正好等於80,那麼就是你要求的數字,分別列印出來就行了

㈥ java如何計算比較復雜的數學計算,比如有括弧同時有+-*/,順序不一定,就是一個運算公式

界面自己畫,核心代碼就幾行很簡單
public class Test {
public static void main(String[] args) throws Exception {
Test t=new Test();
t.js("(1+2)*3/(4*5)^2");
}
private void js(String str) throws Exception{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByMimeType("application/javascript");
String script = "var obj = "+str;
engine.eval(script);
Object obj = engine.get("obj");
System.out.println(obj);
}
}

㈦ Java計算字元串中的數學表達式的值演算法怎麼寫

代碼網上很多,只說說演算法吧
12+8/4-5+(3-4)

把這樣的表達式拆成:(操作數)(操作符) 、
12+
8/
4-
5+(
3-
4)
(術語叫做逆波蘭式)
默認的計算順序是從左往右,記為left。另設從右往左,記為right
設計Element類,具有 操作數 operant, 操作符operator, 操作順序 order三個屬性
用兩個先進後出的棧結構Stack<Element> a,b;
一開始所有的Element都在a中,逐個彈出計算合並值,
當遇到乘、除、括弧時計算順序改變成right,把當前結果放到b中暫存。
直到再次遇到加、減、)右括弧時,意味計算順序復位成left,先把b中的暫存結果全部合並後,再繼續算a中的剩餘數據
最後合並成一個結果值。

㈧ JAVA計算數學表達式的程序

清單 1. Function、Operator 和 Variable 類的定義
public class Function
{
public String function;
public int number_of_arguments;
public Function( String function, int number_of_arguments )
{
this.function=function;
this.number_of_arguments=number_of_arguments;
}
public String toString()
{
return function;
}
}
public class Operator
{
public String operator;
public byte priority;
public Operator( String operator, byte priority )
{
this.operator=operator;
this.priority=priority;
}
public String toString()
{
return operator;
}
}
public class Variable
{
public String variable;
public double value;
public Variable( String variable, double value )
{
this.variable=variable;
this.value=value;
}
public String toString()
{
return variable;
}
}

Token 類如清單 2 所示。

清單 2. Token 類
public class Token
{
public Object token;
public char mark;
public int position;
public int length;
public Token ( Object token, char mark, int position, int length )
{
this.token=token;
this.mark=mark;
this.position=position;
this.length=length;
}
public String toString()
{
return token.toString()+" ; "+mark+" ; "+position+" ; "+length+"
";
}
}

清單 3. 三種括弧
import java.util.Stack;
public class Parentheses_check
{
public static boolean is_open_parenthesis( char c )
{
if ( c=='(' || c=='[' || c=='{' )
return true;
else
return false;
}
public static boolean is_closed_parenthesis( char c )
{
if ( c==')' || c==']' || c=='}' )
return true;
else
return false;
}
private static boolean parentheses_match( char open, char closed )
{
if ( open=='(' && closed==')' )
return true;
else if ( open=='[' && closed==']' )
return true;
else if ( open=='{' && closed=='}' )
return true;
else
return false;
}
public static boolean parentheses_valid( String exp )
{
Stack s = new Stack();
int i;
char current_char;
Character c;
char c1;
boolean ret=true;
for ( i=0; i < exp.length(); i++ )
{
current_char=exp.charAt( i );
if ( is_open_parenthesis( current_char ) )
{
c=new Character( current_char );
s.push( c );
}
else if ( is_closed_parenthesis( current_char ) )
{
if ( s.isEmpty() )
{
ret=false;
break;
}
else
{
c=(Character)s.pop();
c1=c.charValue();
if ( !parentheses_match( c1, current_char ) )
{
ret=false;
break;
}
}
}
}
if ( !s.isEmpty() )
ret=false;
return ret;
}
}
清單 4. 正確的表達式開頭的檢查
private static boolean begin_check( Vector tokens, Range r, StringBuffer err )
{
char mark;
Token t;
t=(Token)tokens.elementAt( 0 );
mark=t.mark;
if ( mark=='P' )
err.append( Messages.begin_operator );
else if ( mark==')' )
err.append( Messages.begin_parenthesis );
else if ( mark=='Z' )
err.append ( Messages.begin_comma );
else
return true;
r.start=0;
r.end=t.length;
return false;
}
清單 5. 找出第一個閉括弧
public static int pos_first_closed_parenthesis( Vector tokens )
{
Token t;
for ( int i=0; i<tokens.size(); i++ )
{
t=(Token)tokens.elementAt( i );
if ( t.mark==')' )
return i;
}
return 0;
}
清單 6. 找出匹配的開括弧
public static int pos_open_parenthesis( Vector tokens, int closed_parenthesis )
{
int i;
Token t;
i=closed_parenthesis-2;
while ( i>=0 )
{
t=(Token)tokens.elementAt( i );
if ( t.mark=='(' )
{
return i;
}
i--;
}
return 0;
}

清單 7. 找出優先順序最高的操作符
public static int pos_operator( Vector tokens, Range r )
{
byte max_priority=Byte.MAX_VALUE;
int max_pos=0;
byte priority;
String operator;
Token t;
for ( int i=r.start+2; i<=r.end-2; i++ )
{
t=(Token)tokens.elementAt( i );
if ( t.mark!='P' )
continue;
priority=((Operator)t.token).priority;
operator=((Operator)t.token).operator;
if ( priority < max_priority || ( operator.equals("^") ||
operator.equals("**") ) && priority == max_priority )
{
max_priority=priority;
max_pos=i;
}
}
return max_pos;
}
清單 8. 檢查是否還有其它操作符
...
int poz_max_op=pos_operator( tokens, range );
// if there are no operators
if ( poz_max_op==0 )
{
if ( no_more_parentheses )
{
return false;
}
else
{
double result;
result=function_result( tokens, range.start-1 );
function_tokens_removal( tokens, range.start-1 );
t = new Token ( new Double(result), 'D', 0, 0 );
tokens.setElementAt( t, range.start-1 );
parentheses_removal( tokens, range.start-1 );
return true;
}
}
...

清單 9. 獲取操作數並執行運算...
double operand1, operand2;
// first operand is before...
t=(Token)tokens.elementAt( poz_max_op-1 );
operand1=operand_value( t );
// ...and second operand is after operator
t=(Token)tokens.elementAt( poz_max_op+1 );
operand2=operand_value( t );
// operator
t=(Token)tokens.elementAt( poz_max_op );
String op=((Operator)t.token).operator;
double result=operation_result( operand1, operand2, op );
tokens.removeElementAt( poz_max_op+1 );
tokens.removeElementAt( poz_max_op );
t = new Token ( new Double(result), 'D', 0, 0 );
tokens.setElementAt( t, poz_max_op-1 );
parentheses_removal( tokens, poz_max_op-1 );
...
清單 10. 獲取操作數
public static double operand_value( Token t )
{
if ( t.mark=='V' )
return ((Variable)t.token).value;
else if ( t.mark=='D' )
return ((Double)t.token).doubleValue();
else if ( t.mark=='H' )
return base_convert( ((String)t.token).substring(2), 16 );
else if ( t.mark=='O' )
return base_convert( ((String)t.token).substring(2), 8 );
else if ( t.mark=='B' )
return base_convert( ((String)t.token).substring(2), 2 );
}
清單 11. 將數轉化為十進制數
public static long base_convert( String s, int base )
{
long r=0;
int i, j;
for ( i=s.length()-1, j=0; i>=0; i--, j++ )
r=r+digit_weight( s.charAt( i ) )*(long)Math.pow( base, j );
return r;
}
public static int digit_weight( char c )
{
if ( Character.isDigit( c ) )
return c-48;
else if ( 'A'<=c && c<='f' )
return c-55;
else if ( 'a'<=c && c<='f' )
return c-87;
return -1;
}
清單 13. 除去冗餘括弧
private static void parentheses_removal( Vector tokens, int pos )
{
if (
pos>1 &&
amp;&&
amp;
((Token)tokens.elementAt( poz-2 )).mark!='F' &&
amp;&&
amp;
((Token)tokens.elementAt( poz-1 )).mark=='(' &&
amp;&&
amp;
((Token)tokens.elementAt( poz+1 )).mark==')'
||
pos==1 &&
amp;&&
amp;
((Token)tokens.elementAt( 0 )).mark=='(' &&
amp;&&
amp;
((Token)tokens.elementAt( 2 )).mark==')'
)
{
tokens.removeElementAt( poz+1 );
tokens.removeElementAt( poz-1 );
}
return;
}

清單 14. 結合符號並顯示結果
public static String token_join( Vector tokens )
{
String result=new String();
Token t;
for ( int i=0; i < tokens.size(); i++ )
{
t=(Token)tokens.elementAt( i );
if ( t.mark=='D' )
{
double n=((Double)t.token).doubleValue();
result=result + formated_number( n );
}
else
result=result + t.token;
if ( result.endsWith( ".0" ) )
result=result.substring( 0, result.length()-2 );
result=result + " ";
}
return result;
}
結論

本文分析了一個 applet ,它能一步一步的對算術表達式求值。同時還按順序回顧了最有意思的代碼片段,並論述了兩種不同的表達式求值方法。

下一版 W3Eval 有望在各方面得到增強,包括有能力添加用戶定義的功能;支持分數、復數和矩陣;改良的圖形用戶界面(GUI);大小和速度優化以及安全性方面的增強。我鼓勵您提供您自己對於增強方面的設想。

我希望您會發現 W3Eval 是個對表達式求值有益的在線工具,它在某種程度上比經典的方法更簡單自然。我還期待這里談到的代碼和演算法使您明白 Java 語言有助於處理數學問題。

!強烈要求加分!

㈨ java怎麼計算一個String類型的數學表達式

只有自己實現一個方法了,先將這類表達式轉換成逆波蘭式表達式,再使用棧進行計算,實現起來是有一定難度的,特別是其中還夾雜著冪、乘、除、括弧等有先後運算順序的運算符。

㈩ 利用Java編程實現要求的數學運算

package javaapplication1;
import java.util.Scanner;
public class testComplex {
public static void main(String[] args){
double x1=0,y1=0,x2=0,y2=0;
double b1=0,b2=0;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入復數C1的實部和虛部:");
x1 = sc.nextDouble();
y1 = sc.nextDouble();
System.out.println("請輸入復數C2的實部和虛部:");
x2 = sc.nextDouble();
y2 = sc.nextDouble();
Complex C1=new Complex(x1,y1);
Complex C2=new Complex(x2,y2);
Complex C3=C1.add(C2);//C1+C2
Complex C4=C2.sub(C1);//C1-C2
b1=C3.imagPart;
b2=C4.imagPart;
if(y1>=0)
System.out.println("復數C1:"+x1+"+"+y1+"i");
else
System.out.println("復數C1:"+x1+y1+"i");
if(y2>=0)
System.out.println("復數C2:"+x2+"+"+y2+"i");
else
System.out.println("復數C2:"+x2+y2+"i");

if(b1>=0)
{ System.out.println("復數C1+C2:"+(x1+x2)+"+"+(y1+y2)+"i");//直接運算
System.out.println("復數C1+C2:"+C3.realPart+"+"+C3.imagPart+"i");//調用函數
}
else
{ System.out.println("復數C1+C2:"+(x1+x2)+(y1+y2)+"i");//直接運算
System.out.println("復數C1+C2:"+C3.realPart+C3.imagPart+"i");//調用函數
}
if(b2>=0)
{ System.out.println("復數C1-C2:"+(x1-x2)+"+"+(y1-y2)+"i");//直接運算
System.out.println("復數C1-C2:"+C4.realPart+"+"+C4.imagPart+"i");//調用函數
}
else
{
System.out.println("復數C1-C2:"+(x1-x2)+(y1-y2)+"i");//直接運算
System.out.println("復數C1-C2:"+C4.realPart+C4.imagPart+"i");//調用函數
}

}
}
class Complex//描述復數
{
public double realPart=0;//表示復數的實部
public double imagPart=0;//表示復數的虛部
public Complex(double real,double image)//定義構造函數
{
realPart=real;
imagPart=image;
}
Complex add(Complex fushu)//實現當前復數和參數復數fushu的加法
{
return new Complex(fushu.realPart+realPart,fushu.imagPart+imagPart);
}
Complex sub(Complex fushu)//實現當前復數和參數復數fushu的減法
{
return new Complex(fushu.realPart-realPart,fushu.imagPart-imagPart);
}
}

//方法很簡單,就是控制虛部是符號很麻煩,順便也把C1、C2也輸出來了

熱點內容
中國房價歷史 發布:2025-07-05 16:22:07 瀏覽:309
2年級的英語 發布:2025-07-05 13:33:31 瀏覽:773
初中物理電動機 發布:2025-07-05 11:48:09 瀏覽:245
慈利教育網 發布:2025-07-05 11:15:09 瀏覽:622
奧特曼黑歷史 發布:2025-07-05 05:13:59 瀏覽:8
2017全國二語文試卷 發布:2025-07-05 02:17:04 瀏覽:679
德陽是哪個省的 發布:2025-07-05 01:20:18 瀏覽:562
歐豪年彩墨教學視頻 發布:2025-07-05 00:38:16 瀏覽:713
教學實踐內容 發布:2025-07-04 21:32:22 瀏覽:431
雲南教育論文 發布:2025-07-04 18:10:10 瀏覽:16