-
-
Save MrVilkaman/a6c8f5fae7df40318ff2e26737e5eba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
import java.io.FileInputStream | |
import java.io.InputStream | |
import java.util.* | |
fun main(args: Array<String>) { | |
val myMatrix1 = MyMatrix2("test.txt") | |
myMatrix1.input() | |
myMatrix1.print() | |
val sum = myMatrix1.getSum() | |
println("Сумма: $sum") | |
} | |
class MyMatrix2( | |
private val fileName: String, | |
n: Int = 2, | |
m: Int = 2 | |
) : MyMatrix1(n, m) { | |
override fun input() { | |
commonInput(FileInputStream(File(fileName))) { | |
println("Была прочитана матрица из файла $fileName>") | |
} | |
} | |
} | |
open class MyMatrix1( | |
val n: Int = 2, | |
val m: Int = 2 | |
) { | |
private var data: Array<IntArray>? = null | |
open fun input() { | |
commonInput(System.`in`) { | |
println("Введите ${n}x${m} элементов") | |
} | |
} | |
protected fun commonInput(input: InputStream, body: () -> Unit) { | |
val dataLocal = Array(n, { _ -> IntArray(m) }) | |
data = dataLocal | |
val scanner = Scanner(input) | |
body.invoke() | |
for (row in 0 until n) { | |
for (col in 0 until m) | |
dataLocal[row][col] = scanner.nextInt() | |
} | |
} | |
fun print() { | |
val localData = data | |
localData ?: return | |
println("Матрица:") | |
for (row in localData) { | |
for (col in row) | |
print("$col ") | |
println() | |
} | |
} | |
fun getSum(): Int = data?.sumBy { it.sum() } ?: 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
println("Я готов)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment