Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.StringBuilder;
- public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface {
- String originalString;
- ArrayList<Integer> polyCoeList = new ArrayList<Integer>();
- ArrayList<Integer> constantList = new ArrayList<Integer>();
- public ArrayWithExponentAsIndexPolynomial(String polyString) {
- originalString = polyString;
- String polyStringStripped = polyString.replaceAll("\\s+","");
- int tempNumCount = 0;
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(polyStringStripped.charAt(i) == 'x') {
- if(getExponent(polyStringStripped, i) > polyCoeList.size()) {
- for(int n = 0; n <= getExponent(polyStringStripped, i); n++) {
- polyCoeList.add(polyCoeList.size(),0);
- }
- }
- try {
- if(polyStringStripped.charAt(i - 1) == '-') {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i), -1 + polyCoeList.get(getExponent(polyStringStripped, i)));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i), -1);
- }
- }
- else if(polyStringStripped.charAt(i - 1) == '+') {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i), 1);
- }
- }
- else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
- tempNumCount = 0;
- for(int n = i - 1; n >= 0; n--) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i),(-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- }
- else {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i),(Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i),(Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- }
- }
- else {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i), 1);
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
- polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
- }
- else {
- polyCoeList.set(getExponent(polyStringStripped, i), 1);
- }
- }
- }
- }
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(Character.isDigit(polyStringStripped.charAt(i))) {
- tempNumCount = 0;
- for(int n = i; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
- try {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- if(polyStringStripped.charAt(i - 1) == '-') {
- constantList.add(-1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- else {
- constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(polyStringStripped.charAt(i - 1) == '-') {
- constantList.add(-1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- else {
- constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(i + tempNumCount < polyStringStripped.length()) {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- else {
- constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- }
- if(constantList.isEmpty()) {
- constantList.add(0);
- }
- }
- private int getExponent(String polyStringStripped, int i) {
- int tempNumCount;
- try {
- if (polyStringStripped.charAt(i + 1) == '^') {
- tempNumCount = 0;
- for(int n = i + 2; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
- }
- else {
- return 1;
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- return 1;
- }
- }
- public String toString() {
- StringBuilder backToString = new StringBuilder();
- ArrayList<Integer> tempList = new ArrayList<Integer>();
- int tempConstant = 0;
- for(int i = 0; i < polyCoeList.size(); i ++) {
- tempList.add(polyCoeList.get(i));
- }
- for(int i = 0; i < polyCoeList.size(); i++) {
- if(polyCoeList.get(i) != 0 ) {
- backToString.insert(backToString.length(),Integer.toString(tempList.get(i)) + "x" + "^" + tempList.indexOf(tempList.get(i)) + "+");
- tempList.set(tempList.indexOf(tempList.get(i)), 0);
- }
- }
- for(int i = 0; i < constantList.size(); i++) {
- tempConstant = tempConstant + constantList.get(i);
- }
- backToString.insert(backToString.length(),tempConstant);
- return backToString.toString();
- }
- public PolynomialInterface add(PolynomialInterface other) {
- ArrayWithExponentAsIndexPolynomial poly1 = (ArrayWithExponentAsIndexPolynomial)other;
- return new ArrayWithExponentAsIndexPolynomial(toString() + "+" + poly1.toString());
- }
- public PolynomialInterface subtract(PolynomialInterface other) {
- ArrayWithExponentAsIndexPolynomial poly1 = (ArrayWithExponentAsIndexPolynomial)other;
- ArrayList<Integer> tempList = new ArrayList<Integer>();
- ArrayList<Integer> tempList1 = new ArrayList<Integer>();
- String tempStr;
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- tempList.add(poly1.polyCoeList.get(i));
- }
- for(int i = 0; i < poly1.constantList.size(); i++) {
- tempList.add(poly1.constantList.get(i));
- }
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- if(poly1.polyCoeList.get(i) != 0 ) {
- poly1.polyCoeList.set(i, -1 * poly1.polyCoeList.get(i));
- }
- }
- for(int i = 0; i < poly1.constantList.size(); i++) {
- poly1.constantList.set(i, -1 * poly1.constantList.get(i));
- }
- tempStr = poly1.toString();
- poly1.polyCoeList.removeAll(poly1.polyCoeList);
- poly1.constantList.removeAll(poly1.constantList);
- for(int i = 0; i < tempList.size(); i++) {
- poly1.polyCoeList.add(tempList.get(i));
- }
- for(int i = 0; i < tempList1.size(); i++) {
- poly1.constantList.add(tempList.get(i));
- }
- return new ArrayWithExponentAsIndexPolynomial(toString() + "+" + tempStr);
- }
- public void readPolynomial() {
- System.out.print(toString());
- }
- public static void main(String[] args)
- {
- }
- }
- class ArraySortedPolynomial implements PolynomialInterface {
- String originalString;
- ArrayList<Node> polyCoeList = new ArrayList<Node>();
- public ArraySortedPolynomial(String polyString) {
- originalString = polyString;
- String polyStringStripped = polyString.replaceAll("\\s+","");
- int tempNumCount = 0;
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(polyStringStripped.charAt(i) == 'x') {
- try {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i), -1, null));
- }
- else if(polyStringStripped.charAt(i - 1) == '+') {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i), 1, null));
- }
- else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
- tempNumCount = 0;
- for(int n = i - 1; n >= 0; n--) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
- }
- else {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
- }
- }
- else {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i),1, null));
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- polyCoeList.add(new Node(getExponent(polyStringStripped, i),1, null));
- }
- }
- }
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(Character.isDigit(polyStringStripped.charAt(i))) {
- tempNumCount = 0;
- for(int n = i; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
- try {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.add(new Node(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- else {
- polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.add(new Node(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- else {
- polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(i + tempNumCount < polyStringStripped.length()) {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- }
- else {
- polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
- }
- }
- }
- }
- }
- private int getExponent(String polyStringStripped, int i) {
- int tempNumCount;
- try {
- if (polyStringStripped.charAt(i + 1) == '^') {
- tempNumCount = 0;
- for(int n = i + 2; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
- }
- else {
- return 1;
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- return 1;
- }
- }
- public String toString() {
- StringBuilder backToString = new StringBuilder();
- addLikeTerms();
- for(int i = 0; i < polyCoeList.size(); i++) {
- if(polyCoeList.get(i).getExp() != 0 && polyCoeList.get(i).getCoe() != 0 ) {
- backToString.insert(backToString.length(),Integer.toString(polyCoeList.get(i).getCoe()) + "x" + "^" + polyCoeList.get(i).getExp() + "+");
- }
- }
- if(polyCoeList.get(polyCoeList.size() - 1).getExp() == 0 && polyCoeList.get(polyCoeList.size() - 1).getCoe() != 0 ) {
- backToString.insert(backToString.length(), polyCoeList.get(polyCoeList.size() - 1).getCoe());
- }
- else {
- backToString.deleteCharAt(backToString.length()- 1);
- }
- return backToString.toString();
- }
- public void addLikeTerms() {
- Collections.sort(polyCoeList);
- Collections.reverse(polyCoeList);
- for(int i = 0; i < polyCoeList.size(); i++) {
- try {
- if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
- polyCoeList.add(new Node(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i + 1).getCoe(), null));
- polyCoeList.remove(i);
- polyCoeList.remove(i);
- i--;
- Collections.sort(polyCoeList);
- Collections.reverse(polyCoeList);
- }
- }
- catch(Exception ArrayIndexOutOfBoundsException) {
- break;
- }
- }
- }
- public PolynomialInterface add(PolynomialInterface other) {
- ArraySortedPolynomial poly1 = (ArraySortedPolynomial)other;
- ArraySortedPolynomial poly2 = new ArraySortedPolynomial(toString() + "+" + poly1.toString());
- poly2.addLikeTerms();
- return new ArraySortedPolynomial(poly2.toString());
- }
- public PolynomialInterface subtract(PolynomialInterface other) {
- ArraySortedPolynomial poly1 = (ArraySortedPolynomial)other;
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- poly1.polyCoeList.get(i).turnNegative();
- }
- ArraySortedPolynomial poly2 = new ArraySortedPolynomial(toString() + "+" + poly1.toString());
- poly2.addLikeTerms();
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- poly1.polyCoeList.get(i).turnNegative();
- }
- return new ArraySortedPolynomial(poly2.toString());
- }
- public void readPolynomial() {
- System.out.print(toString());
- }
- }
- class LinkListPolynomial implements PolynomialInterface {
- String originalString;
- PolyLinkedList polyCoeList = new PolyLinkedList();
- public LinkListPolynomial(String polyString) {
- originalString = polyString;
- String polyStringStripped = polyString.replaceAll("\\s+","");
- int tempNumCount = 0;
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(polyStringStripped.charAt(i) == 'x') {
- try {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.insert(getExponent(polyStringStripped, i), -1);
- }
- else if(polyStringStripped.charAt(i - 1) == '+') {
- polyCoeList.insert(getExponent(polyStringStripped, i), -1);
- }
- else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
- tempNumCount = 0;
- for(int n = i - 1; n >= 0; n--) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
- polyCoeList.insert(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- else {
- polyCoeList.insert(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- polyCoeList.insert(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
- }
- }
- else {
- polyCoeList.insert(getExponent(polyStringStripped, i),1);
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- polyCoeList.insert(getExponent(polyStringStripped, i),1);
- }
- }
- }
- for (int i = 0; i < polyStringStripped.length(); i++){
- if(Character.isDigit(polyStringStripped.charAt(i))) {
- tempNumCount = 0;
- for(int n = i; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- try {
- if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
- try {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.insert(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- else {
- polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(polyStringStripped.charAt(i - 1) == '-') {
- polyCoeList.insert(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- else {
- polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- if(i + tempNumCount < polyStringStripped.length()) {
- if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
- polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- else {
- polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
- }
- }
- }
- }
- }
- private int getExponent(String polyStringStripped, int i) {
- int tempNumCount;
- try {
- if (polyStringStripped.charAt(i + 1) == '^') {
- tempNumCount = 0;
- for(int n = i + 2; n < polyStringStripped.length(); n++) {
- if(Character.isDigit(polyStringStripped.charAt(n))) {
- tempNumCount++;
- }
- else {
- break;
- }
- }
- return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
- }
- else {
- return 1;
- }
- }
- catch(Exception StringIndexOutOfBoundsException) {
- return 1;
- }
- }
- public String toString() {
- //System.out.println(polyCoeList);
- polyCoeList = polyCoeList.sort();
- StringBuilder backToString = new StringBuilder();
- //polyCoeList =addLikeTerms();
- for(int i = 0; i < polyCoeList.size(); i++) {
- if(polyCoeList.get(i).getExp() != 0 && polyCoeList.get(i).getCoe() != 0 ) {
- backToString.insert(backToString.length(),Integer.toString(polyCoeList.get(i).getCoe()) + "x" + "^" + polyCoeList.get(i).getExp() + "+");
- }
- }
- if(polyCoeList.get(polyCoeList.size() - 1).getExp() == 0 && polyCoeList.get(polyCoeList.size() - 1).getCoe() != 0 ) {
- backToString.insert(backToString.length(), polyCoeList.get(polyCoeList.size() - 1).getCoe());
- }
- else {
- backToString.deleteCharAt(backToString.length()- 1);
- }
- return backToString.toString();
- }
- public PolyLinkedList addLikeTerms() {
- //System.out.println(polyCoeList);
- polyCoeList = polyCoeList.sort();
- //System.out.println(polyCoeList);
- PolyLinkedList tempList = new PolyLinkedList();
- for(int i = 0; i < polyCoeList.size(); i++) {
- //System.out.println(polyCoeList);
- //System.out.println(tempList);
- try {
- if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
- tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i + 1).getCoe());
- polyCoeList.deleteAt(i + 1);
- if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
- i--;
- }
- }
- else {
- tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe());
- }
- }
- catch(Exception ArrayIndexOutOfBoundsException) {
- //System.out.println(polyCoeList);
- //System.out.println(tempList);
- if(polyCoeList.get(i).getExp() == polyCoeList.get(i - 1).getExp()) {
- tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i - 1).getCoe());
- }
- else {
- tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe());
- }
- break;
- }
- }
- tempList = tempList.sort();
- return tempList;
- }
- public PolynomialInterface add(PolynomialInterface other) {
- LinkListPolynomial poly1 = (LinkListPolynomial)other;
- LinkListPolynomial poly2 = new LinkListPolynomial(toString() + "+" + poly1.toString());
- poly2.polyCoeList = poly2.addLikeTerms();
- return new LinkListPolynomial(poly2.toString());
- }
- public PolynomialInterface subtract(PolynomialInterface other) {
- LinkListPolynomial poly1 = (LinkListPolynomial)other;
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- poly1.polyCoeList.get(i).turnNegative();
- }
- LinkListPolynomial poly2 = new LinkListPolynomial(toString() + "+" + poly1.toString());
- poly2.polyCoeList = poly2.addLikeTerms();
- for(int i = 0; i < poly1.polyCoeList.size(); i++) {
- poly1.polyCoeList.get(i).turnNegative();
- }
- return new LinkListPolynomial(poly2.toString());
- }
- public void readPolynomial() {
- System.out.print(toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement