It looks like you're new here. If you want to get involved, click one of these buttons!
10+10
5*5
10/2
20/10*2+1
10*10-5+2
10+5+3/2*0
(5*6)-(23/2)
6-(20*(2 +4))+1
(10+5/3)*5
public class Calculator {
public static void main(String[] args) {
String[] testinputs = { \"( 10+2 )*(2+3)\", \"((1+2) - (1+2))\",
\"((1-1)/(2*2)) + 5\", \"5+5\", \"(1)\", \"3.5 - 1\" };
for (String input : testinputs) {
System.out.println(\"calculating \" + input);
System.out.println(calculate(input));
System.out.println(\"-----------\");
}
}
private static double calculate(String input) {
input = new String(removeSurroundingBrackets(input.toCharArray()));
if (isNumber(input)) {
return Double.parseDouble(input);
}
String[] parts = splitIntoParts(input);
if (isNumber(parts[0]) && isNumber(parts[2])) {
double result = operate(Double.parseDouble(parts[0]),
Double.parseDouble(parts[2]), parts[1]);
return result;
}
return operate(calculate(parts[0]), calculate(parts[2]), parts[1]);
}
private static double operate(double a, double b, String operator) {
if (operator.equals(\"+\")) {
return a + b;
}
if (operator.equals(\"-\")) {
return a - b;
}
if (operator.equals(\"/\")) {
return a / b;
}
if (operator.equals(\"*\")) {
return a * b;
}
return 0;
}
private static boolean isNumber(String string) {
try {
Double.parseDouble(string);
} catch (NumberFormatException e) {
return false;
}
return true;
}
private static String[] splitIntoParts(String input) {
StringBuilder[] parts = initParts();
char[] in = input.toCharArray();
int part = 0;
int bracketsOpen = 0;
for (int i = 0; i < in.length; i++) {
char c = in[i];
if (c == '(') {
bracketsOpen++;
} else if (c == ')') {
bracketsOpen--;
}
if (!Character.isWhitespace(c)) {
parts[part].append(c);
if (bracketsOpen == 0 && !(i + 1 >= in.length)
&& !(isNumberOrDot(c) && isNumberOrDot(in[i + 1]))) {
part++;
}
}
}
String[] result = new String[parts.length];
for (int i = 0; i < result.length; i++) {
result[i] = parts[i].toString();
}
return result;
}
private static boolean isNumberOrDot(char c) {
return Character.isDigit(c) || c == '.';
}
private static char[] removeSurroundingBrackets(char[] a) {
if (a[0] == '(') {
int openBrackets = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == '(') {
openBrackets++;
} else if (a[i] == ')') {
openBrackets--;
}
if (openBrackets == 0 && (i == a.length - 1)) {
char[] r = new char[a.length - 2];
for (int j = 1; j < a.length - 1; j++) {
r[j - 1] = a[j];
}
return r;
} else if (openBrackets == 0) {
break;
}
}
}
return a;
}
private static StringBuilder[] initParts() {
StringBuilder[] parts = new StringBuilder[3];
for (int i = 0; i < parts.length; i++) {
parts[i] = new StringBuilder();
}
return parts;
}
}calculating (10+2)*(2+3)
60.0
-----------
calculating ((1+2) - (1+2))
0.0
-----------
calculating ((1-1)/(2*2)) + 5
5.0
-----------
calculating 5+5
10.0
-----------
calculating (1)
1.0
-----------
calculating 3.5 - 1
2.5
-----------
public static void main(String[] args) {
System.out.println(\"type in 'quit' to leave the program\n\");
while (true) {
java.util.Scanner scan = new java.util.Scanner(System.in);
System.out.println(\"Your input\");
String input = scan.nextLine();
if (input.equals(\"quit\")) {
break;
}
try {
double result = calculate(input);
System.out.println(\"Result: \" + result + \"\n\");
} catch (Exception e) {
System.out.println(\"Your input was not correct\n\");
}
}
}
Public Class Form1
Dim calc As String
Dim n1() As String
Dim ans As String
Dim x1 As Integer
Dim x2 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
calc = TextBox1.Text
If calc.Contains(\"+\") Then
n1 = Microsoft.VisualBasic.Split(calc, \"+\", 2)
x1 = (n1(0))
x2 = (n1(1))
ans = x1 + x2
MessageBox.Show(ans)
ElseIf calc.Contains(\"*\") Then
End If
End Sub
End Class
#!/usr/bin/python
print 'Press Ctrl&C to exit\n'
while True:
x = input('Enter operation: ')
print 'Result:', x
Press Ctrl&C to exit
Enter operation: 10+10
Result: 20
Enter operation: 20/10*2+1
Result: 5
Enter operation: (5*6)-(23/2)
Result: 19
#!/usr/bin/python
while True:
op = raw_input(\"Enter operation: \")
if op.find('+') != -1:
sp = op.split('+')
firstnumber = (sp)[0]
secondnumber = (sp)[1]
res = int(firstnumber)+int(secondnumber)
print \"Result:\", res
Enter operation: 35+35
Result: 70
Undead beat me to the Python joke one, so here's the Perl one. :)
#!/usr/bin/perl
print \"Enter your equation: \";
$eq = <STDIN>;
print eval $eq;
$toMakeUserPressEnterToClose = <STDIN>;