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

Труфанова Анастасия СКБ221 #40

Open
wants to merge 1 commit 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
65 changes: 65 additions & 0 deletions Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <cstdlib>
#define SIZE 100

class Stack {
int * arr;
int top;
int capacity;

public:
Stack(int size = SIZE);
~Stack();

void push(int);
int pop();
int peek();

int size();
bool empty();
bool isFull();


};
Stack::Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}

Stack::~Stack() {
delete[] arr;
}

void Stack::push(int x) {
if (isFull()) {
exit(EXIT_FAILURE);
}
arr[++top] = x;
}
int Stack::pop() {
if (empty()) {
exit(EXIT_FAILURE);
}
return arr[top--];
}

int Stack::peek() {
if (!empty()) {
return arr[top];
}
else {
exit(EXIT_FAILURE);
}
}

int Stack::size() {
return top + 1;
}

bool Stack::empty() {
return top == -1;
}

bool Stack::isFull() {
return top == capacity - 1;
}
20 changes: 20 additions & 0 deletions Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define SIZE 100

class Stack {
int * arr;
int top;
int capacity;

public:
Stack(int size = SIZE);
~Stack();

void push(int);
int pop();
int peek();

int size();
bool empty();
bool isFull();
};

26 changes: 26 additions & 0 deletions calculate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
bool isdigit(char b){
if ('0'<=b && b<='9'){
return true;
}
else{
return false;
}
}
int perform(int a, int b, char op){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default:
break;
}
return 0;
}
int precedence(char op){
if(op == '+'||op == '-')
return 1;
if(op == '*'||op == '/')
return 2;
return 0;
}
4 changes: 4 additions & 0 deletions calculate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bool isdigit(char b);
int perform(int a, int b, char op);
int precedence(char op);

180 changes: 180 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include <iostream>
#include <cstring>
#include <fstream>
#include "calculate.h"
#include "Stack.h"
int calculator(const char *s){
Stack numbers;
Stack operations;
int j=0;

while(s[j]!='\0'){
j++;}
for(int i=0;i<=j;i++){
if (s[i]==' ')
continue;
else if (s[i] == '(')
operations.push(s[i]);
if(s[i+1]=='-'){
numbers.push(0);
}

else if (isdigit(s[i])) {
int numb = 0;
while ((s[i] != '\0') && (isdigit(s[i]))) {
numb = numb * 10 + (s[i] - '0');
i++;
}
i--;
numbers.push(numb);

}
else if (s[i] == ')') {
while (!operations.empty() && operations.peek() != '(') {
int numb2 = numbers.peek();
numbers.pop();
int numb1 = numbers.peek();
numbers.pop();
char op = operations.peek();
operations.pop();
numbers.push(perform(numb2, numb1, op));
}
if (!operations.empty()) {
operations.pop();
}
}
else {
if(s[i]=='-' && i==0){
numbers.push(0);
}

while (!operations.empty() && precedence(operations.peek()) >= precedence(s[i])) {
int numb2 = numbers.peek();
numbers.pop();
int numb1 = numbers.peek();
numbers.pop();
char op = operations.peek();
operations.pop();
numbers.push(perform(numb1, numb2, op));
}
operations.push(s[i]);
}

}

return numbers.peek();
}
int calculatorr(const char *s){
Stack numbers;
Stack operations;
int j=0;
while(s[j]!='\0' && s[j]!='\n'){
j++;}
for(int i=0;i<j;i++){
if (s[i]==' ')
continue;

else if (isdigit(s[i])) {
int numb = 0;
while (numbers.size()!=2 && (isdigit(s[i]))) {
numb = numb * 10 + (s[i] - '0');
i++;
}
i--;
numbers.push(numb);

}
else {
operations.push(s[i]);
while (!operations.empty()) {
int numb2 = numbers.peek();
numbers.pop();
int numb1 = numbers.peek();
numbers.pop();
char op = operations.peek();
operations.pop();
numbers.push(perform(numb1, numb2, op));
}
}
}

return numbers.peek();
}


int main(int argc, char ** argv){
std::string name;
char s[1000];
bool forward=false;
bool file=false;
bool reverse=false;
if (argc==1){
std::cerr<<"Nothing was read\n";
return EXIT_FAILURE;
}
else{
for (int i=1;i<argc;++i){
if(std::strcmp(argv[i],"--forward")==0){
forward=true;
}
if(std::strcmp(argv[i],"--file")==0){
if (!argv[i+1]){
std::cerr<<"File name not specified\n";
return EXIT_FAILURE;
}
else {
file=true;
name = argv[i + 1];
}

}
if(std::strcmp(argv[i],"--reverse")==0){
reverse=true;
}
}

if (file && forward){
std::cerr<<"You should choose only file or only forward\n";
return EXIT_FAILURE;
}
if (file && reverse){
std::ifstream f(name);
f.getline(s, 1000);
f.close();
std::cout<<s<<std::endl;
std::cout<<calculatorr(s) << '\n';
}
else if (file){
std::ifstream f(name);
f.getline(s, 1000);
f.close();
std::cout<<s<<std::endl;
std::cout<<calculator(s) << '\n';
}
else if(forward){
char a[1000]={0};
char token;
int i=0;
while(std::cin.get(token)) {
if (token == '\n') {
break;
}
a[i] = token;
i++;
}
if(reverse){
std::cout<<calculatorr(a) << '\n';
}
else{
std::cout<<calculator(a) << '\n';
}

}
}


return 0;
}



11 changes: 11 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
out : main.o calculate.o Stack.o
g++ main.o Stack.o calculate.o -o out
main.o : main.cpp
g++ -c main.cpp -o main.o
Stack.o : Stack.cpp
g++ -c Stack.cpp -o Stack.o
calculate.o : calculate.cpp
g++ -c calculate.cpp -o calculate.o
clean :
rm out *.o