-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS_Interval.cpp
131 lines (110 loc) · 2.56 KB
/
S_Interval.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
#include <iostream>
using namespace std;
int main()
{
double x = 0.0;
cin >> x;
// if (x >= 0 && x <= 25)
// cout << "Interval [0,25]";
// else if (x > 25 && x <= 50)
// cout << "Interval (25,50]";
// else if (x > 50 && x <= 75)
// cout << "Interval (50,75]";
// else if (x > 75 && x <= 100)
// cout << "Interval (75,100]";
// else
// cout << "Out of Intervals";
if (x < 0 || x > 100)
{
cout << "Out of Intervals";
}
else if (x <= 25)
{
cout << "Interval [0,25]";
}
else if (x <= 50)
{
cout << "Interval (25,50]";
}
else if (x <= 75)
{
cout << "Interval (50,75]";
}
else
{
cout << "Interval (75,100]";
}
return 0;
}
/*
1. Using a Function:
#include <iostream>
using namespace std;
string getInterval(double x) {
if (x >= 0 && x <= 25)
return "Interval [0,25]";
else if (x > 25 && x <= 50)
return "Interval (25,50]";
else if (x > 50 && x <= 75)
return "Interval (50,75]";
else if (x > 75 && x <= 100)
return "Interval (75,100]";
else
return "Out of Intervals";
}
int main() {
double x = 0.0;
cin >> x;
cout << getInterval(x);
return 0;
}
3. Using Array of Pairs:
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
double x = 0.0;
cin >> x;
vector<pair<double, double>> intervals = {
{0, 25},
{25, 50},
{50, 75},
{75, 100}
};
string output = "Out of Intervals";
for (const auto& interval : intervals) {
if (x > interval.first && x <= interval.second) {
output = "Interval (" + to_string(interval.first) + "," + to_string(interval.second) + "]";
if (interval.first == 0) {
output.replace(output.find("("), 1, "[");
}
break;
} else if (x == interval.first) {
output = "Interval [" + to_string(interval.first) + "," + to_string(interval.second) + "]";
break;
}
}
cout << output;
return 0;
}
4. Using Better Range Checking Logic:
#include <iostream>
using namespace std;
int main() {
double x = 0.0;
cin >> x;
if (x < 0 || x > 100) {
cout << "Out of Intervals";
} else if (x <= 25) {
cout << "Interval [0,25]";
} else if (x <= 50) {
cout << "Interval (25,50]";
} else if (x <= 75) {
cout << "Interval (50,75]";
} else {
cout << "Interval (75,100]";
}
return 0;
}
*/