Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maxim realization #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ cmake_minimum_required(VERSION 3.5.1)
project(dummy_cmake_project)

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

set(SOURCES project.cc)
set(SOURCES project.cc constant.h project_operation.h project_operation.cpp)
add_library(ProjectLib ${SOURCES})

add_executable(project main.cc)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Polish_notation
24 changes: 24 additions & 0 deletions constant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CONSTANT
#define CONSTANT

#include <limits>
#include <stack>

namespace polish_notation_constant
{
typedef double value_t;
typedef std::stack<value_t> stack_numbers_t;

constexpr value_t ERROR_VALUE = std::numeric_limits<value_t>::max();

enum class error_code {
STACK_EMPTY,
NOT_CORRECT_EXPRESSION,
ZERO_DEVISION,
NO_ERROR,
NOT_CORRECT_COUNT_OF_OPERATION
};
}

#endif // CONSTANT

3 changes: 2 additions & 1 deletion iproject.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include <string>

namespace dev{

class IProject {
virtual int run() = 0;
virtual double run(std::string) = 0;
};

}
20 changes: 18 additions & 2 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#include "project.h"
#include "constant.h"
#include <iostream>
#include <string>

int main() {
return 0;
int main()
{
dev::Project project;
auto value = project.run("2 2 +");

if( value != polish_notation_constant::ERROR_VALUE)
{
std::cout << "Result: " << value << std::endl;
}
else
{
std::cout << "Error code: " << project.get_error_code() << std::endl;
}

return 0;
}
130 changes: 128 additions & 2 deletions project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,133 @@

namespace dev {

int Project::run() {
return 0;

value_t Project::run(std::string input_str) {

return processData(input_str);
}


bool Project::is_valid_count_value(stack_numbers_t& stack)
{
if (stack.size() < 2) {
error_code_val = error_code::NOT_CORRECT_COUNT_OF_OPERATION;

return false;
}
else
{
return true;
}
}

value_t Project::take_value_from_stack(stack_numbers_t& stack)
{
value_t value = ERROR_VALUE;

if (stack.size() != 0) {
value = stack.top();
stack.pop();
}
else{
error_code_val = error_code::STACK_EMPTY;
}

return value;
}



bool Project::is_number(const std::string& s)
{
char* end = 0;
double val = std::strtod(s.c_str(), &end);
return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
}

void Project::clear_stack(stack_numbers_t & stack)
{
while(!stack.empty())
{
stack.pop();
}
}

int Project::get_error_code()
{
return static_cast<uint32_t>(error_code_val);
}

value_t Project::calculateStackValue(std::string temp_str, stack_numbers_t& stack_numbers)
{
double left_operand, right_operand;

right_operand = take_value_from_stack(stack_numbers);
left_operand = take_value_from_stack(stack_numbers);

left_operand = project_operation_.make_operation(temp_str[0], left_operand, right_operand);

if(left_operand == ERROR_VALUE)
{
error_code_val = error_code::NOT_CORRECT_EXPRESSION;
return ERROR_VALUE;
}

if(left_operand != polish_notation_constant::ERROR_VALUE && temp_str[0] == '/') {
error_code_val = error_code::ZERO_DEVISION;
}

return left_operand;
}

value_t Project::processData(std::string& input_str)
{
stack_numbers_t stack_numbers;
error_code_val = error_code::NO_ERROR;

value_t return_value = ERROR_VALUE;
std::istringstream f(input_str);
std::string temp_str;


while (std::getline(f, temp_str, ' ')) {
std::cout << "Stack value: " << temp_str << std::endl;

if(is_number(temp_str)) {
stack_numbers.push(std::stod(temp_str.c_str()));
}
else {
if (true == is_valid_count_value(stack_numbers)) {
value_t value = calculateStackValue(temp_str, stack_numbers);

if (value != ERROR_VALUE){
stack_numbers.push(value);
}
else{
break;
}

} else {
error_code_val = error_code::NOT_CORRECT_COUNT_OF_OPERATION;
}
}

if(error_code_val != error_code::NO_ERROR) {
clear_stack(stack_numbers);
break;
}
}


if (input_str.empty())
{
error_code_val = error_code::STACK_EMPTY;
}

if (stack_numbers.size() != 0) {
return_value = stack_numbers.top();
}

return return_value;
}
} // namespace dev
134 changes: 134 additions & 0 deletions project.cc.autosave
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "project.h"

namespace dev {


value_t Project::run(std::string input_str) {

return processData(input_str);
}


bool Project::is_valid_count_value(stack_numbers_t& stack)
{
if (stack.size() < 2) {
error_code_val = error_code::NOT_CORRECT_COUNT_OF_OPERATION;

return false;
}
else
{
return true;
}
}

value_t Project::take_value_from_stack(stack_numbers_t& stack)
{
value_t value = ERROR_VALUE;

if (stack.size() != 0) {
value = stack.top();
stack.pop();
}
else{
error_code_val = error_code::STACK_EMPTY;
}

return value;
}



bool Project::is_number(const std::string& s)
{
char* end = 0;
double val = std::strtod(s.c_str(), &end);
return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
}

void Project::clear_stack(stack_numbers_t & stack)
{
while(!stack.empty())
{
stack.pop();
}
}

int Project::get_error_code()
{
return static_cast<uint32_t>(error_code_val);
}

value_t Project::calculateStackValue(std::string temp_str, stack_numbers_t& stack_numbers)
{
double left_operand, right_operand;

right_operand = take_value_from_stack(stack_numbers);
left_operand = take_value_from_stack(stack_numbers);

left_operand = project_operation_.make_operation(temp_str[0], left_operand, right_operand);

if(left_operand == ERROR_VALUE)
{
error_code_val = error_code::NOT_CORRECT_EXPRESSION;
return ERROR_VALUE;
}

if(left_operand != polish_notation_constant::ERROR_VALUE && temp_str[0] == '/') {
error_code_val = error_code::ZERO_DEVISION;
}

return left_operand;
}

value_t Project::processData(std::string& input_str)
{
stack_numbers_t stack_numbers;
error_code_val = error_code::NO_ERROR;

value_t return_value = ERROR_VALUE;
std::istringstream f(input_str);
std::string temp_str;


while (std::getline(f, temp_str, ' ')) {
std::cout << "Stack value: " << temp_str << std::endl;

if(is_number(temp_str)) {
stack_numbers.push(std::stod(temp_str.c_str()));
}
else {
if (is_valid_count_value(stack_numbers) == true) {
value_t value = calculateStackValue(temp_str, stack_numbers);

if (value != ERROR_VALUE){
stack_numbers.push(value);
}
else{
break;
}

} else {
error_code_val = error_code::NOT_CORRECT_COUNT_OF_OPERATION;
}
}

if(error_code_val != error_code::NO_ERROR) {
clear_stack(stack_numbers);
break;
}
}


if (input_str.empty())
{
error_code_val = error_code::STACK_EMPTY;
}

if (stack_numbers.size() != 0) {
return_value = stack_numbers.top();
}

return return_value;
}
} // namespace dev
36 changes: 34 additions & 2 deletions project.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
#pragma once
#include "iproject.h"
#include "constant.h"
#include "project_operation.h"

#include <array>
#include <cctype>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>


namespace dev {

using namespace polish_notation_constant;


class Project : public IProject {
// IProject interface
typedef error_code error_t;

// IProject interface
public:
int run();
double run(std::string);

int get_error_code();

private:
void clear_stack(stack_numbers_t&);
value_t calculateStackValue(std::string temp_str, stack_numbers_t& stack_numbers);

value_t take_value_from_stack();
value_t processData(std::string& input_str);
value_t take_value_from_stack(stack_numbers_t&);

bool is_valid_count_value(stack_numbers_t&);
bool is_number(const std::string& s);


project_operation project_operation_;
error_t error_code_val = error_code::NO_ERROR;
};
} // namespace dev
Loading