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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.lo
*.o
*.obj
token

# Precompiled Headers
*.gch
Expand Down Expand Up @@ -256,4 +257,4 @@ _Pvt_Extensions
.paket/paket.exe

# FAKE - F# Make
.fake/
.fake/
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required (VERSION 3.8)

project ("Arithmetic")


set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)

set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE})

add_subdirectory ("gtest")
add_subdirectory ("samples")
add_subdirectory ("src")
add_subdirectory ("test")
1 change: 1 addition & 0 deletions gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_library(gtest STATIC gtest-all.cc)
26 changes: 25 additions & 1 deletion include/arithmetic.h
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
// ���������� ������� � ������� ��� ���������� �������������� ���������
#include <lexeme.h>
#include <string>
#include <vector>
#include <map>

using namespace std;


class TArithmeticExpression {
string infix;
vector<Lexeme> postfix;
vector<Lexeme> lexemes;
map<string, int> priority;
map<string, double> operands;
void Parse();
void ToPostfix();

public:
TArithmeticExpression(string infx);
string GetInfix() const;
vector<Lexeme> GetPostfix() const;
vector<string> GetOperands() const;
vector<string> GetSyntaxErrors() const;
double Calculate(const map<string, double>& values);
};
24 changes: 24 additions & 0 deletions include/lexeme.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string>

#define OPENING_BRACKET 0
#define CLOSING_BRACKET 1
#define OPERATION 2
#define NUMBER 3
#define VARIABLE 4
#define FUNCTION 5
#define CONSTANT 6
// "(" - 0; ")" - 1; арифм. операция - 2; число - 3; имя переменной - 4; функция - 5; константа - 6


struct Lexeme {
std::string token;
unsigned char type;


Lexeme() = default;

Lexeme(std::string lexeme_token, int lexeme_type) {
token = lexeme_token;
type = lexeme_type;
}
};
72 changes: 72 additions & 0 deletions include/some_functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// взял из лабораторной, написанной на 1 курсе
#include <stdlib.h>
#include <math.h>


double FirstSin(double x) {
return x;
}

int JumpSin(){ // шаг в цикле
return 2;
}

double NextSin(double x, int i) {
return -x*x/((i-1)*i);
}


double FirstCos(double x) {
return 1;
}

int JumpCos(){
return 2;
}

double NextCos(double x, int i) {
i--;
return -x*x/((i-1)*i);
}


double FirstExp(double x) {
return 1;
}

int JumpExp(){
return 1;
}

double NextExp(double x, int i) {
i--;
return x/i;
}


typedef double (First)(double);
typedef int (Jump)();
typedef double (Next)(double, int);


double Taylor(double x, int N, First f, Jump h, Next g) {
double prev, next, summ;
int i, jump;

prev = f(x); summ = 0.0;

jump = h();

for (i=1; i<N; i+=jump){
if (i == 1)
summ += prev;

else {
next = prev*g(x, i);
summ += next;
prev = next;
}
}

return summ;
}
77 changes: 68 additions & 9 deletions include/stack.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
// ���������� � ���������� ���������� �����
// ���� ������������ ��������:
// - ������� ��������,
// - ���������� ��������,
// - �������� �������� �������� (��� ��������)
// - �������� �� �������,
// - ��������� ���������� ��������� � �����
// - ������� �����
// ��� ������� � ������ ���� ������ �������������� ������
#include <cstddef>
#include <stdexcept>
#include <algorithm>


template <typename T>
class TDynamicStack {
private:
int top;
size_t mem_size;
T* pMem;

public:
TDynamicStack() {
top = -1;
mem_size = 1;
pMem = new T[mem_size];
}


~TDynamicStack() {
delete[] pMem;
}


size_t size() const {
return top + 1;
}


bool IsEmpty() const {
return (top == -1);
}


void Pop() {
if (this->IsEmpty())
throw std::out_of_range("Index error");

top--;
}


T At() {
if (this->IsEmpty())
throw std::out_of_range("Index error");

return pMem[top];
}


void Push(const T& val) {
if (top == mem_size - 1) {
T* tmpMem = new T[mem_size*2];
std::copy(pMem, pMem + mem_size, tmpMem);
delete[] pMem;
pMem = tmpMem;
mem_size *= 2;
}

pMem[++top] = val;
}


void Clear() {
top = -1;
}
};
7 changes: 7 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(SRC main_arithmetic.cpp)

include_directories(../include)


add_executable(main ${SRC})
target_link_libraries(main arithmetic)
74 changes: 70 additions & 4 deletions samples/main_arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
// реализация пользовательского приложения
#include <arithmetic.h>
#include <iostream>

int main()
{
return 0;
using namespace std;


int main() {
string text_expr = "sqrt(b^2 - 4*a*c)";
TArithmeticExpression expr(text_expr);
vector<string> ops;
map<string, double> values;
double value;

int choice;

do {
cout << "Current expression: " << expr.GetInfix() << "\n\n1. Set new expression\n2. Set variables and calculate\n0. Quit\n>> ";

cin >> choice;

switch (choice) {
case 0:
break;

case 1:
cout << "\nNew expression: ";
cin.ignore();
getline(cin, text_expr);

try {
expr = TArithmeticExpression(text_expr);
}

catch (string error_text) {
cerr << error_text;
cout << "\n\n";
}

break;

case 2:
ops = expr.GetOperands();
values.clear();

cout << "\n";

for (string op: ops) {
cout << "Enter value of " << op << ": ";
cin >> value;
values[op] = value;
}

try {
cout << "\nResult: " << expr.Calculate(values) << "\n\n";
}

catch (string error_text) {
cerr << error_text;
cout << "\n\n";
}

break;

default:
cout << "Wrong action!\n\n";
break;
}
}
while (choice);

return 0;
}
38 changes: 0 additions & 38 deletions sln/vc10/arithmetic.sln

This file was deleted.

Loading