Skip to content
Open
28 changes: 27 additions & 1 deletion include/arithmetic.h
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
// ���������� ������� � ������� ��� ���������� �������������� ���������
// ���������� ������� � ������� ��� ���������� �������������� ���������

#include "stack.h"
#include <vector>
#include <map>
#include <cmath>
#include <string>

class TPostfix {
std::vector<std::string> RPN;

std::string Error_string(const std::string& s, int i); //Method that selects element number "i" in a string. Uses only for selecting incorrect element when throws.

int get_prior(const std::string& s) noexcept; //Method that return the priority of an operation. If it is not an operation returns 0

int TPostfix::get_prior(char c) noexcept; //similar method for characters

Choose a reason for hiding this comment

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

Зачем здесь прямое указание поля имён TPostfix::? Аналогично ниже

Copy link
Author

Choose a reason for hiding this comment

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

Мда, поправил


double valid(const std::string& s); //Method that turns correct string into a number

std::string number_check(const std::string& s, int& i); //Method takes the string being processed and the index from which the expected number begins and checks it for correctness
public:
TPostfix(const std::string& s); //Turns string into a Reverse Polish Notation, if it possible.

double count(); //Count a string stored in a class element. User enter variables

double TPostfix::count(double* variables, int number_of_variables); //Count a string stored in a class element. Variables is entered by programm.
};
55 changes: 54 additions & 1 deletion include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,57 @@
// - �������� �� �������,
// - ��������� ���������� ��������� � �����
// - ������� �����
// ��� ������� � ������ ���� ������ �������������� ������
// ��� ������� � ������ ���� ������ �������������� ������
#include <iostream>

template <class T>
class TStack {
T* mas;
size_t count;
size_t size;
bool isFull() noexcept {
return count == size;
}
void resize() {
T* tmp = new T[size*2];
std::copy(mas, mas + size, tmp);

Choose a reason for hiding this comment

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

Копируется size элементов, хотя значения лежат только в size/2 эл-тов

size *= 2;
delete[] mas;
mas = tmp;
}
public:
TStack() {
mas = new T[10];
count = 0;
size = 10;
}
~TStack() {
delete[] mas;
mas = nullptr;
}
bool isEmpty() noexcept {
return count == 0;
}
void push_back(const T& val) {
if (isFull()) resize();
mas[count++] = val;
}
size_t GetCount() noexcept {
return count;
}
T& top() {
if (!isEmpty())return mas[count-1];
throw std::out_of_range("There are no elements in this Stack");
}
T pop_back() {
if (!isEmpty()) return mas[--count];
throw std::out_of_range("There are no elements in this Stack");
}
void clear() {
T* tmp = new T[10];
count = 0;

Choose a reason for hiding this comment

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

Не ошибка, но всё же: при очистке лучше откатить stack к заводским, т.е. снова сделать его размер маленьким и очистить лишнюю память

size = 10;
delete[] mas;
mas = tmp;
}
};
40 changes: 38 additions & 2 deletions samples/main_arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
// реализация пользовательского приложения

#include "arithmetic.h"
int main()
{
return 0;
std::string str;
while (true) {
try {
int fl = 0;
std::cout << "Enter a string what you want to count or enter \"end\" to end work with program \n";
std::cout << "Availble math fuctions:sin, cos, tan, cot, exp, log \n";
std::cout << "After functions you need to enter \'(\'\n";
std::cout << "You can enter an unlimited number of variables. Available variable names: x, x#any set of numbers#\n";
std::getline(std::cin, str);
if (str == "end") break;
if (str == "") {
system("cls");
continue;
}
for (int i = 0; i < str.size(); i++) if (str[i] == 'x') fl = 1;
TPostfix solution(str);
system("cls");
do {
std::cout << str << '\n';
std::cout << "Res: " << solution.count() << "\n\n";
if (fl) {
do {
std::cout << "Enter \'1\' if you want to recalculate an expression with new variables or enter \'0\' to enter new expression: ";
std::cin >> fl;
if (fl != 0 && fl != 1) std::cout << "This option does not exist\n";
} while (fl != 0 && fl != 1);
system("cls");
}
}
while (fl);
}
catch (std::exception& e) {
system("cls");
std::cout << str << '\n';
std::cout << e.what() << "\n\n";
}
}
}
6 changes: 3 additions & 3 deletions sln/vc12/arithmetic/arithmetic.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -88,4 +88,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
4 changes: 2 additions & 2 deletions sln/vc12/gtest/gtest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
4 changes: 2 additions & 2 deletions sln/vc12/sample/sample.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
4 changes: 2 additions & 2 deletions sln/vc12/tests/tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
Loading