Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.edu.agh.kis;
- import java.math.BigInteger;
- /**
- * Created by Karl on 22.10.2016.
- */
- public class Factorial {
- private static BigInteger obliczSilnie(int n){
- BigInteger v = new BigInteger("1");
- for(int i = 2; i <=n; i++){
- v = v.multiply(new BigInteger("" + i));
- }
- return v;
- }
- private static boolean checkForNumberOfArgs(String[] a, int n){
- if(a.length >= n){
- return true;
- } else{
- return false;
- }
- }
- private static boolean checkForCorrectOperations(String c){
- if (c.equals("-suma") || c.equals("-roznica") || c.equals("-iloczyn") || c.equals("-iloraz") || c.equals("-silnia")){
- return true;
- } else{
- return false;
- }
- }
- private static BigInteger Silnia(String n){
- try{
- int a1 = Integer.parseInt(n);
- BigInteger b = obliczSilnie(a1);
- return b;
- } catch(NumberFormatException e){
- System.out.println("Wrong argument");
- return new BigInteger("0");
- }
- }
- private static BigInteger oblicz(String a, String b, String c){
- try {
- if(c.equals("-silnia")){
- BigInteger a1 = Silnia(a);
- return a1;
- } else {
- BigInteger a1 = new BigInteger(a);
- BigInteger b1 = new BigInteger(b);
- if (c.equals("-iloraz")) {
- if (b1.equals(new BigInteger("0"))) {
- throw new IllegalArgumentException("Cannot divide by 0!!");
- } else {
- a1 = a1.divide(b1);
- }
- } else if (c.equals("-iloczyn")) {
- a1 = a1.multiply(b1);
- } else if (c.equals("-suma")) {
- a1 = a1.add(b1);
- } else if (c.equals("-roznica")) {
- a1 = a1.subtract(b1);
- }
- return a1;
- }
- } catch (NumberFormatException e){
- System.out.println("Wrong arguments! Can't turn them into BigInteger!");
- return new BigInteger("0");
- }
- }
- private static void writeAnswer(String a, String b, String c){
- if(checkForCorrectOperations(c)) {
- BigInteger answer = oblicz(a, b, c);
- System.out.print(a + " ");
- switch (c) {
- case "-iloczyn":
- System.out.printf("* ");
- break;
- case "-iloraz":
- System.out.printf("/ ");
- break;
- case "- suma":
- System.out.printf("+ ");
- break;
- case "-roznica":
- System.out.printf("- ");
- break;
- case "-silnia":
- System.out.printf("! = " + answer);
- }
- if(c.equals("-silnia") == false) {
- System.out.printf(b + " = " + answer);
- }
- }
- }
- private static void Iloraz(String a, String b, String c){
- BigInteger wynik = oblicz(a,b,c);
- System.out.println(a + " / " + b + " = " + wynik);
- }
- /*private static BigInteger obliczIloczyn(String a, String b){
- try {
- BigInteger a1 = new BigInteger(a);
- BigInteger b1 = new BigInteger(b);
- a1 = a1.multiply(b1);
- return a1;
- } catch (NumberFormatException e){
- System.out.println("Wrong arguments! Can't turn them into BigInteger!");
- return new BigInteger("0");
- }
- }*/
- /*private static BigInteger obliczSume(String a, String b){
- try {
- BigInteger a1 = new BigInteger(a);
- BigInteger b1 = new BigInteger(b);
- a1 = a1.add(b1);
- return a1;
- } catch (NumberFormatException e){
- System.out.println("Wrong arguments! Can't turn them into BigInteger!");
- return new BigInteger("0");
- }
- }*/
- public static void main(String[] args){
- // if na ilosc args.length !!!
- if(checkForNumberOfArgs(args, 3)){
- String nStr = args[0];
- /*switch(nStr){
- case "-iloczyn":
- if(checkForNumberOfArgs(args, 3)){
- Iloczyn(args[1], args[2]);
- } else{
- System.out.println("Not enough arguments!");
- }
- break;
- case "-silnia":
- if(checkForNumberOfArgs(args, 2)){
- Silnia(arg[1]);
- } else{
- System.out.println("Not enough arguments!");
- }
- break;
- case "-iloraz":
- if(checkForNumberOfArgs(args, 3)){
- Iloraz(args[1], args[2]);
- } else{
- System.out.println("Not enough arguments!");
- }
- break;
- case "-suma":
- break;
- case "-roznica":
- break;
- default:
- System.out.println("Wrong argument!");
- }*/
- if(checkForCorrectOperations(nStr)){
- writeAnswer(args[1], args[2], args[0]);
- } else{
- System.out.println("Wrong operator!");
- }
- } else if(checkForNumberOfArgs(args,2)){
- if(args[0].equals("-silnia")) {
- writeAnswer(args[1], "", args[0]);
- }
- } else {
- System.out.println("Wrong first argument!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement