-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.cpp
146 lines (118 loc) · 4.36 KB
/
cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Created by ploca14 on 13.01.22.
//
#include <chrono>
#include <random>
#include "cli.h"
const int MAX = 1000;
Matrix generateRandomMatrix(int n, int m);
template <typename TimePoint>
std::chrono::milliseconds to_ms(TimePoint tp) {
return std::chrono::duration_cast<std::chrono::milliseconds>(tp);
}
int get_random_int(int min,int max) {
static std::mt19937 mt{ std::random_device{}() };
static std::uniform_int_distribution<> dist(min, max);
return dist(mt);
}
void user_multiply() {
std::cout << "First specify the size of the first matrix" << std:: endl;
std::cout << "Number of rows: ";
int a_rows = inputInt(true);
std::cout << "Number of columns: ";
int a_cols = inputInt(false);
std::cout << std::endl;
std::cout << "Now specify the size of the second matrix" << std:: endl;
std::cout << "Number of rows: ";
int b_rows = inputInt(false);
std::cout << "Number of columns: ";
int b_cols = inputInt(false);
if (a_cols != b_rows) {
std::cout << std::endl << "Number of columns in first matrix doesn't match number of rows in second matrix" << std:: endl;
return;
}
std::cout << std::endl;
std::cout << "Now enter the values for the first matrix by rows, divided by spaces" << std:: endl;
Matrix a = inputMatrix(a_rows, a_cols);
std::cout << std::endl;
std::cout << "Now enter the values for the second matrix by rows, divided by spaces" << std:: endl;
Matrix b = inputMatrix(b_rows, b_cols);
auto start = std::chrono::high_resolution_clock::now();
Matrix product = a * b;
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::endl;
std::cout << "The result is: " << std:: endl;
std::cout << product << std::endl;
std::cout << "Needed " << to_ms(end - start).count() << " ms to finish.\n";
}
void random_multiply(int size) {
int n, m, p;
if (size > 0) {
n = m = p = size;
} else {
n = get_random_int(1, MAX);
m = get_random_int(1, MAX);
p = get_random_int(1, MAX);
}
Matrix a = generateRandomMatrix(n, m);
Matrix b = generateRandomMatrix(m, p);
std::cout << "For two random matrices of size " << n << "x" << m << " and " << m << "x" << p << std:: endl;
auto start = std::chrono::high_resolution_clock::now();
Matrix product = a * b;
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::endl;
std::cout << "The result is: " << std:: endl;
std::cout << product << std::endl;
std::cout << "Needed " << to_ms(end - start).count() << " ms to finish.\n";
}
Matrix generateRandomMatrix(int rows, int cols) {
int* data = new int[rows*cols];
for (int i = 0; i < rows*cols; ++i) {
data[i] = get_random_int(-10, 10);
}
Matrix result = Matrix(rows, cols, data);
delete[] data;
return result;
}
void print_help() {
std::cout << "Usage: semestralka [--help] [--random [<size>]] " << std::endl << std::endl;
std::cout << "If you want to multiply random matrices:" << std::endl;
std::cout << std::setw(30) << std::left << " semestralka --random [<size>]" << "The program will generate two random matrices and multiply them" << std::endl << std::endl;
std::cout << "If you want to input your matrices:" << std::endl;
std::cout << std::setw(30) << std::left << " semestralka " << "The program will ask you to input two matrices and multiply them";
}
int inputInt(bool positive) {
int a;
std::cin >> a;
while(true)
{
if(std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "You have entered wrong input" << std::endl;
std::cin >> a;
}
if(!std::cin.fail()) {
if (positive && a <= 0) {
std::cout << "You have to enter a positive number" << std::endl;
continue;
}
if (a > MAX) {
std::cout << "Maximum size of matrix is " << MAX << "x" << MAX << std::endl;
continue;
}
break;
}
}
return a;
}
Matrix inputMatrix(int rows, int cols) {
Matrix result = Matrix(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result(i, j) = inputInt(false);
}
}
return result;
}