Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. package pl.edu.agh.kis;
  2.  
  3.  
  4. import java.math.BigInteger;
  5.  
  6. /**
  7. * Created by Karl on 22.10.2016.
  8. */
  9. public class Factorial {
  10. private static BigInteger obliczSilnie(int n){
  11. BigInteger v = new BigInteger("1");
  12.  
  13. for(int i = 2; i <=n; i++){
  14. v = v.multiply(new BigInteger("" + i));
  15.  
  16. }
  17. return v;
  18. }
  19.  
  20. private static boolean checkForNumberOfArgs(String[] a, int n){
  21. if(a.length >= n){
  22. return true;
  23. } else{
  24. return false;
  25. }
  26. }
  27.  
  28. private static boolean checkForCorrectOperations(String c){
  29. if (c.equals("-suma") || c.equals("-roznica") || c.equals("-iloczyn") || c.equals("-iloraz") || c.equals("-silnia")){
  30. return true;
  31. } else{
  32. return false;
  33. }
  34. }
  35.  
  36. private static BigInteger Silnia(String n){
  37. try{
  38. int a1 = Integer.parseInt(n);
  39. BigInteger b = obliczSilnie(a1);
  40.  
  41. return b;
  42.  
  43. } catch(NumberFormatException e){
  44.  
  45. System.out.println("Wrong argument");
  46. return new BigInteger("0");
  47. }
  48. }
  49.  
  50.  
  51.  
  52. private static BigInteger oblicz(String a, String b, String c){
  53. try {
  54.  
  55. if(c.equals("-silnia")){
  56. BigInteger a1 = Silnia(a);
  57.  
  58. return a1;
  59.  
  60.  
  61. } else {
  62. BigInteger a1 = new BigInteger(a);
  63. BigInteger b1 = new BigInteger(b);
  64.  
  65.  
  66. if (c.equals("-iloraz")) {
  67. if (b1.equals(new BigInteger("0"))) {
  68. throw new IllegalArgumentException("Cannot divide by 0!!");
  69. } else {
  70. a1 = a1.divide(b1);
  71. }
  72. } else if (c.equals("-iloczyn")) {
  73. a1 = a1.multiply(b1);
  74. } else if (c.equals("-suma")) {
  75. a1 = a1.add(b1);
  76. } else if (c.equals("-roznica")) {
  77. a1 = a1.subtract(b1);
  78. }
  79.  
  80.  
  81. return a1;
  82. }
  83. } catch (NumberFormatException e){
  84. System.out.println("Wrong arguments! Can't turn them into BigInteger!");
  85. return new BigInteger("0");
  86. }
  87. }
  88.  
  89. private static void writeAnswer(String a, String b, String c){
  90. if(checkForCorrectOperations(c)) {
  91. BigInteger answer = oblicz(a, b, c);
  92. System.out.print(a + " ");
  93. switch (c) {
  94. case "-iloczyn":
  95. System.out.printf("* ");
  96. break;
  97. case "-iloraz":
  98. System.out.printf("/ ");
  99. break;
  100. case "- suma":
  101. System.out.printf("+ ");
  102. break;
  103. case "-roznica":
  104. System.out.printf("- ");
  105. break;
  106. case "-silnia":
  107. System.out.printf("! = " + answer);
  108.  
  109.  
  110. }
  111. if(c.equals("-silnia") == false) {
  112. System.out.printf(b + " = " + answer);
  113. }
  114. }
  115. }
  116.  
  117. private static void Iloraz(String a, String b, String c){
  118. BigInteger wynik = oblicz(a,b,c);
  119. System.out.println(a + " / " + b + " = " + wynik);
  120. }
  121.  
  122. /*private static BigInteger obliczIloczyn(String a, String b){
  123. try {
  124. BigInteger a1 = new BigInteger(a);
  125. BigInteger b1 = new BigInteger(b);
  126.  
  127. a1 = a1.multiply(b1);
  128.  
  129. return a1;
  130. } catch (NumberFormatException e){
  131. System.out.println("Wrong arguments! Can't turn them into BigInteger!");
  132. return new BigInteger("0");
  133. }
  134. }*/
  135.  
  136. /*private static BigInteger obliczSume(String a, String b){
  137. try {
  138. BigInteger a1 = new BigInteger(a);
  139. BigInteger b1 = new BigInteger(b);
  140.  
  141. a1 = a1.add(b1);
  142.  
  143. return a1;
  144. } catch (NumberFormatException e){
  145. System.out.println("Wrong arguments! Can't turn them into BigInteger!");
  146. return new BigInteger("0");
  147. }
  148. }*/
  149.  
  150.  
  151. public static void main(String[] args){
  152.  
  153. // if na ilosc args.length !!!
  154.  
  155. if(checkForNumberOfArgs(args, 3)){
  156. String nStr = args[0];
  157. /*switch(nStr){
  158. case "-iloczyn":
  159. if(checkForNumberOfArgs(args, 3)){
  160. Iloczyn(args[1], args[2]);
  161. } else{
  162. System.out.println("Not enough arguments!");
  163. }
  164. break;
  165.  
  166. case "-silnia":
  167. if(checkForNumberOfArgs(args, 2)){
  168. Silnia(arg[1]);
  169. } else{
  170. System.out.println("Not enough arguments!");
  171. }
  172. break;
  173.  
  174. case "-iloraz":
  175. if(checkForNumberOfArgs(args, 3)){
  176. Iloraz(args[1], args[2]);
  177. } else{
  178. System.out.println("Not enough arguments!");
  179. }
  180. break;
  181.  
  182. case "-suma":
  183.  
  184. break;
  185. case "-roznica":
  186.  
  187. break;
  188. default:
  189. System.out.println("Wrong argument!");
  190. }*/
  191.  
  192. if(checkForCorrectOperations(nStr)){
  193. writeAnswer(args[1], args[2], args[0]);
  194. } else{
  195. System.out.println("Wrong operator!");
  196. }
  197. } else if(checkForNumberOfArgs(args,2)){
  198. if(args[0].equals("-silnia")) {
  199. writeAnswer(args[1], "", args[0]);
  200. }
  201. } else {
  202. System.out.println("Wrong first argument!");
  203. }
  204. }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement