range.cpp
2.14 KB
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
#include "range.h"
range parseRange(char* input, int total)
{
bool includeRange = false;
range r;
if(input[0] == '~')
{
if(strlen(input) > 2)
{
r.min = 0;
if(input[1] == ':')
{
if(input[strlen(input)-1] == '%')
{
input[strlen(input)-1] = '\0';
r.max = total*((double)str2int(string(input+2))/100);
}
r.max = str2int(string(input+2));
}
return r;
}
throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
}
else if(input[0] == '@')
{
includeRange = true;
input++;
}
string str = string(input);
size_t pos = str.find_first_of(":");
if(pos != string::npos)
{
if(str.find_first_of(":",pos+1) != string::npos)
{
throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
}
if(str[pos-1] == '%')
{
r.min = total*((double)str2int(str.substr(0,pos-1))/100);
}
else
{
r.min = str2int(str.substr(0,pos));
}
}
else
{
r.min = 0;
if(str[str.length()-1] == '%')
{
r.max = total*((double)str2int(str.substr(0,str.length()-1))/100);
}
else
{
r.max = str2int(str);
}
}
if(includeRange)
{
r.min--;
r.max++;
}
if(r.min > r.max)
{
throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
}
return r;
}