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

lab_2 #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions Makefile.txt
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 0 additions & 11 deletions README.md

This file was deleted.

164 changes: 164 additions & 0 deletions calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <iostream>
#include <cstring>
#include <fstream>
#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;
}
5 changes: 5 additions & 0 deletions calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int TypeOfOperation(char);

double PerformingOperation(double, double, int);


11 changes: 11 additions & 0 deletions f1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "calculator.h"
#include <cmath>

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;
}
12 changes: 12 additions & 0 deletions f2.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 + 2
Binary file added second_lab
Binary file not shown.