-
Notifications
You must be signed in to change notification settings - Fork 0
/
usefulFunctions.cpp
142 lines (117 loc) · 2.46 KB
/
usefulFunctions.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
#include "Book.h"
#include <fstream>
#include <iostream>
#include "List.h"
#include "Array.h"
#include "recursion.h"
#include "maxAndMin.h"
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
const String WRONG_AUTHOR = "Wrong author's name in the following: ";
const String WRONG_BOOK_NAME = "Wrong book name in the following: ";
const String WRONG_YEAR = "Wrong year of publishing in the followwing: ";
const String WRONG_DIS = "Wrong recommended discipline in the following: ";
const String WRONG_AMO = "Wrong amount of book in the following: ";
char *getline(istream& in)
{
const int BUF_LEN = 50;
int len = BUF_LEN;
int i = 0;
char * str = (char*)malloc(BUF_LEN * sizeof(char)), c;
while (in.get(c) && c != '\n')
{
if (i == len)
{
str = (char*)realloc(str, len += BUF_LEN);
}
str[i++] = c;
}
char * rez = new char[i + 1];
for (int j = 0; j < i; ++j)
{
rez[j] = str[j];
}
rez[i] = 0;
free(str);
return rez;
}
Book split(String& str)
{
String author;
String name;
String year;
String discipline;
String amount;
size_t j = 0;
size_t LAST_SPACE = 0;
size_t length = str.len();
for (size_t i = 0; i < length; i++)
{
if (str[i] == ' ')
{
switch (j)
{
case 0: author = str.substr(0, i); break;
case 1: name = str.substr(LAST_SPACE + 1, i); break;
case 2: year = str.substr(LAST_SPACE + 1, i); break;
case 3: discipline = str.substr(LAST_SPACE + 1, i); break;
}
j++;
LAST_SPACE = i;
}
}
amount = str.substr(LAST_SPACE + 1, length);
if (!isAuth(author))
{
throw WRONG_AUTHOR + " " + str;
}
if (!isName(name))
{
throw WRONG_BOOK_NAME + " " + str;
}
size_t nameLen = name.len();
for (size_t i = 0; i < nameLen; i++)
{
if (name[i] == '_')
{
name[i] = ' ';
}
}
int yearInt = year.to_int();
if (!isYear(yearInt))
{
throw WRONG_YEAR + " " + str;
}
if (!isDiscipline(discipline))
{
throw WRONG_DIS + " " + str;
}
int amountInt = amount.to_int();
if (!isAmount(amountInt))
{
throw WRONG_AMO + " " + str;
}
Book tempBook(author, name, yearInt, discipline, amountInt);
return tempBook;
}
struct maxAndMin maxMin(const Array<Book>& myArray, const size_t size)
{
Book maxBook = myArray[0];
Book minBook = myArray[0];
for (size_t i = 1; i < size; i++)
{
Book tempBook = myArray[i];
if (tempBook > maxBook)
{
maxBook = tempBook;
}
if (tempBook < minBook)
{
minBook = tempBook;
}
}
struct maxAndMin toReturn = { maxBook, minBook };
return toReturn;
}