Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <array>
- #include <string>
- #include <random>
- #include <fstream>
- using namespace std;
- vector <int> v;
- using IntMatrix = vector<vector<int>>;
- using IntMatrix_db = vector<vector<double>>;
- //INFORMACJA
- /* Zadanie 4 jest zawarte pod case 3 razem z zadaniem 3
- Zadania 8 nie ma poniewaz jest z nim problem */
- void Print(const vector <int> &v) //zad1 -odp: modyfikator const mozemy dodac gdy nie chcemy modyfikowac wektora/zmiennej
- {
- cout << "[";
- for (auto i : v)
- {
- cout << i;
- if (i != v[v.size() - 1])
- {
- cout << ',';
- }
- }
- cout << "]";
- cout << endl << endl;
- }
- float sum(vector<float> &v1)
- {
- float suma = 0;
- for (vector<float>::iterator i = v1.begin(); i != v1.end(); i++) suma += *i;
- return suma;
- }
- float average(vector<float> &v1)
- {
- float sum = 0;
- for (vector<float>::iterator i = v1.begin(); i != v1.end(); i++)sum += *i;
- return sum / v1.size();
- }
- array<float, 2> minmax(const vector<float> &v)
- {
- array<float, 2> tab;
- double min = v[0], max = v[0];
- for (auto e : v)
- {
- if (e < min)
- {
- min = e;
- }
- if (e > max)
- {
- max = e;
- }
- }
- tab[0] = min;
- tab[1] = max;
- return tab;
- }
- bool equals(const vector<int> &v2, const vector<int> &v4, int w1)
- {
- int i;
- for (int i = 0; i < w1; i++)
- {
- if (v2[i] == v4[i]) return true;
- else return false;
- }
- }
- //zadanie 2
- vector <int> add(const vector<int> &v2, const vector<int> &v4, int w1)
- {
- int i;
- int sum = 0;
- v.clear();
- for (int i = 0; i < w1; i++)
- {
- sum = (v2[i] + v4[i]);
- v.push_back(sum);
- }
- Print(v);
- return v;
- }
- vector <int> subtract(const vector<int> &v2, const vector<int> &v4, int w1)
- {
- v.clear();
- int i;
- int sum = 0;
- for (int i = 0; i < w1; i++)
- {
- sum = (v2[i] - v4[i]);
- v.push_back(sum);
- }
- Print(v);
- return v;
- }
- vector <int> multiply(const vector<int> &v2, int w1, int skalar)
- {
- v.clear();
- int i;
- int sum = 0;
- for (int i = 0; i < w1; i++)
- {
- sum = (v2[i] * skalar);
- v.push_back(sum);
- }
- Print(v);
- return v;
- }
- //zadanie 3
- int randomInt(int min, int max)
- {
- static default_random_engine e{};
- uniform_int_distribution<int > d(min, max);
- return d(e);
- }
- vector<int> randomVector(unsigned int size, int min, int max) //zad4
- {
- vector<int> v;
- for (int i = 0; i < size; i++) v.push_back(randomInt(min, max));
- return v;
- }
- fstream plik;
- void writeVector(const vector<int >& v2, const string& fileName)
- {
- plik.open(fileName.c_str(), ios::out);
- if (!plik.good())
- {
- cout << "Nie udalo sie utworzyc pliku!\n";
- }
- else
- {
- cout << "Plik utworzony!\n";
- }
- for (int i = 0; i < v2.size(); i++)
- {
- plik << v2[i]<<",";
- }
- plik.close();
- }
- //zadanie 4-- -- - -- - blad
- void readVector(const string& fileName)
- {
- vector<int> v9;
- ifstream plik;
- string linia;
- plik.open(fileName.c_str(), ios::out);
- if(plik)
- {
- string buffer;
- while (!plik.eof())
- {
- getline(plik, linia);
- cout <<"["<< linia<<"]";
- }
- }
- }
- vector<int> bubbleSort(vector <int> &v2)
- {
- for (int i = 0; i < v2.size(); i++)
- for (int j = 1; j < v2.size() - i; j++) //pętla wewnętrzna
- if (v2[j - 1] > v2[j])
- //zamiana miejscami
- swap(v2[j - 1], v2[j]);
- Print(v2);
- return v2;
- }
- //zadanie 6
- IntMatrix createMatrix(array<unsigned int, 2> shape)
- {
- IntMatrix v;
- for (int i = 0; i < shape[0]; i++)
- {
- vector<int> row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(0);
- }
- v.push_back(row);
- }
- return v;
- }
- IntMatrix randomMatrix(array<unsigned int, 2> shape, int min, int max)
- {
- IntMatrix v;
- for (int i = 0; i < shape[0]; i++)
- {
- vector<int> row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(randomInt(min, max));
- }
- v.push_back(row);
- }
- return v;
- }
- void print(const IntMatrix &matrix, array<unsigned int, 2> shape)
- {
- for (int i = 0; i < shape[0]; i++)
- {
- for (int j = 0; j < shape[1]; j++)
- {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
- vector<vector<int>> v_macierz;
- IntMatrix add1(array<unsigned int, 2> shape, int min, int max,IntMatrix &v7,IntMatrix &v8)
- {
- cout << "DODAWANIE" << endl;
- for (int i = 0; i < shape[0]; i++)
- {
- vector<int> row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(v8[i][j] + v7[i][j]);
- }
- v_macierz.push_back(row);
- }
- return v_macierz;
- }
- IntMatrix subtract1(array<unsigned int, 2> shape, int min, int max, IntMatrix &v7, IntMatrix &v8)
- {
- v_macierz.clear();
- cout << "ODEJMOWANIE" << endl;
- for (int i = 0; i < shape[0]; i++)
- {
- vector<int> row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(v8[i][j] - v7[i][j]);
- }
- v_macierz.push_back(row);
- }
- return v_macierz;
- }
- //zadania 8 nie ma, posiadam z nim problem
- //zadanie 9
- vector<vector<double>> m1;
- double randomDouble(double min, double max)
- {
- double f = (double)rand() / RAND_MAX;
- return min + f * (max - min);
- }
- IntMatrix_db createMatrix1(array<double, 2> shape)
- {
- IntMatrix_db v;
- for (int i = 0; i < shape[0]; i++)
- {
- vector<double> row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(0);
- }
- v.push_back(row);
- }
- return v;
- }
- IntMatrix_db randomMatrix1(array<double, 2> shape, double min, double max)
- {
- IntMatrix_db v;
- for (int i = 0; i < shape[0]; i++)
- {
- vector < double > row;
- for (int j = 0; j < shape[1]; j++)
- {
- row.push_back(randomDouble(min, max));
- }
- v.push_back(row);
- }
- return v;
- }
- void print1(const IntMatrix_db &matrix, array< double, 2> shape)
- {
- for (int i = 0; i < shape[0]; i++)
- {
- for (int j = 0; j < shape[1]; j++)
- {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
- double inv(double x)
- {
- return (1 / x);
- }
- double inv_square(double x)
- {
- return (1 / (x*x));
- }
- double sqrt(double x)
- {
- return sqrt(x);
- }
- void forEach(IntMatrix_db &v8 ,double (&fun) (double))
- {
- IntMatrix_db m2;
- for (auto i : v8)
- {
- vector<double> v20;
- for (auto j : i)
- v20.push_back(fun(j));
- m2.push_back(v20);
- }
- for (auto i : m2)
- {
- for (auto j : i)
- cout<<j << ' ';
- cout << endl;
- }
- }
- //funkcja main
- int main()
- { string wybor;
- //petla do while
- do {
- int option;
- cout << "ktore zadanie: ";
- cin >> option;
- //uzyte wektory
- vector <float> v1;
- vector <int> v2;
- vector <int> v4;
- vector<vector<int>> v6;
- vector<vector<int>> v7;
- IntMatrix v8;
- fstream plik;
- string fileName;
- unsigned int minn = 0, maxx = 0;
- unsigned int minnx=0, maxxx=0;
- double minnx1 = 0, maxxx1 = 0;
- //switch i wybor zadania
- switch (option)
- {
- //zadanie 1
- case 1:
- float x;
- int n;
- cout << "podaj ile liczb ma miec vector: ";
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- cout << "podaj " << i + 1 << endl;
- cin >> x;
- v1.push_back(x);
- }
- cout << "suma elementow wektora to: " << sum(v1) << endl;
- cout << "srednia elementow wektora to: " << average(v1) << endl;
- array<float, 2> v3 = minmax(v1);
- cout << "min i max: " << v3[0] << " " << v3[1] << endl;
- break;
- //zadnaie 2
- case 2:
- int w1;
- int i;
- int s;
- cout << "porownywanie wektorow: " << endl;
- cout << "podaj ilu elementowy maja byc wektory: ";
- cin >> w1;
- cout << "wektor 1: " << endl;
- //wektor 1
- for (int i = 0; i < w1; i++) v2.push_back(rand() % 9);
- Print(v2);
- cout << endl;
- cout << "wektor 2: " << endl;
- //wektor 2
- for (int i = 0; i < w1; i++) v4.push_back(rand() % 9);
- Print(v4);
- if (equals(v2, v4, w1) == true) cout << endl << "Wektory sa rowne." << endl;
- else cout << endl << "Wektory nie sa rowne." << endl;
- //suma
- cout << "suma wektorow: " << endl;
- add(v2, v4, w1);
- cout << endl;
- //roznica
- cout << "roznica wektorow: " << endl;
- subtract(v2, v4, w1);
- //mnozenie przez skalar
- int skalar;
- cout << endl;
- cout << "podaj skalar: ";
- cin >> skalar;
- cout << "1 wektor razy skalar: " << endl;
- multiply(v2, w1, skalar);
- break;
- //zadanie 3 i 4
- case 3:
- v2.clear();
- int m;
- int min, max;
- cout << "Podaj nazwe pliku: ";
- cin >> fileName;
- fileName += ".txt";
- cout << "ile liczb ma miec wektor: ";
- cin >> m;
- cout << "podaj min: ";
- cin >> min;
- cout << "podaj max: ";
- cin >> max;
- v2 = randomVector(m, min, max);
- writeVector(v2, fileName);
- //zadanie 4
- cout << "zadanie 4" << endl;
- readVector(fileName);
- break;
- //zadanie 5
- case 5:
- cout << "ile liczb ma miec wektor: ";
- cin >> m;
- cout << "podaj min: ";
- cin >> min;
- cout << "podaj max: ";
- cin >> max;
- v2 = randomVector(m, min, max);
- bubbleSort(v2);
- break;
- case 6:
- unsigned int q, w;
- cout << "podaj ilosc wierszy a potem kolumn: ";
- cin >> q;
- cin >> w;
- v6 = randomMatrix({ q,w },minn,maxx);
- print(v6, { q,w });
- cout << "podaj min i max: ";
- cin >> minn >> maxx;
- v7 = randomMatrix({ q,w }, minn, maxx);
- print(v7, { q,w });
- v6.clear();
- break;
- //zadanie 7
- case 7:
- unsigned int v,l ;
- cout << "podaj ilosc wierszy a potem kolumn: ";
- cin >> v;
- cin >> l;
- cout << "podaj minimalna liczbe: ";
- cin >> minnx;
- cout << "podaj maxymalna liczbe: ";
- cin >> maxxx;
- v6 = randomMatrix({ v,l },minnx,maxxx);
- v7 = randomMatrix({ v,l }, minnx, maxxx);
- print(v6, { v,l });
- cout << endl;
- print(v7, { v,l });
- cout << endl;
- v8 = add1({ v,l },minnx,maxxx,v6, v7);
- print(v8, { v,l });
- v8.clear();
- v8 = subtract1({ v,l }, minnx, maxxx, v6, v7);
- print(v8, { v,l });
- //zadanie 8
- //zadanie 9
- case 9:
- double v1, l1;
- cout << "podaj ilosc wierszy a potem kolumn: ";
- cin >> v1;
- cin >> l1;
- cout << "podaj minimalna liczbe: ";
- cin >> minnx1;
- cout << "podaj maxymalna liczbe: ";
- cin >> maxxx1;
- m1 = randomMatrix1({ v1,l1 }, minnx1, maxxx1);
- forEach(m1, inv);
- cout << endl << endl;
- forEach(m1, inv_square);
- cout << endl;
- forEach(m1, sqrt);
- break;
- }
- cout << endl;
- cout << "ponowic? " << endl;
- cin >> wybor;
- } while (wybor == "tak");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement