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
42 changes: 42 additions & 0 deletions homework-g597-bogdanov/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g597-bogdanov</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
Expand All @@ -19,6 +24,43 @@
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>net.sourceforge.jeval</groupId>
<artifactId>jeval</artifactId>
<version>0.9.4</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.3.13</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>

<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ private void dumpMemtable() {
}

private void clearDeletedElements() {
dumpMemtable();
readWriteLock.writeLock().lock();
dumpMemtable();
try {
HashMap<K, Long> updatedOffsets = new HashMap<>();
File tmpFileForValues = new File(path + File.separator + this.fileName + "_values");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
curl http://localhost:9001/eval -X POST -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "44*3+2"

curl http://localhost:9001/eval -X POST -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "44*3+2"

curl http://localhost:9001/variable/mlgrank -X PUT -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "1"

curl http://localhost:9001/variable/mlgrank -X PUT -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "99999"

curl http://localhost:9001/variable/mlgrank -X GET -H "Authorization: Basic $(echo -n "username:password" | base64)"

curl http://localhost:9001/variable/mlgrank -X GET -H "Authorization: Basic $(echo -n "username:password" | base64)"

curl http://localhost:9001/variable/ml -X PUT -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "max(1.010, 1.009)"

curl http://localhost:9001/function/myfunc?args=x&args=y -X PUT -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "pow(2, x)"

curl http://localhost:9001/function/myfunc1?args=x,y -X PUT -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "log2(x) + y"

curl http://localhost:9001/eval -X POST -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "15 + myfunc1(64, 4)"

curl http://localhost:9001/eval -X POST -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)" --data-raw "15 + myfunc1(64, 4)"

curl http://localhost:9001/function/myfunc1 -X GET -H "Content-Type: text/plain" -H "Authorization: Basic $(echo -n "username:password" | base64)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.mipt.java2016.homework.g597.bogdanov.task4.CalculatorWithTokens;

/**
* Created by Semyo_000 on 20.12.2016.
*/
public class Token {
public enum TokenType {
PLUS, MINUS, MULTIPLY, DIVIDE,
NUMBER, NAME,
LEFT_BRACE, RIGHT_BRACE, COMMA,
UNKNOWN
}

private final TokenType type;
private Double number = null;
private String name = null;

public Token(TokenType type) {
this.type = type;
}

public Token(String name) {
this.name = name;
this.type = TokenType.NAME;
}

public Token(Double number) {
this.number = number;
this.type = TokenType.NUMBER;
}

public TokenType getType() {
return type;
}

public Double getNumber() {
return number;
}

public String getName() {
return name;
}

@Override
public String toString() {
return String.format("Type: %s, Number: %s, Name: %s \n", type, number, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package ru.mipt.java2016.homework.g597.bogdanov.task4.CalculatorWithTokens;

import ru.mipt.java2016.homework.base.task1.Calculator;
import ru.mipt.java2016.homework.base.task1.ParsingException;

import java.util.ArrayList;

/**
* Created by Semyo_000 on 20.12.2016.
*/
public class TokenCalculator implements Calculator {
private final ArrayList<Token> tokens = new ArrayList<>();
private int tokensIndex = 0;


private void parse(String expression) throws ParsingException {
if (expression == null) {
throw new ParsingException("Expression is null.");
}

for (int expressionIndex = 0; expressionIndex < expression.length(); ++expressionIndex) {
Token.TokenType currentTokenType;
Double currentNumber = null;

if (Character.isWhitespace(expression.charAt(expressionIndex)) ||
Character.isSpaceChar(expression.charAt(expressionIndex))) {
continue;
}

switch (expression.charAt(expressionIndex)) {
case '+':
currentTokenType = Token.TokenType.PLUS;
break;

case '-':
currentTokenType = Token.TokenType.MINUS;
break;

case '*':
currentTokenType = Token.TokenType.MULTIPLY;
break;

case '/':
currentTokenType = Token.TokenType.DIVIDE;
break;

case '(':
currentTokenType = Token.TokenType.LEFT_BRACE;
break;

case ')':
currentTokenType = Token.TokenType.RIGHT_BRACE;
break;

default:
if (!Character.isDigit(expression.charAt(expressionIndex))) {
throw new ParsingException(String.format("Unexpected symbol at %d", expressionIndex));
}

boolean readDot = false;
int numberStartIndex = expressionIndex;
for (; expressionIndex < expression.length(); ++expressionIndex) {
Character currentCharacter = expression.charAt(expressionIndex);
if (currentCharacter == '.' && !readDot) {
readDot = true;
} else if (!Character.isDigit(currentCharacter)) {
break;
}
}

currentNumber = Double.parseDouble(expression.substring(numberStartIndex, expressionIndex));
--expressionIndex;
currentTokenType = Token.TokenType.NUMBER;
break;
}

if (currentTokenType != Token.TokenType.NUMBER) {
tokens.add(new Token(currentTokenType));
} else {
tokens.add(new Token(currentNumber));
}
}
}

private void regressTokensIndex() {
--tokensIndex;
}

private Token progressTokens() {
if (tokensIndex >= tokens.size()) {
return null;
}

return tokens.get(tokensIndex++);
}

private Double expression() throws ParsingException {
Double result = multiple();

for (Token token = progressTokens(); token != null; token = progressTokens()) {
if (token.getType() == Token.TokenType.PLUS) {
result += multiple();
} else if (token.getType() == Token.TokenType.MINUS) {
result -= multiple();
} else {
regressTokensIndex();
break;
}
}

return result;
}

private Double multiple() throws ParsingException {
Double result = bracedExpression();

for (Token token = progressTokens(); token != null; token = progressTokens()) {
if (token.getType() == Token.TokenType.MULTIPLY) {
result *= bracedExpression();
} else if (token.getType() == Token.TokenType.DIVIDE) {
result /= bracedExpression();
} else {
regressTokensIndex();
break;
}
}

return result;
}

private Double bracedExpression() throws ParsingException {
Double result;

Token token = progressTokens();
if (token == null) {
throw new ParsingException("Invalid amount of numbers.");
}

if (token.getType() == Token.TokenType.LEFT_BRACE) {
result = expression();
token = progressTokens();

if (token == null || token.getType() != Token.TokenType.RIGHT_BRACE) {
throw new ParsingException("Wrong number of left/right braces");
}
} else {
regressTokensIndex();
result = numberExpression();
}

return result;
}

private Double numberExpression() throws ParsingException {
Double result;

Token token = progressTokens();
if (token == null) {
throw new ParsingException("Invalid amount of numbers");
}

if (token.getType() == Token.TokenType.MINUS) {
result = -expression();
} else if (token.getType() == Token.TokenType.NUMBER) {
result = token.getNumber();
} else {
throw new ParsingException("Invalid order of operations");
}

return result;
}

public double calculate(String expression) throws ParsingException {
tokens.clear();
tokensIndex = 0;

parse(expression);
Double result = expression();

if (tokensIndex != tokens.size()) {
throw new ParsingException("Invalid number of tokens (too many).");
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.mipt.java2016.homework.g597.bogdanov.task4.REST;

import ru.mipt.java2016.homework.base.task1.ParsingException;
import ru.mipt.java2016.homework.g597.bogdanov.task4.REST.functions.CalculatorFunctionObject;

import java.util.List;

/**
* Created by Semyo_000 on 20.12.2016.
*/
public interface IFunctionalCalculator {

Double getVariable(String variableAlias);

boolean putVariable(String variableAlias, Double value);

boolean deleteVariable(String variableAlias);

List<String> getVariableList();

CalculatorFunctionObject getFunction(String functionAlias);

boolean putFunction(String functionAlias, String expression, List<String> arguments);

boolean deleteFunction(String functionAlias);

List<String> getFunctionList();

Double calculate(String expression) throws ParsingException;
}
Loading