Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Bondarenko Artemi/hw6_27.06.17/calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "operation.h"
#include <vector>


class Calculator
{
private:
std::vector<Operation*> m_operations = {
new SummOperation(), new DeductOperation(),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь под операции память выделяется динамически, которая нигде не освобождается.

new DivideOperation(), new MultiplyOperation(),
new SinusOperation(), new CosineOperation(),
new ExponentOperation() };

public:
void ShowMenu()
{
int count = 1;
for (auto it : m_operations)
{
std::cout << count++ << " - " << it->GetName() << "\n";
}

std::cout << "0 - Exit\nPlease choose operation: ";
}

std::vector<Operation*>::iterator Begin()
{
auto it = m_operations.begin();
return it;
}

int GetOperationCuantity()
{
return m_operations.size();
}

void ExecuteOperation(Operation* operation)
{
operation->Execute();
}
};
28 changes: 28 additions & 0 deletions Bondarenko Artemi/hw6_27.06.17/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "calculator.h"

void main()
{
std::cout << "Welcome to Calculator!" << std::endl;
Calculator calc;
std::vector<Operation*>::iterator it;
int choice;
while (true)
{
it = calc.Begin();
calc.ShowMenu();
std::cin >> choice;
if (choice > 0 && choice <= calc.GetOperationCuantity())
{
it += choice - 1;
calc.ExecuteOperation(*it);
continue;
}
else if (choice == 0)
{
break;
}
std::cout << "There is no such item!" << std::endl;

}
std::cout << "Good luck!" << std::endl;
}
159 changes: 159 additions & 0 deletions Bondarenko Artemi/hw6_27.06.17/operation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#pragma once

#include <string>
#include <iostream>
#include <cmath>


class Operation
{
protected:
std::string m_name;

public:
Operation(std::string name) : m_name(name)
{
}

std::string GetName() const
{
return m_name;
}

virtual void Execute() = 0;

};


class SummOperation : public Operation
{
public:
SummOperation() : Operation("Summ")
{
}

void Execute() override
{
double num1;
double num2;
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter second number: ";
std::cin >> num2;
std::cout << num1 << " + " << num2 << " = " << num1 + num2 << std::endl;
}
};


class DeductOperation : public Operation
{
public:
DeductOperation() : Operation("Deduct")
{
}

void Execute() override
{
double num1;
double num2;
std::cout << "Enter minuend number: ";
std::cin >> num1;
std::cout << "Enter subtrahend: ";
std::cin >> num2;
std::cout << num1 << " - " << num2 << " = " << num1 - num2 << std::endl;
}
};


class DivideOperation : public Operation
{
public:
DivideOperation() : Operation("Divide")
{
}

void Execute() override
{
double num1;
double num2;
std::cout << "Enter dividend number: ";
std::cin >> num1;
std::cout << "Enter divider: ";
std::cin >> num2;
if (num2 == 0)
{
std::cout << "Can not divided by 0" << std::endl;
return;
}
std::cout << num1 << " / " << num2 << " = " << num1 / num2 << std::endl;
}
};

class MultiplyOperation : public Operation
{
public:
MultiplyOperation() : Operation("Multiply")
{
}

void Execute() override
{
double num1;
double num2;
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter second number: ";
std::cin >> num2;
std::cout << num1 << " * " << num2 << " = " << num1 * num2 << std::endl;
}
};


class SinusOperation : public Operation
{
public:
SinusOperation() : Operation("Sinus")
{
}

void Execute() override
{
double num;
std::cout << "Enter number: ";
std::cin >> num;
std::cout << "Sin(" << num << ") = " << sin(num) << std::endl;
}
};

class CosineOperation : public Operation
{
public:
CosineOperation() : Operation("Cosine")
{
}

void Execute() override
{
double num;
std::cout << "Enter number: ";
std::cin >> num;
std::cout << "Cos(" << num << ") = " << cos(num) << std::endl;
}
};


class ExponentOperation : public Operation
{
public:
ExponentOperation() : Operation("Exponent")
{
}

void Execute() override
{
double num;
std::cout << "Enter degree: ";
std::cin >> num;
std::cout << "e^" << num << " = " << pow(2.718, num) << std::endl;
}
};