Skip to content

Commit

Permalink
Merge branch 'master' into master-github
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh18RW committed Nov 2, 2024
2 parents 41e425d + 76a3390 commit ef953b9
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 48 deletions.
9 changes: 1 addition & 8 deletions src/main/java/ru/corvinella/App.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package ru.corvinella;

import ru.corvinella.parser.Parser;
import ru.corvinella.parser.ParserIllegalTokenValueException;
import ru.corvinella.parser.ParserUnknownEntityException;

public class App {
/**
* Main method for testing.
* @param args
* @throws ParserIllegalTokenValueException
* @throws ParserUnknownEntityException
*/
public static void main(String[] args) throws ParserUnknownEntityException, ParserIllegalTokenValueException {
public static void main(String[] args) {
// nothing here...
}
}

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/ru/corvinella/expressions/ExpressionState.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ru.corvinella.expressions;

import ru.corvinella.expressions.entries.*;
import ru.corvinella.tokens.TokenType;
import ru.corvinella.expressions.entries.ArgumentsExpression;
import ru.corvinella.expressions.entries.Expression;
import ru.corvinella.expressions.entries.SequenceExpression;

/**
* @author sh18rw
Expand Down Expand Up @@ -40,7 +41,6 @@ public enum ReadingType {
Sum, // reading minus and plus operations
Multiply, // reading multiply and divide operations
Degree, // reading only one expression
Parenthesis, // reading until close parenthesis
}

public enum ReadingTokenType {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ru/corvinella/expressions/ExpressionsTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ private void processToken(Token<?> token) {
expressionToAdd = new ConstantExpression(wordToken);
break;
case Log:
case Sin:
case Cos:
case Tg:
case Ctg:
FunctionExpression functionExpression = new FunctionExpression(wordToken);
ArgumentsExpression argumentsExpression = new ArgumentsExpression();
ExpressionState argumentsState = new ExpressionState(argumentsExpression, ExpressionState.ReadingType.Argument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public boolean equals(Object obj) {

public enum Constant {
Pi,
E,
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.corvinella.expressions.entries;

import ru.corvinella.tokens.Token;

/**
* @author sh18rw
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public FunctionExpression(WordToken token) {
case Log:
functionType = Function.Log;
break;
case Sin:
functionType = Function.Sin;
break;
case Cos:
functionType = Function.Cos;
break;
case Tg:
functionType = Function.Tg;
break;
case Ctg:
functionType = Function.Ctg;
default:
throw new IllegalStateException(
String.format("For some reason there is no realisation for %s function.", token.getValue()));
Expand Down Expand Up @@ -57,5 +68,9 @@ public String toString() {

public enum Function {
Log,
Sin,
Cos,
Tg,
Ctg
}
}
1 change: 0 additions & 1 deletion src/main/java/ru/corvinella/math/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.corvinella.math;

import ru.corvinella.expressions.entries.Expression;
import ru.corvinella.expressions.entries.SequenceExpression;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ru/corvinella/math/ConstantCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public Double calculate(ConstantExpression expression) {
switch (expression.getConstantType()) {
case Pi:
return Math.PI;
case E:
return Math.E;
}

return 0.0;
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/ru/corvinella/math/FunctionCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
public class FunctionCalculator implements ICalculator<FunctionExpression> {
@Override
public Double calculate(FunctionExpression expression) {
ArgumentsExpression argumentsExpression = expression.getArguments();

switch (expression.getFunctionType()) {
case Log:
ArgumentsExpression argumentsExpression = expression.getArguments();
if (argumentsExpression.getArgumentsCount() != 2) {
// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
Expand All @@ -32,9 +33,52 @@ public Double calculate(FunctionExpression expression) {
}

return Math.log(second) / Math.log(first);
case Sin:
case Cos:
case Tg:
case Ctg:
return calculateTrigonometric(expression);
}

// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
}

private Double calculateTrigonometric(FunctionExpression functionExpression) {
ArgumentsExpression argumentsExpression = functionExpression.getArguments();

if (argumentsExpression.getArgumentsCount() != 1) {
// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
}


Double radians = Calculator.getInstance().calculate((SequenceExpression) argumentsExpression.getArgument(0));

switch (functionExpression.getFunctionType()) {
case Sin:
return Math.sin(radians);
case Cos:
return Math.cos(radians);
case Tg:
// checks for tg(x), where x != Pi/2 + Pi*k, where k is Z
// tg(x) = sin(x) / cos(x) => cos(x) != 0
if (radians % (Math.PI) == Math.PI / 2) {
// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
}
return Math.tan(radians);
case Ctg:
// checks for ctg(x), where x != Pi*k, where k is Z
// ctg(x) = cos(x) / sin(x) => sin(x) != 0
if (radians % (Math.PI) == 0) {
// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
}
return 1.0 / Math.tan(radians);
default:
// TODO: make an specific exception for calculator
throw new IllegalArgumentException();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/ru/corvinella/math/ICalculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package ru.corvinella.math;

public interface ICalculator <T> {
public abstract Double calculate(T expression);
Double calculate(T expression);
}
5 changes: 0 additions & 5 deletions src/main/java/ru/corvinella/math/SequenceCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
* @author sh18rw
*/
public class SequenceCalculator implements ICalculator<SequenceExpression> {
private static final SequenceCalculator instance;
private static final VariableCalculator variableCalculator;

static {
instance = new SequenceCalculator();
variableCalculator = new VariableCalculator();
}
public static SequenceCalculator getInstance() {
return instance;
}

@Override
public Double calculate(SequenceExpression expression) {
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/ru/corvinella/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public final List<Token<?>> getResult() {
throw new IllegalStateException("Requested parsing result, but the parse method have not even been called!");
}

private final void parseNumber() throws ParserIllegalTokenValueException {
private void parseNumber() throws ParserIllegalTokenValueException {
if (currentParsingEntityType != TokenType.Number) {
if (currentParsingEntity.length() != 0) {
packToken();
Expand All @@ -122,15 +122,15 @@ private final void parseNumber() throws ParserIllegalTokenValueException {
}
}

private final void parseOperation() throws ParserIllegalTokenValueException {
private void parseOperation() throws ParserIllegalTokenValueException {
if (currentParsingEntity.length() != 0) {
packToken();
}

currentParsingEntityType = TokenType.Operation;
}

private final void parseParenthesis() throws ParserIllegalTokenValueException {
private void parseParenthesis() throws ParserIllegalTokenValueException {
if (currentParsingEntity.length() != 0) {
Token<?> token = packToken();

Expand Down Expand Up @@ -163,7 +163,7 @@ public final void parseArgumentSeparator() throws ParserIllegalTokenValueExcepti
currentParsingEntityType = TokenType.ArgumentsSeparator;
}

private final Token<?> packToken() throws ParserIllegalTokenValueException {
private Token<?> packToken() throws ParserIllegalTokenValueException {
Token<?> token;

if (currentParsingEntity.length() == 0) {
Expand Down Expand Up @@ -232,7 +232,7 @@ private final Token<?> packToken() throws ParserIllegalTokenValueException {
return token;
}

private final OperationType getOperationTypeFromString(String operation) throws ParserIllegalTokenValueException {
private OperationType getOperationTypeFromString(String operation) throws ParserIllegalTokenValueException {
switch (operation) {
case "+":
return OperationType.Plus;
Expand All @@ -249,7 +249,7 @@ private final OperationType getOperationTypeFromString(String operation) throws
}
}

private final WordType getWordTypeFromString(String word) throws ParserIllegalTokenValueException {
private WordType getWordTypeFromString(String word) throws ParserIllegalTokenValueException {
switch (word) {
// Constants
case "pi":
Expand All @@ -260,12 +260,20 @@ private final WordType getWordTypeFromString(String word) throws ParserIllegalTo
// Functions
case "log":
return WordType.Log;
case "sin":
return WordType.Sin;
case "cos":
return WordType.Cos;
case "tg":
return WordType.Tg;
case "ctg":
return WordType.Ctg;
default:
throw new ParserIllegalTokenValueException(word, TokenType.Word, expression, index);
}
}

private interface IProcessable {
public abstract void process() throws ParserIllegalTokenValueException;
void process() throws ParserIllegalTokenValueException;
}
}
8 changes: 0 additions & 8 deletions src/main/java/ru/corvinella/tokens/ValueToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
* @author sh18rw
*/
public abstract class ValueToken <T> extends Token<T>{
private boolean isNegative;
public ValueToken(T value, TokenType type, int tracer) {
super(value, type, tracer);
}

public void makeNegative() {
this.isNegative = true;
}

public final boolean isNegative() {
return this.isNegative;
}
}
4 changes: 4 additions & 0 deletions src/main/java/ru/corvinella/tokens/WordType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public enum WordType {

// Functions
Log,
Sin,
Cos,
Tg,
Ctg,
}
Loading

0 comments on commit ef953b9

Please sign in to comment.