-
Notifications
You must be signed in to change notification settings - Fork 12
Fifth #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fifth #7
Changes from all commits
c53ba67
3166585
1f7fd5c
f317f77
d2b05b2
6949e5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| class ComplexNumber { | ||
| double re; | ||
| double im; | ||
|
|
||
| public ComplexNumber(double re, double im) { | ||
| this.re = re; | ||
| this.im = im; | ||
| } | ||
|
|
||
| public double getRe() { | ||
| return this.re; | ||
| } | ||
|
|
||
| public double getIm() { | ||
| return this.im; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) {return true;} | ||
| if (obj == null) {return false;} | ||
| if (obj instanceof ComplexNumber) { | ||
| ComplexNumber num = (ComplexNumber) obj; | ||
| return this.getRe() == num.getRe() && this.getIm() == num.getIm(); | ||
| } | ||
| return false; | ||
| } | ||
| @Override | ||
| public int hashCode() { | ||
| return this.toString().hashCode(); | ||
| } | ||
| public String toString() { | ||
| return "" + this.re + " + i*" + this.im; | ||
| } | ||
|
|
||
| ComplexNumber add(ComplexNumber num) { | ||
| ComplexNumber result = new ComplexNumber(0, 0); | ||
| result.re = num.re + this.re; | ||
| result.im = num.im + this.im; | ||
| return result; | ||
| } | ||
|
|
||
| ComplexNumber mul(ComplexNumber num) { | ||
| ComplexNumber result = new ComplexNumber(0, 0); | ||
| result.re = num.re * this.re - num.im * this.im; | ||
| result.im = num.re * this.im + num.im * this.re; | ||
| return result; | ||
| } | ||
|
|
||
| ComplexNumber inverse() { | ||
| ComplexNumber result = new ComplexNumber(0, 0); | ||
| double module = 0; | ||
| module = this.re * this.re + this.im * this.im; | ||
| result.re = this.re / module; | ||
| result.im = -this.im / module; | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,40 @@ | ||
| package solver; | ||
| import java.util.Scanner; | ||
| import java.util.Arrays; | ||
| import java.util.*; | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.print("Hello world!"); | ||
| } | ||
| } | ||
| public static void main(String[] args) { | ||
|
|
||
| makeGood maker = new makeGood(); | ||
|
|
||
| File file = new File("in.txt"); | ||
| try(Scanner scanner = new Scanner(file)) { | ||
| int n = scanner.nextInt(); | ||
| int m = scanner.nextInt(); | ||
| // n - число неизвестных (без ответа), m - число уравнений | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can write comments in English? Think about someone reading your code and improve the functionality. He won't understand anything in your comments. And also, consider using better names for the variables (this way you won't even use a comment to describe the code, the self-describing code is always better than a comment). |
||
| ComplexNumber[] line = new ComplexNumber[n + 1]; | ||
| Row[] row = new Row[m]; | ||
| String s = ""; | ||
| for (int j = 0; j < m; j++) | ||
| { | ||
| for (int i = 0; i <= n; i++){ | ||
| s = scanner.next(); | ||
| line[i] = maker.stringToComplex(s); | ||
| } | ||
| row[j] = new Row(n, line); | ||
| } | ||
| Matrix matrix = new Matrix(row, n, m); | ||
|
|
||
| matrix.steppedViewDown(); | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("I hope that never happen"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there needs to be something more describing the exception. In case it happens you will know what causes the exception to happen and then fix the bug or add some readable output on what causes the exception to happen. |
||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import java.util.Scanner; | ||
| import java.util.Arrays; | ||
| import java.util.*; | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| class Matrix { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let the code be more readable and add here an empty line to distinguish imports and the class definition. |
||
| int N; | ||
| int M; | ||
| Row[] rows = new Row[1]; | ||
| public Matrix(Row[] row, int n, int m) { | ||
| this.rows = row; | ||
| this.N = n; | ||
| this.M = m; | ||
| } | ||
| // true - ненулевое | ||
| boolean checkValue(int i, int j) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the comment and rename the method something like "isNotZero". |
||
| ComplexNumber zero = new ComplexNumber(0, 0); | ||
| return (this.rows[j].getValue(i).equals(zero) ? false: true); | ||
| } | ||
|
|
||
|
|
||
| String resetAllDown(int i, int j) { | ||
| String s = ""; | ||
| ComplexNumber minOne = new ComplexNumber(-1, 0); | ||
| ComplexNumber zero = new ComplexNumber(0, 0); | ||
| ComplexNumber coef = this.rows[j].getValue(i); | ||
| this.rows[j].mul(coef.inverse()); | ||
| s = (coef.inverse().toString()) + "*R" + (j + 1) + " -> R" + (j + 1) + "\n"; | ||
| for (int k = j + 1; k < this.M; k++) { | ||
| coef = this.rows[k].getValue(i); | ||
| if (!coef.equals(zero)) { | ||
| this.rows[k].mul( coef.inverse().mul(minOne)); | ||
| this.rows[k].add(this.rows[j]); | ||
| s = s + coef.inverse().mul(minOne).toString() + "*R" + (k + 1) + " + R" + (j + 1) + " -> R" + (k + 1) + "\n"; | ||
| } | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| String resetAllUp(int i, int j) { | ||
| String s = ""; | ||
| ComplexNumber zero = new ComplexNumber(0, 0); | ||
| ComplexNumber minOne = new ComplexNumber(-1, 0); | ||
| ComplexNumber coef = this.rows[j].getValue(i); | ||
| this.rows[j].mul(coef.inverse()); | ||
| s = coef.inverse().toString() + "*R" + (j + 1) + " -> R" + (j + 1) + "\n"; | ||
| for (int k = j - 1; k >= 0; k--) { | ||
| coef = this.rows[k].getValue(i); | ||
| if (!coef.equals(zero)) { | ||
| this.rows[k].mul(coef.inverse().mul(minOne)); | ||
| this.rows[k].add(this.rows[j]); | ||
| s = s + coef.inverse().mul(minOne).toString() + "*R" + (k + 1) + " + R" + (j + 1) + " -> R" + (k + 1) + "\n"; | ||
| } | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| void steppedViewDown () { | ||
| int j = 0; | ||
| int i = 0; | ||
| String s = ""; | ||
| s += "start solving the equation\n"; | ||
| s += "Rows manipulation\n"; | ||
| for (; i <= this.N && j < this.M; i++) { | ||
| if (this.checkValue(i, j)) { | ||
| s += this.resetAllDown(i, j); | ||
| j += 1; | ||
| } | ||
| else { //пробегаю вниз. Если есть ненулевой - свапаю, если нет - перехожу к след i и j; | ||
| for (int k = j + 1; k < this.M; k++) { | ||
| if (this.checkValue(i, k)) { | ||
| this.swap(i, k); | ||
| s = s + "Swap R" + (i + 1) + " with R" + (k + 1) + "\n"; | ||
| } | ||
| } | ||
| if (!this.checkValue(i, j)) { | ||
| i += 1; | ||
| } | ||
| i -= 1; | ||
| } | ||
| } | ||
|
|
||
| if (j == 0) { | ||
| s += "infinity solutions\n"; | ||
| } | ||
|
|
||
| else { | ||
| if (!this.checkNonzero(0, j - 1)) { | ||
| s += "no solutions\n"; | ||
| } | ||
| else { | ||
| if (j != this.N) { | ||
| s += "infinity solutions\n"; | ||
| } | ||
| else { | ||
| s += this.steppedViewUp(); | ||
| } | ||
| } | ||
| } | ||
| File file = new File("out.txt"); | ||
| try (FileWriter writer = new FileWriter(file)) { | ||
| writer.write(s); | ||
| } catch (IOException e) { | ||
| System.out.printf("Exception"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the program prints "Exception" it gives you nothing to fix it. It doesn't print where the exception happened. So, if you have a lot of these you wouldn't even know where the exception happened. |
||
| } | ||
| } | ||
|
|
||
| String steppedViewUp () { | ||
| String s = ""; | ||
| for (int j = this.N - 1; j > 0; j--) { | ||
| s += this.resetAllUp(j, j); | ||
| } | ||
| s += this.solve(); | ||
| return s; | ||
| } | ||
| // true - есть ненулевой элемент помимо ответа | ||
| boolean checkNonzero(int i, int j) { | ||
| if (i < this.N - 1) { | ||
| return (this.checkNonzero(i + 1, j) || this.checkValue(i, j)); | ||
| } | ||
| else { | ||
| return this.checkValue(i, j); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you using "this" everywhere. It is not necessary to write in places where it is not ambiguous. When you have the local variable that has the same name as your class member variable then you need to use "this" and nowhere else. |
||
| } | ||
| } | ||
|
|
||
| void swap(int i, int j) { | ||
| Row row = this.rows[i]; | ||
| this.rows[i] = this.rows[j]; | ||
| this.rows[j] = row; | ||
| } | ||
| String getString() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use empty lines to separate different methods. |
||
| String s = ""; | ||
| for (int i = 0; i < this.M; i++) { | ||
| s += this.rows[i].getString(); | ||
| s += "\n"; | ||
| } | ||
| return s; | ||
| } | ||
| String solve () { | ||
| String s = ""; | ||
| s += "The solution is: ("; | ||
| for (int i = 0; i < this.N - 1; i++) { | ||
| ComplexNumber now = this.rows[i].getValue(i); | ||
| now = now.inverse(); | ||
| s = s + this.rows[i].getValue(this.N).mul(now).toString() + ", "; | ||
| } | ||
| s = s + this.rows[this.N - 1].getValue(this.N).toString() + ")\n"; | ||
| return s; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| Я решительно не понимаю 1 важную вещь. Сейчас в конструкторе у меня цикл и вроде как лишние приравнения, сделано это потому, | ||
| что иначе при задании массива строк у нас присваивание будет (как я понимаю) по ссылке и в такой ситуации: | ||
| Row[] row = new Row[5]; | ||
| row[0] = first_row; ... row[4] = fifth_row; в row[0] будет храниться fifth_row и я не совсем понимаю почему. Точнее, я не понимаю | ||
| почему при таком задании конструктора как тут, все работает правильно. | ||
| */ | ||
| class Row { // N на 1 больше, чем вход, т.к. тут я учитываю и ответ (то, что справа от равно) | ||
| int N; | ||
| ComplexNumber[] line = new ComplexNumber[1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to write an initial value for the array if you will rewrite it anyway. Like in the constructor. |
||
| public Row(int N, ComplexNumber[] row) { | ||
| this.line = new ComplexNumber[N + 1]; | ||
| for (int i = 0; i <= N; i++) { | ||
| this.line[i] = row[i]; | ||
| } | ||
| this.N = N; | ||
| } | ||
|
|
||
| ComplexNumber getValue(int i) { | ||
| return this.line[i]; | ||
| } | ||
|
|
||
| void add(Row other) { //поидее можно сделать private? | ||
| for (int i = 0; i <= this.N; i++) { | ||
| this.line[i] = this.line[i].add(other.line[i]); | ||
| } | ||
| } | ||
| void mul(ComplexNumber num) { //умножение строки на константу | ||
| for (int i = 0; i <= this.N; i++) { | ||
| this.line[i] = this.line[i].mul(num); | ||
| } | ||
| } | ||
|
|
||
| String getString() { | ||
| String s = "["; | ||
| for (int i = 0; i <= this.N - 1; i++) { | ||
| s = s + this.getValue(i).toString() + ", "; | ||
| } | ||
| s = s + this.getValue(this.N).toString() + "]"; | ||
| return s; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| class makeGood { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I wrote above, the name of the class should start with an uppercase letter. |
||
| ComplexNumber stringToComplex(String s) { | ||
| ComplexNumber num = new ComplexNumber(0, 0); | ||
| String[] parts = new String[2]; | ||
| if (s.contains("+")) { | ||
| parts = s.split("\\+"); | ||
| String re = parts[0]; | ||
| String im = parts[1].substring(0, parts[1].length() -1); | ||
| double Re = Double.parseDouble(re); | ||
| double Im = Double.parseDouble(im); | ||
| num = new ComplexNumber(Re, Im); | ||
| } | ||
|
|
||
| else if (s.indexOf("-") > 0 || s.substring(1, s.length()).indexOf("-") >= 0) | ||
| { | ||
| // System.out.println("Yep, I can parse it too"); | ||
| if (s.indexOf("-") == 0) { | ||
| parts = s.substring(1, s.length()).split("-"); | ||
| double Re = -Double.parseDouble(parts[0]); | ||
| double Im = -Double.parseDouble(parts[1].substring(0, parts[1].length() - 1)); | ||
| num = new ComplexNumber(Re, Im); | ||
| } | ||
| else { | ||
| parts = s.substring(0, s.length()).split("-"); | ||
| double Re = Double.parseDouble(parts[0]); | ||
| double Im = -Double.parseDouble(parts[1].substring(0, parts[1].length() - 1)); | ||
| num = new ComplexNumber(Re, Im); | ||
| } | ||
| } | ||
|
|
||
| else { | ||
| if (s.contains("i")) { | ||
| double Re = 0; | ||
| double Im = Double.parseDouble(s.substring(0, s.length() - 1)); | ||
| num = new ComplexNumber(Re, Im); | ||
| } | ||
| else { | ||
| double Re = Double.parseDouble(s); | ||
| double Im = 0; | ||
| num = new ComplexNumber(Re, Im); | ||
| } | ||
| } | ||
| return num; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strongly recommend having class names that start with an uppercase letter, not lowercase.