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
12 changes: 12 additions & 0 deletions Calculator_main/Calculator_main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# CMakeList.txt : CMake project for Calculator_main_v.f, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

project( Calculator_main)
set(CMAKE_CXX_STANDARD 20)
# Add source to this project's executable.

set(PROJECT_HEADERS Calculator_main.h, calcul.h)
add_executable(Calculator_main ${PROJECT_SOURCE_DIR}/ Calculator_main.cpp calcul.cpp "dll_read.cpp" "dll_read.h")

33 changes: 33 additions & 0 deletions Calculator_main/Calculator_main/Calculator_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Calculator_main.cpp : Defines the entry point for the application.
//

#include "Calculator_main.h"

// Calculator_main_v.f.cpp : Defines the entry point for the application.
//
#include <stack>
#include "calcul.h"

void main()
{
std::cout << "This is a calculator" << std::endl;
std::cout << "Standart operations: +, -, *, /, ^, ()" << std::endl;
std::cout << "Non-standart mathematical operations: cos lg sin tg ctg ln sqrt cubsqrt abs round" << std::endl;
std::cout << "cubsqrt - counts the cubic root" << std::endl;
std::cout << "round - rounds to the largest integer, but not more than this number" << std::endl;
std::cout << "To complete the work enter #" << std::endl << std::endl;

calcul cal;
cal.validation();
std::cout << "Enter the expression: ";
char c;
while (c = std::cin.peek())
{
if (c == '#') break;//interrupting the recording cycle
cal.readthread();
cal.maths();
std::cin.ignore();//extracting a scanned character for a common expression from a stream
std::cout << std::endl << "Enter the expression: ";
}

}
8 changes: 8 additions & 0 deletions Calculator_main/Calculator_main/Calculator_main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Calculator_main.h : Include file for standard system include files,
// or project specific include files.

#pragma once

#include <iostream>

// TODO: Reference additional headers your program requires here.
281 changes: 281 additions & 0 deletions Calculator_main/Calculator_main/calcul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
#include "calcul.h"

calcul::calcul()
{
oper_priority = {
{"+",1},
{"-",1},
{"*",2},
{"/",2},
{"^",3}
};
function.insert(
{ "+",
[&](double a,double b) {double c = a + b; number.push(c); }//put the result into the stack of numbers
}
);
function.insert(
{ "-",
[&](double a,double b)
{double c = b - a;//change order because the stack order is different from the initial order in the console
number.push(c);
}//put the result into the stack of numbers
}
);
function.insert(
{ "*",
[&](double a,double b) {double c = a * b; number.push(c); }//put the result into the stack of numbers
}
);
function.insert(
{ "/",
[&](double a,double b) {
if (a == 0)
{
std::cout << "Error. You can't divide by zero" << std::endl;
//In order not to continue processing operations, we clear stacks of numbers and operations
deleteStacksEr();
return;
}
double c = b / a;//change order because the stack order is different from the initial order in the console
number.push(c);//put the result into the stack of numbers
}
}
);
function.insert(
{
"^",
[&](double a,double b) {double c = pow(b,a);//change order because the stack order is different from the initial order in the console
number.push(c); }
});
}

void calcul::validation()
{
//exception handling
try
{
dll.read_files();
}
catch (std::exception)
{
std::cout << "Error in dll" << std::endl;
}
}

template <typename T>//template, because we have two stacks of different types
void calcul::clearStack(std::stack <T>& s)
{
while (s.size() != 0) {
s.pop();
}
return;
}

void calcul::readthread()
{
char c;
while (c = std::cin.peek())
{
if (c == '\n') break;
if (c == ' ')
{
std::cin.ignore();
continue;
};
if (((c >= '0') && (c <= '9')) || check_neg(c))//number 0 to 9 + negative number check
{
is_number(c);
continue;
}
if ((c == '+') || (c == '-') || (c == '/') || (c == '*') || (c == '(') || (c == '^'))//checking if the function is standard
{
is_standart(c);
continue;
}
if (isalpha(c))//checking whether the function is non-standard (determines the first character of a non-standard operation and reads it before the opening parenthesis,
// if a non-standard function was read, it will be found out later when checking)
{
if (!is_nonstandart(c)) break;
continue;
}
if (c == ')')
{
if (!is_brecket(c)) break;
continue;
}

Choose a reason for hiding this comment

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

Очень длинная функция

//caught an incorrect character
deleteStacksEr();
break;
}
}


