Skip to content

Commit

Permalink
Create task 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KachalovM committed Sep 27, 2024
1 parent 30851a6 commit ac2121e
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 5 deletions.
20 changes: 19 additions & 1 deletion include/circle.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// Copyright 2022 UNN-CS
// Copyright 2024 Kachalov Mikhail
#ifndef INCLUDE_CIRCLE_H_
#define INCLUDE_CIRCLE_H_
#include <cstdint>

class Circle {
private:
double radius;
double ference;
double area;

public:
Circle(double r);

void setRadius(double r);
void setFerence(double f);
void setArea(double a);

double getRadius() const;
double getFerence() const;
double getArea() const;
};


#endif // INCLUDE_CIRCLE_H_
8 changes: 8 additions & 0 deletions include/tasks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Kachalov Mikhail
#ifndef TASKS_H
#define TASKS_H

double solveEarthAndRope(double earthRadius);
double solveSwimmingPool(double poolRadius, double walkwayWidth);

#endif // INCLUDE_TASKS_H_
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(header_path "${${PROJECT_NAME}_SOURCE_DIR}/include")
set(header ${header_path}/circle.h)
set(src circle.cpp)
set(header ${header_path}/circle.h
${header_path}/tasks.h)
set(src circle.cpp
tasks.cpp)

add_library(${PROJECT_NAME} SHARED
${header}
Expand Down
38 changes: 37 additions & 1 deletion src/circle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
// Copyright 2022 UNN-CS
// Copyright 2024 Kachalov Mikhail
#include <cstdint>
#include "circle.h"
#include <cmath>

const double PI = 3.14159265358979323846;

Circle::Circle(double r) {
setRadius(r);
}

void Circle::setRadius(double r) {
radius = r;
ference = 2 * PI * radius;
area = PI * radius * radius;
}

void Circle::setFerence(double f) {
ference = f;
radius = ference / (2 * PI);
area = PI * radius * radius;
}

void Circle::setArea(double a) {
area = a;
radius = std::sqrt(area / PI);
ference = 2 * PI * radius;
}

double Circle::getRadius() const {
return radius;
}

double Circle::getFerence() const {
return ference;
}

double Circle::getArea() const {
return area;
}
32 changes: 32 additions & 0 deletions src/tasks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Kachalov Mikhail
#include "circle.h"
#include "tasks.h"
#include <iostream>

double solveEarthAndRope(double earthRadius) {
Circle earth(earthRadius);

double newFerence = earth.getFerence() + 1;

Circle ropeCircle(earthRadius);
ropeCircle.setFerence(newFerence);

double gap = ropeCircle.getRadius() - earth.getRadius();

return gap;
}

double solveSwimmingPool(double poolRadius, double walkwayWidth) {

Circle pool(poolRadius);

Circle poolWithWalkway(poolRadius + walkwayWidth);

double walkwayArea = poolWithWalkway.getArea() - pool.getArea();

double concreteCost = walkwayArea * 1000;

double fenceCost = poolWithWalkway.getFerence() * 2000;

return fenceCost + concreteCost;
}
71 changes: 70 additions & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@

// Copyright 2024 Kachalov Mikhail
#include <gtest/gtest.h>
#include <cstdint>
#include "circle.h"
#include "tasks.h"

TEST(CircleTest, SetRadius) {
Circle circle(10);
EXPECT_DOUBLE_EQ(circle.getRadius(), 10);
EXPECT_DOUBLE_EQ(circle.getFerence(), 2 * PI * 10);
EXPECT_DOUBLE_EQ(circle.getArea(), PI * 10 * 10);
}

TEST(CircleTest, SetFerence) {
Circle circle(10);
circle.setFerence(2 * PI * 15);
EXPECT_DOUBLE_EQ(circle.getRadius(), 15);
EXPECT_DOUBLE_EQ(circle.getFerence(), 2 * PI * 15);
EXPECT_DOUBLE_EQ(circle.getArea(), PI * 15 * 15);
}

TEST(CircleTest, SetArea) {
Circle circle(10);
circle.setArea(PI * 20 * 20);
EXPECT_DOUBLE_EQ(circle.getRadius(), 20);
EXPECT_DOUBLE_EQ(circle.getFerence(), 2 * PI * 20);
EXPECT_DOUBLE_EQ(circle.getArea(), PI * 20 * 20);
}

TEST(TasksTest, SolveEarthAndRope) {
double earthRadius = 6378100;
double gap = solveEarthAndRope(earthRadius);
EXPECT_NEAR(gap, 0.159154943, 1e-9);
}

TEST(TasksTest, SolveEarthAndRopeSmallRadius) {
double smallRadius = 1;
double gap = solveEarthAndRope(smallRadius);
EXPECT_NEAR(gap, 0.159154943, 1e-9);
}

TEST(TasksTest, SolveSwimmingPool) {
double poolRadius = 3;
double walkwayWidth = 1;
double totalCost = solveSwimmingPool(poolRadius, walkwayWidth);
EXPECT_NEAR(totalCost, 75424.77796, 1e-2);
}

TEST(TasksTest, SolveSwimmingPoolDifferentSizes) {
double poolRadius = 5;
double walkwayWidth = 2;
double totalCost = solveSwimmingPool(poolRadius, walkwayWidth);
EXPECT_NEAR(totalCost, 207345.1151, 1e-2);
}

TEST(CircleTest, ZeroRadius) {
Circle circle(0);
EXPECT_DOUBLE_EQ(circle.getRadius(), 0);
EXPECT_DOUBLE_EQ(circle.getFerence(), 0);
EXPECT_DOUBLE_EQ(circle.getArea(), 0);
}

TEST(CircleTest, FractionalRadius) {
Circle circle(2.5);
EXPECT_DOUBLE_EQ(circle.getRadius(), 2.5);
EXPECT_DOUBLE_EQ(circle.getFerence(), 2 * PI * 2.5);
EXPECT_DOUBLE_EQ(circle.getArea(), PI * 2.5 * 2.5);
}

TEST(CircleTest, SmallFerence) {
Circle circle(1);
circle.setFerence(1);
EXPECT_NEAR(circle.getRadius(), 1 / (2 * PI), 1e-9);
EXPECT_NEAR(circle.getArea(), PI * std::pow(1 / (2 * PI), 2), 1e-9);
}

0 comments on commit ac2121e

Please sign in to comment.