Skip to content

Commit

Permalink
impl lab3 app (#410)
Browse files Browse the repository at this point in the history
* impl lab3 app

* format
  • Loading branch information
Hugelka authored Nov 18, 2024
1 parent 28e1935 commit d922593
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/kachalov_m_calc_polygon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")
set(APPLICATION "app_${MODULE}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(application)

include("CTestTests.txt")
51 changes: 51 additions & 0 deletions modules/kachalov_m_calc_polygon/CTestTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#############################################
##### Testing
#############################################

set(prefix "${MODULE}")

add_test(
NAME ${prefix}_run
COMMAND ${APPLICATION}
)
set_tests_properties (${prefix}_run PROPERTIES
PASS_REGULAR_EXPRESSION "Usage:"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_empty
COMMAND ${APPLICATION}
)
set_tests_properties (${prefix}_run_empty PROPERTIES
PASS_REGULAR_EXPRESSION "Usage:"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_invalid_vertices
COMMAND ${APPLICATION} 0 0 1 0 1000
)
set_tests_properties(${prefix}_run_invalid_vertices PROPERTIES
PASS_REGULAR_EXPRESSION "invalid arguments number"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_invalid_arguments
COMMAND ${APPLICATION} 0 0 2 e 1 1 2 0 0 2 1000
)
set_tests_properties(${prefix}_run_invalid_arguments PROPERTIES
PASS_REGULAR_EXPRESSION "invalid_argument"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_valid
COMMAND ${APPLICATION} 0 0 1 0 1 1 0 1 1000
)
set_tests_properties(${prefix}_run_valid PROPERTIES
PASS_REGULAR_EXPRESSION "Area: 1.000000 square meters"
PASS_REGULAR_EXPRESSION "Cost: 1000.000000 currency units"
LABELS "${MODULE}"
)
15 changes: 15 additions & 0 deletions modules/kachalov_m_calc_polygon/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(target ${APPLICATION})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Application")

add_executable(${target} ${srcs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Application")

target_link_libraries(${target} ${LIBRARY})
if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
11 changes: 11 additions & 0 deletions modules/kachalov_m_calc_polygon/application/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2024 Mironov Ilya

#include <iostream>

#include "include/polygon_price_app.h"

int main(int argc, char** argv) {
PolygonPriceApp ppapp;
std::cout << ppapp(argc, argv) << '\n';
return 0;
}
22 changes: 22 additions & 0 deletions modules/kachalov_m_calc_polygon/include/polygon_price_app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Mironov Ilya
#pragma once

#include <string>
#include <vector>

#include "include/polygon_area_calculator.h"

class PolygonPriceApp {
private:
PolygonAreaCalculator areaCalculator{};
std::string sMessage{};
double pricePerSquareMeter{};

public:
PolygonPriceApp() = default;
std::string operator()(int argc, char** argv);

private:
void help(const char* appname, const char* message = "");
void parseArgs(int argc, char** argv);
};
48 changes: 48 additions & 0 deletions modules/kachalov_m_calc_polygon/src/polygon_price_app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Mironov Ilya

#include "include/polygon_price_app.h"

#include <stdexcept>

std::string PolygonPriceApp::operator()(int argc, char** argv) {
if (!(argc >= 6 + 2 && argc % 2 == 0)) {
help(argv[0], "invalid arguments number");
return sMessage;
}

try {
parseArgs(argc, argv);
} catch (const std::invalid_argument& e) {
help(argv[0], "invalid_argument");
return sMessage;
} catch (const std::out_of_range& e) {
help(argv[0], "numbers are to large");
return sMessage;
}

double area = areaCalculator.calculateArea();
double cost = area * pricePerSquareMeter;

sMessage = "Area: " + std::to_string(area) + " square meters\n" +
"Cost: " + std::to_string(cost) + " currency units\n";

return sMessage;
}

void PolygonPriceApp::help(const char* appname, const char* message) {
sMessage = "Usage: " + std::string(appname) +
" <x1> <y1> <x2> <y2> <x3> <y3> ... <price_per_square_meter>\n" +
std::string(message) + "\n";
}

void PolygonPriceApp::parseArgs(int argc, char** argv) {
areaCalculator.clearVertices();

for (int i = 1; i < argc - 1; i += 2) {
double x = std::stod(argv[i]);
double y = std::stod(argv[i + 1]);
areaCalculator.addVertex(x, y);
}

pricePerSquareMeter = std::stod(argv[argc - 1]);
}

0 comments on commit d922593

Please sign in to comment.