Advertisement
Guest User

calculator rough draft 2

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