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

Краснопевцева ЛР 3 #18

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.0)

set(CMAKE_CXX_STANDARD 17)

project(arithmetic)

set(MP2_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)

include_directories(${MP2_INCLUDE} gtest)

add_subdirectory(gtest)
add_subdirectory(test)
add_subdirectory(samples)
add_subdirectory(src)
3 changes: 3 additions & 0 deletions gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(target "gtest")

add_library(${target} STATIC gtest-all.cc)
60 changes: 59 additions & 1 deletion include/arithmetic.h
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
// ���������� ������� � ������� ��� ���������� �������������� ���������
// объявление функций и классов для вычисления арифметических выражений
#pragma once
#include "stack.h"
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

bool SurchChar(char arr[], char ch, int size);
int GetType(char c);
int GetType(string c);

class Lexema {
string str;
int type;
public:
Lexema() : str(""), type(0) {}
Lexema(string s, int t) {
str = s;
type = GetType(str);
}
string GetStr() const { return str; }
int GetT() const { return type; }
void SetStr(string name) { str = name; }
void SetType(int i) { type = i; }
void PrintStr() const {
cout << str;
}
bool operator ==(const Lexema& v) const
{
if ((str == v.str) && (type == v.type))
return true;
else return false;
}
};

class Arithmetic {
string infix;
string postfix;
vector <Lexema> input;
vector <Lexema> Vpostfix;
map <char, int> priority;
map <string, double> operands;
void Parse();
void ToPostfix();
public:
Arithmetic(string str);
void CheckCharacters() const;
void Check() const;
vector <Lexema> GetInfixLexems() const;
vector <Lexema> GetPostfixLexems() const;
string GetInfix() const;
string GetPostfix() const;
void PrintInfixLexems();
void PrintPostfixLexems();
double Calculate(const map <string, double>& values);
vector <string> GetOperands();
};
51 changes: 42 additions & 9 deletions include/stack.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
// ���������� � ���������� ���������� �����
// ���� ������������ ��������:
// - ������� ��������,
// - ���������� ��������,
// - �������� �������� �������� (��� ��������)
// - �������� �� �������,
// - ��������� ���������� ��������� � �����
// - ������� �����
// ��� ������� � ������ ���� ������ �������������� ������
// ���������� � ���������� ���������� �����
// ���� ������������ ��������:
// - ������� ��������,
// - ���������� ��������,
// - �������� �������� �������� (��� ��������)
// - �������� �� �������,
// - ��������� ���������� ��������� � �����
#pragma once
#ifndef _Stack_H_
#define _Stack_H_

#include <iostream>
#include <vector>
//using namespace std;

template<typename T>
class TVectorStack {
int top; //������ ��������
std::vector<T> mem;
public:
TVectorStack() : top(-1) { }
size_t size() const { return top + 1; } //������� ����� �����
bool IsEmpty() const { return top == -1; } //������� ����� ��
void Push(const T& val) { //��������� ������
mem.push_back(val); top++;
}
T Pop() { //������� ������
if (IsEmpty()) {
throw std::out_of_range("try to get element from empty stack");
}
T val = mem.back();
mem.pop_back(); top--; return val;
}
T Top() { //������� ��������
if (IsEmpty()) {
throw std::out_of_range("try to check element from empty stack");
}
return mem.back();
}
};

#endif
7 changes: 7 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(name "sample")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../include")

add_executable(${name} main_arithmetic.cpp)

target_link_libraries (${name} "src")
37 changes: 31 additions & 6 deletions samples/main_arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
// реализация пользовательского приложения

int main()
{
return 0;
}
// реализация пользовательского приложения
#include "stack.h"
#include "arithmetic.h"
using namespace std;

int main() {
setlocale(LC_ALL, "Russian");
std::cout << "enter expression : ";
string in;
getline(cin, in);
std::cout << std::endl;
Arithmetic expr(in);
std::cout << expr.GetInfix() << std::endl;
cout << endl;
std::cout << expr.GetPostfix() << std::endl;
expr.PrintPostfixLexems();
vector<string> operands = expr.GetOperands();
double val;
map<string, double> values;
for (const auto& op : operands) {
if (GetType(op) == 2)
values[op] = std::stod(op);
else {
std::cout << "enter value of " << op << ": ";
std::cin >> val;
values[op] = val;
}
}
cout << "result: " << expr.Calculate(values) << endl;
return 0;
}
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(target_src "src")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../include")

file(GLOB src "arithmetic.cpp")

add_library(${target_src} STATIC ${src})
Loading