Evaluating an infix expression (accounting for doubles and negatives) - java

The program should have a loop asking the user whether or not they have an infix expression to enter. If the answer is “yes”, the program should prompt the user to input the next expression.
I’ve got this down
The expression must be entered as a single string. The input string must be echoed back out as a string.
The program must use the 2-stack method to evaluate the expression. Print the numerical value.
Use this data:
3*(4+2*(6-4)+1)+2*3
2.75 * ((3 + 1.) – (_3.14159*7+3.1))
The underscore symbol _ means the number is negative.
I understand how to use the 2-stack method, I’m just having trouble coming up with a way to build up the number piece by piece. For example, _3.14159, I’m thinking I’ll need a loop to go through and update a number (int n for example) after each char is read in. I just can’t wrap my head around exactly how to do it. If someone could point me in the right direction it’d be greatly appreciated.
Edit: I haven’t found anything else that addresses this specifically. We can’t use a built in method to do this, we need to make it ourselves. Also, other examples don’t seem to account for doubles.

Related

Stucked with Reverse Polish Notation

I'm having a homework: When we input the math expression like -(2 + 3) * 1/5 , the output should be -1. After doing some research, I found that RPN algorithm is the way to solve this problem. So what I did is convert the expression from infix to postfix. But the problem is I can't determine the operands in some situations, like this:
Input: 11+((10-2)*6)+7
Infix-to-Postfix-----------
Output: 11102-6*+7+
There is no white space between "11" and "10", and between "10" and "2" so I can't determine every single operand correctly .
Because my output (postfix) is a string, I'm totally don't know how to solve this problem. Is there any idea for this?
You described the problem -- and obvious solution -- in your posting: the postfix output you chose destroys critical information from the original expression. The obvious solution is that you have to change your postfix routine to preserve that information.
The particular problem is that you can no longer parse a string of digits into the original integers. The obvious solution is to retain or insert a unique separator. When you emit (output) an integer, add some sort of punctuation. Since RPN uses only numbers and a handful of operators, choose something easy for you to detect and to read yourself: a space, comma, or anything else that would work for you.
For instance, if you use a simple space, then you'd have RPN format as
11 10 2 -6 *+7 +
When you read this in your RPN evaluator, use the separator as a "push integer" signal (or operator).
Note that I have used this separator as a terminal character on every integer, not merely a separator between consecutive integers. Making it a terminal simplifies both output processing and input parsing. Deciding whether or not to add that symbol depends on only one token (the integer), rather than making it conditional upon two adjacent tokens (requiring a small amount of context status).

how to make collapse number with hypen

I want to make my number in collapse format with hypen symbol.
Suppose my input is like "1,2,4,6,7,8,9,11,12,14,16,17,18,19"
and i want output as below
"1-2,4,6,7-9,11-12,14,16-19
This sounds like a homework question to me, but I'll at least point you in the correct direction.
Personally I would probably use StringTokenizer and split the string into an integer array. Then loop through the array and checking if the contents of the current position are related to the next position or not, then find a way to save the values and print them later. You might require a second loop to print later, however I think you could be creative and find a solution that would be faster with only a single loop. ;)
If you want to get fancy, I might take a look into recursion and seeing if you can do this using a single function and a string.
Hope you find this helpful and it sends you in the right direction.
One solution would be to store all the inputs in an array, check if the next number in the array is equal to one higher than the one before and keep doing so until it isn't, when it isn't, replace the numbers in between with a "-".
Look into the .replace() method that comes with the String-class

How to make a scanner in Java that checks if the first letter is a character between A-V and if the second character is a number between 1-20?

