Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: cpp
only:
- calculator

addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.9
- g++-4.9

script:
- mkdir build && cd build && g++ -std=c++0x -o Calculator ../CalculatorSources/Source.cpp ../CalculatorSources/Calculator.cpp

notifications:
email:
- valera.malkof@gmail.com
217 changes: 217 additions & 0 deletions CalculatorSources/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#include"Calculator.h"
double Calculator::calculate(const char* s)
{
char strochka[255];
Stack<char> operations;
int i = 0;
while (*s)
{
while (!isCorrect(*s))s++;
if (isdigit(*s))
{
while (*s == '.' || isdigit(*s))
{
strochka[i] = *s;
i++;
s++;
}
strochka[i] = ' ';
i++;
if (!operations.isEmpty())
{
if (*s != '^'&&*s != '*'&&*s != '/')
{
if (operations.popReturn() == '-')
{
strochka[i] = operations.pop();
i++;
}
}

}
}
else if (*s == ')')
{

while (operations.popReturn() != '(')
{
strochka[i] = operations.pop();
i++;
}
s++;
if (*s == '^')
{
operations.push(*s);
s++;
if (isdigit(*s))
{
while (*s == '.' || isdigit(*s))
{
strochka[i] = *s;
i++;
s++;
}
}
strochka[i] = operations.pop();
i++;
}
if (*s == '/')
{
operations.push(*s);
s++;
if (isdigit(*s))
{
while (*s == '.' || isdigit(*s))
{
strochka[i] = *s;
i++;
s++;
}
}
strochka[i] = operations.pop();
i++;
}
if (*s == '*')
{
operations.push(*s);
s++;
if (isdigit(*s))
{
while (*s == '.' || isdigit(*s))
{
strochka[i] = *s;
i++;
s++;
}
}
strochka[i] = operations.pop();
i++;
}
operations.pop();
if (!operations.isEmpty())
{
if (operations.popReturn() == '*' || operations.popReturn() == '/' || operations.popReturn() == '-')
{
strochka[i] = operations.pop();
i++;
}
}

strochka[i] = ' ';
i++;
}
else if (*s == '(')
{
operations.push(*s);
s++;
}
else if (*s == '*' || *s == '/' || *s == '^')
{
operations.push(*s);
s++;
}
else if (*s == '+' || *s == '-')
{
char op;
if (!operations.isEmpty())
{
if (operations.popReturn() == '*' || operations.popReturn() == '/' || operations.popReturn() == '^')
{
while (!operations.isEmpty())
{
if (operations.popReturn() == '*' || operations.popReturn() == '/' || operations.popReturn() == '^')
{
strochka[i] = operations.pop();
i++;
}
else break;
}
operations.push(*s);
s++;
}
else
{
operations.push(*s);
s++;
}
}
else
{
operations.push(*s);
s++;
}
}
}
while (!operations.isEmpty())
{

strochka[i] = operations.pop();
i++;
}
if (strochka[i - 1] == ' ')
strochka[i - 1] = '\0';
else strochka[i] = '\0';
cout << endl;
return postFix(strochka);
}
double Calculator::postFix(const char* s)
{
double a = 0;
double b = 0;
Stack<double> numbers;
while (*s)
{
while (!isCorrect(*s))s++;
if (isdigit(*s))
{
double d = atof(s);
numbers.push(d);
while (*s == '.' || isdigit(*s))s++;
}
else if (*s == '+')
{
b = numbers.pop();
a = numbers.pop();
numbers.push(a + b);
s++;

}
else if (*s == '-')
{
b = numbers.pop();
a = numbers.pop();
numbers.push(a - b);
s++;
}
else if (*s == '*')
{
b = numbers.pop();
a = numbers.pop();
numbers.push(a * b);
s++;
}
else if (*s == '/')
{
b = numbers.pop();
a = numbers.pop();
numbers.push(a / b);
s++;
}
else if (*s == '^')
{
b = numbers.pop();
a = numbers.pop();
numbers.push(pow(a, b));
s++;
}
}

return numbers.pop();
}
bool Calculator::isCorrect(const char s)
{
return isdigit(s) || s == '.' ||
s == '+' || s == '-' || s == '*' ||
s == '/' || s == '^' || s == '(' ||
s == ')';
}
16 changes: 16 additions & 0 deletions CalculatorSources/Calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include"Stack.h"
#include<iostream>
#include <cmath>
using namespace std;
class Calculator
{
public:
static double calculate(const char* s);
private:
static double postFix(const char* s);
static bool isCorrect(const char s);
};

#endif;
Loading