Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Template_Example
- //
- // Created by Lado on 15/12/17.
- // Copyright © 2017 Lado. All rights reserved.
- //
- #include <iostream>
- #include <cstddef>
- #include <utility>
- #include <cstdlib>
- #include <vector>
- #include <functional>
- #include <iterator>
- #include <algorithm> // std::copy
- #include <memory>
- #include <random>
- using namespace std;
- template<typename F>
- class data
- {
- public:
- inline F dataa(F x)
- {
- F result;
- result = x;
- return result;
- }
- inline F datas(int &size)
- {
- int n = 10;
- size = n;
- F *array = new F[size];
- //TODO:using library random
- random_device rd; //Will be used to obtain a seed for the random number engine
- mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
- uniform_int_distribution<> dis(1, 40);
- for(int i = 0; i < size; ++i)
- {
- //rand numbers decimal
- //array[i] = rand()%100+1;
- array[i] = dis(gen);
- }
- return *array;
- }
- };
- int GreatestCommonDivisor(int m,int n) //Наибольший общий делитель
- {
- int r;
- do
- {
- r = m % n;
- m = n;
- n = r;
- } while ( r != 0 );
- return m;
- }
- template<class T1, class T2>
- class Fraction{
- private:
- T1 fenzi; T2 fenmu; //чеслитель и знаминатель
- public:
- Fraction(){}
- ~Fraction(){}
- Fraction(T1 fenzi,T2 fenmu);
- double sever(T1 zi, T2 mu);
- double *maker();
- //friend ostream& operator<<(ostream& out,const Fraction& fra);//i/O must use a heavy overload
- //friend istream& operator>>(istream& in,const Fraction& fra);
- void setValue(T2 mu,T1 zi);
- };
- template<class T1, class T2>
- Fraction<T1, T2>::Fraction(T1 zi,T2 mu):fenzi(zi),fenmu(mu){
- if(mu==0)
- {
- cout << "Error";
- exit(1);
- }
- }
- template<class T1, class T2>
- void Fraction<T1, T2>::setValue(T2 mu,T1 zi){
- fenmu=mu;
- fenzi=zi;
- }
- template<class T1, class T2>
- double Fraction<T1, T2>::sever(T1 zi, T2 mu)
- {
- fenzi = zi;
- fenmu = mu;
- if(mu == 0)
- {
- cout << "Error";
- exit(1);
- }
- //cout << fenzi << " " << fenmu;
- return (fenzi/fenmu);
- }
- template<class T1, class T2>
- double* Fraction<T1, T2>::maker()
- {
- int n = 10;
- //size_out = n;
- T1 *arr = new T1[n];
- random_device rd;
- mt19937 gen(rd());
- uniform_real_distribution<> dis(1.0, 2.0);
- for(int i = 0; i < n; ++i)
- {
- arr[i] = dis(gen);
- //arr[i] = ((double)rand()) / ((double)RAND_MAX) * 10 + 0.5;
- }
- return arr;
- }
- template<typename Type1, typename NN>
- class cocktailSort
- {
- public:
- void sort( Type1* arr, NN len)
- {
- bool notSorted = true;
- while( notSorted )
- {
- notSorted = false;
- for( int a = 0; a < len - 1; a++ )
- {
- if( arr[a] > arr[a + 1] )
- {
- sSwap( arr[a], arr[a + 1] );
- notSorted = true;
- }
- }
- if( !notSorted ) break;
- notSorted = false;
- for( int a = len - 1; a > 0; a-- )
- {
- if( arr[a - 1] > arr[a] )
- {
- sSwap( arr[a], arr[a - 1] );
- notSorted = true;
- }
- }
- }
- }
- private:
- void sSwap( NN& a, NN& b )
- {
- NN t = a;
- a = b; b = t;
- }
- };
- int main(int argc, const char * argv[]) {
- // insert code here...
- Fraction<double, double> f(2, 3);
- Fraction<double, double> n;
- //int size = 10;
- int sizer;
- //TODO:using smart pointer for arra 1
- double *arr1;
- arr1 = n.maker();
- data <int> d;
- data <double> sw;
- //int sizer;
- //TODO:using smart pointer for arra 2
- double arr2;
- arr2 = sw.datas(sizer);
- double k = d.dataa(3);
- cout << k << endl;
- //output array 1
- cout << "arr1n";
- for(int i=0; i < 10; i++)
- {
- cout << *(arr1+i) << " ";
- }
- //output array 2
- cout << "narr2n";
- for(int i = 0; i < 10; i++)
- {
- cout << (arr2+i) << " ";
- }
- cocktailSort<double, double> cs;
- //copy arr1 and arr2 into one general array
- double *mass = new double[20];
- for(int i =0; i < 10; i++)
- {
- mass[i] = *(arr1+i);
- mass[i + 10]=(arr2+i);
- }
- cout << "nKKKn";
- for(int i = 0; i < 20; i++)
- {
- cout << mass[i] << " ";
- }
- //using sort function
- cs.sort(mass, 20);
- cout << endl << "Sorted: " << endl << "========" << endl;
- for( int x = 0; x < 20; x ++ )
- {
- //for( int s = x; s < 20; s++ )
- cout << mass[x] << " ";
- }
- cout << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment