* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package poly;
//import packages
import java.io.*;
import java.util.*;
import java.lang.*;
/**
*
* @author obobotette0
*/
public class Poly {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
//iterator variable
int i = 0;
//Create strings for input and output
String read = new String();
//Four rows from sample data
String[] save = new String[4];
String inputTxt = new String();
String outputTxt = new String();
//Directory location of files
inputTxt = "..\\Poly\\src\\poly\\input.txt";
outputTxt = "..\\Poly\\src\\poly\\output.txt";
//Create files for read and write
File inputFile = new File(inputTxt);
BufferedReader readFile = new BufferedReader(new FileReader(inputFile));
//loop to read file line by line
read = readFile.readLine();
while (read != null) {
save[i] = read;
read = readFile.readLine();
i++;
}
//close input file
readFile.close();
//Begin output files
FileWriter outputFile = new FileWriter(outputTxt);
PrintWriter writeFile = new PrintWriter(outputFile);
String write = new String();
//Begin separating string
String deg1 = save[0];
String deg2 = save[2];
int iDeg1 = Integer.valueOf(deg1);
int iDeg2 = Integer.valueOf(deg2);
String[] nums1 = new String[i];
nums1 = save[1].split(" ");
String[] nums2 = new String[i];
nums2 = save[3].split(" ");
int[] mathResult = new int[i];
//To calulate the multiplying product
int[][] mathResult1 = new int[2][2];
int multiply = 1;
int[] multiDeg = new int[i];
if (iDeg1 == iDeg2) {
//Variable for viewing results
String[] result = new String[i];
int degree = iDeg1;
//Calculate result
for (int x = 0; x < nums1.length; x++) {
//add matching nodes
mathResult[x] = Integer.valueOf(nums1[x]) + Integer.valueOf(nums2[x]);
//if greater than zero print with exponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + "x^" + degree + " ";
writeFile.append(result[x]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + " ";
writeFile.append(result[x]);
} else {
}
}
--degree;
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg1;
for (int y = 0; y < nums1.length; y++) {
//subtract matching nodes
mathResult[y] = Integer.valueOf(nums1[y]) - Integer.valueOf(nums2[y]);
//if greater than zero print with exponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + "x^" + degree + " ";
writeFile.append(result[y]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + " ";
writeFile.append(result[y]);
} else {
}
}
--degree;
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg1;
int count = 0;
for (int z = 0; z < nums1.length; z++) {
for (int z1 = 0; z1 < nums2.length; z1++) {
//multiply matching nodes
//if coeffiecients are greater than zero multiply together
if (Integer.valueOf(nums1[z]) != 0 && Integer.valueOf(nums2[z1]) != 0) {
mathResult[z1] = Integer.valueOf(nums1[z]) * Integer.valueOf(nums2[z1]);
//else negate zero by adding node before multiplying
}
}
//Calculate degree of result
multiDeg[z] = degree + iDeg2;
--degree;
//Form output string
result[z] = mathResult[z] + "x^" + multiDeg[z] + " ";
writeFile.append(result[z]);
count = z;
}
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg1;
//Loop to add to array
for (int z = 0; z < nums1.length; z++) {
if (Integer.valueOf(nums1[z]) == 0) {
result[count + z] = nums1[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg1;
//Loop to add to array
for (int z = 0; z < nums2.length; z++) {
if (Integer.valueOf(nums2[z]) == 0) {
result[count + z] = nums2[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
writeFile.println("");
} else if (iDeg1 > iDeg2) {
String[] result = new String[i];
int degree = iDeg1;
//count difference in arrays
int count = 0;
for (int x = 0; x < nums1.length; x++) {
//if one array in longer than the other
//copy contents till sizes are equal
if (degree > iDeg2) {
mathResult[x] = Integer.valueOf(nums1[x]);
result[x] = mathResult[x] + "x^" + degree + " ";
writeFile.append(result[x]);
--degree;
count++;
} else {
//add matching nodes
mathResult[x] = Integer.valueOf(nums1[x]) + Integer.valueOf(nums2[x - count]);
//if greater than zero print with exponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + "x^" + degree + " ";
writeFile.append(result[x]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + " ";
writeFile.append(result[x]);
} else {
}
}
--degree;
}
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg1;
//count difference in arrays
count = 0;
for (int y = 0; y < nums1.length; y++) {
//if one array in longer than the other
//copy contents till sizes are equal
if (degree > iDeg2) {
mathResult[y] = Integer.valueOf(nums1[y]);
result[y] = mathResult[y] + "x^" + degree + " ";
writeFile.append(result[y]);
--degree;
count++;
} else {
//subtract matching nodes
mathResult[y] = Integer.valueOf(nums1[y]) - Integer.valueOf(nums2[y - count]);
//if greater than zero print with eyponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + "x^" + degree + " ";
writeFile.append(result[y]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + " ";
writeFile.append(result[y]);
} else {
}
}
--degree;
}
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg1;
count = 0;
for (int z = 0; z < nums1.length; z++) {
for (int z1 = 0; z1 < nums2.length; z1++) {
//multiply matching nodes
//if coeffiecients are greater than zero multiply together
if (Integer.valueOf(nums2[z1]) != 0 && Integer.valueOf(nums1[z]) != 0) {
mathResult1[z][z1] = Integer.valueOf(nums2[z1]) * Integer.valueOf(nums1[z]);
}
}
count = z;
}
//Using an (x + y)(x + y) example
//Reassign array to create an x^2 + 2xy + y^2 format
mathResult[0] = mathResult1[0][0];
mathResult[1] = mathResult1[0][1] + mathResult1[1][0];
mathResult[2] = mathResult1[1][1];
multiply += iDeg1;
//Add variable to string and append to file
for (int x = 0; x < mathResult.length - 1; x++) {
result[x] = mathResult[x] + "x^" + multiply + " ";
writeFile.append(result[x]);
multiply--;
}
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg1;
//Loop to add to array
for (int z = 0; z < nums1.length; z++) {
if (Integer.valueOf(nums1[z]) == 0) {
result[count + z] = nums1[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg1;
//Loop to add to array
for (int z = 0; z < nums2.length; z++) {
if (Integer.valueOf(nums2[z]) == 0) {
result[count + z] = nums2[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
writeFile.println("");
} else if (iDeg1 < iDeg2) {
String[] result = new String[i];
int degree = iDeg2;
//count difference in arrays
int count = 0;
for (int x = 0; x < nums2.length; x++) {
//if one array in longer than the other
//copy contents till sizes are equal
if (degree > iDeg1) {
mathResult[x] = Integer.valueOf(nums2[x]);
result[x] = mathResult[x] + "x^" + degree + " ";
writeFile.append(result[x]);
--degree;
count++;;
} else {
//add matching nodes
mathResult[x] = Integer.valueOf(nums1[x - count]) + Integer.valueOf(nums2[x]);
//if greater than zero print with exponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + "x^" + degree + " ";
writeFile.append(result[x]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[x] != 0) {
result[x] = mathResult[x] + " ";
writeFile.append(result[x]);
} else {
}
}
--degree;
}
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg2;
//count difference in arrays
count = 0;
for (int y = 0; y < nums2.length; y++) {
//if one array in longer than the other
//copy contents till sizes are equal
if (degree > iDeg1) {
mathResult[y] = Integer.valueOf(nums2[y]);
result[y] = "-" + mathResult[y] + "x^" + degree + " ";
writeFile.append(result[y]);
--degree;
count++;
} else {
//subtract matching nodes
mathResult[y] = Integer.valueOf(nums1[y - count]) - Integer.valueOf(nums2[y]);
//if greater than zero print with eyponenet
if (degree > 0) {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + "x^" + degree + " ";
writeFile.append(result[y]);
} else {
}
//else print the constant
} else {
//Only show non zero results
if (mathResult[y] != 0) {
result[y] = mathResult[y] + " ";
writeFile.append(result[y]);
} else {
}
}
--degree;
}
}
//Write to output file
writeFile.flush();
writeFile.println("");
degree = iDeg2;
count = 0;
for (int z = 0; z < nums1.length; z++) {
for (int z1 = 0; z1 < nums2.length; z1++) {
//multiply matching nodes
//if coeffiecients are greater than zero multiply together
if (Integer.valueOf(nums2[z1]) != 0 && Integer.valueOf(nums1[z]) != 0) {
mathResult1[z][z1] = Integer.valueOf(nums2[z1]) * Integer.valueOf(nums1[z]);
}
}
count = z;
}
//Using an (x + y)(x + y) example
//Reassign array to create an x^2 + 2xy + y^2 format
mathResult[0] = mathResult1[0][0];
mathResult[1] = mathResult1[0][1] + mathResult1[1][0];
mathResult[2] = mathResult1[1][1];
multiply += iDeg2;
//Add variable to string and append to file
for (int x = 0; x < mathResult.length - 1; x++) {
result[x] = mathResult[x] + "x^" + multiply + " ";
writeFile.append(result[x]);
multiply--;
}
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg2;
//Loop to add to array
for (int z = 0; z < nums1.length; z++) {
if (Integer.valueOf(nums1[z]) == 0) {
result[count + z] = nums1[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
//Resize array for polynomials with zero coefficients
result = Arrays.copyOf(result, result.length + count);
degree = iDeg2;
//Loop to add to array
for (int z = 0; z < nums2.length; z++) {
if (Integer.valueOf(nums2[z]) == 0) {
result[count + z] = nums2[z] + "x^" + degree + " ";
writeFile.append(result[count + z]);
}
--degree;
}
//Write to output file
writeFile.flush();
writeFile.println("");
}
}
}
No comments:
Post a Comment
Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!