[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

JAVA第三方计算工具: jcalc

[1.0.0.6]版,修复数组边界漏洞,修复随机函数算法。
跨平台JAVA第三方计算工具: jcalc.jar,使用增强版revpolish核心打造的。从 手机端 到 桌面端、服务器端 全覆盖运行。(所有图片一律为外链,jar包仅10kb,请下载附件)
链接: https://pan.baidu.com/s/1HhUgmb_dcFTqVD4v42TRrw?pwd=kx3x
兼容性   :java 1.6、java7、java8
跨平台性:完全做到一次编译,处处运行。可在各类安卓手机上直接运行,windows、linux、unix、mac、树莓派、各类微型智能设备,只要具备jre环境均可运行,不具备也可使用API转化技术运行。针对IOS设备可使用j2oc转化技术运行。
WINDOWS下运行图


linux下运行图

手机端运行请使用 ej 脚本运行http://www.bathome.net/thread-44950-1-1.html,更为便捷。
安卓手机下运行图


原创代码:
  1. package com.android.jcalc;
  2. /**Main主类
  3. * @author Happy from BatHome
  4. * @date Thu Aug 06 2017 20:11:16 GMT+0800
  5. * @description jcalc.jar
  6. */
  7. public class Main
  8. {
  9. //帮助说明
  10. private static final String HELP_INFORMATION =
  11.     ""
  12.     + "JCALC.JAR - JAVA CONSOLE CALCULATE TOOL, COPYRIGHT@2017~2019 BY HAPPY\n"
  13.     + "-----------------------------------------------------------------------\n"
  14.     + "Usage instructions:\n"
  15.     + "       java -jar jcalc.jar [expression]\n"
  16.     + "       java -Xms128m -Xmx1024m -jar jcalc.jar ...\n"
  17.     + "-----------------------------------------------------------------------\n"
  18.     + "General options:\n"
  19.     + "       --help, -h  Show help information\n\n"
  20.     + "Available math function:\n"
  21.     + "       pi=3.1415926535897932, e=2.7182818284590452\n"
  22.     + "       rand, round, int, ceil, floor, abs, sqrt, lg, ln, exp, cbrt\n"
  23.     + "       torad, sin, cos, tan, arcsin, arccos, arctan\n"
  24.     + "       sinh, cosh, tanh, arcsinh, arccosh, arctanh\n\n"
  25.     + "Official website:\n"
  26.     + "       http://www.bathome.net/thread-18688-1-1.html\n"
  27.     + "-----------------------------------------------------------------------\n"
  28.     + "AT LEAST JRE1.6, VERSION: 1.0.0.6\n";
  29. //数学函数关键词
  30. private static final String KEY_WORDS[]= {"e", "pi", "sqrt", "lg", "ln", "sin", "cos", "tan", "arcsin", "arccos", "arctan", "torad", "abs", "round", "floor", "ceil", "exp", "sinh", "cosh", "tanh", "arcsinh", "arccosh", "arctanh", "int", "cbrt", "rand", null};
  31. //创建计算的堆栈
  32. private static final int STACK_SIZE = 1024;
  33. private static char[] STACK1= new char[STACK_SIZE];
  34. private static char[] STACK2= new char[STACK_SIZE];
  35. private static double[] STACK3= new double[STACK_SIZE];
  36. /**阶乘函数
  37. * @param n
  38. * @return
  39. */
  40. private static long fact(long n)
  41. {
  42. return (n<2) ?1 :n*(fact(n-1));
  43. }
  44. /**计算器核心
  45. * @param expression
  46. */
  47. private static double JCalcCore(String expression)
  48. {
  49. char[] parseArray = expression.toCharArray();
  50. int parseArrayLen = parseArray.length;
  51. char[] tmpKeyCharArray;
  52. int tmpKeyCharArrayLen;
  53. int indexS1=0, indexS2=0, indexS3=0, brackets=0, indexKey, op, cp, kp;
  54. double di, ni;
  55. STACK3[0]=0;
  56. op = 0;
  57. //生成逆波兰
  58. while(op < parseArrayLen)
  59. {
  60. switch(parseArray[op])
  61. {
  62. case ' ' :
  63. case '\t':
  64. case '\r':
  65. case '\n':
  66. //过滤空字符
  67. op++;
  68. continue;
  69. case '(':
  70. brackets++;
  71. STACK1[++indexS1]=parseArray[op];
  72. if(parseArray[op+1]=='-' || parseArray[op+1]=='+')
  73. {
  74. STACK2[indexS2++]='0';
  75. STACK2[indexS2++]=' ';
  76. }
  77. break;
  78. case ')':
  79. //验证括号是否闭合
  80. if(brackets ==0)
  81. {
  82. System.out.println("The brackets or ')' are not need");
  83. System.exit(1);
  84. }
  85. brackets--;
  86. while(STACK1[indexS1] != '(')
  87. {
  88. STACK2[indexS2++] = STACK1[indexS1--];
  89. }
  90. //舍弃'('
  91. indexS1--;
  92. break;
  93. case '+':
  94. case '-':
  95. while( (indexS1 != 0) && (STACK1[indexS1] != '(') )
  96. {
  97. STACK2[indexS2++] = STACK1[indexS1--];
  98. }
  99. STACK1[++indexS1] = parseArray[op];
  100. break;
  101. case '^':
  102. //指数符
  103. while('A'<=STACK1[indexS1] && STACK1[indexS1]<='Z')
  104. {
  105. STACK2[indexS2++] = STACK1[indexS1--];
  106. }
  107. STACK1[++indexS1] = parseArray[op];
  108. break;
  109. case '!':
  110. //阶乘符
  111. STACK2[indexS2++] = parseArray[op];
  112. break;
  113. case '%':
  114. case '*':
  115. case '/':
  116. while(
  117.     (
  118.         ('A' <= STACK1[indexS1]) &&
  119.         (STACK1[indexS1] <= 'Z')
  120.     )                    ||
  121.     STACK1[indexS1]=='%' ||
  122.     STACK1[indexS1]=='*' ||
  123.     STACK1[indexS1]=='/' ||
  124.     STACK1[indexS1]=='^'
  125. )
  126. {
  127. STACK2[indexS2++] = STACK1[indexS1--];
  128. }
  129. STACK1[++indexS1] = parseArray[op];
  130. break;
  131. default :
  132. if('a'<=parseArray[op] && parseArray[op]<='z')
  133. {
  134. //识别数学函数关键词
  135. indexKey = 0;
  136. while(KEY_WORDS[indexKey] != null)
  137. {
  138. cp=op;
  139. kp=0;
  140. tmpKeyCharArray = KEY_WORDS[indexKey].toCharArray();
  141. tmpKeyCharArrayLen = tmpKeyCharArray.length;
  142. //比对关键词字母
  143. while(
  144. (kp < tmpKeyCharArrayLen) &&
  145. (cp < parseArrayLen)      &&
  146. (parseArray[cp] == tmpKeyCharArray[kp])
  147. ){
  148. cp++;
  149. kp++;
  150. }
  151. //验证关键词结尾
  152. if(
  153. (kp == tmpKeyCharArrayLen) &&
  154. (
  155. (tmpKeyCharArrayLen == parseArrayLen)      ||
  156. (parseArray[cp]<'a' || parseArray[cp]>'z')
  157. )
  158. )
  159. {
  160. op=cp;
  161. break;
  162. }
  163. indexKey++;
  164. }
  165. //构建伪双目
  166. if(KEY_WORDS[indexKey] != null)
  167. {
  168. STACK2[indexS2++]='.';
  169. STACK2[indexS2++]=' ';
  170. //伪双目入栈
  171. while('A'<=STACK1[indexS1] && STACK1[indexS1]<='Z')
  172. {
  173. STACK2[indexS2++] = STACK1[indexS1--];
  174. }
  175. STACK1[++indexS1] = (char)(indexKey+65);
  176. continue;
  177. }
  178. else
  179. {
  180. //无法识别的数学函数
  181. System.out.println("Unrecognized math function\n");
  182. System.exit(1);
  183. }
  184. }
  185. else if(('0'<=parseArray[op] && parseArray[op]<='9') || (parseArray[op]=='.'))
  186. {
  187. //浮点数入栈
  188. while(
  189. (op < parseArrayLen) &&
  190. (
  191. ('0'<=parseArray[op] && parseArray[op]<='9') ||
  192. (parseArray[op]=='.')
  193. )
  194. ){
  195. STACK2[indexS2++] = parseArray[op++];
  196. }
  197. if(
  198. (op < parseArrayLen) &&
  199. ('a'<=parseArray[op] && parseArray[op]<='z')
  200. ){
  201. //缺少必要的运算符
  202. System.out.println("Missing required operator\n");
  203. System.exit(1);
  204. }
  205. op--;
  206. STACK2[indexS2++] = ' ';
  207. }
  208. else
  209. {
  210. //无法识别的运算符
  211. System.out.println("Unrecognized operator\n");
  212. System.exit(1);
  213. }
  214. break;
  215. }
  216. op++;
  217. }
  218. //验证括号是否闭合
  219. if(brackets !=0)
  220. {
  221. System.out.println("The brackets '(' are not closed\n");
  222. System.exit(1);
  223. }
  224. //收尾逆波兰
  225. while(indexS1 !=0)
  226. {
  227. STACK2[indexS2++] = STACK1[indexS1--];
  228. }
  229. STACK2[indexS2] = ' ';
  230. //计算逆波兰
  231. op=0;
  232. while(STACK2[op] != ' ')
  233. {
  234. switch(STACK2[op])
  235. {
  236. case 'A':
  237. STACK3[indexS3] = Math.E;
  238. break;
  239. case 'B':
  240. STACK3[indexS3] = Math.PI;
  241. break;
  242. case 'C':
  243. if(STACK3[indexS3] <0)
  244. {
  245. //负数没有平方根
  246. System.out.println("Negative numbers have no square root\n");
  247. System.exit(1);
  248. }
  249. STACK3[indexS3 - 1] = Math.sqrt(STACK3[indexS3]);
  250. indexS3--;
  251. break;
  252. case 'D':
  253. if(STACK3[indexS3] <0)
  254. {
  255. //负数没有对数
  256. System.out.println("Negative numbers are not logarithmic\n");
  257. System.exit(1);
  258. }
  259. STACK3[indexS3 - 1] = Math.log10(STACK3[indexS3]);
  260. indexS3--;
  261. break;
  262. case 'E':
  263. if(STACK3[indexS3] <0)
  264. {
  265. //负数没有自然对数
  266. System.out.println("Negative numbers have no natural logarithms\n");
  267. System.exit(1);
  268. }
  269. STACK3[indexS3 - 1] = Math.log(STACK3[indexS3]);
  270. indexS3--;
  271. break;
  272. case 'F':
  273. STACK3[indexS3 - 1] = Math.sin(STACK3[indexS3]);
  274. indexS3--;
  275. break;
  276. case 'G':
  277. STACK3[indexS3 - 1] = Math.cos(STACK3[indexS3]);
  278. indexS3--;
  279. break;
  280. case 'H':
  281. if(STACK3[indexS3] == Math.PI/2)
  282. {
  283. //90度没有正切值
  284. System.out.println("The pi/2 has no tangent\n");
  285. System.exit(1);
  286. }
  287. STACK3[indexS3 - 1] = Math.tan(STACK3[indexS3]);
  288. indexS3--;
  289. break;
  290. case 'I':
  291. STACK3[indexS3 - 1] = Math.asin(STACK3[indexS3]);
  292. indexS3--;
  293. break;
  294. case 'J':
  295. STACK3[indexS3 - 1] = Math.acos(STACK3[indexS3]);
  296. indexS3--;
  297. break;
  298. case 'K':
  299. STACK3[indexS3 - 1] = Math.atan(STACK3[indexS3]);
  300. indexS3--;
  301. break;
  302. case 'L':
  303. STACK3[indexS3 - 1] = Math.toRadians(STACK3[indexS3]);
  304. indexS3--;
  305. break;
  306. case 'M':
  307. STACK3[indexS3 - 1] = Math.abs(STACK3[indexS3]);
  308. indexS3--;
  309. break;
  310. case 'N':
  311. STACK3[indexS3 - 1] = Math.round(STACK3[indexS3]);
  312. indexS3--;
  313. break;
  314. case 'O':
  315. STACK3[indexS3 - 1] = Math.floor(STACK3[indexS3]);
  316. indexS3--;
  317. break;
  318. case 'P':
  319. STACK3[indexS3 - 1] = Math.ceil(STACK3[indexS3]);
  320. indexS3--;
  321. break;
  322. case 'Q':
  323. STACK3[indexS3 - 1] = Math.exp(STACK3[indexS3]);
  324. indexS3--;
  325. break;
  326. case 'R':
  327. STACK3[indexS3 - 1] = Math.sinh(STACK3[indexS3]);
  328. indexS3--;
  329. break;
  330. case 'S':
  331. STACK3[indexS3 - 1] = Math.cosh(STACK3[indexS3]);
  332. indexS3--;
  333. break;
  334. case 'T':
  335. STACK3[indexS3 - 1] = Math.tanh(STACK3[indexS3]);
  336. indexS3--;
  337. break;
  338. case 'U':
  339. STACK3[indexS3 - 1] = Math.log(STACK3[indexS3]+Math.sqrt(STACK3[indexS3]*STACK3[indexS3]+1));
  340. indexS3--;
  341. break;
  342. case 'V':
  343. STACK3[indexS3 - 1] = Math.log(STACK3[indexS3]+Math.sqrt(STACK3[indexS3]*STACK3[indexS3]-1));
  344. indexS3--;
  345. break;
  346. case 'W':
  347. STACK3[indexS3 - 1] = Math.log((1+STACK3[indexS3])/(1-STACK3[indexS3]))/2;
  348. indexS3--;
  349. break;
  350. case 'X':
  351. STACK3[indexS3 - 1] = (int)(STACK3[indexS3]);
  352. indexS3--;
  353. break;
  354. case 'Y':
  355. STACK3[indexS3 - 1] = Math.cbrt(STACK3[indexS3]);
  356. indexS3--;
  357. break;
  358. case 'Z':
  359. //随机数生成器
  360. if(STACK3[indexS3] <0)
  361. {
  362. //负数不能作为随机数上限
  363. System.out.println("A negative number can not be used as a random upper bound");
  364. System.exit(1);
  365. }
  366. else if(STACK3[indexS3] <2)
  367. {
  368. //小于2的随机数为0~1之间的小数
  369. STACK3[indexS3 - 1] = Math.random();
  370. }
  371. else
  372. {
  373. STACK3[indexS3 - 1] = (int)(Math.random() * (STACK3[indexS3]));
  374. }
  375. indexS3--;
  376. break;
  377. case '+':
  378. STACK3[indexS3 - 1] += STACK3[indexS3];
  379. indexS3--;
  380. break;
  381. case '-':
  382. STACK3[indexS3 - 1] -= STACK3[indexS3];
  383. indexS3--;
  384. break;
  385. case '*':
  386. STACK3[indexS3 - 1] *= STACK3[indexS3];
  387. indexS3--;
  388. break;
  389. case '%':
  390. case '/':
  391. if(STACK3[indexS3] != 0)
  392. {
  393. if(STACK2[op] == '%')
  394. {
  395. //取余数
  396. STACK3[indexS3 - 1]=(int)STACK3[indexS3 - 1] % (int)STACK3[indexS3];
  397. }
  398. else
  399. {
  400. STACK3[indexS3 - 1] /= STACK3[indexS3];
  401. }
  402. }
  403. else
  404. {
  405. //除数不能为零
  406. System.out.println("Divisor is zero error\n");
  407. System.exit(1);
  408. }
  409. indexS3--;
  410. break;
  411. case '^':
  412. if(STACK3[indexS3 - 1]==0 && STACK3[indexS3]<0)
  413. {
  414. //除数不能为零
  415. System.out.println("Function pow's divisor is zero error\n");
  416. System.exit(1);
  417. }
  418. STACK3[indexS3 - 1] = Math.pow(STACK3[indexS3 - 1], STACK3[indexS3]);
  419. indexS3--;
  420. break;
  421. case '!':
  422. if(STACK3[indexS3] <0)
  423. {
  424. //负数没有阶乘
  425. System.out.println("Negative numbers have no factorial\n");
  426. System.exit(1);
  427. }
  428. STACK3[indexS3]=fact((long)(STACK3[indexS3]));
  429. break;
  430. default :
  431. //字符串转浮点
  432. di=0;
  433. ni=1;
  434. while('0'<=STACK2[op] && STACK2[op]<='9')
  435. {
  436. di=10*di+(STACK2[op])-'0';
  437. op++;
  438. }
  439. if(STACK2[op] == '.')
  440. {
  441. op++;
  442. while('0'<=STACK2[op] && STACK2[op]<='9')
  443. {
  444. di=10*di+(STACK2[op])-'0';
  445. op++;
  446. ni *= 10;
  447. }
  448. }
  449. STACK3[++indexS3] = di/ni;
  450. break;
  451. }
  452. op++;
  453. }
  454. //判断结果是否异常或溢出
  455. if( Double.isInfinite((float)STACK3[indexS3]) || Double.isNaN((float)STACK3[indexS3]) )
  456. {
  457. System.out.println("Overflow or illegal operation is calculated");
  458. System.exit(1);
  459. }
  460. //返回计算结果
  461. //return STACK3[indexS3];
  462. //打印中缀式
  463. System.out.printf("OriginalExp: %s\n", expression);
  464. //打印后缀式
  465. System.out.printf("Revpolish:   ");
  466. op = 0;
  467. while(op != indexS2)
  468. {
  469. if(STACK2[op]=='.' && STACK2[op+1]==' ')
  470. {
  471. op++;
  472. }
  473. else if('A'<=STACK2[op] && STACK2[op]<='Z')
  474. {
  475. System.out.printf("%s ", KEY_WORDS[STACK2[op]-65]);
  476. }
  477. else
  478. {
  479. System.out.printf("%c", STACK2[op]);
  480. if(
  481.     STACK2[op] == '+' ||
  482.     STACK2[op] == '-' ||
  483.     STACK2[op] == '*' ||
  484.     STACK2[op] == '/' ||
  485.     STACK2[op] == '%' ||
  486.     STACK2[op] == '^' ||
  487.     STACK2[op] == '!'
  488. )
  489. {
  490. System.out.printf(" ");
  491. }
  492. }
  493. op++;
  494. }
  495. System.out.printf("%c", '\n');
  496. //打印计算结果
  497. System.out.printf("Result:      %.12f\n", STACK3[indexS3]);
  498. return 0.0f;
  499. }
  500. /**主函数入口
  501. * @param args
  502. */
  503. public static void main(String[] args)
  504. {
  505. //抛出使用说明
  506. if (args.length != 1 || args[0].equals("-h") || args[0].equals("--help"))
  507. {
  508. System.out.println(HELP_INFORMATION);
  509. return;
  510. }
  511. //调用计算核心
  512. JCalcCore(args[0]);
  513. return;
  514. }
  515. }
复制代码
1

评分人数

返回列表