From 985c4ea20842e2db917b7ebbedc51c87d3291300 Mon Sep 17 00:00:00 2001 From: Anastasia Miroshnikova Date: Tue, 25 Sep 2018 11:45:29 +0300 Subject: [PATCH 01/10] First commit --- .gitignore | 23 ----- .idea/description.html | 1 + .idea/misc.xml | 12 +++ .idea/modules.xml | 8 ++ .idea/project-template.xml | 3 + .idea/workspace.xml | 147 ++++++++++++++++++++++++++++++++ README.md | 5 -- REPL2.iml | 12 +++ out/production/REPL2/Main.class | Bin 0 -> 1391 bytes src/Main.java | 29 +++++++ 10 files changed, 212 insertions(+), 28 deletions(-) delete mode 100644 .gitignore create mode 100644 .idea/description.html create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/project-template.xml create mode 100644 .idea/workspace.xml delete mode 100644 README.md create mode 100644 REPL2.iml create mode 100644 out/production/REPL2/Main.class create mode 100644 src/Main.java diff --git a/.gitignore b/.gitignore deleted file mode 100644 index a1c2a23..0000000 --- a/.gitignore +++ /dev/null @@ -1,23 +0,0 @@ -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..727c21e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1037313 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5d84278 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -103,33 +109,33 @@ - + - - - - - - - - - - - - - + + + + + + + + + + + + - - + + + - - - - + + + + @@ -143,5 +149,12 @@ + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index de0104d..bc52a39 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,10 +8,16 @@ public static void main(String[] args) { int ans = 0; while (true) { str = scanner.nextLine().trim(); + if (str != null && !str.isEmpty()) { + if (str.equals("/exit")) { System.out.println("Bye!"); break; + + } else if (str.equals("/help")) { + System.out.println("The program calculates the sum of numbers"); + } else if (str.contains(" ")) { strs = str.split("\\s+"); for (int i = 0; i < strs.length; i++) { @@ -19,7 +25,8 @@ public static void main(String[] args) { } System.out.println(ans); ans = 0; - } else { + + } else { // If there was only one number or non-service string System.out.println(str); } } From 48509043d36e2b9a95f01ff41b18ab54d83e5524 Mon Sep 17 00:00:00 2001 From: Anastasia Miroshnikova Date: Wed, 26 Sep 2018 19:20:02 +0300 Subject: [PATCH 03/10] Stage 4 completed --- .idea/workspace.xml | 48 +++++++++++++++++----- out/production/REPL2/Main.class | Bin 1391 -> 2310 bytes src/Main.java | 70 ++++++++++++++++++++++++-------- 3 files changed, 90 insertions(+), 28 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0eb84a2..77a3614 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -18,8 +18,14 @@ - - + + + + + + + + @@ -37,14 +43,16 @@ + + @@ -61,9 +69,8 @@ + - @@ -116,7 +125,6 @@ - @@ -127,7 +135,7 @@ - + @@ -141,12 +149,24 @@ + + + + + + + + + + + + @@ -182,7 +202,7 @@ - + @@ -190,5 +210,12 @@ + + + + + + + \ No newline at end of file From 561ad30e7d2ab541524d505ae13baba00b6ca481 Mon Sep 17 00:00:00 2001 From: Anastasia Miroshnikova Date: Sat, 8 Dec 2018 13:31:28 +0300 Subject: [PATCH 08/10] Removed out/ folder from repository --- out/production/REPL2/Main.class | Bin 2490 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 out/production/REPL2/Main.class diff --git a/out/production/REPL2/Main.class b/out/production/REPL2/Main.class deleted file mode 100644 index 604ce7959183c3067f16977523cb2b3050ef2797..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2490 zcmZuyTUQfT6#j->W*CNxfhY=;gn9v@rdmsF1aA~;6x51h6%>bLgwbRYXC?@}SgqRD z)?QrgL-EPgm%Q{vAEK*W^#}OgztCUMr!H6NcP3Gzgta*5%syxD{q1jm=j7>szdr%6 z9d}i%$C!d6Dvsiqf)B-fT*0`QPt@ZiCKQ}f@exidIHO=v!IXltV)vYiX`Gj#E0|G` zQjk_)h>Z(km=!*AGE5QUqKdV+B!-OG%ZkBL@g{O&$cy2!7;G7i3|B&M(6mfH3)-Jl4&bVRE zn<-=5wM}bQLb!MIqCT(3GrBb!FB^CDoskeQt$cy$Lo2r>tTLSmJ!7Uv7V@^?IA+e0 zuv)jAD@2p4BpA+Rv$~a*(B!ze{IuyBw(fc-C5CStclFezF+E=a+Cl88LPmFueOCGy zp}EzuYl0*h66=mu0W1@8AZKTF`lpP%NXW7!lmAH;Yk7=0(*!o1%L_y~oBXs+Rt2e$ zK*CuOxhwlfsJEuGrd4nZ8db|!aLb=E3Ni-Sd-f(vmdQkKjc{J?bbFT0vXjy+%dnZZ zoVMVanfQ3wNVGo3W%mhL9nTf)lySfm{jC=*+$wsdVFNa@pg2=&n8y}wPEMs`Q(Gcu zV;ZgqLm+M}n68EeEXw#;!zcKZ`1UUvQ5m0UxQYRBX3oguC2Tk`XGHRLZr0Yb5r^_H zoQUp3vS!K_lJeS;BiiPZ)+H-<#fqe!x7Ii1FyZI8CgZw>FK|Ob_e(X^>;@H;wC06) zB1V;Zf|G=QHbzjB=eUpn4L5O1!gX> zV~d7uVt7jo?}&Q+fb=xuc`5N+y*!y@nRNKgB^pvqLlaa8qMz)oAq;I8(6lDx+ zRZ0!C60|8H}NaZnd(BI{}H4IT-2eratQFDk87=b0R4#Zt6pfS;=GNg zg|plrsw=_w8<*AY!76tdTbWC^Q-CYo{UromcUNn=Q^XbbpP8(VAqRYe{h?q9@)BAe zLJ8HEp!x@k==4;}UadV)xNc&sW;CD$tyqP2j#0F+)-~9{ zA1A@X>THus@LZ--qlkLgXmIE4=KuR;2T%??Yv<&Jx;h2BqTi)dxh{g9L_H zbD519)(&(*#v81qn~X%rO_cjR=%9#tD4u@GCrE>@kCT}_jQ%cmGFKaJ@l1l^BwH1RWuD)KK_aZjU3w+R ze*>Sh-CX&I=??u9@Dtf>>}4kcUMJS0VVU7%8T(}Hm&6|s_3f82tZmvW`PJrsd9ohw zW2D?A{K;rO<{kM1YfnGKx<}|3jg>%>y2dt_@W#F6e>Oix_YxX8MV6NTE~1mGC>zD8 zr?~vri~cM>6<4TCWIt4{_Y(3j#WaF-q$i4llw6W@9VR`aEcqybjC+Jc${=unC4*iG ko Date: Sat, 8 Dec 2018 17:53:34 +0300 Subject: [PATCH 09/10] Part 6 - variables basic functionality added --- .idea/workspace.xml | 67 ++++++++++++++++++------------------ src/Main.java | 82 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 97 insertions(+), 52 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3022eb7..47a417a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,8 @@ - + - + - - + + - - + + + + + - - - - - - - - - @@ -45,8 +39,8 @@ @@ -125,6 +119,7 @@ + @@ -135,7 +130,7 @@ - + @@ -149,7 +144,6 @@ - @@ -161,8 +155,11 @@ - - + + + + + @@ -172,8 +169,11 @@ - - + + + + + @@ -199,21 +199,24 @@ - + - - - - - - + + - + - - + + + + + + + + + diff --git a/src/Main.java b/src/Main.java index bd7ce48..6985a10 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,14 +1,10 @@ -import java.util.Scanner; +import java.util.*; public class Main { - public static boolean isServiceString(String input){ return input.startsWith("/"); } - public static boolean isValidExpression(String input) { - return input.matches("[\\d+\\- ]+"); - } public static boolean answerToServiceCommand(String input){ boolean stop_iteration = false; @@ -17,7 +13,7 @@ public static boolean answerToServiceCommand(String input){ stop_iteration = true; } else if (input.equals("/help")) { - System.out.println("The program serves as microcalculator"); + System.out.println("The program serves as microcalculator\nInput /exit to exit the program"); } else { System.out.println("Unknown command"); @@ -25,36 +21,77 @@ public static boolean answerToServiceCommand(String input){ return stop_iteration; } - public static void calculateAndPrintExpression(String input){ - String[] numbers_and_operators = input.split("\\s+"); + public static boolean isAssignment(String input) { + return input.matches(".+=.+"); + } + + public static boolean isValidExpression(String input) { + return input.matches("[A-Za-z\\d\\-+\\s]+"); + } + + public static void tryAssignment(String input, Map variables) { + String[] variableAndValue = input.split("=", 2); + boolean sussess = assign(variableAndValue, variables); + } + + public static boolean assign(String[] variableAndValue, Map variables) { + int value = 0; + String variable = variableAndValue[0].trim(); + String valueS = variableAndValue[1].trim(); + try { + value = Integer.parseInt(valueS); + } catch (NumberFormatException e) { + if (variables.containsKey(valueS)) value = variables.get(valueS); + else { + System.out.println("Unknown variable"); + return false; + } + } + variables.put(variable, value); + return true; + } + + public static void calculateAndPrintExpression(String input, Map variables){ + String[] numbersAndOperators = input.split("\\s+"); int ans = 0; int number = 0; - int n_minuses = 0; - boolean next_minus = false; - for (int i = 0; i < numbers_and_operators.length; i++) { + String value; + int nMinuses = 0; + boolean nextMinus = false; + for (int i = 0; i < numbersAndOperators.length; i++) { if (i % 2 == 0) { + value = numbersAndOperators[i].trim(); try { - number = Integer.parseInt(numbers_and_operators[i]); - if (next_minus) { + number = Integer.parseInt(value); + if (nextMinus) { ans -= number; } else { ans += number; } } catch (NumberFormatException e) { - System.out.println("Invalid expression"); - break; + if (variables.containsKey(value)) { + number = variables.get(value); + if (nextMinus) { + ans -= number; + } else { + ans += number; + } + } else { + System.out.println("Invalid expression"); + break; + } } } else { - if (numbers_and_operators[i].matches("[+\\-]+")) { - n_minuses = numbers_and_operators[i].replaceAll("[^\\-]", "").length(); - next_minus = n_minuses % 2 == 1; + if (numbersAndOperators[i].matches("[+\\-]+")) { + nMinuses = numbersAndOperators[i].replaceAll("[^\\-]", "").length(); + nextMinus = nMinuses % 2 == 1; } else { System.out.println("Invalid expression"); break; } } - if (i == numbers_and_operators.length - 1) { + if (i == numbersAndOperators.length - 1) { System.out.println(ans); } } @@ -63,6 +100,7 @@ public static void calculateAndPrintExpression(String input){ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str; + Map variables = new HashMap(); while (true) { str = scanner.nextLine().trim(); @@ -72,8 +110,12 @@ public static void main(String[] args) { if (answerToServiceCommand(str)) { break; } + + } else if (isAssignment(str)) { + tryAssignment(str, variables); + } else if (isValidExpression(str)) { - calculateAndPrintExpression(str); + calculateAndPrintExpression(str, variables); } else { // Invalid string input System.out.println("Invalid expression"); From 5d6e2227648cd66d77ce71ac03f5913544be6863 Mon Sep 17 00:00:00 2001 From: Anastasia Miroshnikova Date: Sun, 9 Dec 2018 15:04:27 +0300 Subject: [PATCH 10/10] Part 6 - final version --- .idea/workspace.xml | 53 +++++++++++++++--------------- src/Main.java | 78 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 43 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 47a417a..c354770 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -18,14 +17,12 @@ - - + + - - - - + + @@ -53,6 +50,9 @@ + + + @@ -69,9 +69,6 @@