Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a calculator, it should receive the input as a String and then perform the calculation, outputting the result.
For example, the input could be
((23+17) mod 7 × 4 AND 13
and the output would be 4, as expected.
How can I parse the input, to extract all the operands and perform the calculation ?
The other answers are just "how to set a variable to this result" but if you're actually looking to parse input, you should refer to this:
Equation (expression) parser with precedence?
There are a number of ways to go about solving this kind of problem, and a number of algorithms for doing so. Some of them are stack based, some perform a descent of the "tree", and I can even think of a (somewhat) convoluted way to OOP-ize it. I would start with the link above.
You can use regular expressions to parse the string.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
First you have to look for the most important arguments like [() - parentheses], then less [*/] and [+-]. You have to divide the whole string into parts.
Examples:
Simple calculator (bottom of the page)
Another calculator
Both in Java.
see this page for your reference.
http://www.tutorialspoint.com/java/java_basic_operators.htm
all operators in java are explained very clearly.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I want to validate different kinds of strings which are of different format like
10JUN2022, 2Mx1D, 4M, 1D, TEN, ONE|TEN etc.. and I have written regular expression for that '''^([0-9A-WYZa-wyz ]+)([xX|]([0-9A-WYZa-wyz ]+))?$''' and it's working fine but I also need to validate one more string 2022-06-10, but the expression is failing.
When it comes to regex, don't try to get overly clever. Just solve the basic problem. If that takes multiple regex patterns, so be it. It's much easier to maintain and read.
I would use this for the first regex: [0-3]?\d\w{3}(1|2)\d{3}
and this for the second regex: (1|2)\d{3}(-\d{2}){2}
or combine them if you must: ([0-3]?\d\w{3}(1|2)\d{3})|((1|2)\d{3}(-\d{2}){2})
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I created a same sized - stack based simple calculator for adding 2 operands. I want to know whether my addition becomes incorrect when specific (valid) values are entered. The 2 stacks take integer values and are having the same number of digits (i.e. 400 [3 digits] and 900 [3 digits]).
It depends on the algorithm you're using. From the question it is not clear, but let's assume your calculator can perform basic arithmetic. First off, you want to test each operation separately, because they have different equivalence classes of their inputs. For example, for multiplication it would be: 0, 1, minimum and maximum values, and their negations. Testing almost always will not be exhaustive, but using equivalence classes, you can pick one value from each class to make sure that each class is covered with a test.
Back to your question, you may use min/max values, and anything that you think may break your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way to print a fraction in any form? I'm working on a math program and a fraction is much easier to read than 1/2.
If there is any API that can do written math, I would also be glad.
Example:
Instead of 1/2
There isn't a one-liner answer for your question, so try approaching it from a different angle. You could write a method that takes the numerator and denominator as arguments and then return an output string in any format you want, such as String.format("%d\n---\n%d",numerator,denominator);
if your printing it without quotes your pretty much just turning it into a decimal because your dividing the 2. An example to avoid this:
System.out.println("1/2");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.