Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.22 KB | None | 0 0
  1. import java.math.*;
  2. import java.util.*;
  3. public class FractionalCalculator
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8. initialization();
  9. fractionalTesting();
  10. boolean error = false;
  11. while (error == false)
  12. {
  13.  
  14.  
  15. String operation = console.nextLine();
  16. int operationType = -1;
  17. int inputType = -1;
  18. //first input type test
  19. char[] character_ = new char[1000];
  20. char[] character__ = new char[1000];
  21. int slashcount = 0;
  22. int[] slashlocation = new int[1000];
  23.  
  24. int underscorecount = 0;
  25. int[] underscorelocation = new int[1000];
  26.  
  27. int spacecount = 0;
  28. int[] spacelocation = new int[1000];
  29.  
  30. int dividecount = 0;
  31. int[] dividelocation = new int[1000];
  32.  
  33. int multiplycount = 0;
  34. int[] multiplylocation = new int[1000];
  35.  
  36. int addcount = 0;
  37. int[] addlocation = new int[1000];
  38.  
  39. int subtractcount = 0;
  40. int[] subtractlocation = new int[1000];
  41.  
  42. int negativecount = 0;
  43. int[] negativelocation = new int[1000];
  44. negativelocation[1] = -1;
  45.  
  46. //count and location
  47. for (int i = 0; i < operation.length(); i++)
  48. {
  49. character_[i] = operation.charAt(i);
  50. if (character_[i] == '*')
  51. {
  52. if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
  53. {
  54. operationType = 1;
  55. multiplycount += 1;
  56. multiplylocation[multiplycount] = i;
  57. }
  58. }
  59. if (character_[i] == '+')
  60. {
  61. if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
  62. {
  63. operationType = 3;
  64. addcount += 1;
  65. addlocation[addcount] = i;
  66. }
  67. }
  68. if (character_[i] == '-')
  69. {
  70. //potential for one bug here
  71. if (operation.charAt(i+1) == ' ')
  72. {
  73. operationType = 4;
  74. subtractcount += 1;
  75. subtractlocation[subtractcount] = i;
  76. }
  77. if (Character.isDigit(operation.charAt(i+1)) == true)
  78. {
  79. if (operation.charAt(0) == '-')
  80. {
  81. negativecount += 1;
  82. negativelocation[negativecount] = i;
  83. }
  84. else
  85. {
  86. System.out.println("AT " + i + "THERE IS A -");
  87. negativecount += 1;
  88. negativelocation[negativecount-1] = i;
  89. }
  90. }
  91. if (i > 0)
  92. {
  93. if (Character.isDigit(operation.charAt(i+1)) == true && operation.charAt(i-1) == ' ')
  94. {
  95. negativecount += 1;
  96. negativelocation[negativecount] = i;
  97. }
  98. }
  99. }
  100.  
  101. if (character_[i] == ' ')
  102. {
  103. if (i == 0)
  104. {
  105. error = true;
  106. System.out.println("space before first number error");
  107. }
  108. if (i == (operation.length() - 1))
  109. {
  110. error = true;
  111. System.out.println("space after last number error");
  112. }
  113. if (operation.charAt(i-1) != ' ' && operation.charAt(i+1) == '-')
  114. {
  115. spacecount += 1;
  116. spacelocation[spacecount] = i;
  117. }
  118.  
  119.  
  120. if (Character.isDigit(operation.charAt(i-1)) == true && operation.charAt(i+1) != ' ')
  121. {
  122. spacecount += 1;
  123. spacelocation[spacecount] = i;
  124. }
  125. if (Character.isDigit(operation.charAt(i+1)) ==true && operation.charAt(i-1) != ' ')
  126. {
  127. spacecount += 1;
  128. spacelocation[spacecount] = i;
  129. }
  130. //FOR BUG TEST OVERHAUL (going to take like 20 minutes to sort the stupid extra spaces
  131. /*if (i == 0)
  132. {
  133. for (int j = 0; j < operation.length(); j++)
  134. {
  135. character__[i] = operation.charAt(i);
  136. }
  137. }*/
  138. }
  139.  
  140. if (character_[i] == '/')
  141. {
  142. if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
  143. {
  144. operationType = 2;
  145. dividecount += 1;
  146. dividelocation[dividecount] = i;
  147. }
  148. if (Character.isDigit(operation.charAt(i-1)) == true && Character.isDigit(operation.charAt(i+1)) == true)
  149. {
  150. slashcount += 1;
  151. slashlocation[slashcount] = i;
  152. System.out.println();
  153. }
  154. if (operation.charAt(i-1) != ' ' && operation.charAt(i+1) != ' ')
  155. {
  156. if (Character.isDigit(operation.charAt(i-1)) == false || Character.isDigit(operation.charAt(i+1)) == false)
  157. {
  158. System.out.println("no space or digit around / error");
  159. error = true;
  160. }
  161. }
  162. }
  163. if (character_[i] == '_')
  164. {
  165. underscorecount += 1;
  166. underscorelocation[underscorecount] = i;
  167. }
  168. }
  169. //System.out.println("fbbarcount: " + slashcount);
  170. //System.out.println("_count: " + underscorecount);
  171.  
  172.  
  173.  
  174. //type sorter
  175.  
  176. //whole number + whole number
  177. if (underscorecount == 0 && slashcount == 0)
  178. {
  179. inputType = 1;
  180. }
  181. //whole number + fraction
  182. else if (underscorecount == 0 & slashcount == 1 && slashlocation[1] > spacelocation[2])
  183. {
  184. inputType = 2;
  185. }
  186. //whole number + mixed number
  187. else if (underscorecount == 1 && underscorelocation[1] > spacelocation[2] && slashcount == 1 && slashlocation[1] > spacelocation[2])
  188. {
  189. inputType = 3;
  190. }
  191.  
  192.  
  193. //fraction + whole number
  194. else if (underscorecount == 0 && slashcount == 1 && slashlocation[1] < spacelocation[1])
  195. {
  196. inputType = 4;
  197. }
  198. //fraction + fraction
  199. else if (underscorecount == 0 && slashcount == 2 && slashlocation[1] < spacelocation[1] && slashlocation[2] > spacelocation[2])
  200. {
  201. inputType = 5;
  202. }
  203. ///fraction + mixed number
  204. else if (underscorecount == 1 & underscorelocation[1] > spacelocation[2] && slashcount == 2 && slashlocation[1] < spacelocation[1])
  205. {
  206. inputType = 6;
  207. }
  208. //mixed number + whole number
  209. else if (underscorecount == 1 && underscorelocation[1] < spacelocation[1] && slashcount == 1)
  210. {
  211. inputType = 7;
  212. }
  213. //mixed number + fraction
  214. else if (underscorecount == 1 & underscorelocation[1] < spacelocation[1] && slashcount == 2 && slashlocation[2] > spacelocation[2])
  215. {
  216. inputType = 8;
  217. }
  218. //mixed number + mixed number
  219. else if (underscorecount == 2 && slashcount == 2)
  220. {
  221. inputType = 9;
  222. }
  223.  
  224. else
  225. {
  226. System.out.println("too many underscores error");
  227. error = true;
  228. }
  229. System.out.println("negative count = " + negativecount + "negative location =" + negativelocation[1]);
  230. //initializations
  231. char character = '0';
  232. int wholenumber1 = 0;
  233. int numerator1 = 0;
  234. int denominator1 = 0;
  235. int wholenumber2 = 0;
  236. int numerator2 = 0;
  237. int denominator2 = 0;
  238.  
  239.  
  240. //first error test, does the whole number have any alphabetic values? if so, error = true.
  241. for (int i = 0; i < operation.length(); i++)
  242. {
  243. character_[i] = operation.charAt(i);
  244. if (Character.isAlphabetic(character_[i]))
  245. {
  246. error = true;
  247. System.out.println("alphabet detected");
  248. }
  249. }
  250.  
  251.  
  252.  
  253.  
  254. //converts the whole number given in the string to an int value to be used for math! :)
  255. boolean negativewhole1 = false;
  256. int negoffset1 = 0;
  257. int negmultiplier1 = 1;
  258. int negoffset2 = 0;
  259. int negmultiplier2 = 1;
  260. boolean negativewhole2 = false;
  261.  
  262. //first word test
  263. int firstword = 0;
  264. //second word test
  265. int secondword = 0;
  266.  
  267.  
  268. //negative offset boolean test
  269.  
  270. if (negativelocation[1] == 0)
  271. {
  272. negoffset1 = 1;
  273. negmultiplier1 = -1;
  274. negativewhole1 = true;
  275.  
  276. }
  277. //System.out.println("NEGLOCATION1: " + negativelocation[1]);
  278. if (negativelocation[2] == spacelocation[2] + 1)
  279. {
  280. negoffset2 = 1;
  281. negmultiplier2 = -1;
  282. negativewhole2 = true;
  283. }
  284. else if (negativelocation[2] == spacelocation[3] + 1)
  285. {
  286. negoffset2 = 1;
  287. negmultiplier2 = -1;
  288. negativewhole2 = true;
  289. }
  290.  
  291.  
  292.  
  293. //wholenumber test for mixed numbers
  294. if (inputType == 7 || inputType == 8 || inputType == 9)
  295. {
  296. for (int i = 0 + negoffset1; i <= underscorelocation[1]; i++)
  297. {
  298. character = operation.charAt(i);
  299. int numcharacter = Character.getNumericValue(character);
  300. wholenumber1 += numcharacter * Math.pow(10, underscorelocation[1]-i-1);;
  301. }
  302. wholenumber1 += 1;
  303. wholenumber1 *= negmultiplier1;
  304.  
  305. firstword++;
  306. }
  307. //whole number test for whole numbers
  308. if (inputType == 1 || inputType == 2 || inputType == 3)
  309. {
  310.  
  311.  
  312.  
  313.  
  314. for (int i = 0 + negoffset1; i <= spacelocation[1]; i++)
  315. {
  316. character = operation.charAt(i);
  317. int numcharacter = Character.getNumericValue(character);
  318. wholenumber1 += numcharacter * Math.pow(10, spacelocation[1]-i-1);
  319. }
  320. if (operation.charAt(0) != '-')
  321. {
  322. wholenumber1 += 1;
  323. }
  324.  
  325. wholenumber1 *= negmultiplier1;
  326. if (operation.charAt(0) == '-')
  327. {
  328. wholenumber1 -= 1;
  329. }
  330.  
  331. firstword++;
  332. if (operation.charAt(0) == '0')
  333. {
  334. wholenumber1 = 0;
  335. }
  336. if (operation.charAt(1) == '0' && operation.charAt(0) == '-')
  337. {
  338. wholenumber1 = 0;
  339. }
  340. }
  341.  
  342.  
  343.  
  344. //converts first numerator of mixednumber to math
  345. if (inputType == 7 || inputType == 8 || inputType == 9)
  346. {
  347. for (int i = underscorelocation[1]+1; i < slashlocation[1]; i++)
  348. {
  349. character = operation.charAt(i);
  350.  
  351. int characternum = Character.getNumericValue(character);
  352. numerator1 += characternum * Math.pow(10,slashlocation[1] - i-1);
  353. }
  354. firstword++;
  355.  
  356. }
  357.  
  358. //converts first numerator of fraction to math
  359. if (inputType == 4 || inputType == 5 || inputType == 6)
  360. {
  361. for (int i = 0 + negoffset1; i < slashlocation[1]; i++)
  362. {
  363. character = operation.charAt(i);
  364.  
  365. int characternum = Character.getNumericValue(character);
  366. numerator1 += characternum * Math.pow(10,slashlocation[1] - i-1);
  367. }
  368. numerator1 *= negmultiplier1;
  369. firstword++;
  370. }
  371.  
  372.  
  373.  
  374. //converts denominator to a number for math
  375. if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
  376. {
  377. for (int i = slashlocation[1]+1; i < spacelocation[1]; i++)
  378. {
  379. character = operation.charAt(i);
  380.  
  381. int characternum = Character.getNumericValue(character);
  382. denominator1 += characternum * Math.pow(10,spacelocation[1] - i-1);
  383. }
  384. firstword++;
  385. }
  386. //converts second whole number of mixednumber to a number for math
  387. if (inputType == 9)
  388. {
  389. if (negativelocation[2] != 0)
  390. {
  391. for (int i = negativelocation[2] + 1; i < underscorelocation[2]; i++)
  392. {
  393. character = operation.charAt(i);
  394. int characternum = Character.getNumericValue(character);
  395. wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
  396. }
  397. if (operationType == 4)
  398. {
  399. wholenumber2 *= -1;
  400. }
  401. wholenumber2 *=negmultiplier2;
  402. }
  403. else
  404. {
  405.  
  406. for (int i = spacelocation[2] + 1; i < underscorelocation[2]; i++)
  407. {
  408. character = operation.charAt(i);
  409. int characternum = Character.getNumericValue(character);
  410. wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
  411. }
  412. }
  413. secondword++;
  414. }
  415. if (inputType == 3 || inputType == 6)
  416. {
  417.  
  418. if (negativelocation[2] != 0)
  419. {
  420.  
  421. for (int i = negativelocation[2] + 1; i < underscorelocation[1]; i++)
  422. {
  423. character = operation.charAt(i);
  424. int characternum = Character.getNumericValue(character);
  425. wholenumber2 += characternum * Math.pow(10,underscorelocation[1] - i-1);
  426. }
  427. if (operationType == 4)
  428. {
  429. wholenumber2 *= -1;
  430. }
  431. wholenumber2 *=negmultiplier2;
  432. }
  433. else
  434. {
  435.  
  436. for (int i = spacelocation[2] + 1; i < underscorelocation[1]; i++)
  437. {
  438. character = operation.charAt(i);
  439. int characternum = Character.getNumericValue(character);
  440. wholenumber2 += characternum * Math.pow(10,underscorelocation[1] - i-1);
  441. }
  442.  
  443. }
  444. secondword++;
  445. }
  446. /*{
  447. for (int i = spacelocation[2]+1 + negoffset2; i < underscorelocation[2]; i++)
  448. {
  449. character = operation.charAt(i);
  450. int characternum = Character.getNumericValue(character);
  451. wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
  452. }
  453. wholenumber2 *= negmultiplier2;
  454. secondword++;
  455. }*/
  456. //converts second whole number to int value for math
  457. if (inputType == 1 || inputType == 4 || inputType == 7)
  458. {
  459. if (negativelocation[2] != 0)
  460. {
  461. for (int i = negativelocation[2] + 1; i < operation.length(); i++)
  462. {
  463. character = operation.charAt(i);
  464. int characternum = Character.getNumericValue(character);
  465. wholenumber2 += characternum * Math.pow(10,operation.length() - i-1);
  466. }
  467. if (operationType == 4)
  468. {
  469. wholenumber2 *= -1;
  470. }
  471. wholenumber2 *=negmultiplier2;
  472. }
  473. else
  474. {
  475. for (int i = spacelocation[2] + 1; i < operation.length(); i++)
  476. {
  477. character = operation.charAt(i);
  478. int characternum = Character.getNumericValue(character);
  479. wholenumber2 += characternum * Math.pow(10,operation.length() - i-1);
  480. }
  481. }
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488. secondword++;
  489. }
  490. //converts second numerator of mixed number to a number for math
  491. if (inputType == 3)
  492. {
  493.  
  494.  
  495. for (int i = underscorelocation[1]; i < slashlocation[1]; i++)
  496. {
  497. character = operation.charAt(i);
  498.  
  499. int characternum = Character.getNumericValue(character);
  500. numerator2 += characternum * Math.pow(10,slashlocation[1] - i-1);
  501.  
  502. }
  503.  
  504. secondword++;
  505. }
  506. if (inputType == 6)
  507. {
  508.  
  509. for (int i = underscorelocation[1] + 1; i < slashlocation[2]; i++)
  510. {
  511. character = operation.charAt(i);
  512.  
  513. int characternum = Character.getNumericValue(character);
  514.  
  515. numerator2 += characternum * Math.pow(10,slashlocation[2] - i-1);
  516.  
  517. }
  518.  
  519. secondword++;
  520. }
  521.  
  522. if (inputType == 9)
  523. {
  524. for (int i = underscorelocation[2] + 1; i < slashlocation[2]; i++)
  525. {
  526.  
  527. character = operation.charAt(i);
  528. int characternum = Character.getNumericValue(character);
  529. numerator2 += characternum * Math.pow(10,slashlocation[2] - i-1);
  530. }
  531. secondword++;
  532. }
  533. int slashmodifier = 0;
  534.  
  535. //bugfixing
  536. if (inputType == 2)
  537. {
  538. slashmodifier = 1;
  539. }
  540. if (inputType == 5 || inputType == 8)
  541. {
  542. slashmodifier = 2;
  543. }
  544. //converts second numerator of fraction to a number for math
  545. if (inputType == 2 || inputType == 5 || inputType == 8)
  546. {
  547.  
  548.  
  549. for (int i = (spacelocation[2]+3 + negoffset2); i <slashlocation[slashmodifier]; i++)
  550. {
  551. character = operation.charAt(i);
  552. System.out.println(character);
  553. int characternum = Character.getNumericValue(character);
  554.  
  555. numerator2 += characternum * Math.pow(10,slashlocation[slashmodifier] - i-1);
  556. }
  557. numerator2 *= negmultiplier2;
  558. secondword++;
  559. }
  560.  
  561.  
  562.  
  563. //converts second denominator to a number for math
  564. if (inputType == 2 || inputType == 3)
  565. {
  566. for (int i = slashlocation[1] + 1; i < operation.length(); i++)
  567. {
  568. character = operation.charAt(i);
  569. int characternum = Character.getNumericValue(character);
  570. denominator2 += characternum * Math.pow(10,operation.length() - i-1);
  571. }
  572. secondword++;
  573. }
  574. if (inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
  575. {
  576. for (int i = slashlocation[2] + 1; i < operation.length(); i++)
  577. {
  578. character = operation.charAt(i);
  579. int characternum = Character.getNumericValue(character);
  580. denominator2 += characternum * Math.pow(10,operation.length() - i-1);
  581. }
  582. secondword++;
  583. }
  584.  
  585. //fraction simplifier
  586.  
  587. //fraction to mixed number
  588.  
  589. if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
  590. {
  591. if (numerator1 > denominator1 && denominator1 != 0)
  592. {
  593. wholenumber1 += (numerator1 / denominator1);
  594. numerator1 = (numerator1 % denominator1);
  595.  
  596. }
  597. }
  598. if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
  599. {
  600. if (numerator2 > denominator2 && denominator2 != 0)
  601. {
  602. wholenumber2 += (numerator2 / denominator2);
  603. numerator2 = (numerator2 % denominator2);
  604.  
  605. }
  606. }
  607.  
  608. int x1 = denominator1;
  609. int y1 = numerator1;
  610. int x2 = denominator2;
  611. int y2 = numerator2;
  612.  
  613. if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
  614. {
  615. int z = 0;
  616. while (y1 != 0)
  617. {
  618. z = y1;
  619. y1 = x1 % y1;
  620. x1 = z;
  621.  
  622. }
  623. }
  624. if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
  625. {
  626. int z = 0;
  627. while (y2 != 0)
  628. {
  629. z = y2;
  630. y2 = x2 % y2;
  631. x2 = z;
  632. }
  633. }
  634.  
  635.  
  636. if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
  637. {
  638. if (negmultiplier1 == -1)
  639. {
  640. numerator1 /= -x1;
  641. denominator1 /= -x1;
  642. }
  643. else
  644. {
  645. numerator1/= x1;
  646. denominator1 /= x1;
  647. }
  648. }
  649. if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
  650. {
  651. if (negmultiplier2 == -1)
  652. {
  653. numerator2 /= -x2;
  654. denominator2 /= -x2;
  655. }
  656. else
  657. {
  658. numerator2 /= x2;
  659. denominator2 /= x2;
  660. }
  661. }
  662. String operationsorter = "";
  663. if (operationType == 1)
  664. {
  665. operationsorter = "multiplied by";
  666. }
  667. if (operationType == 2)
  668. {
  669. operationsorter = "divided by";
  670. }
  671. if (operationType == 3)
  672. {
  673. operationsorter = "added to";
  674. }
  675. if (operationType == 4)
  676. {
  677. operationsorter = "subtracted by";
  678. }
  679. String fword = " ";
  680. String sword = " ";
  681. if (firstword == 1)
  682. {
  683. fword = "whole number";
  684. }
  685. if (firstword == 2)
  686. {
  687. fword = "fraction";
  688. }
  689. if (firstword == 3)
  690. {
  691. fword = "mixed number";
  692. }
  693. if (secondword == 1)
  694. {
  695. sword = "whole number";
  696. }
  697. if (secondword == 2)
  698. {
  699. sword = "fraction";
  700. }
  701. if (secondword == 3)
  702. {
  703. sword = "mixed number";
  704. }
  705.  
  706.  
  707. System.out.println("You inputed a " + fword + " " + operationsorter + " a " + sword);
  708.  
  709. System.out.println("Is this your input?");
  710. System.out.println("\n" + wholenumber1 + "_" + numerator1 + "/" + denominator1 + " " + operationsorter + " " + wholenumber2 + "_" + numerator2 + "/" + denominator2);
  711.  
  712.  
  713. if (operation == "quit")
  714. {
  715. error = true;
  716. }
  717.  
  718. }
  719. }
  720. public static void initialization()
  721. {
  722. System.out.println("This is a fully functional fractional calculator with");
  723. System.out.println("four operations. The calculator accepts the number format");
  724. System.out.println("of a_b/c, so for example you'd write five and three tenths");
  725. System.out.println("as 5_3/10. Use + for addition, - for subtraction, * for");
  726. System.out.println("multiplication, and / for division.\n");
  727. System.out.println("EX.) 26_1/2 * 5_21/30");
  728.  
  729. }
  730. public static void fractionalTesting()
  731. {
  732.  
  733. }
  734. /*LIST OF THINGS TO DO
  735. *
  736. * TEST FOR BAD VALUES
  737. * FIX SUBTRACTION BUG
  738. *
  739. * MORE EXTENSIVE ERROR TESTING + MESSAGE SYSTEM
  740.  
  741. *
  742. *
  743. *
  744.  
  745. *
  746. *
  747. * operations
  748. *
  749. *
  750. * cleaner code
  751. * ??
  752. *
  753. *
  754. */
  755. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement