Сравнить коммиты

..

12 Коммитов

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

@ -2,6 +2,18 @@
.AppleDouble .AppleDouble
.LSOverride .LSOverride
# Xcode проекты
lab01.xcodeproj/
*.xcworkspace/
xcuserdata/
# Файлы пользовательских настроек Xcode
*.xcodeproj/project.xcworkspace/xcuserdata/
*.xcodeproj/xcuserdata/
UserInterfaceState.xcuserstate
# Системные файлы
.DS_Store
# Xcode # Xcode
*.xcuserdata/ *.xcuserdata/
*.xcuserstate *.xcuserstate

Разница между файлами не показана из-за своего большого размера Загрузить разницу

@ -0,0 +1,45 @@
#include "histogram.h"
#include <vector>
using namespace std;
void find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
vector<size_t> bins(bin_count, 0);
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / 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 * 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,16 @@
//
// histogram.h
// lab01
//
// Created by Светлана Рыбакова on 01.05.2025.
//
#ifndef histogram_h
#define histogram_h
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif /* histogram_h */

@ -0,0 +1,9 @@
// histogram_internal.h
#ifndef HISTOGRAM_INTERNAL_H
#define HISTOGRAM_INTERNAL_H
#include <vector>
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif // HISTOGRAM_INTERNAL_H

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

@ -1,84 +1,38 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std; using namespace std;
int main() { struct Input {
const size_t SCREEN_WIDTH = 80; vector<double> numbers;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t bin_count{};
};
Input input_data() {
Input in;
size_t number_count; size_t number_count;
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
vector<double> numbers(number_count); in.numbers.resize(number_count);
cerr << "Enter numbers: "; cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++) { for (size_t i = 0; i < number_count; i++) {
cin >> numbers[i]; cin >> in.numbers[i];
} }
size_t bin_count;
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> bin_count; cin >> in.bin_count;
vector<size_t> bins(bin_count, 0);
double min = numbers[0];
double max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
double bin_size = (max - min) / bin_count;
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]++;
}
}
size_t max_bin_count = 0;
for (size_t bin : bins) {
if (bin > max_bin_count) {
max_bin_count = bin;
}
}
if (max_bin_count <= MAX_ASTERISK) { return in;
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 10) cout << " ";
cout << " " << bins[i] << "|";
for (size_t j = 0; j < bins[i]; j++) {
cout << "*";
} }
cout << endl;
}
} else {
for (size_t i = 0; i < bin_count; i++) {
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin_count));
if (bins[i] < 100) cout << " ";
if (bins[i] < 10) cout << " ";
cout << bins[i] << "|";
for (size_t j = 0; j < height; j++) { int main() {
cout << "*"; auto in = input_data();
} auto bins = make_histogram(in.numbers, in.bin_count);
cout << endl; show_histogram_svg(bins);
}
}
return 0; return 0;
} }

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>
</svg>

После

Ширина:  |  Высота:  |  Размер: 134 B

@ -0,0 +1,4 @@
10
3 3 4 4 4 4 4 5 5 5
3

@ -0,0 +1,58 @@
#include "svg.h"
#include <iostream>
using namespace std;
void svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' 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, const string& text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}
void svg_rect(double x, double y, double width, double height,
const string& stroke, const string& fill) {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
<< "' height='" << height
<< "' stroke='" << stroke
<< "' fill='" << fill << "' />\n";
}
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;
size_t max_count = 0;
for (size_t count : bins) {
if (count > max_count) {
max_count = count;
}
}
double BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / static_cast<double>(max_count);
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaaaff");
top += BIN_HEIGHT;
}
svg_end();
}

15
svg.h

@ -0,0 +1,15 @@
#ifndef SVG_H
#define SVG_H
#include <vector>
#include <string>
void svg_begin(double width, double height);
void svg_end();
void svg_text(double left, double baseline, const std::string& text);
void svg_rect(double x, double y, double width, double height,
const std::string& stroke = "black",
const std::string& fill = "black");
void show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H

@ -0,0 +1,40 @@
#include "text.h"
#include <iostream>
using namespace std;
void show_histogram_text(const vector<size_t>& bins) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
size_t max_bin_count = 0;
for (size_t bin : bins) {
if (bin > max_bin_count) {
max_bin_count = bin;
}
}
if (max_bin_count <= MAX_ASTERISK) {
for (size_t bin : bins) {
if (bin < 10) cout << " ";
cout << " " << bin << "|";
for (size_t j = 0; j < bin; j++) {
cout << "*";
}
cout << endl;
}
} else {
for (size_t bin : bins) {
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bin) / max_bin_count));
if (bin < 100) cout << " ";
if (bin < 10) cout << " ";
cout << bin << "|";
for (size_t j = 0; j < height; j++) {
cout << "*";
}
cout << endl;
}
}
}

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

