/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* * File: main.cpp * Author: yurec * * Created on 21 липня 2016, 9:55 */ #include #include #include #include #include "String.h" #include "Arr.h" //#include "Arr.cpp" #define LINE cout << "-------------------------------------------------------------\n\n"; using namespace std; //template //void generateArray(T (&arr)[N], T(*randFunc)()){ // for (int i = 0; i < N; ++i) // arr[i] = randFunc(); //} template void generateArray1(T (&arr)[N], T(*randFunc)()){ for (int i = 0; i < N; ++i) arr[i] = randFunc(); } void testString(); void testEx1(); void testArr(); void testEx3(); void func1(); void func2(); void func3(); template struct result { T minimum, maximum; }; template result max_min(const std::initializer_list &list); int main(int argc, char** argv) { //testEx1(); testArr(); //testEx3(); //testString(); return 0; } template result max_min(const std::initializer_list &list) { T min = *(list.begin()); T max = *(list.end()-1); for (auto i : list){ if (i > max) max = i; if (i < min) min = i; } return result {min, max}; } void func1(){ cout << "Start func1! " << endl; cout << " func1 catch exception with char type\n"; cout << "Menu :\n" << " 1 - for throw int type \n" << " 2 - for throw double type\n" << " 3 - for thro char* type\n"; String s; while (1){ try{ s.input("Enter your choice: "); if (s == "2") throw 1.0; if (s == "1") throw 1; if (s == "3") throw "choice numbe 3"; cout << "Unknown choice. Repeate please." << endl;; } catch (const char* c){ cout << "Func1 has caught exception type char and value = " << c << endl; } catch (...){ cout << "Func1 has caught exception unknown type, throwing forward." << endl; throw; } } cout << "End func1! " << endl; } void func2(){ cout << "Start func2! " << endl; cout << " func2 start func1 and catch exception with double type" << endl; try{ func1(); } catch (double d){ cout << "Func2 has caught exception type double and value = " << d << endl; } catch (...){ cout << "Func2 has caught exception unknown type, throwing forward." << endl; throw; } cout << "End func2! " << endl; } void testEx1(){ cout << "-------------------------------------------------------------\n"; cout << "--------------- Testing ------------------\n"; cout << "-------------------------------------------------------------\n"; cout << endl; try{ func2(); } catch (int d){ cout << "Have caught exception type int and value = " << d << endl; } catch (...){ cout << "Have caught exception unknown type, throwing forward." << endl; throw; } } template bool logicF(T t){ if (t>10) return true; else return false; } template T replaceF(T t){ return t*10; } template void printArr(T* arr, int n){ for(int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } void testArr(){ cout << "-------------------------------------------------------------\n"; cout << "------------------- Testing --------------------\n"; cout << "-------------------------------------------------------------\n"; cout << endl; double arr_d1[20]; double arr_d2[11]; int arr_i1[20]; int arr_i2[20]; string arr_s2[5]; generateArray(arr_d1, rnd); generateArray(arr_i1, rnd); generateArray(&arr_d2[0], 11, rnd); generateArray(&arr_i2[0], 20, rnd); generateArray(&arr_s2[0], 5, rnd); ofstream f; f.open("array.txt", ios::out); printArrayToFile(f, arr_i1); printArrayToFile(f, arr_d1); printArrayToFile(f, &arr_s2[0], 5); printArrayToFile(f, &arr_d2[0], 11); printArrayToFile(f, &arr_i2[0], 20); f.close(); cout << "\n arr_d1 \n"; printArr(arr_d1, 20); cout << "\n arr_i1 \n"; printArr(arr_i1, 20); cout << "\n arr_s2 \n"; printArr(arr_s2, 5); LINE cout << "/n countInArrayIf(arr_d1, logicF) = " << countInArrayIf(arr_d1, logicF) << endl; cout << "/n countInArrayIf(arr_i1, logicF) = " << countInArrayIf(arr_i1, logicF) << endl; LINE replacetInArrayIf(arr_d1, replaceF); replacetInArrayIf(arr_i1, replaceF); cout << " After fuction replacetInArrayIf() \n" << endl; printArr(arr_d1, 20); cout << endl; printArr(arr_i1, 20); LINE } void testEx3(){ cout << "-------------------------------------------------------------\n"; cout << "--------------- Testing ------------------\n"; cout << "-------------------------------------------------------------\n"; cout << endl; result res = max_min({10,2,3,-40,5,6,7,8,9,10}); cout << "result res = max_min({1,2,3,4,5,6,7,8,9,10}); \n"; cout << "res.minimum = " << res.minimum << endl; cout << "res.maximum = " << res.maximum << endl; result res2 = max_min({1.10,1.2,0.3,6.40,5.,6.,7.,8.,9.,10.}); cout << "result res2 = max_min({1.10,1.2,0.3,6.40,5.,6.,7.,8.,9.,10.}); \n"; cout << "res2.minimum = " << res2.minimum << endl; cout << "res2.maximum = " << res2.maximum << endl; } void testString(){ cout << "-------------------------------------------------------------\n"; cout << "-------------- Testing class ------------------\n"; cout << "-------------------------------------------------------------\n"; cout << endl; String s("Helllo World!!"); cout << "String s(\"Helllo World!!\");" << endl; cout << "s.countChars({'e', 'l', 'd', '!'}) = " << s.countChars({'e', 'l', 'd', '!'}) << endl; s.removeChars({2, 6, 13}); cout << "s.removeChars({2, 6, 13});" << endl; cout << "s = " << s << endl; }