Omar_Natour

Natour, O. 3/1/16 Csc-112 Regex Complex Numbers

Mar 2nd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 3/1/16
  4.  * Csc-112
  5.  * Intro to Java
  6.  * Implementing Complex Numbers p3
  7.  * Ojnatour0001@student.stcc.edu or natour@cs.stcc.edu
  8.  * Implement support for imaginary numbers through the use of objects and classes and regex.
  9.  */
  10. import javax.swing.JOptionPane;
  11. import java.util.regex.*;
  12.  
  13. public class ComplexNumberDriver {
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         final String TITLE = "Complex Numbers";
  18.  
  19.         double real = 0;
  20.         double imag = 0;
  21.         double real2 = 0;
  22.         double imag2 = 0;
  23.         String output = "";
  24.  
  25.         while (true) {
  26.  
  27.             String a = JOptionPane.showInputDialog(null, "Enter the first complex number", TITLE,
  28.                     JOptionPane.INFORMATION_MESSAGE);
  29.  
  30.             String b = JOptionPane.showInputDialog(null, "Enter the second complex number", TITLE,
  31.                     JOptionPane.INFORMATION_MESSAGE);
  32.  
  33.             Pattern p = Pattern.compile(
  34.                     ("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+) *(i)?)( *(([\\+\\-]) *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i *)?"));
  35.             Matcher match = p.matcher(a);
  36.             Matcher match1 = p.matcher(b);
  37.  
  38.             while (match.find()) {
  39.  
  40.                 String mat1 = match.group(1).replaceAll(" ", "");
  41.                 String matI = mat1.replaceAll("i", "");
  42.  
  43.                 if (match.group(1).matches("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i")) {
  44.                     imag = Double.parseDouble(matI);
  45.                     real = 0;
  46.                 } else if (match.group(6) != null) {
  47.                     String mat6 = match.group(6).replaceAll(" ", "");
  48.  
  49.                     real = Double.parseDouble(mat1);
  50.                     imag = Double.parseDouble(mat6);
  51.                 } else {
  52.                     real = Double.parseDouble(mat1);
  53.                     imag = 0;
  54.                 }
  55.  
  56.             }
  57.             while (match1.find()) {
  58.  
  59.                 String mat1 = match1.group(1).replaceAll(" ", "");
  60.                 String matI = mat1.replaceAll("i", "");
  61.  
  62.                 if (match1.group(1).matches("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i")) {
  63.                     imag2 = Double.parseDouble(matI);
  64.                     real2 = 0;
  65.                 } else if (match1.group(6) != null) {
  66.                     String mat6 = match1.group(6).replaceAll(" ", "");
  67.  
  68.                     real2 = Double.parseDouble(mat1);
  69.                     imag2 = Double.parseDouble(mat6);
  70.                 } else {
  71.                     real2 = Double.parseDouble(mat1);
  72.                     imag2 = 0;
  73.                 }
  74.             }
  75.  
  76.             Complex c1 = new Complex(real, imag);
  77.             Complex c2 = new Complex(real2, imag2);
  78.  
  79.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Negate", "Conjugate", "Absolute Value",
  80.                     "Distance", "Equals", "A Greater Than B", "A Less Than B", "Quit" };
  81.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  82.                     JOptionPane.PLAIN_MESSAGE, null, options, 11);
  83.  
  84.             output = "";
  85.             switch (option) {
  86.             case 0:
  87.                 Complex plus = c1.add(c2);
  88.                 output += (c1.toString(c1) + " + " + c2.toString(c2) + " = " + plus.toString(plus));
  89.                 break;
  90.             case 1:
  91.                 Complex minus = c1.subtract(c2);
  92.                 output += (c1.toString(c1) + " - " + c2.toString(c2) + " = " + minus.toString(minus));
  93.                 break;
  94.             case 2:
  95.                 Complex times = c1.multiply(c2);
  96.                 output += (c1.toString(c1) + " * " + c2.toString(c2) + " = " + times.toString(times));
  97.                 break;
  98.             case 3:
  99.                 Complex quot = c1.divide(c2);
  100.                 output += (c1.toString(c1) + " / " + c2.toString(c2) + " = " + quot.toString(quot));
  101.                 break;
  102.             case 4:
  103.                 output += ("-(" + c1.toString(c1) + " ) = " + (c1.negate()).toString(c1.negate()) + "\n");
  104.                 output += ("-(" + c2.toString(c2) + " ) = " + (c2.negate()).toString(c2.negate()));
  105.                 break;
  106.             case 5:
  107.                 output += (c1.toString(c1) + " conjugated = " + (c1.conjugate()).toString(c1.conjugate()) + "\n");
  108.                 output += (c2.toString(c2) + " conjugated = " + (c2.conjugate()).toString(c2.conjugate()));
  109.                 break;
  110.             case 6:
  111.                 output += ("|" + c1.toString(c1) + "| =" + c1.abs() + "\n");
  112.                 output += ("|" + c2.toString(c2) + "| =" + c2.abs());
  113.                 break;
  114.             case 7:
  115.                 output += ("Distance of (" + c1.toString(c1) + ") to (" + c2.toString(c2) + ") = " + c1.distance(c2));
  116.                 break;
  117.             case 8:
  118.                 output += ("Does " + c1.toString(c1) + " = " + c2.toString(c2) + "?\n" + c1.equals(c2));
  119.                 break;
  120.             case 9:
  121.                 output += ("Is " + c1.toString(c1) + " Greater than " + c2.toString(c2) + " ?\n" + c1.greaterThan(c2));
  122.                 break;
  123.             case 10:
  124.                 output += ("Is " + c1.toString(c1) + " Less than " + c2.toString(c2) + " ?\n" + c1.lessThan(c2));
  125.                 break;
  126.             }
  127.             if (option == 11)
  128.                 break;
  129.             JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.PLAIN_MESSAGE);
  130.  
  131.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
  132.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  133.             if (option != JOptionPane.YES_OPTION)
  134.                 break;
  135.  
  136.         }
  137.  
  138.     }
  139.  
  140. }
Add Comment
Please, Sign In to add comment