@ -0,0 +1,39 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
TEST_CASE("Test find_minmax with distinct positive numbers") {
double min, max;
find_minmax({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("Test find_minmax with single element") {
double min, max;
find_minmax({5}, min, max);
CHECK(min == 5);
CHECK(max == 5);
}
TEST_CASE("Test find_minmax with negative numbers") {
double min, max;
find_minmax({-3, -1, -2}, min, max);
CHECK(min == -3);
CHECK(max == -1);
}
TEST_CASE("Test find_minmax with equal elements") {
double min, max;
find_minmax({7, 7, 7}, min, max);
CHECK(min == 7);
CHECK(max == 7);
}
TEST_CASE("Mixed positive and negative numbers") {
double min, max;
find_minmax({-5.0, 0.0, 5.0}, min, max);
CHECK(min == -5.0);
CHECK(max == 5.0);
}

@ -7,11 +7,12 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
356052752DC3A6B7003C360D /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 356052742DC3A6B7003C360D /* main.cpp */; }; 3595A9962DC3B5DF0027AF89 /* histogram.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3595A9952DC3B5DF0027AF89 /* histogram.cpp */; };
3595A9992DC3B6080027AF89 /* unittest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3595A9982DC3B6080027AF89 /* unittest.cpp */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
3560526F2DC3A6B7003C360D /* CopyFiles */ = { 3560529E2DC3B4B2003C360D /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase; isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/; dstPath = /usr/share/man/man1/;
@ -23,12 +24,15 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
356052712DC3A6B7003C360D /* lab01 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = lab01; sourceTree = BUILT_PRODUCTS_DIR; }; 356052A02DC3B4B2003C360D /* unittest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = unittest; sourceTree = BUILT_PRODUCTS_DIR; };
356052742DC3A6B7003C360D /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; }; 3595A9952DC3B5DF0027AF89 /* histogram.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = histogram.cpp; sourceTree = "<group>"; };
3595A9972DC3B5EC0027AF89 /* histogram_internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = histogram_internal.h; sourceTree = "<group>"; };
3595A9982DC3B6080027AF89 /* unittest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = unittest.cpp; sourceTree = "<group>"; };
3595A99A2DC3B6930027AF89 /* doctest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = doctest.h; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
3560526E2DC3A6B7003C360D /* Frameworks */ = { 3560529D2DC3B4B2003C360D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
@ -38,18 +42,21 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
356052682DC3A6B7003C360D = { 356052972DC3B4B2003C360D = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
356052742DC3A6B7003C360D /* main.cpp */, 3595A9982DC3B6080027AF89 /* unittest.cpp */,
356052722DC3A6B7003C360D /* Products */, 3595A99A2DC3B6930027AF89 /* doctest.h */,
3595A9972DC3B5EC0027AF89 /* histogram_internal.h */,
3595A9952DC3B5DF0027AF89 /* histogram.cpp */,
356052A12DC3B4B2003C360D /* Products */,
); );
sourceTree = "<group>"; sourceTree = "<group>";
}; };
356052722DC3A6B7003C360D /* Products */ = { 356052A12DC3B4B2003C360D /* Products */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
356052712DC3A6B7003C360D /* lab01 */, 356052A02DC3B4B2003C360D /* unittest */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
@ -57,38 +64,38 @@
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
356052702DC3A6B7003C360D /* lab01 */ = { 3560529F2DC3B4B2003C360D /* unittest */ = {
isa = PBXNativeTarget; isa = PBXNativeTarget;
buildConfigurationList = 356052782DC3A6B7003C360D /* Build configuration list for PBXNativeTarget "lab01" */; buildConfigurationList = 356052A72DC3B4B2003C360D /* Build configuration list for PBXNativeTarget "unittest" */;
buildPhases = ( buildPhases = (
3560526D2DC3A6B7003C360D /* Sources */, 3560529C2DC3B4B2003C360D /* Sources */,
3560526E2DC3A6B7003C360D /* Frameworks */, 3560529D2DC3B4B2003C360D /* Frameworks */,
3560526F2DC3A6B7003C360D /* CopyFiles */, 3560529E2DC3B4B2003C360D /* CopyFiles */,
); );
buildRules = ( buildRules = (
); );
dependencies = ( dependencies = (
); );
name = lab01; name = unittest;
productName = lab01; productName = unittest;
productReference = 356052712DC3A6B7003C360D /* lab01 */; productReference = 356052A02DC3B4B2003C360D /* unittest */;
productType = "com.apple.product-type.tool"; productType = "com.apple.product-type.tool";
}; };
/* End PBXNativeTarget section */ /* End PBXNativeTarget section */
/* Begin PBXProject section */ /* Begin PBXProject section */
356052692DC3A6B7003C360D /* Project object */ = { 356052982DC3B4B2003C360D /* Project object */ = {
isa = PBXProject; isa = PBXProject;
attributes = { attributes = {
BuildIndependentTargetsInParallel = 1; BuildIndependentTargetsInParallel = 1;
LastUpgradeCheck = 1540; LastUpgradeCheck = 1540;
TargetAttributes = { TargetAttributes = {
356052702DC3A6B7003C360D = { 3560529F2DC3B4B2003C360D = {
CreatedOnToolsVersion = 15.4; CreatedOnToolsVersion = 15.4;
}; };
}; };
}; };
buildConfigurationList = 3560526C2DC3A6B7003C360D /* Build configuration list for PBXProject "lab01" */; buildConfigurationList = 3560529B2DC3B4B2003C360D /* Build configuration list for PBXProject "unittest" */;
compatibilityVersion = "Xcode 14.0"; compatibilityVersion = "Xcode 14.0";
developmentRegion = en; developmentRegion = en;
hasScannedForEncodings = 0; hasScannedForEncodings = 0;
@ -96,29 +103,30 @@
en, en,
Base, Base,
); );
mainGroup = 356052682DC3A6B7003C360D; mainGroup = 356052972DC3B4B2003C360D;
productRefGroup = 356052722DC3A6B7003C360D /* Products */; productRefGroup = 356052A12DC3B4B2003C360D /* Products */;
projectDirPath = ""; projectDirPath = "";
projectRoot = ""; projectRoot = "";
targets = ( targets = (
356052702DC3A6B7003C360D /* lab01 */, 3560529F2DC3B4B2003C360D /* unittest */,
); );
}; };
/* End PBXProject section */ /* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */
3560526D2DC3A6B7003C360D /* Sources */ = { 3560529C2DC3B4B2003C360D /* Sources */ = {
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
356052752DC3A6B7003C360D /* main.cpp in Sources */, 3595A9962DC3B5DF0027AF89 /* histogram.cpp in Sources */,
3595A9992DC3B6080027AF89 /* unittest.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
/* End PBXSourcesBuildPhase section */ /* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */ /* Begin XCBuildConfiguration section */
356052762DC3A6B7003C360D /* Debug */ = { 356052A52DC3B4B2003C360D /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
@ -179,7 +187,7 @@
}; };
name = Debug; name = Debug;
}; };
356052772DC3A6B7003C360D /* Release */ = { 356052A62DC3B4B2003C360D /* Release */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
@ -233,7 +241,7 @@
}; };
name = Release; name = Release;
}; };
356052792DC3A6B7003C360D /* Debug */ = { 356052A82DC3B4B2003C360D /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
@ -241,7 +249,7 @@
}; };
name = Debug; name = Debug;
}; };
3560527A2DC3A6B7003C360D /* Release */ = { 356052A92DC3B4B2003C360D /* Release */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
@ -252,25 +260,25 @@
/* End XCBuildConfiguration section */ /* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */ /* Begin XCConfigurationList section */
3560526C2DC3A6B7003C360D /* Build configuration list for PBXProject "lab01" */ = { 3560529B2DC3B4B2003C360D /* Build configuration list for PBXProject "unittest" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
buildConfigurations = ( buildConfigurations = (
356052762DC3A6B7003C360D /* Debug */, 356052A52DC3B4B2003C360D /* Debug */,
356052772DC3A6B7003C360D /* Release */, 356052A62DC3B4B2003C360D /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
356052782DC3A6B7003C360D /* Build configuration list for PBXNativeTarget "lab01" */ = { 356052A72DC3B4B2003C360D /* Build configuration list for PBXNativeTarget "unittest" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
buildConfigurations = ( buildConfigurations = (
356052792DC3A6B7003C360D /* Debug */, 356052A82DC3B4B2003C360D /* Debug */,
3560527A2DC3A6B7003C360D /* Release */, 356052A92DC3B4B2003C360D /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
/* End XCConfigurationList section */ /* End XCConfigurationList section */
}; };
rootObject = 356052692DC3A6B7003C360D /* Project object */; rootObject = 356052982DC3B4B2003C360D /* Project object */;
} }
Загрузка…
Отмена
Сохранить