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
64 changes: 64 additions & 0 deletions homework-g599-lantsetov/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mipt-java-2016</artifactId>
<groupId>ru.mipt.java2016</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g599-lantsetov</artifactId>
<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<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.5.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>

<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package ru.mipt.java2016.homework.g599.lantsetov.task1;

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


import java.util.Stack;
import java.util.Arrays;
import java.util.Scanner;
import java.util.HashSet;
import java.security.InvalidParameterException;


class MyCalculator implements Calculator {
static final Calculator INSTANCE = new MyCalculator();
private static final HashSet<String> OPERATORS = new HashSet<>(Arrays.asList("+", "-", "*", "/"));

@Override
public double calculate(String expression) throws ParsingException, ArithmeticException {
if (expression == null) {
throw new ParsingException("Null string");
}
String rpn = buildRPN(expression);
return eval(rpn);
}

private static double count(String operator, double op1, double op2) throws InvalidParameterException {
switch (operator) {
case "+": return op1 + op2;
case "-": return op1 - op2;
case "*": return op1 * op2;
case "/": return op1 / op2;
default: return 0;
}
}

private static int getPriority(String operator) throws InvalidParameterException {
switch (operator) {
case "+": return 1;
case "-": return 1;
case "*": return 2;
case "/": return 2;
case "(": return 0;
case ")": return 0;
case "#": return 3;
default: return -1;
}
}

private double eval(String reverseNotation) throws ParsingException {
Stack<Double> operands = new Stack<>();
Double a;
Double b;
Double result = 0.0;
String token;
try (Scanner sc = new Scanner(reverseNotation)) {
while (sc.hasNext()) {
token = sc.next();
if (OPERATORS.contains(token)) {
if (operands.size() < 2) {
throw new ParsingException("Incorrect expression");
}
b = operands.pop();
a = operands.pop();
try {
result = count(token, a, b);
} finally {
operands.push(result);
}
} else if (token.equals("#")) {
if (operands.size() < 1) {
throw new ParsingException("Invalid expression: expected number");
} else {
double operand = operands.pop();
operands.push(-1 * operand);
}
} else {
try {
a = Double.parseDouble(token);
operands.push(a);
} catch (NumberFormatException npe) {
throw new ParsingException(String.format("Invalid operand: %s", token));
}
}
}
if (operands.size() != 1) {
throw new ParsingException("Incorrect expression");
} else {
return operands.pop();
}
}
}

private String buildRPN(String expression) throws ParsingException {
StringBuilder answer = new StringBuilder();
Stack<String> operators = new Stack<>();
Character c;
boolean unary = true;
boolean prevIsDot = false;
String prevUnary = "";
int searchCondition = 0;
for (int i = 0; i < expression.length(); ++i) {
c = expression.charAt(i);
if (prevIsDot && !(Character.isDigit(c))) {
throw new ParsingException("Invalid expression: unexpected token after dot");
}
if (Character.isDigit(c)) {
unary = false;
answer.append(c);
searchCondition = 1;
prevIsDot = false;
} else if (Character.isWhitespace(c)) {
answer.append(' ');
} else if (c.equals('.')) {
if (searchCondition == 0) {
throw new ParsingException("Invalid expression: unexpected symbol '.'");
} else {
answer.append('.');
searchCondition = 1;
}
prevIsDot = true;
} else if (c.equals('(')) {
if (searchCondition == 1) {
throw new ParsingException("Invalid expression: unexpected symbol '('");
} else {
unary = true;
answer.append(' ');
operators.push("(");
searchCondition = 0;
}
prevIsDot = false;
} else if (c.equals(')')) {
if (searchCondition == 0) {
throw new ParsingException("Invalid expression: unexpected ) after operator");
} else {
boolean hasCorrespondingOpeningBracket = false;
while (!(operators.empty())) {
String curOperator = operators.pop();
if (curOperator.equals("(")) {
hasCorrespondingOpeningBracket = true;
break;
} else {
answer.append(' ').append(curOperator).append(' ');
}
}
if (!hasCorrespondingOpeningBracket) {
throw new ParsingException("Invalid expression: invalid brackets");
}

}
prevIsDot = false;
} else if (OPERATORS.contains(c.toString())) {
if (unary) {
switch (c.toString()) {
case "-":
operators.push("#");
case "+":
if (prevUnary.equals("+")) {
throw new ParsingException("Invalid expression: unexpected+");
}
unary = true;
break;
default:
throw new ParsingException(
String.format("Invalid expression: invalid unary operator: %c", c));
}
prevUnary = c.toString();
} else {
unary = true;
answer.append(' ');
int curOperatorPriority = getPriority(c.toString());
while (!(operators.empty()) && curOperatorPriority <= getPriority(operators.peek())) {
answer.append(' ').append(operators.pop()).append(' ');
}
operators.push(c.toString());
}

searchCondition = 0;
} else {
throw new ParsingException(String.format("Invalid expression: unexpected %s", c.toString()));
}
}
while (!operators.empty()) {
String curOperator = operators.pop();
if (OPERATORS.contains(curOperator) || curOperator == "#") {
answer.append(String.format(" %s ", curOperator));
} else {
throw new ParsingException(String.format("Invalid expression: unexpected %s", curOperator));
}
}
return answer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.mipt.java2016.homework.g599.lantsetov.task2;

import java.io.IOException;
import java.io.RandomAccessFile;


public class DoubleSerializer implements MySerializer<Double> {
@Override
public Double read(RandomAccessFile file) throws IOException {
return file.readDouble();
}

@Override
public void write(RandomAccessFile file, Double arg) throws IOException {
file.writeDouble(arg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.mipt.java2016.homework.g599.lantsetov.task2;

import java.io.IOException;
import java.io.RandomAccessFile;


public class IntegerSerializer implements MySerializer<Integer> {
@Override
public Integer read(RandomAccessFile file) throws IOException {
return file.readInt();
}

@Override
public void write(RandomAccessFile file, Integer arg) throws IOException {
file.writeInt(arg);
}
}
Loading