Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.lang.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * This Program Will Perform The Following:
- * There are Rocket Objects, or Rocket States
- * There are Correction Objects, or Correction Factor.
- * A Rocket Object is created with an initial thrust of 30000, Direction of 75.2, RetroPressure of 10.0 and RetroPercent of 3000
- * Corrections are made upon the initial rocket given a equation, read from Expressions3.txt and modified in the following ways:
- * a is given a value of (0,12.3)
- * b is given a value of (15000,0.0)
- * c is given a value of (-2000,5.0)
- * d is given a value of (0,-14.5)
- * With operators given the following priority and actions:
- * Parenthesis are given the utmost priority
- * % Has the highest priority after that, and modifies the direction.
- * * Has the second highest priority and modifies the thrust of the initial rocket.
- * + Has the lowest priority and modifies the thrust AND angle of the rocket.
- * Each part of the expression is read and applied in order of priority from left to right.
- * Two stacks are created One for Operands and one for Operators.
- * As the Equation is read each character is individually read, assigned a value, and placed either on the Operator or Operand Stack.
- * Each character read causes the parser to peek at the top of the stack, and place it in the appropriate place, or operate on the function as defined.
- * The print is designed to appear as a character by character parser.
- * Once equations are complete the new path of the rocket is displayed.
- */
- public class RocketParser {
- public static void main(String[] args) throws Exception {
- //Create a scanner to read the file Expressions.text
- try{
- String Test;
- Scanner input = new Scanner(new File("src/main/Expressions3.txt"));
- int i;
- Object ivalu;
- //A Character Array for the variables we'll be using A and B for rockets, and a-d for correction factors
- char[] vart = {'A', 'B', 'a', 'b','c','d'};
- //An object array, for storing the rocket states and corrections
- Object[] ivalue = {
- new RocketState(30000, 75.2, 10.0, 3000),
- new RocketState(0,0,0,0),
- new CorrectionFactor(0, 12.3),
- new CorrectionFactor(15000, 0.0),
- new CorrectionFactor(-2000, 5.0),
- new CorrectionFactor(0, -14.5),
- };
- //A Character array used to provide the Symbols used for Operators in our equations.
- char[] opert = { '%', '*', '+', ')', '(', '#'};
- //A int array used to provide priority for the Operator's used in our formulas.
- int[] intvalp = {3, 2, 1, 99, -99, -100};
- while(input.hasNextLine()){
- Test= input.next();
- //A new instance of GenericManagerStacks meant to hold Integers, our Operands.
- GenericManagerStacks<Object> opnd = new GenericManagerStacks<Object>();
- //A new instance of GenericManagerStacks meant to hold Operator Objects, our operators.
- GenericManagerStacks<Opertobj> oper = new GenericManagerStacks<Opertobj>();
- System.out.println("pushing Operator #with priority -100");
- //Our end character being added, the symbol we use to mark the end of an equation.
- Opertobj pnode1 = new Opertobj('#', -100);
- oper.pushnode(pnode1);
- int oprior;
- Object exvalue;
- //Character array's for the formulas we are parsing.
- char[] express = Test.toCharArray();
- //Loops through each read of the equation, evaluating each character, peeking at the top of its particular stack, and placing it in the correct place it will continue this action until a # is found.
- i = 0;
- while (express[i] != '#') {
- System.out.println("Parsing " + express[i]);
- if (((express[i] >= 'a') && (express[i] <= 'z')) || ((express[i] >= 'A') && (express[i] <= 'Z'))) {
- System.out.println("This is an operand " + express[i]);
- ivalu = findval(express[i], vart, ivalue, vart.length);
- if (ivalu == null) System.out.println("No value in table for" + express[i]);
- System.out.println("We're pushing it on the operand stack " + ivalu);
- opnd.pushnode(ivalu);
- } else {
- System.out.println("This is an operator " + express[i]);
- if (express[i] == '(') {
- System.out.println("Pushing on operator stack " + express[i]);
- Opertobj pnodeo = new Opertobj(express[i], -99);
- oper.pushnode(pnodeo);
- } else if (express[i] == ')') {
- while ((oper.peeknode()).operator != '(') {
- popevalandpush(oper, opnd);
- }
- oper.popnode();
- } else {
- oprior = findval(express[i], opert, intvalp, opert.length);
- System.out.println("Peeking at top of stack " + (oper.peeknode()).priority);
- while (oprior <= (oper.peeknode()).priority) popevalandpush(oper, opnd);
- System.out.println("pushing Operator " + express[i] + "with priority " + oprior);
- Opertobj pnodeo = new Opertobj(express[i], oprior);
- oper.pushnode(pnodeo);
- }
- }
- i++;
- }
- while ((oper.peeknode()).operator != '#') {
- popevalandpush(oper, opnd);
- }
- exvalue = opnd.popnode();
- //Assigning values for ivalue
- System.out.println("result = " + exvalue);
- }
- }
- catch (FileNotFoundException e) {
- System.err.println("File was not found");
- }
- }
- //A method to check each operand and assign it a function.
- public static Object IntEval(Object oper1, char oper, Object oper2) {
- RocketState r = null;
- if (oper1 instanceof RocketState) {
- r = (RocketState)oper1;
- } else if (oper2 instanceof RocketState) {
- r = (RocketState)oper2;
- }
- CorrectionFactor c = null;
- if (oper1 instanceof CorrectionFactor) {
- c = (CorrectionFactor)oper1;
- } else if (oper2 instanceof CorrectionFactor) {
- c = (CorrectionFactor)oper2;
- }
- if (r == null || c == null) {
- System.out.println("Invalid operands: oper1 " + oper1 + ", oper2 " + oper2);
- }
- switch (oper) {
- case '+':
- r.setThrust(r.getThrust() + c.getThrust());
- r.setDirection(r.getDirection() + c.getDirection());
- break;
- case '*':
- r.setThrust(r.getThrust() + c.getThrust());
- break;
- case '%':
- r.setDirection(r.getDirection() + c.getDirection());
- break;
- default:
- System.out.println("bad operator " + oper);
- return null;
- }
- r.setRetroPercent(r.getThrust() * (r.getRetroPressure()/100.0));
- return r;
- }
- //Finds the value of each character and returns its value
- public static Object findval(char x, char[] vtab, Object[] valtb, int last) {
- int i = -99;
- Object vreturn = null;
- for (i = 0; i < last; i++)
- if (vtab[i] == x) vreturn = valtb[i];
- System.out.println("Found this char: " + x + " value is: " + vreturn);
- return vreturn;
- }
- //Finds the value of each character and returns its value
- public static int findval(char x, char[] vtab, int[] valtb, int last) {
- int i, vreturn = -99;
- for (i = 0; i < last; i++)
- if (vtab[i] == x) vreturn = valtb[i];
- System.out.println("Found this char: " + x + " value is: " + vreturn);
- return vreturn;
- }
- //Pops, evaluates, and Pushes the stacks as needed.
- public static void popevalandpush(GenericManagerStacks<Opertobj> x, GenericManagerStacks<Object> y) {
- Object a, b, c;
- char operandx;
- operandx = (x.popnode()).GetOpert();
- a = y.popnode();
- b = y.popnode();
- System.out.println("in popeval " + b + operandx + a);
- c = IntEval(b, operandx, a);
- y.pushnode(c);
- return;
- }
- }
- //The Generic Manager Stacks class
- class GenericManagerStacks<T> {
- protected ArrayList<T> mystack;
- protected int number;
- //Constructor
- public GenericManagerStacks() {
- number = 0;
- mystack = new ArrayList<T>(100);
- }
- //Getter for Number
- public int getnumber() {
- return number;
- }
- //Pushes the node
- public int pushnode(T x) {
- System.out.println("in pushnode " + number + " x is " + x);
- mystack.add(number, x);
- number++;
- System.out.println("Leaving pushnode");
- return number;
- }
- // Pops the next node
- public T popnode() {
- T nodeval;
- nodeval = mystack.get(number - 1);
- mystack.remove(number - 1);
- number--;
- return nodeval;
- }
- //Peeks at the top of the stack
- public T peeknode() {
- T nodeval;
- nodeval = mystack.get(number - 1);
- return nodeval;
- }
- //Checks if the stack is empty
- boolean stackempty() {
- if (number == 0) return true;
- else return false;
- }
- }
- //The class that holds the operator objects and their priority
- class Opertobj {
- protected char operator;
- protected int priority;
- //Constructor
- public Opertobj(char opert, int pri) {
- operator = opert;
- priority = pri;
- }
- //Getter for priority
- public int GetPrior() {
- return priority;
- }
- //Getter for Operator
- public char GetOpert() {
- return operator;
- }
- }
- class RocketState {
- private int thrust;
- private double direction;
- private double retroPressure;
- private double retroPercent;
- public RocketState(int thrust, double direction, double retroPressure, double retroPercent) {
- this.thrust = thrust;
- this.direction = direction;
- this.retroPressure = retroPressure;
- this.retroPercent = retroPercent;
- }
- public int getThrust() {
- return thrust;
- }
- public void setThrust(int thrust) {
- this.thrust = thrust;
- }
- public double getDirection() {
- return direction;
- }
- public void setDirection(double direction) {
- this.direction = direction;
- }
- public double getRetroPressure() {
- return retroPressure;
- }
- public void setRetroPressure(double retroPressure) {
- this.retroPressure = retroPressure;
- }
- public double getRetroPercent() {
- return retroPercent;
- }
- public void setRetroPercent(double retroPercent) {
- this.retroPercent = retroPercent;
- }
- @Override
- public String toString() {
- return "RocketState [thrust=" + thrust + ", direction=" + direction + ", retroPressure=" + retroPressure
- + ", retroPercent=" + retroPercent + "]";
- }
- }
- class CorrectionFactor {
- private int thrust;
- private double direction;
- public CorrectionFactor(int thrust, double direction) {
- this.thrust = thrust;
- this.direction = direction;
- }
- public int getThrust() {
- return thrust;
- }
- public void setThrust(int thrust) {
- this.thrust = thrust;
- }
- public double getDirection() {
- return direction;
- }
- public void setDirection(double direction) {
- this.direction = direction;
- }
- @Override
- public String toString() {
- return "CorrectionFactor [thrust=" + thrust + ", direction=" + direction + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement