code: замена потока cin

master
KonovalovaAA 2 лет назад
Родитель 3f2a04f00d
Сommit f1d0f2c795

3
.gitignore поставляемый

@ -0,0 +1,3 @@
/bin
/obj
/1.layout

@ -0,0 +1,53 @@
# depslib dependency file v1.0
1677500178 source:l:\i êóðñ\À-2-22\Êîíîâàëîâà\1\main.cpp
<iostream>
<vector>
1681133505 source:c:\users\u111-09\desktop\1\main.cpp
<iostream>
<vector>
"histogram.h"
1681133536 source:c:\users\u111-09\desktop\1\histogram.cpp
<iostream>
<vector>
"histogram.h"
1681131906 c:\users\u111-09\desktop\1\histogram.h
<vector>
1681132321 source:c:\users\u111-09\desktop\1\text.cpp
<iostream>
<vector>
1684753111 source:c:\users\79156\desktop\3\main.cpp
<iostream>
<vector>
<cmath>
<windows.h>
"histogram.h"
"svg.h"
1681131908 c:\users\79156\desktop\3\histogram.h
<vector>
1682253694 c:\users\79156\desktop\3\svg.h
<vector>
1682333182 source:c:\users\79156\desktop\3\histogram.cpp
<iostream>
<vector>
<windows.h>
"histogram.h"
1682263714 c:\users\79156\desktop\3\histogram_internal.h
<vector>
1682340835 source:c:\users\79156\desktop\3\svg.cpp
<math.h>
<iostream>
<conio.h>
<vector>
<string>
"svg.h"

@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <windows.h>
//#include "histogram_internal.h"
#include "histogram.h"
using namespace std;
bool
find_minmax(vector<double> numbers, double& min, double& max)
{
if(numbers.size() == 0)
return false;
min = numbers[0];
max = numbers[0];
size_t number_count = numbers.size();
for (size_t i = 1; i < number_count; i++)
{
if (numbers[i] < min)
{
min = numbers[i];
}
else if (numbers[i] > max)
{
max = numbers[i];
}
}
return true;
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t bin_count)
{
double min, max;
vector<size_t> bins(bin_count);
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
size_t number_count = numbers.size();
for (size_t i = 0; i < number_count; i++)
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}

@ -0,0 +1,11 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -0,0 +1,9 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
bool
find_minmax(std::vector<double> numbers, double& min, double& max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -15,10 +15,10 @@ struct Input
Input
input_data()
input_data(istream& tin)
{
size_t number_count;
cerr << "Enter number_count "; cin >> number_count;
cerr << "Enter number_count "; tin >> number_count;
vector<double> numbers(number_count);
@ -32,7 +32,7 @@ input_data()
size_t bin_count;
cerr << "Enter bin_count ";
cin >> in.bin_count;
tin >> in.bin_count;
return in;
}
@ -44,7 +44,7 @@ input_data()
int main()
{
auto in = input_data();
auto in = input_data(cin);
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;

Двоичные данные
main.exe

Двоичный файл не отображается.

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="svg" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/svg" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/svg" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,73 @@
#include <math.h>
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#include "svg.h"
using namespace std;
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' ";
cout << "height='" << height << "' ";
cout << "viewBox='0 0 " << width << " " << height << "' ";
cout << "xmlns='http://www.w3.org/2000/svg'> \n";
}
void
svg_end() {
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
void
svg_rect(double x, double y, double width, double height, string stroke, string fill) {
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
void
show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++) {
if (bins[i] > max_count)
max_count = bins[i];
}
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, "black", "#fde910");
top += BIN_HEIGHT;
}
cout << "<line x1='2' y1='1' x2 = '"<<MAX_WIDTH+TEXT_WIDTH<<"' y2 = '1' stroke='black' stroke-dasharray='10 10' />\n";
cout << "<line x1='2' y1='1' x2 = '2' y2 = '"<<top+10<<"' stroke='black' stroke-dasharray='10 10' />\n";
cout << "<line x1='2' y1='"<<top+10<<"' x2 = '"<<MAX_WIDTH+TEXT_WIDTH<<"' y2 = '"<<top+10<<"' stroke='black' stroke-dasharray='10 10' />\n";
cout << "<line x1='"<<MAX_WIDTH+TEXT_WIDTH<<"' y1='"<<top+10<<"' x2 = '"<<MAX_WIDTH+TEXT_WIDTH<<"' y2 = '1' stroke='black' stroke-dasharray='10 10' />\n";
svg_end();
}

@ -0,0 +1,19 @@
# depslib dependency file v1.0
1682280278 source:c:\users\79156\desktop\3\svg.cpp
<math.h>
<iostream>
<conio.h>
<vector>
<string>
"svg.h"
1682253694 c:\users\79156\desktop\3\svg.h
<vector>
1682252851 c:\users\79156\desktop\3\svg.cpp
<iostream>
<vector>
<windows.h>
<string>
"svg.h"

@ -0,0 +1,9 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
//#include "svg.cpp"
#include <vector>
void
show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="svg.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="test" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/test" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/test" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="doctest.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,130 @@
# depslib dependency file v1.0
1681135804 source:c:\users\u111-09\desktop\1\histogram.cpp
<iostream>
<vector>
"histogram_internal.h"
1681131906 c:\users\u111-09\desktop\1\histogram.h
<vector>
1681135819 c:\users\u111-09\desktop\1\histogram_internal.h
<vector>
1681135806 source:d:\3\histogram.cpp
<iostream>
<vector>
"histogram_internal.h"
1681135820 d:\3\histogram_internal.h
<vector>
1682078902 source:d:\3\unittest.cpp
"doctest.h"
"histogram_internal.h"
1681134424 d:\3\doctest.h
<signal.h>
<ciso646>
<cstddef>
<ostream>
<istream>
<type_traits>
"doctest_fwd.h"
<ctime>
<cmath>
<climits>
<math.h>
<new>
<cstdio>
<cstdlib>
<cstring>
<limits>
<utility>
<fstream>
<sstream>
<iostream>
<algorithm>
<iomanip>
<vector>
<atomic>
<mutex>
<set>
<map>
<unordered_set>
<exception>
<stdexcept>
<csignal>
<cfloat>
<cctype>
<cstdint>
<string>
<sys/types.h>
<unistd.h>
<sys/sysctl.h>
<AfxWin.h>
<windows.h>
<io.h>
<sys/time.h>
<unistd.h>
1682333182 source:c:\users\79156\desktop\3\histogram.cpp
<iostream>
<vector>
<windows.h>
"histogram.h"
1682282091 c:\users\79156\desktop\3\histogram_internal.h
<vector>
1682338695 source:c:\users\79156\desktop\3\unittest.cpp
"doctest.h"
"histogram_internal.h"
1681134424 c:\users\79156\desktop\3\doctest.h
<signal.h>
<ciso646>
<cstddef>
<ostream>
<istream>
<type_traits>
"doctest_fwd.h"
<ctime>
<cmath>
<climits>
<math.h>
<new>
<cstdio>
<cstdlib>
<cstring>
<limits>
<utility>
<fstream>
<sstream>
<iostream>
<algorithm>
<iomanip>
<vector>
<atomic>
<mutex>
<set>
<map>
<unordered_set>
<exception>
<stdexcept>
<csignal>
<cfloat>
<cctype>
<cstdint>
<string>
<sys/types.h>
<unistd.h>
<sys/sysctl.h>
<AfxWin.h>
<windows.h>
<io.h>
<sys/time.h>
<unistd.h>
1681131908 c:\users\79156\desktop\3\histogram.h
<vector>

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="histogram.cpp" open="0" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="149" topLine="33" />
</Cursor>
</File>
<File name="histogram_internal.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="176" topLine="0" />
</Cursor>
</File>
<File name="doctest.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<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="1428" topLine="32" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -0,0 +1,23 @@
#include <iostream>
#include <vector>
using namespace std;
void show_histogram_text(vector <size_t> bins, size_t bin_count){
for (size_t i = 0; i < bin_count; i++){
if (bins[i]<100){
cout << " ";
}
if (bins[i]<10){
cout << " ";
}
cout << bins[i] << "|";
for (size_t j = 0; j < bins[i]; j++){
cout << "*";
}
cout << "\n";
}
}

@ -0,0 +1,9 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void
show_histogram_text(std::vector <size_t> bins, size_t bin_count);
#endif // TEXT_H_INCLUDED

@ -0,0 +1,61 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
TEST_CASE("null vector") {
double min = 0;
double max = 0;
find_minmax({}, min, max);
CHECK(find_minmax({}, min, max) == true);
CHECK(min == 1);
CHECK(max == 2);
CHECK(find_minmax({}, min, max) == false);
}
TEST_CASE("one number") {
double min = 0;
double max = 0;
find_minmax({1}, min, max);
CHECK(find_minmax({1}, min, max) == true);
CHECK(min == 1);
CHECK(max == 2);
CHECK(find_minmax({}, min, max) == false);
}
TEST_CASE("negative numbers") {
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(find_minmax({-1, -2}, min, max) == true);
CHECK(min == 1);
CHECK(max == 2);
CHECK(find_minmax({}, min, max) == false);
}
TEST_CASE("same numbers") {
double min = 0;
double max = 0;
find_minmax({1, 1}, min, max);
CHECK(find_minmax({1, 1}, min, max) == true);
CHECK(min == 1);
CHECK(max == 2);
CHECK(find_minmax({}, min, max) == false);
}
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({1, 2}, min, max);
CHECK(find_minmax({1, 2}, min, max) == true);
CHECK(min == 1);
CHECK(max == 2);
CHECK(find_minmax({}, min, max) == false);
}

@ -0,0 +1,9 @@
# depslib dependency file v1.0
1681133536 source:c:\users\u111-09\desktop\1\histogram.cpp
<iostream>
<vector>
"histogram.h"
1681131906 c:\users\u111-09\desktop\1\histogram.h
<vector>
Загрузка…
Отмена
Сохранить