From bf71e6acfae3c335f73e6bf4090babbda5eaa6ef Mon Sep 17 00:00:00 2001 From: neko-nyashka Date: Wed, 16 Nov 2022 20:16:42 +0300 Subject: [PATCH] final --- Stack.cpp | 65 ++++++++++++++++++ Stack.h | 20 ++++++ calculate.cpp | 26 ++++++++ calculate.h | 4 ++ main.cpp | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 11 +++ 6 files changed, 306 insertions(+) create mode 100644 Stack.cpp create mode 100644 Stack.h create mode 100644 calculate.cpp create mode 100644 calculate.h create mode 100644 main.cpp create mode 100644 makefile diff --git a/Stack.cpp b/Stack.cpp new file mode 100644 index 0000000..264472d --- /dev/null +++ b/Stack.cpp @@ -0,0 +1,65 @@ +#include +#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; +} diff --git a/Stack.h b/Stack.h new file mode 100644 index 0000000..b604963 --- /dev/null +++ b/Stack.h @@ -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(); +}; + diff --git a/calculate.cpp b/calculate.cpp new file mode 100644 index 0000000..3240d2a --- /dev/null +++ b/calculate.cpp @@ -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; +} \ No newline at end of file diff --git a/calculate.h b/calculate.h new file mode 100644 index 0000000..fd52ad3 --- /dev/null +++ b/calculate.h @@ -0,0 +1,4 @@ +bool isdigit(char b); +int perform(int a, int b, char op); +int precedence(char op); + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..dd7d2d4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#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