-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.java
159 lines (153 loc) · 5.83 KB
/
calc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class calc {
private static char[] meow = {'+', '-', '*', '/', '%', '^'};
private static String prrow = "0.0";
private static double prowrr = 6.626e-34;
private static boolean hiss = true;
private static boolean hisss = false;
public static void main(String[] args) {
cls();
System.out.println("\u001b[1mwelcome to \u001b[35mhayley's \u001b[36msuper \u001b[34mcalculator\u001b[0m!!!!!");
while (true) {
System.out.println("enter an equation to run (or type \"info\", \"settings\" or \"quit\")");
Scanner mroww = new Scanner(System.in);
String nyaa = mroww.nextLine().trim().replace(" ", "");
if (nyaa.equalsIgnoreCase("info")) {
cls();
System.out.println("\u001b[1m\u001b[35m** about hayley's super calculator *\u001b[0m*");
System.out.println("hayley's super calculator is program by hayley breakfast, originally created for her ap computer science class. it features a buncha cool stuff like addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponents (^)! more features coming in the near future :3");
System.out.println("equation format: [num1] [operator] [num2]");
System.out.println("\u001b[1m\u001b[35m** environment variables **\u001b[0m");
System.out.println("\"ans\": the answer to the last equation ran by the calculator (if no previous equation, 0.0 is returned)");
System.out.println("\"c\": the speed of light in meters per second");
System.out.println("\"e\": the mathematical constant of euler's number (approx. 2.71828)");
System.out.println("\"h\": the mathematical constant of planck's constant (represented as 6.626e-34)");
System.out.println("\"phi\": the mathematical constant of the golden ratio (approx. 1.61803)");
System.out.println("\"pi\": the mathematical constant of pi (approx. 3.14159)");
mroww.nextLine();
cls();
} else if (nyaa.equalsIgnoreCase("settings")) {
cls();
while (true) {
System.out.println("\u001b[1m\u001b[35m** settings menu **\u001b[0m");
System.out.println("\u001b[36m0 - exit settings\u001b[0m");
System.out.println("\u001b[36m1 - recite equations (" + (hiss ? "on" : "off") + ")\u001b[0m");
System.out.println("\u001b[36m2 - display internal java parser errors (" + (hisss ? "on" : "off") + ")\u001b[0m");
String mrrp = mroww.nextLine().trim().replace(" ", "");
cls();
if (mrrp.equalsIgnoreCase("0")) {
break;
} else if (mrrp.equalsIgnoreCase("1")) {
hiss = !hiss;
notice("recite equations turned " + (hiss ? "on" : "off"));
} else if (mrrp.equalsIgnoreCase("2")) {
hisss = !hisss;
notice("display internal java parser errors turned " + (hisss ? "on" : "off"));
} else {
error(mrrp + " is not a valid option");
}
}
} else if (nyaa.equalsIgnoreCase("quit")) {
cls();
System.out.println("see you later");
break;
} else {
try {
String prrr = "";
for (int i = 0; !checkoperator(nyaa.charAt(i)); i++) {
prrr = prrr + String.valueOf(nyaa.charAt(i));
}
char prrrr = nyaa.charAt(prrr.length());
String prrrrr = "";
for (int i = prrr.length()+1; i < nyaa.length(); i++) {
prrrrr = prrrrr + String.valueOf(nyaa.charAt(i));
}
cls();
if (prrr.equalsIgnoreCase("ans")) {
prrr = prrow;
}
if (prrrrr.equalsIgnoreCase("ans")) {
prrrrr = prrow;
}
if (prrr.equalsIgnoreCase("c")) {
prrr = "299792458";
}
if (prrrrr.equalsIgnoreCase("c")) {
prrrrr = "299792458";
}
if (prrr.equalsIgnoreCase("e")) {
prrr = "" + Math.E;
}
if (prrrrr.equalsIgnoreCase("e")) {
prrrrr = "" + Math.E;
}
if (prrr.equalsIgnoreCase("h")) {
prrr = "" + prowrr;
}
if (prrrrr.equalsIgnoreCase("h")) {
prrrrr = "" + prowrr;
}
if (prrr.equalsIgnoreCase("phi")) {
prrr = "" + ((1 + Math.sqrt(5)) / 2);
}
if (prrrrr.equalsIgnoreCase("phi")) {
prrrrr = "" + ((1 + Math.sqrt(5)) / 2);
}
if (prrr.equalsIgnoreCase("pi")) {
prrr = "" + Math.PI;
}
if (prrrrr.equalsIgnoreCase("pi")) {
prrrrr = "" + Math.PI;
}
if (prrrr == '+') {
prrow = String.valueOf(Double.valueOf(prrr) + Double.valueOf(prrrrr));
} else if (prrrr == '+') {
prrow = String.valueOf(Double.valueOf(prrr) + Double.valueOf(prrrrr));
} else if (prrrr == '-') {
prrow = String.valueOf(Double.valueOf(prrr) - Double.valueOf(prrrrr));
} else if (prrrr == '*') {
prrow = String.valueOf(Double.valueOf(prrr) * Double.valueOf(prrrrr));
} else if (prrrr == '/') {
prrow = String.valueOf(Double.valueOf(prrr) / Double.valueOf(prrrrr));
} else if (prrrr == '%') {
prrow = String.valueOf(Double.valueOf(prrr) % Double.valueOf(prrrrr));
} else if (prrrr == '^') {
prrow = String.valueOf(Math.pow(Double.valueOf(prrr), Double.valueOf(prrrrr)));
}
if (hiss) {
notice(prrr + " " + prrrr + " " + prrrrr + " = " + prrow);
} else {
notice(prrow);
}
} catch (Exception mewrp) {
cls();
error("invalid equation format" + (hisss ? ": " + mewrp : ""));
}
}
}
}
private static void cls() {
System.out.print("\033[H\033[2J\n");
System.out.flush();
}
private static void error(String purrr) {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\u001b[1m\u001b[31m/!\\ error: " + purrr + "\u001b[0m");
}
private static void notice(String purrr) {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\u001b[1m\u001b[36m(i) notice: " + purrr + "\u001b[0m");
}
private static boolean checkoperator(char mrowr) {
for (int i = 0; i < meow.length; i++) {
if (mrowr == meow[i]) {
return true;
}
}
return false;
}
}