Can I Count The Ways? Let me...
I lost my lunch writing this, so in recompense, I decided to play some games online. There were so many options. “Like the Mahjong game?” Smiled. “I speak of it all the time, right?” “Okay which now?” “Won't tell. Since you can be Harry Porter, how soon will Oz be at your doorstep?” “What's the game, Cyp?”
But before you play, the code is long, your fingers have need some getting used to the typing.Nice day.
/*
*I JUST TRIED DOING A LITTLE BINARY OPERATORS
*HERE AFTER HAVING A LOOK AT SOME OLD CALC
*MACHINE. HOPY YOU LIKE IT.
*/
package overall;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Postfix {
static ListstackList = new ArrayList ();
static List operatorList = new ArrayList(); //ignore generics.
//the index counts to tail lof list
static int listCounter = -1;
protected static void checkInput(String keyedIn){
//check for exit character once and for all
if (keyedIn.equals("@")){
System.exit(0);
}
//not ready to exit?
//make sure first number is a number
if (listCounter == -1){ //nothing in list at all
try{
//see if we can parse the keyed in figure to int
int received = Integer.parseInt(keyedIn);
//insert first number into the list
stackList.add(received);
//increase the counter
++listCounter;
System.out.println("Key in an operator, please.");
}
catch(NumberFormatException e){
System.err.println("First number must be a number. Thanks.");
}
}
//where the keyedin number is not the first number i.e something in stack
//we expect an operator for next number
//check if an operator is already waiting for an operand
int opCheck = operatorList.size();
if (opCheck == 0) {
//no operator waiting! if operand replace it with previous.
try{
int checking = Integer.parseInt(keyedIn);
//replace first number and count is still 0
stackList.remove(0);
stackList.add(checking);
}
//if operator, what was expected
catch(NumberFormatException e){
//send to operator then
doOperation(keyedIn);
}
}
//operator waiting, waiting for a number.
//number expected
if(opCheck > 0){
try{
int checked = Integer.parseInt(keyedIn);
//if number add to list and increase count
stackList.add(checked);
++listCounter;
compResult();
}
//if not number ask for number
catch(NumberFormatException e){
System.out.println("Input a valid number please.");
}
}
}
protected static void doOperation(String ourOperator){
//whenever any character is assumed operator, check for
//validity of operator
if (ourOperator.equals("/") || ourOperator.equals("*") || ourOperator.equals("%")
|| ourOperator.equals("-") || ourOperator.equals("+")){
//perform required operation. operator valid
char calc = ourOperator.charAt(0);
//do calculation now
doCalculate(calc);
}
else {
//ask for valid operator
System.out.println("Key in a valid operator please.");
}
}
protected static void doCalculate(final char theOperator){
//store in operator list
operatorList.add(theOperator);
//if only one operand in list, wait for next input
//lock on stack size
int contains = stackList.size();
if (contains == 1){
//lock on the size
System.out.println("Insert next number for operation.");
}
else if(contains == 2){
compResult();
}
}
public static void compResult(){
//get the last two elements in the stack list
//get the head of the operator list
//do the binary operation
//output the result
//get stack list tail
int stackTail = stackList.remove(listCounter);
--listCounter;
int tailAfterLast = stackList.remove(listCounter);
--listCounter;
//get operator list head
Object opHead = operatorList.remove(0);
System.out.print(tailAfterLast + " " + opHead + " "+stackTail + " is = ");
//do the binary operation
//check what operator we're using first though
if (opHead.equals('+')){
tailAfterLast += stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('-')){
tailAfterLast -= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('%')){
tailAfterLast %= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('*')){
tailAfterLast *= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('/')){
tailAfterLast /= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else {
System.out.println("We're going nowhere.");
}
}
public static void main(String[] args) throws IOException{
BufferedReader bufR = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any character. First character must be a number");
System.out.println("Enter @ to end the program.");
boolean keepOn = true;
//keep getting input until single @ entered or end of operation
while (keepOn){
//get the input from keyboard
String dInserted = bufR.readLine();
//check whether it is an operator, operand or @ sign
checkInput(dInserted);
}
}
}
6 comments:
Gostei muito desse post e seu blog é muito interessante, vou passar por aqui sempre =) Depois dá uma passada lá no meu site, que é sobre o CresceNet, espero que goste. O endereço dele é http://www.provedorcrescenet.com . Um abraço.
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Webcam, I hope you enjoy. The address is http://webcam-brasil.blogspot.com. A hug.
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Monitor de LCD, I hope you enjoy. The address is http://monitor-de-lcd.blogspot.com. A hug.
wonder why coder has been missing for some days now?
На этом прекрасном новостном портале вы и ваши друзья узнаете, что такое хостинг для сателлитов, настройка Wordpess%сайт сателлит. Кроме этого вы найдете для себя еще кое-какие неизвестные секреты успеха. Окунитесь в реальный мир, где электронные деньги становятся реальностью! You are welcome! Be happy!
Can you tell us more about thіs? I'd want to find out more details.
my page best long term loans
Post a Comment