資源簡介
做Java課程設(shè)計(jì)時(shí)做的一個(gè)項(xiàng)目,基本功能如下:
1、能輸入算術(shù)表達(dá)式,表達(dá)式用的是數(shù)學(xué)符號(hào)表示,不是計(jì)算機(jī)專業(yè)符合,如乘是符合“X”,而不是“*”;
2、能保存和瀏覽計(jì)算記錄,并能選擇某條記錄重新計(jì)算;
3、能修改輸入的表達(dá)式;
4、按等號(hào)計(jì)算;
5、能計(jì)算+、-,X,/, 乘方、開方,對(duì)數(shù)等多種計(jì)算;
順便附帶課程設(shè)計(jì)報(bào)告,注意用的開發(fā)包為Java1.8_101。由于調(diào)用的用于計(jì)算的數(shù)學(xué)函數(shù)的關(guān)系,只能在Java1.8_101及其以下版本的jfk下運(yùn)行。jdk1.8的2版本會(huì)出現(xiàn)調(diào)用函數(shù)錯(cuò)誤的情況,更別說不兼容的javajdk9及其以上版本了。
代碼片段和文件信息
package?sample;
import?java.math.BigDecimal;
import?java.math.RoundingMode;
import?javax.script.scriptEngine;
import?javax.script.scriptEngineManager;
import?javax.script.scriptException;
/**
?*?Analysis?class
?*
?*?@author?康勤
?*?@data?2019/07/08
?*/
public?class?Analysis{
????private?String?analysisObj;
// private?String?operatorChar[]?=?{“+““-““ד“÷““^““√““l(fā)og““%“};
????/**
?????*?運(yùn)算符
?????*?private?char[]?operator=?{‘+‘‘-‘‘*‘‘/‘‘^‘‘v‘‘l‘};
?????*?@下標(biāo)0?+?加
?????*?@下標(biāo)1?-?減
?????*?@下標(biāo)2?*?乘
?????*?@下標(biāo)3?/?除
?????*?@下標(biāo)4?^?冪
?????*?@下標(biāo)5?v?開方
?????*?@下標(biāo)6?l?對(duì)數(shù)
?????*?@下標(biāo)7?%?百分比
?????*/
????public?Analysis(String?analysisObj)
????{
????????this.analysisObj?=?analysisObj;
????}
????private?boolean?isComplete(int[]?count)
????{
????????return?count[0]+count[1]+count[2]+count[3]!=4;
????}
????private?void?AnalysisString()?throws?FormulaAnalysis
????{
????????int[]?count?=?new?int[]?{0000};
????????while(isComplete(count))
????????{
????????????int?index?=?analysisObj.lastIndexOf(“%“);
????????????if(index!=-1)
????????????????//有%
????????????{
????????????????int?preIndex?=?getPreNumIndex(index);
????????????????if(preIndex==-1)
????????????????????throw?new?FormulaAnalysis(“Math?error“);
????????????????BigDecimal?per_num?=?new?BigDecimal(analysisObj.substring(preIndex?index));
????????????????analysisObj=analysisObj.replace(analysisObj.substring(preIndex?index+1)clearDecimal(percent(per_num).toString()));
????????????}
????????????else
????????????{
????????????????count[0]=1;
????????????????System.out.println(“not?found?%“);
????????????}
????????????index?=?analysisObj.lastIndexOf(“√“);
????????????if(index!=-1)
????????????????//有√--root
????????????{
????????????????int?nextIndex?=?getNextNumIndex(indexfalse);
????????????????if(nextIndex==-1)
????????????????????throw?new?FormulaAnalysis(“Math?error“);
????????????????if(nextIndex==analysisObj.length()-1)
????????????????????nextIndex++;
????????????????BigDecimal?root_num?=?new?BigDecimal(analysisObj.substring(index+1?nextIndex));
????????????????analysisObj=analysisObj.replace(analysisObj.substring(index?nextIndex)?clearDecimal(square(root_num).toString()));
????????????}
????????????else
????????????{
????????????????count[2]=1;
????????????????System.out.println(“not?found?root“);
????????????}
????????????index?=?analysisObj.lastIndexOf(“^“);
????????????if(index!=-1)
????????????????//有冪
????????????{
????????????????int?nextIndex?=?getNextNumIndex(indexfalse);
????????????????int?pretIndex?=?getPreNumIndex(index);
????????????????if(nextIndex==-1){
????????????????????throw?new?FormulaAnalysis(“Math?error“);
????????????????}
????????????????if(nextIndex==analysisObj.length()-1){
????????????????????nextIndex++;
????????????????}
????????????????BigDecimal?pow_num1?=?new?BigDecimal(analysisObj.substring(pretIndex?index));
????????????????BigDecimal?pow_num2?=?n
評(píng)論
共有 條評(píng)論