diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..78749ca --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(second_lab) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(second_lab calculator.cpp f2.cpp calculator.h f1.cpp) diff --git a/Makefile.txt b/Makefile.txt new file mode 100644 index 0000000..3b85fda --- /dev/null +++ b/Makefile.txt @@ -0,0 +1,17 @@ +TARGET = second_lab +CC=g++ +CFLAGS = -c + +SOURCES = calculator.cpp f1.cpp f2.cpp +OBJ = $(SOURCES: .cpp=.o) + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CC) $(OBJ) -o $(TARGET) + +%.o : %.cpp + $(CC) $(CFLAGS) $< -o $@ + +clean: + rm -rf *.o $(TARGET) \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 406a423..0000000 --- a/README.md +++ /dev/null @@ -1,11 +0,0 @@ -Это нужно форкнуть! - - - -Оценки: - -| Фамилия, Имя | Группа | Оценка (предв)| -|--------------|:-----------:|---------------:| -|Иванов Иван | 213 | 10 | - - diff --git a/calculator.cpp b/calculator.cpp new file mode 100644 index 0000000..b8339dc --- /dev/null +++ b/calculator.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include "calculator.h" + +using std::cout; +using std::cin; +using std::endl; + +int main(int argc, char *argv[]) { + int i = 0, j = 0, n = 50; + double *numbers = new double[n], value, res; + char *signs = new char[n], arg = ' ', end = ' '; + bool first_flag = true; + if (argc == 2 && (strcmp(argv[1], "--forward") == 0 || strcmp(argv[1], "--reverse") == 0)) { + end = '\n'; + while (arg != end) { + arg = cin.peek(); + if (arg == ' ') { + cin.ignore(); + continue; + } + if (arg >= '0' && arg <= '9') { + cin >> value; + numbers[i] = value; + i += 1; + continue; + } + if (arg == '+' || arg == '-' || arg == '*' || arg == '/' || arg == '(' || arg == ')' || arg == '^') { + if (j == 0 || (abs(TypeOfOperation(arg)) > abs(TypeOfOperation(signs[j - 1]))) || + TypeOfOperation(arg) == 1) { + if ((j == 0 || j > 0 && TypeOfOperation(arg) == -2 && TypeOfOperation(signs[j - 1]) == 1) && + TypeOfOperation(arg) == -2 && strcmp(argv[1], "--forward") == 0) { + numbers[i] = 0; + i += 1; + signs[j] = arg; + j += 1; + } else { + signs[j] = arg; + j += 1; + } + cin.ignore(); + } else { + if (TypeOfOperation(arg) == -1) { + while (abs(TypeOfOperation(arg)) != abs(TypeOfOperation(signs[j - 1]))) { + res = PerformingOperation(numbers[i - 2], numbers[i - 1], TypeOfOperation(signs[j - 1])); + numbers[i - 2] = res; // i - 2 + j -= 1; + i -= 1; + } + j -= 1; + cin.ignore(); + } else { + while (first_flag && abs(TypeOfOperation(arg)) <= abs(TypeOfOperation(signs[j - 1]))) { + res = PerformingOperation(numbers[i - 2], numbers[i - 1], TypeOfOperation(signs[j - 1])); + numbers[i - 2] = res; // i - 2 + j -= 1; + i -= 1; + if (j == 0) { + signs[j] = arg; + j += 1; + cin.ignore(); + first_flag = false; + } + } + } + + } + continue; + } + } + + if (j == 0) { + cout << numbers[0] << endl; + } else { + j -= 1; + i -= 1; + while (j != -1) { + res = PerformingOperation(numbers[i - 1], numbers[i], TypeOfOperation(signs[j])); + numbers[i - 1] = res; + j -= 1; + i -= 1; + } + cout << numbers[0] << endl; + } + } else if (argc >= 2 && strcmp("--file", argv[2]) == 0) { + std::ifstream input(argv[3]); + cin.rdbuf(input.rdbuf()); + end = EOF; + while (arg != end) { + arg = cin.peek(); + if (arg == ' ') { + cin.ignore(); + continue; + } + if (arg >= '0' && arg <= '9') { + cin >> value; + numbers[i] = value; + i += 1; + continue; + } + if (arg == '+' || arg == '-' || arg == '*' || arg == '/' || arg == '(' || arg == ')' || arg == '^') { + if (j == 0 || (abs(TypeOfOperation(arg)) > abs(TypeOfOperation(signs[j - 1]))) || + TypeOfOperation(arg) == 1) { + if ((j == 0 || j > 0 && TypeOfOperation(arg) == -2 && TypeOfOperation(signs[j - 1]) == 1) && + TypeOfOperation(arg) == -2 && + strcmp(argv[1], "--forward") == 0) { + numbers[i] = 0; + i += 1; + signs[j] = arg; + j += 1; + } else { + signs[j] = arg; + j += 1; + } + cin.ignore(); + } else { + if (TypeOfOperation(arg) == -1) { + while (abs(TypeOfOperation(arg)) != abs(TypeOfOperation(signs[j - 1]))) { + res = PerformingOperation(numbers[i - 2], numbers[i - 1], TypeOfOperation(signs[j - 1])); + numbers[i - 2] = res; // i - 2 + j -= 1; + i -= 1; + } + j -= 1; + cin.ignore(); + } else { + while (first_flag && abs(TypeOfOperation(arg)) <= abs(TypeOfOperation(signs[j - 1]))) { + res = PerformingOperation(numbers[i - 2], numbers[i - 1], TypeOfOperation(signs[j - 1])); + numbers[i - 2] = res; // i - 2 + j -= 1; + i -= 1; + if (j == 0) { + signs[j] = arg; + j += 1; + cin.ignore(); + first_flag = false; + } + } + } + + } + continue; + } + } + + if (j == 0) { + cout << numbers[0] << endl; + } else { + j -= 1; + i -= 1; + while (j != -1) { + res = PerformingOperation(numbers[i - 1], numbers[i], TypeOfOperation(signs[j])); + numbers[i - 1] = res; + j -= 1; + i -= 1; + } + cout << numbers[0] << endl; + } + input.close(); + } + delete[] numbers; + delete[] signs; +} \ No newline at end of file diff --git a/calculator.h b/calculator.h new file mode 100644 index 0000000..456760a --- /dev/null +++ b/calculator.h @@ -0,0 +1,5 @@ +int TypeOfOperation(char); + +double PerformingOperation(double, double, int); + + diff --git a/f1.cpp b/f1.cpp new file mode 100644 index 0000000..a3e0707 --- /dev/null +++ b/f1.cpp @@ -0,0 +1,11 @@ +#include "calculator.h" +#include + +double PerformingOperation(double number_1, double number_2, int type_of_operation) { + if (type_of_operation == 2) return number_1 + number_2; + if (type_of_operation == -2) return number_1 - number_2; + if (type_of_operation == 3) return number_1 * number_2; + if (type_of_operation == -3) return number_1 / number_2; + if (type_of_operation == 4) return pow(number_1, number_2); + else return 0; +} \ No newline at end of file diff --git a/f2.cpp b/f2.cpp new file mode 100644 index 0000000..75c5d72 --- /dev/null +++ b/f2.cpp @@ -0,0 +1,12 @@ +#include "calculator.h" + +int TypeOfOperation(char sign) { + if (sign == '+') return 2; + if (sign == '-') return -2; + if (sign == '*') return 3; + if (sign == '/') return -3; + if (sign == '^') return 4; + if (sign == '(') return 1; + if (sign == ')') return -1; + else return 0; +} diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..0c1faa8 --- /dev/null +++ b/input.txt @@ -0,0 +1 @@ +2 + 2 \ No newline at end of file diff --git a/second_lab b/second_lab new file mode 100644 index 0000000..9befa8f Binary files /dev/null and b/second_lab differ