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
201 changes: 201 additions & 0 deletions Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package my_calc;

/**
* Created by Nadya Zueva
* при написании использована статья на e-maxx
*/

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
import java.util.Iterator;

public interface Calculator {
public static void main(String[] args) throws Exception {
System.out.println("Enter string:\n");
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String string;

try {
string = read.readLine();
string = to_polish(string);
// System.out.println(string);
System.out.println(count(string));
} catch (Exception error) {
System.out.println(error.getMessage());
}
}

public static float calculate(String s) throws Parsing Exception {
String buffer;
float num_1 = 0;
float num_2 = 0;

//Stack<Float> stack = new ArrayStack<Float>();
Deque<Float> stack = new ArrayDeque<Float>();
StringTokenizer tok = new StringTokenizer(s);
while (tok.hasMoreTokens()) {
try {
buffer = tok.nextToken().trim();
if (oper(buffer.charAt(0))&& buffer.length() == 1) {
num_1 = stack.pop(); num_2 = stack.pop();
switch (buffer.charAt(0)) {
case '+': {
num_2 = num_2 + num_1;
break;
}
case '-': {
num_2 = num_2 - num_1;
break;
}
case '*': {
num_2 = num_2 * num_1;
break;
}
case '/': {
num_2 = (Float)num_2 / num_1;
break;
}

default:
throw new Exception("\nIndefinite operator\n");
}
stack.push(num_2);
} else {
num_2 = Float.parseFloat(buffer);
stack.push(num_2);
}
} catch (Exception error) {
throw new Exception("\nIndefinite symbol\n");
}
}


return stack.pop();
}


//является ли символ оператором
public static boolean oper(char a) {
if (a == '+' || a == '-' || a == '*' || a == '/') {
return true;
} else {
return false;
}
}

/*является ли символ скобкой
public static boolean brack(char a) {
if (a == '(' || a == ')') {
return true;
}
else {
return false;
}
}*/
//является ли числом
/* public static boolean numb(char a) {
switch (a) {
case '0': {
return true;
}
case '1': {
return true;
}
case '2': {
return true;
}
case '3': {
return true;
}
case '4': {
return true;
}
case '5': {
return true;
}
case '6': {
return true;
}
case '7': {
return true;
}
case '8': {
return true;
}
case '9': {
return true;
}
}
return false;
}*/

//приоритет операции
public static short priority(char a) {
switch (a) {
case '*':
return 2;
case '/':
return 2;
case '+':
return 1;
case '-':
return 1;
default:
return -1;
}
}

public static String to_polish(String input_string) throws Exception {

char c;
char t;
StringBuilder buffer = new StringBuilder("");
StringBuilder output = new StringBuilder("");

for (int i = 0; i < input_string.length(); ++i) {
c = input_string.charAt(i);
if (oper(c)) {
// System.out.println("@");
while (buffer.length() > 0) {
t = buffer.substring(buffer.length() - 1).charAt(0);
if (oper(t) && (priority(c) <= priority(t))) {
output.append(" ").append(t).append(" ");
buffer.setLength(buffer.length() - 1);
} else {
output.append(" ");
break;
}
}
output.append(" ");
buffer.append(c);
} else if ('(' == c) {
buffer.append(c);
} else if (')' == c) {
t = buffer.substring(buffer.length() - 1).charAt(0);
while ('(' != t) {
// System.out.println("&");
output.append(" ").append(t);
buffer.setLength(buffer.length() - 1);
t = buffer.substring(buffer.length() - 1).charAt(0);
}
buffer.setLength(buffer.length() - 1);
} else {

output.append(c);
}
}

while (buffer.length() > 0) {
output.append(" ").append(buffer.substring(buffer.length() - 1));
buffer.setLength(buffer.length() - 1);
}

return output.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @since 04.10.16
*/
public interface KeyValueStorage<K, V> extends Closeable {
void isFileClosed();

/**
* Возвращает значение для данного ключа, если оно есть в хранилище.
* Иначе возвращает null.
Expand Down
28 changes: 28 additions & 0 deletions homework-g595-zueva/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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-g595-zueva</artifactId>
<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Loading