Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.linkedList.cs2cs16;
- import java.util.Scanner;
- public class PolynomialSolver implements IPolynomialSolver {
- DLinkedList A, B, C, R;
- public void sort(int[][] arr)
- {
- int n = arr.length;
- for (int i = n - 2; i >= 0; i--) {
- int keyCoef = arr[i][0];
- int keyExp = arr[i][1];
- int j = i + 1;
- while (j < n && arr[j][1] > keyCoef) {
- arr[j - 1][0] = arr[j][0];
- arr[j - 1][1] = arr[j][1];
- j = j + 1;
- }
- arr[j - 1][1] = keyCoef;
- arr[j - 1][1] = keyExp;
- }
- }
- public DLinkedList choose (char poly) {
- DLinkedList n = new DLinkedList();
- if (poly == 'A') {
- A = new DLinkedList();
- n = A;
- }
- else if (poly == 'B') {
- B = new DLinkedList();
- n = B;
- }
- else if (poly == 'C') {
- C = new DLinkedList();
- n = C;
- }
- return n;
- }
- public void setPolynomial(char poly, int[][] terms) {
- DLinkedList n = choose(poly);
- for (int i = 0; i < terms.length; i++) {
- n.add(terms[i]);
- }
- }
- public String print(char poly) {
- DLinkedListNode n = choose(poly).head;
- String ans = "";
- int[] tmp = new int[2];
- while (n != null) {
- tmp = (int[])n.value;
- if (tmp[0] == 0) {
- continue;
- }
- else {
- ans = ans + Integer.toString(tmp[0]);
- if (tmp[1] == 1) {
- ans = ans + 'x';
- }
- else {
- ans = ans + 'x' + '^' + tmp[1];
- }
- }
- n = n.next;
- }
- return ans;
- }
- public void clearPolynomial(char poly) {
- }
- public float evaluatePolynomial(char poly, float value) {
- return 0;
- }
- public int[][] add(char poly1, char poly2) {
- return new int[0][];
- }
- public int[][] subtract(char poly1, char poly2) {
- return new int[0][];
- }
- public int[][] multiply(char poly1, char poly2) {
- return new int[0][];
- }
- public static void main(String[] args) {
- PolynomialSolver polynomialSolver = new PolynomialSolver();
- while (true) {
- System.out.println("Please choose an action");
- System.out.println("-----------------------");
- System.out.println("1- Set a polynomial variable");
- System.out.println("2- Print the value of a polynomial variable");
- System.out.println("3- Add two polynomials");
- System.out.println("4- Subtract two polynomials");
- System.out.println("5- Multiply two polynomials");
- System.out.println("6- Evaluate a polynomial at some point");
- System.out.println("7- Clear a polynomial variable");
- Scanner input = new Scanner(System.in).useDelimiter("\n");
- int action = input.nextInt();
- char variableName;
- if (action == 1) {
- do {
- System.out.println("Insert the variable name: A, B or C");
- variableName = input.next().charAt(0);
- } while (variableName != 'A' && variableName != 'B' && variableName != 'C');
- System.out.println("Insert the polynomial terms in the form:");
- System.out.println("(coeff1, exponent1), (coeff2, exponent2), ..");
- String line = input.next();
- int counter = 0;
- for (int i = 0, len = line.length(); i < len; i++) {
- if (line.charAt(i) == '(') {
- counter++;
- }
- }
- int[][] terms = new int[counter][2];
- int k = 0, len = line.length();
- for (int i = 0; i < counter; i++) {
- for (int j = 0; j < 2; j++) {
- for (; k < len; k++) {
- if (Character.isDigit(line.charAt(k))) {
- terms[i][j] = Character.getNumericValue(line.charAt(k));
- k++;
- break;
- }
- }
- }
- }
- polynomialSolver.sort(terms);
- polynomialSolver.setPolynomial(variableName, terms);
- }
- if (action == 2) {
- do {
- System.out.println("Insert the variable name: A, B or C");
- variableName = input.next(".").charAt(0);
- } while (variableName != 'A' && variableName != 'B' && variableName != 'C');
- System.out.println(polynomialSolver.print(variableName));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement