Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c3ae7a
First commit
midnsun Nov 20, 2024
773063b
2
midnsun Nov 20, 2024
00ac471
3
midnsun Nov 20, 2024
5bfcfb9
Parser, stack, vector are ready
midnsun Nov 20, 2024
a529ded
Some virtual functions added
midnsun Nov 22, 2024
fdd34a9
аааа
midnsun Nov 23, 2024
e27d4c9
Update arithmetic.h
midnsun Nov 23, 2024
f6a0f1f
All except the parsing of expression
midnsun Nov 23, 2024
8869286
project settings commit
midnsun Nov 23, 2024
0278319
some changes from lection questions
midnsun Nov 25, 2024
1fb50d3
Code have written, starting debugging
midnsun Nov 28, 2024
b1a4af4
Summary (required)
midnsun Nov 28, 2024
bb00b8d
Code is ready. Tests are left
midnsun Nov 29, 2024
ee1b254
Started writing tests
midnsun Nov 29, 2024
05cc2cd
Pow changed into ^
midnsun Dec 3, 2024
413e284
Summaty (required)
midnsun Dec 3, 2024
5aa61da
Project complete, gtests left
midnsun Dec 4, 2024
eca7532
Vector and stack tests were done
midnsun Dec 4, 2024
c694054
Error while compiling parser_test
midnsun Dec 5, 2024
aae35eb
Cmake tests changed
midnsun Dec 5, 2024
34df214
Update test_parser.cpp
midnsun Dec 5, 2024
c02598f
Update test_parser.cpp
midnsun Dec 5, 2024
518f1c9
Parser tests are done
midnsun Dec 5, 2024
20b873a
arithmetic tests are done
midnsun Dec 5, 2024
bce8222
Update RPN.cpp
midnsun Dec 5, 2024
e39e51b
RPN tests done
midnsun Dec 5, 2024
bc32712
Update test_lexems.cpp
midnsun Dec 5, 2024
fc5740a
Delete Tests.txt
midnsun Dec 5, 2024
6bc546f
Some bugs fixed
midnsun Dec 8, 2024
3acc4ce
All except arithmetics tests done
midnsun Dec 9, 2024
a48972c
All done
midnsun Dec 9, 2024
38af1c4
Update arithmetic.cpp
midnsun Dec 9, 2024
999ba1c
Stack design errors fixed
midnsun Dec 10, 2024
f953fd6
Made more convinient i/o
midnsun Dec 12, 2024
1b67d84
Minor changes in variable's input
midnsun Dec 13, 2024
a644db8
Hide some code
midnsun Dec 15, 2024
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
13 changes: 13 additions & 0 deletions include/RPN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "lexems.h"
#pragma once

class RPN { // reverse polish noration
private:
myVector<lexem*> infix;
myVector<lexem*> postfix;
void toRpn();
public:
const myVector<lexem*>& getPostfix() const noexcept;
RPN(const myVector<lexem*>& s);
double calculate();
};
48 changes: 48 additions & 0 deletions include/Tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
good:
1+2
1*2
1-2
1/2
-1
--1
---1
111
1+2*3
2*3*4
4*7-2*6
-1+2
2+-1
f+-r !!!!
4*(1+2)
(1.42e-1+.4e+1)/(77e-2-sin(1)): -57.9536
(sin((1+2)/2)+cos((1-2)/2))/(2*3.14): 0.298579
(x+y)/(xy-sin(yx)), x = 1.42e-1, y = .4e+1, xy = 77e-2, yx = 1
x+x*x*(x+x)/(x) = x+2x^2
pow(1.5e-1, -.5): 2.58199
f + 40
*(1+2, 3)
*((1+2), 3)
pow(((sin((1+2)/2)+cos((1-2)/2))/(2*3.14)), (x+x*x*(x+x)/(x)))
1.3E+1 - runtime input
(1.42E-1+.4e+1)/(77e-2-sin(1))
(1+2)*(3-4)/(sin6): 10.7367
12 * (var)

bad:
sin(1, 2)+3 what is this error?
1+2)
(1.42e-1+..4e+1)/(77e-2-sin(1))
1ee-1
pow1
(1+2)*(3-)/(sin6) !!! ERROR !!!
(1+2)*(3-+4)/(sin6) !!! ERROR !!!
+(1+2)*(3-4)/(sin6)
(1+2)*(3-4)/(sin6)-
)(2 * 3) * 4
(2 * 3 () ) * 4
(2 * 3 + () ) * 4
1 + var(sin(1))
2*sin1 + log
x + ( + 1)
48 + v1 v2 * 12
+-r
Binary file modified include/arithmetic.h
Binary file not shown.
61 changes: 61 additions & 0 deletions include/lexems.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <string>
#include "myvector.h"
#pragma once

class lexem {
private:
std::string sym;
int pos;
public:
lexem(const std::string& s, int p);
const std::string& getSym() const noexcept;
const int& getPos() const noexcept;
virtual bool isOperation() = 0;
virtual ~lexem();
};

class operation : public lexem {
int priority;
int operandsCount;
static myVector<std::string> availableOperations;
static myVector<int> priotities;
static myVector<int> vOperandsCount;
static myVector<bool> vCanBeAfterOperand;
public:
static void fillOperations();
bool static isOperation(const std::string& s);
bool static canBeAfterOperand(const std::string& s);
operation(const std::string& s, int p);
bool isOperation() noexcept override final;
int getPriority() noexcept;
int getOperandsCount() noexcept;
double execute(const myVector<double>& operands);
virtual ~operation();
};

class operand : public lexem {
protected:
double value;
public:
operand(const std::string& s, int p);
double getValue() noexcept;
bool isOperation() noexcept override final;
virtual void fillValue() = 0;
virtual ~operand();
};

class constant : public operand {
public:
constant(const std::string& s, int p);
void fillValue() override;
virtual ~constant();
};

class variable : public operand {
public:
static myVector<std::string> vectorOfVariablesNames;
static myVector<double> VectorOfVariablesValues;
void fillValue() override;
variable(const std::string& s, int p);
virtual ~variable();
};
87 changes: 87 additions & 0 deletions include/myvector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <algorithm>
#pragma once

template <typename T>
class myVector {
private:
T* data;
size_t capacity;
const size_t k;
protected:
size_t sizevar;
public:
myVector(size_t sizevar = 0, const T& v = T()) : k(2), capacity(1) {
if (sizevar > 0) {
capacity = sizevar;
}
data = new T[capacity];
std::fill(data, data + capacity, v);
this->sizevar = sizevar;
}
myVector(const myVector& vec) : k(2) {
data = new T[vec.capacity]();
capacity = vec.capacity;
sizevar = vec.sizevar;
std::copy(vec.data, vec.data + sizevar, data);
}
myVector(myVector&& vec) : k(2) {
data = nullptr;
std::swap(data, vec.data);
capacity = vec.capacity;
sizevar = vec.sizevar;
}
~myVector() {
delete[] data;
}
myVector& operator=(const myVector& vec) {
if (this == &vec) return *this;
if (capacity < vec.sizevar) {
T* t = new T[vec.capacity]();
delete[] data;
data = t;
}
capacity = vec.capacity;
sizevar = vec.sizevar;
std::copy(vec.data, vec.data + vec.sizevar, data);
return *this;
}
myVector& operator=(myVector&& vec) {
delete[] data;
data = nullptr;
capacity = vec.capacity;
sizevar = vec.sizevar;
std::swap(data, vec.data);
return *this;
}
const size_t size() const {
return sizevar;
}
const T& operator[](size_t i) const {
// if (i >= sizevar) throw std::out_of_range("OUT OF RANGE"); //
return data[i];
}
T& operator[](size_t i) {
// if (i >= sizevar) throw std::out_of_range("OUT OF RANGE"); //
return data[i];
}
void push_back(const T& v) {
if (sizevar >= capacity) {
T* t = new T[capacity * k];
std::copy(data, data + capacity, t);
delete[] data;
data = t;
capacity *= k;
}
data[sizevar++] = v;
}
void resize(size_t n) {
if (n > capacity) {
T* t = new T[n]();
std::copy(data, data + capacity, t);
capacity = n;
delete[] data;
data = t;
}
sizevar = n;
}
};
5 changes: 5 additions & 0 deletions include/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdexcept>
#include <string>
#pragma once

double parser(const std::string& s);
47 changes: 38 additions & 9 deletions include/stack.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
// ���������� � ���������� ���������� �����
// ���� ������������ ��������:
// - ������� ��������,
// - ���������� ��������,
// - �������� �������� �������� (��� ��������)
// - �������� �� �������,
// - ��������� ���������� ��������� � �����
// - ������� �����
// ��� ������� � ������ ���� ������ �������������� ������
#include "myvector.h"
#pragma once

template <typename T>
class myStack : protected myVector<T> {
private:
using myVector::sizevar;
public:
using myVector::size;
myStack() : myVector() {} /*
myStack(const myStack& st) : myVector(&dynamic_cast<myVector*>(&st)) {}
myStack(const myStack& st) : myVector(st) {} //?
myStack(myStack&& st) : myVector(st) {} //?
myStack& operator=(const myStack& st) {
this->myVector::operator=(st);
return *this;
}
myStack& operator=(myStack&& st) {
this->myVector::operator=(st);
return *this;
}*/
bool isEmpty() const { return sizevar == 0; }
void push(const T& v) {
this->myVector::push_back(v);
}
const T& pop() {
// const T& t = this->myVector::operator[](this->myVector::sizevar - 1);
// --this->myVector::sizevar;
// return t;
return this->myVector::operator[](--this->myVector::sizevar);
}
T& top() {
return this->myVector::operator[](this->myVector::sizevar - 1);
}
void clear() {
this->myVector::resize(0);
}
};
2 changes: 1 addition & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file(GLOB hdrs "*.h*" "../include/*.h")
file(GLOB srcs "*.cpp" "../src/arithmetic.cpp")
file(GLOB srcs "*.cpp" "../src/*.cpp")

add_executable(postfix ${srcs} ${hdrs})
72 changes: 71 additions & 1 deletion samples/main_arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,76 @@
// реализация пользовательского приложения
#include "stack.h"
#include "parser.h"
#include "myvector.h"
#include "arithmetic.h"
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
return 0;
cout << "MAIN RULES: ANY of words, except for reservated words of operations and pure numbers, are variables.\n";
cout << "--var not allowed! Instead, write -(-1). Please, do not divide numbers with space (e.g., 12 != 1 2)\n";
cout << "You can write sinx, but not like sin expression. Write sin(expression) instead\n";
cout << "Operations: sin, cos, exp, ^, log. Allowed numbers: x.ye-z; x.ye+z; .y; xe+z; .ye+z; x.y; x; But x.; x.e+1; xe1; e+1 NOT ALLOWED! \n" << endl;
cout << "Enter the expression" << endl;
string str;
string tmpstr;
getline(cin, str);
try {
calculator expr(str);
if (expr.araThereAnyVariables()) {
while (true) {
cout << "Now enter your variables" << endl;
expr.askForVariablesValues(cin, cout);
cout << endl << "Result: " << expr.calculate() << endl;
cout << "Do you want to enter another values of variables (y/n)?" << endl;
getline(cin, tmpstr);
if (tmpstr == "y") continue;
else if (tmpstr == "n") break;
else {
cout << "Unknown input\n";
return 0;
}
}
}
else {
expr.askForVariablesValues(cin, cout);
cout << endl << "Result: " << expr.calculate() << endl;
}
}
catch (exception& e) {
// cout << e.what() << endl;
string errstr = e.what();
string numstr = "";
size_t i;
int len = 1;
for (i = 0; i < errstr.size(); ++i) {
numstr += errstr[i];
if (errstr[i] == 'L') break;
}
if (i < errstr.length()) {
len = stoi(numstr);
++i;
}
else i = 0;
numstr = "";

for (; i < errstr.size(); ++i) {
numstr += errstr[i];
if (errstr[i] == 'E') break;
}
if (i >= errstr.length()) throw runtime_error("Bad exception");
int it = stoi(numstr);
if (it != -1) {
cout << str.substr(0, it) << " ";
if (it + len > str.length()) throw runtime_error("Bad exception");
cout << "\033[31m" << str.substr(it, len) << "\033[0m";
if (it + len < str.length()) cout << " " << str.substr(it + len, str.length()) << endl;
}
cout << endl << errstr.substr(i + 1);
}

return 0;
}
10 changes: 7 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 @@ -83,9 +83,13 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\include\arithmetic.h" />
<ClInclude Include="..\..\..\include\lexems.h" />
<ClInclude Include="..\..\..\include\myvector.h" />
<ClInclude Include="..\..\..\include\parser.h" />
<ClInclude Include="..\..\..\include\RPN.h" />
<ClInclude Include="..\..\..\include\stack.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
Loading