Advertisement
Luninariel

Nasa Parser

Apr 5th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.42 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.lang.*;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  * This Program Will Perform The Following:
  10.  * There are Rocket Objects, or Rocket States
  11.  * There are Correction Objects, or Correction Factor.
  12.  * A Rocket Object is created with an initial thrust of 30000, Direction of 75.2, RetroPressure of 10.0 and RetroPercent of 3000
  13.  * Corrections are made upon the initial rocket given a equation, read from Expressions3.txt and modified in the following ways:
  14.  * a is given a value of (0,12.3)
  15.  * b is given a value of (15000,0.0)
  16.  * c is given a value of (-2000,5.0)
  17.  * d is given a value of (0,-14.5)
  18.  * With operators given the following priority and actions:
  19.  * Parenthesis are given the utmost priority
  20.  * % Has the highest priority after that, and modifies the direction.
  21.  * * Has the second highest priority and modifies the thrust of the initial rocket.
  22.  * + Has the lowest priority and modifies the thrust AND angle of the rocket.
  23.  * Each part of the expression is read and applied in order of priority from left to right.
  24.  * Two stacks are created One for Operands and one for Operators.
  25.  * As the Equation is read each character is individually read, assigned a value, and placed either on the Operator or Operand Stack.
  26.  * 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.
  27.  * The print is designed to appear as a character by character parser.
  28.  * Once equations are complete the new path of the rocket is displayed.
  29.  */
  30.  
  31. public class RocketParser {
  32.     public static void main(String[] args) throws Exception {
  33.  
  34.         //Create a scanner to read the file Expressions.text
  35.         try{
  36.             String Test;
  37.             Scanner input = new Scanner(new File("src/main/Expressions3.txt"));
  38.  
  39.             int i;
  40.             Object ivalu;
  41.             //A Character Array for the variables we'll be using A and B for rockets, and a-d for correction factors
  42.             char[] vart = {'A', 'B', 'a', 'b','c','d'};
  43.  
  44.             //An object array, for storing the rocket states and corrections
  45.             Object[] ivalue = {
  46.                     new RocketState(30000, 75.2, 10.0, 3000),
  47.                     new RocketState(0,0,0,0),
  48.                     new CorrectionFactor(0, 12.3),
  49.                     new CorrectionFactor(15000, 0.0),
  50.                     new CorrectionFactor(-2000, 5.0),
  51.                     new CorrectionFactor(0, -14.5),
  52.             };
  53.             //A Character array used to provide the Symbols used for Operators in our equations.
  54.             char[] opert = { '%', '*', '+',  ')', '(', '#'};
  55.  
  56.             //A int array used to provide priority for the Operator's used in our formulas.
  57.             int[] intvalp = {3, 2, 1, 99, -99, -100};
  58.  
  59.             while(input.hasNextLine()){
  60.                 Test= input.next();
  61.  
  62.                 //A new instance of GenericManagerStacks meant to hold Integers, our Operands.
  63.                 GenericManagerStacks<Object> opnd = new GenericManagerStacks<Object>();
  64.  
  65.                 //A new instance of GenericManagerStacks meant to hold Operator Objects, our operators.
  66.                 GenericManagerStacks<Opertobj> oper = new GenericManagerStacks<Opertobj>();
  67.  
  68.                 System.out.println("pushing Operator #with priority -100");
  69.  
  70.                 //Our end character being added, the symbol we use to mark the end of an equation.
  71.                 Opertobj pnode1 = new Opertobj('#', -100);
  72.                 oper.pushnode(pnode1);
  73.                 int oprior;
  74.                 Object exvalue;
  75.  
  76.                 //Character array's for the formulas we are parsing.
  77.                 char[] express = Test.toCharArray();
  78.  
  79.                 //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.
  80.                 i = 0;
  81.                 while (express[i] != '#') {
  82.                     System.out.println("Parsing " + express[i]);
  83.  
  84.                     if (((express[i] >= 'a') && (express[i] <= 'z')) || ((express[i] >= 'A') && (express[i] <= 'Z'))) {
  85.                         System.out.println("This is an operand " + express[i]);
  86.  
  87.                         ivalu = findval(express[i], vart, ivalue, vart.length);
  88.                         if (ivalu == null) System.out.println("No value in table for" + express[i]);
  89.  
  90.                         System.out.println("We're pushing it on the operand stack " + ivalu);
  91.                         opnd.pushnode(ivalu);
  92.                     } else {
  93.                         System.out.println("This is an operator " + express[i]);
  94.  
  95.                         if (express[i] == '(') {
  96.                             System.out.println("Pushing on operator stack " + express[i]);
  97.                             Opertobj pnodeo = new Opertobj(express[i], -99);
  98.                             oper.pushnode(pnodeo);
  99.                         } else if (express[i] == ')') {
  100.                             while ((oper.peeknode()).operator != '(') {
  101.                                 popevalandpush(oper, opnd);
  102.                             }
  103.                             oper.popnode();
  104.                         } else {
  105.                             oprior = findval(express[i], opert, intvalp, opert.length);
  106.                             System.out.println("Peeking at top of stack " + (oper.peeknode()).priority);
  107.  
  108.                             while (oprior <= (oper.peeknode()).priority) popevalandpush(oper, opnd);
  109.                             System.out.println("pushing Operator " + express[i] + "with priority " + oprior);
  110.                             Opertobj pnodeo = new Opertobj(express[i], oprior);
  111.                             oper.pushnode(pnodeo);
  112.                         }
  113.                     }
  114.                     i++;
  115.                 }
  116.                 while ((oper.peeknode()).operator != '#') {
  117.                     popevalandpush(oper, opnd);
  118.                 }
  119.                 exvalue = opnd.popnode();
  120.  
  121.                 //Assigning values for ivalue
  122.                 System.out.println("result = " + exvalue);
  123.             }
  124.         }
  125.         catch (FileNotFoundException e) {
  126.             System.err.println("File  was not found");
  127.         }
  128.  
  129.  
  130.     }
  131.  
  132.     //A method to check each operand and assign it a function.
  133.     public static Object IntEval(Object oper1, char oper, Object oper2) {
  134.         RocketState r = null;
  135.         if (oper1 instanceof RocketState) {
  136.             r = (RocketState)oper1;
  137.         } else if (oper2 instanceof RocketState) {
  138.             r = (RocketState)oper2;
  139.         }
  140.         CorrectionFactor c = null;
  141.         if (oper1 instanceof CorrectionFactor) {
  142.             c = (CorrectionFactor)oper1;
  143.         } else if (oper2 instanceof CorrectionFactor) {
  144.             c = (CorrectionFactor)oper2;
  145.         }
  146.         if (r == null || c == null) {
  147.             System.out.println("Invalid operands: oper1 " + oper1 + ", oper2 " + oper2);
  148.         }
  149.         switch (oper) {
  150.             case '+':
  151.                 r.setThrust(r.getThrust() + c.getThrust());
  152.                 r.setDirection(r.getDirection() + c.getDirection());
  153.                 break;
  154.  
  155.             case '*':
  156.                 r.setThrust(r.getThrust() + c.getThrust());
  157.                 break;
  158.  
  159.             case '%':
  160.                 r.setDirection(r.getDirection() + c.getDirection());
  161.                 break;
  162.  
  163.             default:
  164.                 System.out.println("bad operator " + oper);
  165.                 return null;
  166.         }
  167.         r.setRetroPercent(r.getThrust() * (r.getRetroPressure()/100.0));
  168.         return r;
  169.     }
  170.  
  171.     //Finds the value of each character and returns its value
  172.     public static Object findval(char x, char[] vtab, Object[] valtb, int last) {
  173.         int i = -99;
  174.         Object vreturn = null;
  175.  
  176.         for (i = 0; i < last; i++)
  177.             if (vtab[i] == x) vreturn = valtb[i];
  178.         System.out.println("Found this char: " + x + " value is: " + vreturn);
  179.         return vreturn;
  180.     }
  181.     //Finds the value of each character and returns its value
  182.     public static int findval(char x, char[] vtab, int[] valtb, int last) {
  183.         int i, vreturn = -99;
  184.  
  185.         for (i = 0; i < last; i++)
  186.             if (vtab[i] == x) vreturn = valtb[i];
  187.         System.out.println("Found this char: " + x + " value is: " + vreturn);
  188.         return vreturn;
  189.     }
  190.     //Pops, evaluates, and Pushes the stacks as needed.
  191.     public static void popevalandpush(GenericManagerStacks<Opertobj> x, GenericManagerStacks<Object> y) {
  192.         Object a, b, c;
  193.         char operandx;
  194.         operandx = (x.popnode()).GetOpert();
  195.         a = y.popnode();
  196.         b = y.popnode();
  197.         System.out.println("in popeval " + b + operandx + a);
  198.         c = IntEval(b, operandx, a);
  199.         y.pushnode(c);
  200.         return;
  201.     }
  202. }
  203.  
  204. //The Generic Manager Stacks class
  205. class GenericManagerStacks<T> {
  206.     protected ArrayList<T> mystack;
  207.     protected int number;
  208.  
  209.     //Constructor
  210.     public GenericManagerStacks() {
  211.         number = 0;
  212.  
  213.         mystack = new ArrayList<T>(100);
  214.     }
  215.  
  216.     //Getter for Number
  217.     public int getnumber() {
  218.         return number;
  219.     }
  220.  
  221.     //Pushes the node
  222.     public int pushnode(T x) {
  223.         System.out.println("in pushnode " + number + " x is " + x);
  224.  
  225.         mystack.add(number, x);
  226.         number++;
  227.         System.out.println("Leaving pushnode");
  228.         return number;
  229.     }
  230.  
  231.     // Pops the next node
  232.     public T popnode() {
  233.         T nodeval;
  234.  
  235.         nodeval = mystack.get(number - 1);
  236.         mystack.remove(number - 1);
  237.         number--;
  238.         return nodeval;
  239.     }
  240.  
  241.     //Peeks at the top of the stack
  242.     public T peeknode() {
  243.         T nodeval;
  244.  
  245.         nodeval = mystack.get(number - 1);
  246.         return nodeval;
  247.     }
  248.  
  249.     //Checks if the stack is empty
  250.     boolean stackempty() {
  251.         if (number == 0) return true;
  252.         else return false;
  253.     }
  254.  
  255. }
  256.  
  257. //The class that holds the operator objects and their priority
  258. class Opertobj {
  259.     protected char operator;
  260.     protected int priority;
  261.  
  262.     //Constructor
  263.     public Opertobj(char opert, int pri) {
  264.         operator = opert;
  265.         priority = pri;
  266.     }
  267.  
  268.     //Getter for priority
  269.     public int GetPrior() {
  270.         return priority;
  271.     }
  272.  
  273.     //Getter for Operator
  274.     public char GetOpert() {
  275.         return operator;
  276.     }
  277. }
  278.  
  279.  
  280. class RocketState {
  281.     private int thrust;
  282.     private double direction;
  283.     private double retroPressure;
  284.     private double retroPercent;
  285.  
  286.     public RocketState(int thrust, double direction, double retroPressure, double retroPercent) {
  287.         this.thrust = thrust;
  288.         this.direction = direction;
  289.         this.retroPressure = retroPressure;
  290.         this.retroPercent = retroPercent;
  291.     }
  292.  
  293.     public int getThrust() {
  294.         return thrust;
  295.     }
  296.  
  297.     public void setThrust(int thrust) {
  298.         this.thrust = thrust;
  299.     }
  300.  
  301.     public double getDirection() {
  302.         return direction;
  303.     }
  304.  
  305.     public void setDirection(double direction) {
  306.         this.direction = direction;
  307.     }
  308.  
  309.     public double getRetroPressure() {
  310.         return retroPressure;
  311.     }
  312.  
  313.     public void setRetroPressure(double retroPressure) {
  314.         this.retroPressure = retroPressure;
  315.     }
  316.  
  317.     public double getRetroPercent() {
  318.         return retroPercent;
  319.     }
  320.  
  321.     public void setRetroPercent(double retroPercent) {
  322.         this.retroPercent = retroPercent;
  323.     }
  324.  
  325.     @Override
  326.     public String toString() {
  327.         return "RocketState [thrust=" + thrust + ", direction=" + direction + ", retroPressure=" + retroPressure
  328.                 + ", retroPercent=" + retroPercent + "]";
  329.     }
  330. }
  331.  
  332. class CorrectionFactor {
  333.     private int thrust;
  334.     private double direction;
  335.     public CorrectionFactor(int thrust, double direction) {
  336.         this.thrust = thrust;
  337.         this.direction = direction;
  338.     }
  339.     public int getThrust() {
  340.         return thrust;
  341.     }
  342.     public void setThrust(int thrust) {
  343.         this.thrust = thrust;
  344.     }
  345.     public double getDirection() {
  346.         return direction;
  347.     }
  348.     public void setDirection(double direction) {
  349.         this.direction = direction;
  350.     }
  351.     @Override
  352.     public String toString() {
  353.         return "CorrectionFactor [thrust=" + thrust + ", direction=" + direction + "]";
  354.     }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement