code: заменил поток cin на выборочный поток

master
MakhnovGA 2 лет назад
Родитель 3f5042566b
Сommit 5113fdf3b4

@ -6,53 +6,54 @@
#include "histogram_internal.h"
using namespace std;
void
bool
find_minmax(vector<double> numbers, double &min, double &max) {
min = numbers[0];
max = numbers[0];
for (size_t i = 1; i < numbers.size(); i++) {
if (min > numbers[i])
min = numbers[i];
if (max < numbers[i])
max = numbers[i];
bool notEmpty = true;
if (numbers.size() == 0 || numbers.size() == 1)
notEmpty = false;
else{
min = numbers[0];
max = numbers[0];
for (size_t i = 1; i < numbers.size(); i++) {
if (min > numbers[i])
min = numbers[i];
if (max < numbers[i])
max = numbers[i];
}
}
return notEmpty;
}
vector <size_t> make_histogramm(vector<double>numbers, size_t bin_count){
double min, max;
find_minmax(numbers, min, max);
double binSize = (max - min) / bin_count;
vector<size_t> bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (size_t j = 0; (j <= bin_count - 1) && !found; j++) {
auto lo = min + j * binSize;
auto hi = min + (j + 1) * binSize;
if ((numbers[i] >= lo) && (numbers[i] < hi)) {
bins[j]++;
found = true;
bool haveElements = find_minmax(numbers, min, max);
if (haveElements){
double binSize = (max - min) / bin_count;
vector<size_t> bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (size_t j = 0; (j <= bin_count - 1) && !found; j++) {
auto lo = min + j * binSize;
auto hi = min + (j + 1) * binSize;
if ((numbers[i] >= lo) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found)
bins[bin_count - 1]++;
}
if (!found)
bins[bin_count - 1]++;
}
/*int max_count = bins[0];
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] > max_count)
max_count = bins[i];
return bins;
}
if (max_count > 76) {
for (size_t i = 0; i < bin_count; i++) {
int count = bins[i];
size_t height = 76 * (static_cast<double>(count) / max_count);
bins[i] = height;
}
}*/
return bins;
else
cerr << "Empty massive of numbers";
}
void check_interval(size_t &interval){
bool check_interval(size_t interval){
bool need;
if (interval > 9 || interval < 2)
interval = 0;
return;
need = false;
else
need = true;
return need;
}

@ -5,6 +5,5 @@
std::vector<size_t>
make_histogramm(std::vector<double> numbers, size_t bin_count);
void check_interval(size_t &interval);
#endif // HISTOGRAM_H_INCLUDED

@ -2,6 +2,6 @@
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
void find_minmax(std::vector<double> numbers, double &min, double &max);
void check_interval(size_t &interval);
bool find_minmax(std::vector<double> numbers, double &min, double &max);
bool check_interval(size_t interval);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -1,5 +1,5 @@
# depslib dependency file v1.0
1682183565 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.cpp
1682340368 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.cpp
<math.h>
<iostream>
<conio.h>
@ -7,10 +7,10 @@
"histogram.h"
"histogram_internal.h"
1682184342 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.h
1682341619 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.h
<vector>
1682184846 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\main.cpp
1682338657 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\main.cpp
<math.h>
<iostream>
<conio.h>
@ -18,6 +18,7 @@
"histogram.h"
"text.h"
"svg.h"
"histogram_internal.h"
1681931523 c:\users\gmack\onedrive\đŕáî÷čé ńňîë\lab01\lab_01\text.h
<vector>
@ -29,12 +30,12 @@
<vector>
"text.h"
1682183685 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram_internal.h
1682339360 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram_internal.h
<vector>
1682183926 c:\users\gmack\onedrive\đŕáî÷čé ńňîë\lab01\lab_01\svg.h
1682189679 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\svg.cpp
1682337971 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\svg.cpp
<math.h>
<iostream>
<conio.h>

@ -2,44 +2,44 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="text.cpp" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-1" zoom_2="0">
<Cursor>
<Cursor1 position="211" topLine="0" />
<Cursor1 position="824" topLine="19" />
</Cursor>
</File>
<File name="text.h" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="histogram.h" open="0" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="73" topLine="0" />
<Cursor1 position="207" topLine="0" />
</Cursor>
</File>
<File name="svg.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="svg.h" open="0" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="123" topLine="0" />
</Cursor>
</File>
<File name="svg.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="text.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2180" topLine="43" />
<Cursor1 position="211" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="text.h" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
<Cursor1 position="73" topLine="0" />
</Cursor>
</File>
<File name="histogram_internal.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="svg.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="173" topLine="0" />
<Cursor1 position="1618" topLine="44" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="histogram.cpp" open="1" top="1" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="885" topLine="22" />
<Cursor1 position="0" topLine="32" />
</Cursor>
</File>
<File name="histogram.h" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="histogram_internal.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="207" topLine="0" />
<Cursor1 position="173" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -5,6 +5,7 @@
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include "histogram_internal.h"
using namespace std;
struct Input {
@ -14,29 +15,30 @@ struct Input {
};
Input
input_data(){
input_data(istream &tin){
cerr << "Input numbers count: ";
size_t number_count;
cin >> number_count;
tin >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Input numbers: ";
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
tin >> in.numbers[i];
}
cerr << "Input bin count: ";
cin >> in.bin_count;
tin >> in.bin_count;
return in;
}
int main() {
Input in = input_data();
Input in = input_data(cin);
auto bins = make_histogramm(in.numbers, in.bin_count);
size_t interval;
cerr << "Input interval: "; cin >> interval;
check_interval(interval);
if (interval == 0){
bool inRange;
inRange = check_interval(interval);
if (!inRange){
cerr << "ERROR";
return 0;
}

@ -49,7 +49,7 @@ show_histogram_svg(const vector<size_t>& bins, size_t interval) {
for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#FF00FF");
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "green", "#FF00FF");
top += BIN_HEIGHT;
}
top+=BIN_HEIGHT;

@ -4,36 +4,37 @@
#include "histogram_internal.h"
#include <vector>
#include <iostream>
/*TEST_CASE("distinct positive numbers") {
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
std::vector<double>v{1};
CHECK(v.size() != 0);
CHECK(v.size() != 1);
find_minmax(v, min, max);
std::vector<double>v{1, 2};
bool haveElements = find_minmax(v, min, max);
CHECK(haveElements);
CHECK(min == 1);
CHECK(max == 2);
CHECK(min != max);
}*/
TEST_CASE("interval check"){
size_t interval = 3;
check_interval(interval);
CHECK(interval != 0);
interval = 10;
check_interval(interval);
CHECK(interval == 0);
}
TEST_CASE("EMPTY MASSIVE OR 1 ELEMENT IN MASSIVE"){
double min = 0;
double max = 0;
std::vector<double>v{};
bool haveElements = find_minmax(v, min, max);
CHECK(!haveElements);
}
/*#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
#include <vector>
#include <iostream>
TEST_CASE("interval check"){
/*TEST_CASE("interval check"){
size_t interval = 3;
bool flag = check_interval(interval);
CHECK(flag == true);
bool inRange;
inRange = check_interval(interval);
CHECK(inRange);
inerval = 1
inRange = check_interval(interval);
CHECK(!inRange);
interval = 2
inRange = check_interval(interval);
CHECK(inRange);
interval = 9
inRange = check_interval(interval);
CHECK(inRange);
}
*/

@ -1,5 +1,5 @@
# depslib dependency file v1.0
1682183565 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.cpp
1682340368 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.cpp
<math.h>
<iostream>
<conio.h>
@ -7,10 +7,10 @@
"histogram.h"
"histogram_internal.h"
1681931103 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.h
1682341619 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram.h
<vector>
1682183685 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram_internal.h
1682339360 c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\histogram_internal.h
<vector>
1682182932 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\unittest_15.cpp
@ -19,11 +19,7 @@
<vector>
<iostream>
1682183802 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\unittest.cpp
"doctest.h"
"histogram_internal.h"
<vector>
<iostream>
1682341526 source:c:\users\gmack\onedrive\ðàáî÷èé ñòîë\lab01\lab_01\unittest.cpp
"doctest.h"
"histogram_internal.h"
<vector>

@ -7,19 +7,19 @@
<Cursor1 position="97" topLine="0" />
</Cursor>
</File>
<File name="doctest.h" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="histogram.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="168296" topLine="3336" />
<Cursor1 position="1542" topLine="0" />
</Cursor>
</File>
<File name="unittest.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="doctest.h" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="485" topLine="6" />
<Cursor1 position="168296" topLine="3336" />
</Cursor>
</File>
<File name="histogram.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1542" topLine="0" />
<Cursor1 position="650" topLine="3" />
</Cursor>
</File>
</CodeBlocks_layout_file>

Загрузка…
Отмена
Сохранить