How to make a scanner that checks if the first letter is a character between A-V and if the second character is a number between 1-20? Some examples are: '.B4', 'H10.', '**V1', 'L19*', 'M12', or 'N14'.
I'm kind a new to Java. Still learning it in school. I've followed the lessons for about half a year now.
Now I've got an assignment for school. It is about creating a text-based minesweeper. I succeeded in printing the board and placing the mines. But now I'm stuck on getting the right input.
If you use '*' in the scanner like * B4 or B4* it should mark a square.
If you use '.' in the scanner like .B4 or B4. it should unmark a square.
And if you enter B4 it should open.
But I can't get this done in a neat way. I've tried to make sub-strings of it to check if every character is the right one but after I did that my code was kind of chaotic and it didn't work as supposed to.
I've tried it like: "Example 3 : Validating vowels in: Validating input using java.util.Scanner" only I used a variable of the length of my board. So if the board was 10 by 10 it wouldn't go further than J10. But that didn't work either for me.
So I was hoping that you could help me solving this problem.
As this is an assignment, I'll just give you a guideline rather than actual code.
First, you need to get the input into some format. Consider reading the input in from the scanner and storing it into a string.
We can then make use of Java's String functions, a list of which can be found here. Try to find a function that could be useful, perhaps one that lets us get the character at a certain index.
We can then do checks on the string. First we check the first character (the character at index 0), we want to know if that is a letter from A-V. To do this we can do a check on the ASCII numbers. Assuming you just want capital letters, if we convert A to an int, then it will have the value 65. V has the value 86. All the numbers in between correspond to the ASCII values of the letters in between.
Thus we can do a check, convert the first character to an integer, let's call it x. If x >= 65 && x <= 86, then it's a letter we can care about.
Next, you need to do the number checking. For this, take a look at the function Integer.parseInt(String s). It takes a String and then converts it to an integer. You'll have to do some checks to see if it's >= 10 or <10.

expression evaluation with right-associativity in java

I am trying to solve a problem in which I have to solve a given expression consisting of one or more initialization in a same string with no operator precedence (although with bracketed sub-expressions). All the operators have right precedence so I have to evaluate it from right to left. I am confused how to proceed for the given problem. Detailed problem is given here : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=108
I'll give you some ideas to try:
First off, you need to recursively evaluate inside brackets. You want to do brackets from most nested to least nested, so use a regex that matches brackets with no ) inside of them. Substring the result of the computation into the part of the string the bracketed expression took up.
If there are no brackets, then now you need to evaluate operators. The reason why the question requires right precedence is to force you to think about how to answer it - you can't just read the string and do calculations. You have to consider the whole string THEN start doing calculations, which means storing some structure describing it. There's a number of strategies you could use to do this, for example:
-You could tokenize the string, either using a scanner or regexes - continually try to see if the next item in the string is a number or which of the operators it is, and push what kind of token it is and its value onto a list. Then, you can evaluate the list from right to left using some kind of case/switch structure to determine what to do for each operator (either that, or each operator is associated with what it does to numbers). = itself would address a map of variable name keys to values, and insert the value under that variable's key, and then return (to be placed into the list) the value it produced, so it can be used for another assignment. It also seems like - can be determined as to whether it's subtraction or a negative number by whether there's a space on its right or not.
-Instead of tokenization, you could use regexes on the string as a whole. But tokenization is more robust. I tried to build a calculator based on applying regexes to the whole string over and over but it's so difficult to get all the rules right and I don't recommend it.
I've written an expression evaluating calculator like this before, so you can ask me questions if you run into specific problems.

Recursively verifying if a string is a valid prefix expression?

I'm rather new to the community but I've seen some helpful posts on here so I thought I'd ask.
I've got a homework question that asks us to recursively check whether a given string is a valid prefix expression given by the two following rules (standard):
Variables (a-z) are prefix expressions
If O is a binary operator and F and E are prefix expressions, OFE
Now, I kind of get the evaluation and have looked at the prefix-to-infix algorithms, but I can't for the life of me figure out how to implement just the evaluation methods (as I only need to check if it's valid, so not +a-b for example).
I know most of the implementation for these problems is done using stacks but I don't see how I would do it recursively here... some help would be tremendously appreciated.
Think of it this way. (I'm not going to write the code, since that's what you need to learn).
You want to check if a certain string is a prefix expression, so you have a function:
boolean isPrefix(string)
Now, there's two way that string could be a prefix:
It's a character from a-z
It's in the format O(prefix)(prefix)
So first, you check if the string has a length of one and is between a-z, and if so, the answer is yes.
Next you can check if the string starts with an O. If it does, you need to test the rest of the string to see if it is composed of two prefix expressions (FE).
So you start iterating from 1 to length, and passing each substring (0->i, i->length) into isPrefix(). If both substrings are also valid prefix expressions, the answer is yes.
Otherwise, the answer is no.
That's pretty much it, but the implementation, however, is up to you.
I'm not sure I entirely understand the point of this, but I imagine you should have some method like checkPrefixIn(String s) that looks at only part of the given String, returns true if it is only a prefix, false if it is only an operator (or invalid character), or the return value of checkPrefixIn(partOfS), where partOfS is a substring of the input s

Categories