void calcul::is_number(char c)
{
double value;
std::cin >> value;//extracting a number from the stream
number.push(value);
}
void calcul::is_standart(char c)
{
std::string s;
std::stringstream ss;
ss << c;
ss >> s;
processing_oper(s);
if ((number.size() != 0) || (operation.size() != 0)) std::cin.ignore();//extracting the viewed symbol from the stream
}
bool calcul::is_nonstandart(char c)
{
std::string nstand;
while (c != '(') {
if (std::isupper(c)) c=(char)std::tolower(c);
nstand.push_back(c);
std::cin.ignore();
c = std::cin.peek();
if ((c == ')') || (c == '\n')) break;//the expression was entered incorrectly
}
if ((c == ')') || (c == '\n'))
{
deleteStacksEr();
return false;
}
operation.push(nstand);
return true;
}
bool calcul::is_brecket(char c)
{
while (operation.top() != "(")//we perform operations in parentheses
{
if (function.find(operation.top()) != function.end())
{
oper();
if (operation.size() == 0) break;//if there was a division by 0, then the stacks became empty and it is necessary to stop the cycle
continue;
}
expr_incor();
break;
}
if (number.size() != 0) std::cin.ignore();//extracting the scanned symbol from the stream, provided that there was no stack clearing due to an incorrect expression
if (operation.size() != 0)
{
operation.pop();//if there was no division by zero, then the opening bracket remains in the stack and it must be removed from the stack
if (nonstandart() == -1)//checking that a non-standard function is valid
{
deleteStacksEr();
return false;
}
}
return true;
}

void calcul::expr_incor()
{
std::cout << "Expression entered incorrectly" << std::endl;
clearStack(operation);
clearStack(number);
}

void calcul::deleteStacksEr()
{
std::string s;
std::getline(std::cin, s, '\n');
expr_incor();
std::cout << "To continue, press Enter" << std::endl;
}

void calcul::processing_oper(std::string c)//processing of the received options
{
if ((operation.empty()) || (c == "("))//in any case, the opening bracket is either at the beginning of the stack, or we put it on the stack on top of other operations
{
operation.push(c);
return;
}

if (oper_priority[c] <= oper_priority[operation.top()])
//if the operation in question has a priority less than or equal to the operation already lying on the stack, then first we will count the operation that is on the stack, and then we will add a new one
{
oper();
if (number.size() != 0) operation.push(c);//check if the stack is not cleared after dividing by 0
return;
}
//the stack with operations is not empty and the priority of the operation in question is not less than the one already placed on the stack
operation.push(c);
return;
}

bool calcul::check_neg(char c)//processing negative numbers if it is the first number in the expression or comes after the opening parenthesis
{
if (c == '-')
{
if (number.size() == 0)
return true;
if (operation.size() != 0)
{
if ((operation.top() == "(") && (operation.size() > number.size()))//when '-' is immediately after the parentheses, the number of operations exceeds the number of numbers read
return true;
}
return false;//c='-' but none of the conditions are met
}
return false;
}

void calcul::maths()//to process all raw operations with numbers
{
while (operation.size() != 0)
{
if (function.find(operation.top()) != function.end())
{
oper();
continue;
}
//there is no processing of functions from plugins as we consider them as soon as we met,
//so that complex expressions using non-standard functions can be correctly calculated
expr_incor();
break;
}
if (number.size() != 0)
{
std::cout << "Result=" << number.top() << std::endl;
number.pop();//deleting the last number in the stack (result)
}
}

int calcul::nonstandart()
{
if (!operation.empty())
{
if (dll.is_plugin(operation.top()))
{
double a = number.top();
number.pop();
if (!dll.calculation(operation.top(), a)) return -1;
number.push(a);
operation.pop();
return 1;
}
}
return 0;
}

void calcul::oper()//handles only one standard operation with numbers
{
double a, b;
a = number.top();//took the top number from the stack
number.pop();//removed the taken number from the stack
b = number.top();//took the top number from the stack
number.pop();//removed the taken number from the stack

//taking an operation from the stack with operations
function[operation.top()](a, b);
if (operation.size() != 0) operation.pop();//we remove the operation from the stack, provided that it is not empty, after an attempt to divide by 0
}

calcul::~calcul()
{
clearStack(operation);//clearing the stack with operations
clearStack(number);//clearing the stack with numbers
function.clear();//clearing the map with operations
oper_priority.clear();//clearing the map with priorities
}
45 changes: 45 additions & 0 deletions Calculator_main/Calculator_main/calcul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#ifndef calcul_h
#define calcul_h

#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <functional>
#include <map>
#include <limits>
#include <ios>
#include <ctype.h>
#include "dll_read.h"


class calcul
{
private:
std::stack <double> number;
std::stack <std::string> operation;
std::map <std::string, int> oper_priority;//mathematical operations priorities
std::map <std::string, std::function<void(double, double)>> function;//correlation of a mathematical operation and what it does
dll_read dll;
bool check_neg(char c);//checking whether the number is negative
void is_number(char c);//checking whether a number is
void is_standart(char c);//checking whether it is a standard operation
bool is_nonstandart(char c);//checking whether it is a non-standard operation
bool is_brecket(char c);//checking whether the bracket is closing
void oper();//processing of a single mathematical operation
int nonstandart();//working with non-standard functions
void deleteStacksEr();//clearing stacks if an error occurred when evaluating an expression (exception handling)
void processing_oper(std::string c);//processing of mathematical operations, namely checking for the emptiness of the stack and the priority of the mathematical operation
void expr_incor();//clearing stacks when an expression is entered incorrectly
template <typename T>
void clearStack(std::stack <T>& s);

public:
calcul();
void validation();//checking exceptions from dynamic libraries
void readthread();//reading an expression from a stream
void maths();//perform mathematical operations and is responsible for the output of the result to the console
~calcul();
};
#endif
Loading