File size: 6,901 Bytes
8be366a | 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | /*
* 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 <cstdlib>
#include <iostream>
#include <limits.h>
#include <fstream>
#include "String.h"
#include "Arr.h"
//#include "Arr.cpp"
#define LINE cout << "-------------------------------------------------------------\n\n";
using namespace std;
//template <typename T, int N>
//void generateArray(T (&arr)[N], T(*randFunc)()){
// for (int i = 0; i < N; ++i)
// arr[i] = randFunc();
//}
template <typename T, int N>
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<typename T>
struct result
{
T minimum, maximum;
};
template <typename T>
result<T> max_min(const std::initializer_list<T> &list);
int main(int argc, char** argv) {
//testEx1();
testArr();
//testEx3();
//testString();
return 0;
}
template <typename T>
result<T> max_min(const std::initializer_list<T> &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<T> {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 <Exercise N1> ------------------\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 <typename T> bool logicF(T t){
if (t>10)
return true;
else
return false;
}
template <typename T> T replaceF(T t){
return t*10;
}
template <typename T> void printArr(T* arr, int n){
for(int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
void testArr(){
cout << "-------------------------------------------------------------\n";
cout << "------------------- Testing <Array> --------------------\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 <Exercise N3> ------------------\n";
cout << "-------------------------------------------------------------\n";
cout << endl;
result<int> res = max_min({10,2,3,-40,5,6,7,8,9,10});
cout << "result<int> 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<double> res2 = max_min({1.10,1.2,0.3,6.40,5.,6.,7.,8.,9.,10.});
cout << "result<double> 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 <String> ------------------\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;
}
|