From 5e20cf97260bb72861723c13a142ccd68c50cdaa Mon Sep 17 00:00:00 2001 From: DaniilPavlov Date: Mon, 19 Feb 2018 19:32:23 +0300 Subject: [PATCH 1/5] 1 --- src/main/java/ru/spbstu/kspt/task1/Main.java | 33 +++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/spbstu/kspt/task1/Main.java b/src/main/java/ru/spbstu/kspt/task1/Main.java index 0b91675..0efb985 100644 --- a/src/main/java/ru/spbstu/kspt/task1/Main.java +++ b/src/main/java/ru/spbstu/kspt/task1/Main.java @@ -6,11 +6,34 @@ /** * Main class */ +import java.util.Scanner; + public class Main { - private static final Logger logger = LogManager.getLogger(Main.class); - public static void main(String[] args) { - logger.debug("Logging example"); - System.out.println("Hello World!"); + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + BigBigInt bbInt1; + BigBigInt bbInt2; + + do { + System.out.println("Введите ПЕРВОЕ число из интервала [0,+oo]"); + String string1 = in.nextLine(); + bbInt1 = new BigBigInt(string1); + }while (bbInt1.getValue() == null); + + do { + System.out.println("Введите ВТОРОЕ число из интервала [0,+oo]"); + String string2 = in.nextLine(); + bbInt2 = new BigBigInt(string2); + }while (bbInt2.getValue() == null); + + System.out.println("Первое число: " + bbInt1.getValue()); + System.out.println("Второе число: " + bbInt2.getValue()); + System.out.println("Сравнение двух чисел: " + bbInt1.Comparison(bbInt2)); + System.out.println("Сумма двух чисел: " + BigBigInt.addition(bbInt1, bbInt2).getValue()); + System.out.println("Разность двух чисел: " + BigBigInt.subtraction(bbInt1, bbInt2).getValue()); + System.out.println("Произведение двух чисел: " + BigBigInt.multiplication(bbInt1, bbInt2).getValue()); + System.out.println("Деление двух чисел: " + BigBigInt.division(bbInt1, bbInt2).getValue()); + //System.out.println("Остаток от деления двух чисел: " + BigBigInt.residue(bbInt1, bbInt2).getValue()); } -} +} \ No newline at end of file From 1ec870f575d806152e58d28bacf9257e98a5e85e Mon Sep 17 00:00:00 2001 From: DaniilPavlov Date: Mon, 19 Feb 2018 21:19:52 +0300 Subject: [PATCH 2/5] 2 --- .../java/ru/spbstu/kspt/task1/BigBigInt.java | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 src/main/java/ru/spbstu/kspt/task1/BigBigInt.java diff --git a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java new file mode 100644 index 0000000..868c7d4 --- /dev/null +++ b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java @@ -0,0 +1,187 @@ +package ru.spbstu.kspt.task1; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +/** +Объекты данного класса (bbInt) - положительные числа длинной от одного, до 2^31-1 элементов +Объекты умеют: + +сравниваться + +складываться + +вычитаться + +умножаться + -делиться + -находить остаток от деления + */ + + +class BigBigInt { + + private String value;//Хранимое значение исходного огромного числа (bbInt) + + String getValue() {//Получение значения bbInt + return value; + } + + BigBigInt(String string) { //Конструктор класса + //Обработка впередиидущих '0' и ' ' + int i; + for (i = 0; i < string.length(); i++) { + if (string.charAt(i) != '0' && string.charAt(i) != ' ') { + break; + } + if (i == string.length() - 1 && string.charAt(i) == '0') { + value = "0"; + return; + } + } + //Преобразование строик в число без впередиидущих '0' и ' ' + char[] help = new char[string.length() - i]; + string.getChars(i, string.length(), help, 0); + value = new String(help); + //Обработка ввода строки не содержащей чисел + if (value.compareTo("") == 0) { + System.out.println("Ошибка ввода числа"); + value = null; + } + } + + int Comparison(BigBigInt bbInt) {//Сравнение двух bbInt + int result = -2; + if (this.value.compareTo(bbInt.value) == 0) { + result = 0; + return result; + } + if (this.value.length() > bbInt.value.length()) { + result = 1; + return result; + } + if (this.value.length() < bbInt.value.length()) { + result = -1; + return result; + } + for (int i = 0; i < Math.min(this.value.length(), bbInt.value.length()); i++) { + if (this.value.charAt(i) > bbInt.value.charAt(i)) { + result = 1; + return result; + } + if (this.value.charAt(i) < bbInt.value.charAt(i)) { + result = -1; + return result; + } + } + System.out.println("Ошибка при попытке сравнения"); + return result; + } + + static BigBigInt addition(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух bbInt + if (bbInt1.Comparison(bbInt2) < 0) { + BigBigInt bbInt; + bbInt = bbInt1; + bbInt1 = bbInt2; + bbInt2 = bbInt; + } + String answer = ""; + String str2 = ""; + for (int i = 0; i < bbInt1.value.length() - bbInt2.value.length(); i++) { + str2 = str2 + "0"; + } + str2 = str2 + bbInt2.value; + boolean ten = false; + int help; + for (int i = bbInt1.value.length() - 1; i >= 0; i--) { + if (ten) + help = 1; + else + help = 0; + help += Integer.parseInt(bbInt1.value.charAt(i) + "") + Integer.parseInt(str2.charAt(i) + ""); + if (help >= 10) { + ten = true; + help -= 10; + } else + ten = false; + answer = Integer.toString(help) + answer; + } + if (ten) + answer = "1" + answer; + return new BigBigInt(answer); + } + + static BigBigInt subtraction(BigBigInt bbInt1, BigBigInt bbInt2) {//Разность двух bbInt + if (bbInt1.Comparison(bbInt2) < 0) { + BigBigInt bbInt; + bbInt = bbInt1; + bbInt1 = bbInt2; + bbInt2 = bbInt; + } + String answer = ""; + String str2 = ""; + for (int i = 0; i < bbInt1.value.length() - bbInt2.value.length(); i++) { + str2 = str2 + "0"; + } + str2 = str2 + bbInt2.value; + boolean ten = false; + int help; + for (int i = bbInt1.value.length() - 1; i >= 0; i--) { + if (ten) + help = -1; + else + help = 0; + help += Integer.parseInt(bbInt1.value.charAt(i) + "") - Integer.parseInt(str2.charAt(i) + ""); + if (help < 0) { + ten = true; + help += 10; + } else + ten = false; + answer = Integer.toString(help) + answer; + } + return new BigBigInt(answer); + } + + static BigBigInt multiplication(BigBigInt bbInt1, BigBigInt bbInt2) {//Произведение двух bbIn + if (bbInt1.Comparison(bbInt2) < 0) { + BigBigInt bbInt; + bbInt = bbInt1; + bbInt1 = bbInt2; + bbInt2 = bbInt; + } + BigBigInt answer = new BigBigInt("0"); + for (int i = bbInt2.value.length() - 1; i >= 0; i--) { + int help = 0; + String ans = ""; + for (int j = bbInt1.value.length() - 1; j >= 0; j--) { + help += Integer.parseInt(bbInt2.value.charAt(i) + "") * Integer.parseInt(bbInt1.value.charAt(j) + ""); + ans = (help % 10) + ans; + help /= 10; + } + ans = Integer.toString(help) + ans; + for (int j = i; j < bbInt2.value.length() - 1; j++) + ans = ans + "0"; + answer = BigBigInt.addition(answer, new BigBigInt(ans)); + } + return answer; + } + + static BigBigInt division(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление двух bbInt + BigBigInt bbInt; + if (bbInt1.Comparison(bbInt2) < 0) { + bbInt = bbInt1; + bbInt1 = bbInt2; + bbInt2 = bbInt; + } + bbInt = new BigBigInt("0"); + String str2; + String counter = "0"; + do { + str2 = addition(bbInt, bbInt2).value; + bbInt = new BigBigInt(str2); + counter = addition(new BigBigInt(counter), new BigBigInt("1")).value; + } while (bbInt1.Comparison(bbInt) == 1); + if (bbInt.Comparison(bbInt1) == 1) + counter = subtraction(new BigBigInt(counter), new BigBigInt("1")).value; + return new BigBigInt(counter); + } + + static BigBigInt residue(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток от деления двух bbInt + return new BigBigInt("В процессе разработки"); + } +} From da64ec1b204fe0183203e5a88d1fb430a870b3d7 Mon Sep 17 00:00:00 2001 From: DaniilPavlov Date: Sun, 4 Mar 2018 21:05:01 +0300 Subject: [PATCH 3/5] Changes --- .../java/ru/spbstu/kspt/task1/BigBigInt.java | 110 +++++++++--------- src/main/java/ru/spbstu/kspt/task1/Main.java | 9 +- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java index 868c7d4..257749e 100644 --- a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java +++ b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java @@ -1,16 +1,14 @@ package ru.spbstu.kspt.task1; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; /** -Объекты данного класса (bbInt) - положительные числа длинной от одного, до 2^31-1 элементов -Объекты умеют: - +сравниваться - +складываться - +вычитаться - +умножаться - -делиться - -находить остаток от деления + * Объекты данного класса (bbInt) - положительные числа длинной от одного, до 2^31-1 элементов + * Объекты умеют: + * +сравниваться + * +складываться + * +вычитаться + * +умножаться + * -делиться + * -находить остаток от деления */ @@ -23,23 +21,12 @@ String getValue() {//Получение значения bbInt } BigBigInt(String string) { //Конструктор класса - //Обработка впередиидущих '0' и ' ' - int i; - for (i = 0; i < string.length(); i++) { - if (string.charAt(i) != '0' && string.charAt(i) != ' ') { - break; - } - if (i == string.length() - 1 && string.charAt(i) == '0') { - value = "0"; - return; - } - } //Преобразование строик в число без впередиидущих '0' и ' ' - char[] help = new char[string.length() - i]; - string.getChars(i, string.length(), help, 0); - value = new String(help); + string = string.replaceAll("\\D", ""); + string = string.replaceFirst("^0+(?!$)", ""); //Обработка ввода строки не содержащей чисел - if (value.compareTo("") == 0) { + value = string; + if (value.isEmpty()) { System.out.println("Ошибка ввода числа"); value = null; } @@ -47,36 +34,20 @@ String getValue() {//Получение значения bbInt int Comparison(BigBigInt bbInt) {//Сравнение двух bbInt int result = -2; - if (this.value.compareTo(bbInt.value) == 0) { - result = 0; - return result; - } - if (this.value.length() > bbInt.value.length()) { - result = 1; - return result; - } - if (this.value.length() < bbInt.value.length()) { - result = -1; - return result; - } + if (this.value.compareTo(bbInt.value) == 0) return 0; + if (this.value.length() > bbInt.value.length()) return 1; + if (this.value.length() < bbInt.value.length()) return -1; for (int i = 0; i < Math.min(this.value.length(), bbInt.value.length()); i++) { - if (this.value.charAt(i) > bbInt.value.charAt(i)) { - result = 1; - return result; - } - if (this.value.charAt(i) < bbInt.value.charAt(i)) { - result = -1; - return result; - } + if (this.value.charAt(i) > bbInt.value.charAt(i)) return 1; + if (this.value.charAt(i) < bbInt.value.charAt(i)) return -1; } System.out.println("Ошибка при попытке сравнения"); return result; } - static BigBigInt addition(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух bbInt + static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух bbInt if (bbInt1.Comparison(bbInt2) < 0) { - BigBigInt bbInt; - bbInt = bbInt1; + BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; } @@ -99,17 +70,17 @@ static BigBigInt addition(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма дв help -= 10; } else ten = false; - answer = Integer.toString(help) + answer; + StringBuilder builder = new StringBuilder(); + answer = builder.append(Integer.toString(help)).append(answer).toString(); } if (ten) answer = "1" + answer; return new BigBigInt(answer); } - static BigBigInt subtraction(BigBigInt bbInt1, BigBigInt bbInt2) {//Разность двух bbInt + static BigBigInt subtract(BigBigInt bbInt1, BigBigInt bbInt2) {//Разность двух bbInt if (bbInt1.Comparison(bbInt2) < 0) { - BigBigInt bbInt; - bbInt = bbInt1; + BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; } @@ -138,6 +109,7 @@ static BigBigInt subtraction(BigBigInt bbInt1, BigBigInt bbInt2) {//Разнос } static BigBigInt multiplication(BigBigInt bbInt1, BigBigInt bbInt2) {//Произведение двух bbIn + if ((bbInt1 == new BigBigInt("0")) || (bbInt2 == new BigBigInt("0"))) return new BigBigInt("0"); if (bbInt1.Comparison(bbInt2) < 0) { BigBigInt bbInt; bbInt = bbInt1; @@ -156,12 +128,13 @@ static BigBigInt multiplication(BigBigInt bbInt1, BigBigInt bbInt2) {//Прои ans = Integer.toString(help) + ans; for (int j = i; j < bbInt2.value.length() - 1; j++) ans = ans + "0"; - answer = BigBigInt.addition(answer, new BigBigInt(ans)); + answer = BigBigInt.add(answer, new BigBigInt(ans)); } return answer; } static BigBigInt division(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление двух bbInt + if ((bbInt1.toString() == "0") || (bbInt2.toString() == "0")) return new BigBigInt("0"); BigBigInt bbInt; if (bbInt1.Comparison(bbInt2) < 0) { bbInt = bbInt1; @@ -172,16 +145,37 @@ static BigBigInt division(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление String str2; String counter = "0"; do { - str2 = addition(bbInt, bbInt2).value; + str2 = add(bbInt, bbInt2).value; bbInt = new BigBigInt(str2); - counter = addition(new BigBigInt(counter), new BigBigInt("1")).value; + counter = add(new BigBigInt(counter), new BigBigInt("1")).value; } while (bbInt1.Comparison(bbInt) == 1); if (bbInt.Comparison(bbInt1) == 1) - counter = subtraction(new BigBigInt(counter), new BigBigInt("1")).value; + counter = subtract(new BigBigInt(counter), new BigBigInt("1")).value; return new BigBigInt(counter); } - static BigBigInt residue(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток от деления двух bbInt - return new BigBigInt("В процессе разработки"); + static BigBigInt remaind(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток от деления двух bbInt + if ((bbInt1 == new BigBigInt("0")) || (bbInt2 == new BigBigInt("0"))) return new BigBigInt("0"); + BigBigInt bbInt; + if (bbInt1.Comparison(bbInt2) < 0) { + bbInt = bbInt1; + bbInt1 = bbInt2; + bbInt2 = bbInt; + } + bbInt = new BigBigInt("0"); + String str2; + String counter = "0"; + do { + str2 = add(bbInt, bbInt2).value; + bbInt = new BigBigInt(str2); + counter = add(new BigBigInt(counter), new BigBigInt("1")).value; + } while (bbInt1.Comparison(bbInt) == 1); + if (bbInt.Comparison(bbInt1) == 0) return new BigBigInt("0"); + else { + bbInt = subtract(bbInt, bbInt2); + counter = subtract(bbInt1, bbInt).value; + } + return new BigBigInt(counter); } + } diff --git a/src/main/java/ru/spbstu/kspt/task1/Main.java b/src/main/java/ru/spbstu/kspt/task1/Main.java index 0efb985..b9411e1 100644 --- a/src/main/java/ru/spbstu/kspt/task1/Main.java +++ b/src/main/java/ru/spbstu/kspt/task1/Main.java @@ -1,8 +1,5 @@ package ru.spbstu.kspt.task1; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - /** * Main class */ @@ -30,10 +27,10 @@ public static void main(String[] args){ System.out.println("Первое число: " + bbInt1.getValue()); System.out.println("Второе число: " + bbInt2.getValue()); System.out.println("Сравнение двух чисел: " + bbInt1.Comparison(bbInt2)); - System.out.println("Сумма двух чисел: " + BigBigInt.addition(bbInt1, bbInt2).getValue()); - System.out.println("Разность двух чисел: " + BigBigInt.subtraction(bbInt1, bbInt2).getValue()); + System.out.println("Сумма двух чисел: " + BigBigInt.add(bbInt1, bbInt2).getValue()); + System.out.println("Разность двух чисел: " + BigBigInt.subtract(bbInt1, bbInt2).getValue()); System.out.println("Произведение двух чисел: " + BigBigInt.multiplication(bbInt1, bbInt2).getValue()); System.out.println("Деление двух чисел: " + BigBigInt.division(bbInt1, bbInt2).getValue()); - //System.out.println("Остаток от деления двух чисел: " + BigBigInt.residue(bbInt1, bbInt2).getValue()); + System.out.println("Остаток от деления двух чисел: " + BigBigInt.remaind(bbInt1, bbInt2).getValue()); } } \ No newline at end of file From 6a018f363e00829411c5a88522d0bd788be1cc0e Mon Sep 17 00:00:00 2001 From: DaniilPavlov Date: Sun, 4 Mar 2018 22:53:34 +0300 Subject: [PATCH 4/5] Tests was added. --- .../java/ru/spbstu/kspt/task1/BigBigInt.java | 39 +++++------ src/main/java/ru/spbstu/kspt/task1/Main.java | 8 +-- .../java/ru/spbstu/kspt/task1/MainTest.java | 69 ++++++++++++++++--- 3 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java index 257749e..94f7954 100644 --- a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java +++ b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java @@ -32,21 +32,21 @@ String getValue() {//Получение значения bbInt } } - int Comparison(BigBigInt bbInt) {//Сравнение двух bbInt + static int compare(BigBigInt bbInt1, BigBigInt bbInt2) {//Сравнение двух bbInt int result = -2; - if (this.value.compareTo(bbInt.value) == 0) return 0; - if (this.value.length() > bbInt.value.length()) return 1; - if (this.value.length() < bbInt.value.length()) return -1; - for (int i = 0; i < Math.min(this.value.length(), bbInt.value.length()); i++) { - if (this.value.charAt(i) > bbInt.value.charAt(i)) return 1; - if (this.value.charAt(i) < bbInt.value.charAt(i)) return -1; + if (bbInt1.value.compareTo(bbInt2.value) == 0) return 0; + if (bbInt1.value.length() > bbInt2.value.length()) return 1; + if (bbInt1.value.length() < bbInt2.value.length()) return -1; + for (int i = 0; i < Math.min(bbInt1.value.length(), bbInt2.value.length()); i++) { + if (bbInt1.value.charAt(i) > bbInt2.value.charAt(i)) return 1; + if (bbInt1.value.charAt(i) < bbInt2.value.charAt(i)) return -1; } System.out.println("Ошибка при попытке сравнения"); return result; } static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух bbInt - if (bbInt1.Comparison(bbInt2) < 0) { + if (compare(bbInt1, bbInt2) < 0) { BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; @@ -79,7 +79,7 @@ static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух } static BigBigInt subtract(BigBigInt bbInt1, BigBigInt bbInt2) {//Разность двух bbInt - if (bbInt1.Comparison(bbInt2) < 0) { + if (compare(bbInt1, bbInt2) < 0) { BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; @@ -108,9 +108,9 @@ static BigBigInt subtract(BigBigInt bbInt1, BigBigInt bbInt2) {//Разност return new BigBigInt(answer); } - static BigBigInt multiplication(BigBigInt bbInt1, BigBigInt bbInt2) {//Произведение двух bbIn + static BigBigInt multiply(BigBigInt bbInt1, BigBigInt bbInt2) {//Произведение двух bbIn if ((bbInt1 == new BigBigInt("0")) || (bbInt2 == new BigBigInt("0"))) return new BigBigInt("0"); - if (bbInt1.Comparison(bbInt2) < 0) { + if (compare(bbInt1, bbInt2) < 0) { BigBigInt bbInt; bbInt = bbInt1; bbInt1 = bbInt2; @@ -133,10 +133,9 @@ static BigBigInt multiplication(BigBigInt bbInt1, BigBigInt bbInt2) {//Прои return answer; } - static BigBigInt division(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление двух bbInt - if ((bbInt1.toString() == "0") || (bbInt2.toString() == "0")) return new BigBigInt("0"); + static BigBigInt divide(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление двух bbInt BigBigInt bbInt; - if (bbInt1.Comparison(bbInt2) < 0) { + if (compare(bbInt1, bbInt2) < 0) { bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; @@ -148,16 +147,16 @@ static BigBigInt division(BigBigInt bbInt1, BigBigInt bbInt2) {//Деление str2 = add(bbInt, bbInt2).value; bbInt = new BigBigInt(str2); counter = add(new BigBigInt(counter), new BigBigInt("1")).value; - } while (bbInt1.Comparison(bbInt) == 1); - if (bbInt.Comparison(bbInt1) == 1) + } while (compare(bbInt1, bbInt) == 1); + if (compare(bbInt, bbInt1) == 1) counter = subtract(new BigBigInt(counter), new BigBigInt("1")).value; return new BigBigInt(counter); } - static BigBigInt remaind(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток от деления двух bbInt + static BigBigInt remain(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток от деления двух bbInt if ((bbInt1 == new BigBigInt("0")) || (bbInt2 == new BigBigInt("0"))) return new BigBigInt("0"); BigBigInt bbInt; - if (bbInt1.Comparison(bbInt2) < 0) { + if (compare(bbInt1, bbInt2) < 0) { bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; @@ -169,8 +168,8 @@ static BigBigInt remaind(BigBigInt bbInt1, BigBigInt bbInt2) {//Остаток str2 = add(bbInt, bbInt2).value; bbInt = new BigBigInt(str2); counter = add(new BigBigInt(counter), new BigBigInt("1")).value; - } while (bbInt1.Comparison(bbInt) == 1); - if (bbInt.Comparison(bbInt1) == 0) return new BigBigInt("0"); + } while (compare(bbInt1, bbInt) == 1); + if (compare(bbInt1, bbInt) == 0) return new BigBigInt("0"); else { bbInt = subtract(bbInt, bbInt2); counter = subtract(bbInt1, bbInt).value; diff --git a/src/main/java/ru/spbstu/kspt/task1/Main.java b/src/main/java/ru/spbstu/kspt/task1/Main.java index b9411e1..f985cbc 100644 --- a/src/main/java/ru/spbstu/kspt/task1/Main.java +++ b/src/main/java/ru/spbstu/kspt/task1/Main.java @@ -26,11 +26,11 @@ public static void main(String[] args){ System.out.println("Первое число: " + bbInt1.getValue()); System.out.println("Второе число: " + bbInt2.getValue()); - System.out.println("Сравнение двух чисел: " + bbInt1.Comparison(bbInt2)); + System.out.println("Сравнение двух чисел: " + BigBigInt.compare(bbInt1, bbInt2)); System.out.println("Сумма двух чисел: " + BigBigInt.add(bbInt1, bbInt2).getValue()); System.out.println("Разность двух чисел: " + BigBigInt.subtract(bbInt1, bbInt2).getValue()); - System.out.println("Произведение двух чисел: " + BigBigInt.multiplication(bbInt1, bbInt2).getValue()); - System.out.println("Деление двух чисел: " + BigBigInt.division(bbInt1, bbInt2).getValue()); - System.out.println("Остаток от деления двух чисел: " + BigBigInt.remaind(bbInt1, bbInt2).getValue()); + System.out.println("Произведение двух чисел: " + BigBigInt.multiply(bbInt1, bbInt2).getValue()); + System.out.println("Деление двух чисел: " + BigBigInt.divide(bbInt1, bbInt2).getValue()); + System.out.println("Остаток от деления двух чисел: " + BigBigInt.remain(bbInt1, bbInt2).getValue()); } } \ No newline at end of file diff --git a/src/test/java/ru/spbstu/kspt/task1/MainTest.java b/src/test/java/ru/spbstu/kspt/task1/MainTest.java index 21d5f4a..053cba9 100644 --- a/src/test/java/ru/spbstu/kspt/task1/MainTest.java +++ b/src/test/java/ru/spbstu/kspt/task1/MainTest.java @@ -1,17 +1,70 @@ package ru.spbstu.kspt.task1; import org.junit.jupiter.api.Test; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; class MainTest { - private static final Logger logger = LogManager.getLogger(MainTest.class); + + + @Test + void compare() { + assertEquals(-1, + BigBigInt.compare(new BigBigInt("461849213528055328502"), + new BigBigInt("461849213628055328502"))); + assertEquals(-1, + BigBigInt.compare(new BigBigInt(" udsihfs 3798196214 uaoudj21 "), + new BigBigInt(" 0000 3423248jajklf =12321321"))); + + assertEquals(0, + BigBigInt.compare(new BigBigInt("9172470126471204862176"), + new BigBigInt("9172470126471204862176"))); + assertEquals(0, + BigBigInt.compare(new BigBigInt(" fusaljfa4378748320023432yo7hfa "), + new BigBigInt(" fusaljfa4378748320023432yo7hfa "))); + + assertEquals(1, + BigBigInt.compare(new BigBigInt("763068415235893205639275"), + new BigBigInt("763068415235893105639275"))); + assertEquals(1, + BigBigInt.compare(new BigBigInt(" 743276yuafhsbnamm;of73269432"), + new BigBigInt("8032-79126147"))); + } + + @Test + void add() { + assertEquals(new BigBigInt("28530940900897641653").getValue(), + BigBigInt.add(new BigBigInt("2853094012060ufjkaj7840812ijfga"), + new BigBigInt(" 7802u89jiufsa800841")).getValue()); + } + + @Test + void subtract() { + assertEquals(new BigBigInt("28530939340318039971").getValue(), + BigBigInt.subtract(new BigBigInt("2853094012060ufjkaj7840812ijfga"), + new BigBigInt(" 7802u89jiufsa800841")).getValue()); + } + + @Test + void multiply() { + assertEquals(new BigBigInt("22262401584515588627058511722892").getValue(), + BigBigInt.multiply(new BigBigInt("2853094012060ufjkaj7840812ijfga"), + new BigBigInt(" 7802u89jiufsa800841")).getValue()); + } + + + @Test + void divide() { + assertEquals(new BigBigInt("39652").getValue(), + BigBigInt.divide(new BigBigInt("3094012060ufjkaj7840812ijfga"), + new BigBigInt(" 7802u89jiufsa800841")).getValue()); + } + @Test - void exampleTest() { - logger.info("Test started"); - assertEquals(10, 10); - logger.info("Test finished"); + void remain() { + assertEquals(new BigBigInt("406444945376").getValue(), + BigBigInt.remain(new BigBigInt("3094012060ufjkaj78412ijfga"), + new BigBigInt(" 7802u89jiufsa800841")).getValue()); } } From a7eb8c0c203474db6bcf538b4864544eb03a2583 Mon Sep 17 00:00:00 2001 From: DaniilPavlov Date: Sun, 4 Mar 2018 23:16:22 +0300 Subject: [PATCH 5/5] Aaaaaaaaaaaand other changes. --- .../java/ru/spbstu/kspt/task1/BigBigInt.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java index 94f7954..f7d5877 100644 --- a/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java +++ b/src/main/java/ru/spbstu/kspt/task1/BigBigInt.java @@ -7,8 +7,8 @@ * +складываться * +вычитаться * +умножаться - * -делиться - * -находить остаток от деления + * +делиться + * +находить остаток от деления */ @@ -20,12 +20,12 @@ String getValue() {//Получение значения bbInt return value; } - BigBigInt(String string) { //Конструктор класса + BigBigInt(String clearLine) { //Конструктор класса //Преобразование строик в число без впередиидущих '0' и ' ' - string = string.replaceAll("\\D", ""); - string = string.replaceFirst("^0+(?!$)", ""); + clearLine = clearLine.replaceAll("\\D", ""); + clearLine = clearLine.replaceFirst("^0+(?!$)", ""); //Обработка ввода строки не содержащей чисел - value = string; + value = clearLine; if (value.isEmpty()) { System.out.println("Ошибка ввода числа"); value = null; @@ -46,16 +46,17 @@ static int compare(BigBigInt bbInt1, BigBigInt bbInt2) {//Сравнение д } static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух bbInt + StringBuilder builder = new StringBuilder(); if (compare(bbInt1, bbInt2) < 0) { BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; } String answer = ""; - String str2 = ""; for (int i = 0; i < bbInt1.value.length() - bbInt2.value.length(); i++) { - str2 = str2 + "0"; + builder.append("0"); } + String str2 = builder.toString(); str2 = str2 + bbInt2.value; boolean ten = false; int help; @@ -70,7 +71,7 @@ static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух help -= 10; } else ten = false; - StringBuilder builder = new StringBuilder(); + builder = new StringBuilder(); answer = builder.append(Integer.toString(help)).append(answer).toString(); } if (ten) @@ -79,16 +80,17 @@ static BigBigInt add(BigBigInt bbInt1, BigBigInt bbInt2) {//Сумма двух } static BigBigInt subtract(BigBigInt bbInt1, BigBigInt bbInt2) {//Разность двух bbInt + StringBuilder builder = new StringBuilder(); if (compare(bbInt1, bbInt2) < 0) { BigBigInt bbInt = bbInt1; bbInt1 = bbInt2; bbInt2 = bbInt; } String answer = ""; - String str2 = ""; for (int i = 0; i < bbInt1.value.length() - bbInt2.value.length(); i++) { - str2 = str2 + "0"; + builder.append("0"); } + String str2 = builder.toString(); str2 = str2 + bbInt2.value; boolean ten = false; int help; @@ -103,7 +105,8 @@ static BigBigInt subtract(BigBigInt bbInt1, BigBigInt bbInt2) {//Разност help += 10; } else ten = false; - answer = Integer.toString(help) + answer; + builder = new StringBuilder(); + answer = builder.append(Integer.toString(help)).append(answer).toString(); } return new BigBigInt(answer); }