Skip to content
Open

Mock #13

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
49 changes: 49 additions & 0 deletions Mock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
16 changes: 16 additions & 0 deletions Mock/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Mock/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Mock/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Mock/Mock.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.8" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:r05" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-all:1.9.5" level="project" />
</component>
</module>
47 changes: 47 additions & 0 deletions Mock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.spbau.mit.kazakov</groupId>
<artifactId>Mock</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r05</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mit.spbau.kazakov.arithmetic;

import ru.mit.spbau.kazakov.exception.ArithmeticException;
import ru.mit.spbau.kazakov.util.Stack;

import java.util.List;

/**
* A class for evaluating arithmetic expressions.
* Operators and operands must be separated by whitespace character.
* Supported operators: +, -, *, /, %, ^
* Don't use brackets for unary minus.
*/
public class ArithmeticCalculator {
public static void main(String[] args) {
String expression = String.join(" ", args);
PolishNotationCalculator calculator = new PolishNotationCalculator(new Stack<>());

try {
List<String> polishNotation = ExpressionParser.toPolishNotation(expression);
System.out.println(calculator.evaluate(polishNotation));
} catch (ArithmeticException exception) {
System.out.println(exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package ru.mit.spbau.kazakov.arithmetic;

import org.jetbrains.annotations.NotNull;
import ru.mit.spbau.kazakov.exception.ArithmeticException;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
* A class with arithmetic utilities.
*/
public class ArithmeticUtility {
private static final Map<String, Operator> OPERATORS = new HashMap<>();

static {
OPERATORS.put("+", Operator.ADDITION);
OPERATORS.put("-", Operator.SUBTRACTION);
OPERATORS.put("*", Operator.MULTIPLICATION);
OPERATORS.put("/", Operator.DIVISION);
OPERATORS.put("%", Operator.MODULO);
OPERATORS.put("^", Operator.EXPONENTIATION);
}

private enum Operator {
ADDITION(0, true) {
@Override
public double apply(double augend, double addend) {
return augend + addend;
}
},
SUBTRACTION(0, true) {
@Override
public double apply(double minuend, double subtrahend) {
return minuend - subtrahend;
}
},
MULTIPLICATION(5, true) {
@Override
public double apply(double multiplier, double multiplicand) {
return multiplier * multiplicand;
}
},
DIVISION(5, true) {
@Override
public double apply(double dividend, double divisor) throws ArithmeticException {
if (divisor == 0) {
throw new ArithmeticException("Division by zero");
}
return dividend / divisor;
}
},
MODULO(5, true) {
@Override
public double apply(double dividend, double divisor) {
return dividend % divisor;
}
},
EXPONENTIATION(10, false) {
@Override
public double apply(double base, double exponent) {
return Math.pow(base, exponent);
}
};

private final int precedence;
private final boolean isLeftAssociative;

Operator(int precedence, boolean isLeftAssociative) {
this.precedence = precedence;
this.isLeftAssociative = isLeftAssociative;
}

public abstract double apply(double operand1, double operand2) throws ArithmeticException;
}

private static final Pattern IS_NUMERIC = Pattern.compile("[+-]?\\d+(\\.\\d+)?");

/**
* Checks if specified token is a number.
*
* @param token to check
* @return true if specified token is a number, and false otherwise
*/
public static boolean isNumeric(@NotNull String token) {
return IS_NUMERIC.matcher(token).matches();
}

/**
* Checks if specified token is an operator.
*
* @param token to check
* @return true if specified token is an operator, and false otherwise
*/
public static boolean isOperator(@NotNull String token) {
return OPERATORS.containsKey(token);
}

/**
* Checks if specified operator is left associative.
*
* @param operator to check
* @return true if specified operator is left associative, and false otherwise
*/
public static boolean isLeftAssociative(@NotNull String operator) throws ArithmeticException {
if (!ArithmeticUtility.isOperator(operator)) {
throw new ArithmeticException("Unsupported operator: " + operator);
}
return OPERATORS.get(operator).isLeftAssociative;
}

/**
* Compares precedences of specified operators.
*
* @param operator1 the first operator
* @param operator2 the second operator
* @return negative number if first operator's precedence is smaller than second operator's precedence,
* zero if precedences are equal, and positive number otherwise.
*/
public static int comparePrecedences(@NotNull String operator1, @NotNull String operator2) throws ArithmeticException {
if (!isOperator(operator1)) {
throw new ArithmeticException("Unsupported operator: " + operator1);
}
if (!isOperator(operator2)) {
throw new ArithmeticException("Unsupported operator: " + operator2);
}
return OPERATORS.get(operator1).precedence - OPERATORS.get(operator2).precedence;
}

/**
* Determines which of two operators evaluates first.
*
* @param operator1 the first operator
* @param operator2 the second operator
* @return true if the second operator evaluates before the first one, and false otherwise
* @throws ArithmeticException if there is unsupported operator
*/
public static boolean isGoesAfter(@NotNull String operator1, @NotNull String operator2) throws ArithmeticException {
return (isLeftAssociative(operator1) && comparePrecedences(operator1, operator2) <= 0)
|| (!isLeftAssociative(operator1) && comparePrecedences(operator1, operator2) < 0);
}

/**
* Applies specified operator to specified arguments.
*
* @param operator operator for applying
* @param operand1 the first argument
* @param operand2 the second argument
* @return result of application
* @throws ArithmeticException if there is unsupported operator
*/
public static double apply(@NotNull String operator, double operand1, double operand2) throws ArithmeticException {
if (!isOperator(operator)) {
throw new ArithmeticException("Unsupported operator: " + operator);
}
return OPERATORS.get(operator).apply(operand1, operand2);
}
}
Loading