java 链表实现多项式加法!
来源:互联网 发布:淘宝质量鉴定什么意思 编辑:程序博客网 时间:2024/06/05 04:44
<p style="color:rgb(85, 85, 85);"></p>package pers.mao.test.ACM;import java.util.Scanner;class Node {int coef;int exp;Node next;public Node(int coef, int exp) {this.coef = coef;this.exp = exp;this.next = null;}}class PolyLinked {public Node frist;public Node last;public void createNode(int coef, int exp) {Node newNode = new Node(coef, exp);if (frist == null) {frist = newNode;last = newNode;} else {last.next = newNode;last = newNode;}}public int size() {Node newNode;int i=0;if(frist==null)return 0;if(frist==last)return 1;newNode=frist;if(newNode.next!=last)newNode.next=newNode;i++;return i;}public void printPolyList() {Node current = frist;StringBuilder print = new StringBuilder(""); //用于显示表达式;while (current != null) {if (current.coef != 0 && current.exp == 1) { //指数为1,StringBuilder中有数据,若系数为负数则删除之前的加号(注意加号前后都需空格),再追加新的数据;if (current.coef < 0 && print.length() >= 2) print.delete(print.length() - 2, print.length() - 1);//指数为1,若系数为1则不显示系数;if (current.coef == 1) {print.append("X + ");}else {print.append(current.coef + "X + ");}} else if (current.coef != 0 && current.exp == 0) { //指数为0,即常数项;//指数为0,StringBuilder中有数据,若系数为负数则删除之前的加号(注意加号前后都需空格),再追加新的数据;if (current.coef < 0 && print.length() >= 2) print.delete(print.length() - 2, print.length() - 1);print.append(current.coef);} else if (current.coef != 0 && current.exp != 0) { //指数大于1;//StringBuilder中有数据,若系数为负数则删除之前的加号(注意加号前后都需空格),再追加新的数据;if (current.coef < 0 && print.length() >= 2)print.delete(print.length() - 2, print.length() - 1);if (current.coef == 1) { print.append( "X^" + current.exp + " + ");} else if (current.coef == -1) {print.append("-"+"X^" + current.exp + " + ");}else {print.append(current.coef + "X^" + current.exp + " + ");}}current = current.next;}if (last.coef == 0)print.delete(print.length() - 2, print.length() - 1);System.out.print(print.toString());}public PolyLinked Polyadd(PolyLinked b) {int max = Math.max(this.frist.exp, b.frist.exp);int i = 0;int[][] sum = new int[max + 1][2];PolyLinked tem = new PolyLinked();PolyLinked a = new PolyLinked();a = this;while (a.frist != null && b.frist != null) {if (a.frist.exp == b.frist.exp) {sum[i][0] = a.frist.coef + b.frist.coef;sum[i][1] = a.frist.exp;a.frist = a.frist.next;b.frist = b.frist.next;i++;} else if (a.frist.exp > b.frist.exp) {sum[i][0] = a.frist.coef;sum[i][1] = a.frist.exp;a.frist = a.frist.next;i++;} else if (b.frist.exp > a.frist.exp) {sum[i][0] = b.frist.coef;sum[i][1] = b.frist.exp;b.frist = b.frist.next;i++;}}while (a.frist != null) {sum[i][0] = a.frist.coef;sum[i][1] = a.frist.exp;a.frist = a.frist.next;i++;}while (b.frist != null) {sum[i][0] = b.frist.coef;sum[i][1] = b.frist.exp;b.frist = b.frist.next;i++;}int maxnuber = i - 1;for (int j = 0; j < maxnuber + 1; j++) {tem.createNode(sum[j][0], sum[j][1]);}return tem;}}public class PolyLinkedAdd {public static void main(String[] args) {int[] coef = new int[10];int[] exp = new int[10];PolyLinked a = new PolyLinked();PolyLinked b = new PolyLinked();PolyLinked c = new PolyLinked();Scanner scan = new Scanner(System.in); // 接收键盘的输入String str = scan.nextLine();int line = 0;while (!str.equals(" ")) {String[] num = str.split(" ");coef[line] = Integer.parseInt(num[0]);exp[line] = Integer.valueOf(num[1]);line++;str = scan.nextLine();}scan.close();/* flag 用于存储输入的0 和-1的位置,以便将两组数据分开 */int flag[] = new int[2];int ii = 0;for (int j = 0; j < line; j++) {if (coef[j] == 0 && exp[j] == -1) {flag[ii] = j;ii++;}}/* * data用于存储两个多项式的各项系数,长度分别为各多项式的最高项加1 * flag[0]为exp[]中,第一次出现0,-1的位置,所以加1就表示第二组多项式的第一个元素 */int[] data1 = new int[exp[0] + 1];int[] data2 = new int[exp[flag[0] + 1] + 1];/* 前两个for循环,先给多项式的所有系数,初始化为0,后两个for循环再为非零项赋值 */for (int k = 0; k < data1.length; k++) {data1[k] = 0;}for (int k = 0; k < data2.length; k++) {data2[k] = 0;}for (int k = 0; k < flag[0]; k++) {data1[data1.length - 1 - exp[k]] = coef[k];}for (int k = flag[0] + 1; k < flag[1]; k++) {data2[data2.length - 1 - exp[k]] = coef[k];}/* 输出显示各多项式! */System.out.print("原多项式: \nA=");for (int i = 0; i < data1.length; i++)a.createNode(data1[i], data1.length - i - 1);for (int i = 0; i < data2.length; i++)b.createNode(data2[i], data2.length - i - 1);a.printPolyList();System.out.print("\nB=");b.printPolyList();System.out.print("\n多项式相加的结果: \nC=");c = a.Polyadd(b);c.printPolyList();}}
0 0
- java 链表实现多项式加法!
- Java链表练习实现一元多项式的加法
- 多项式加法的Java实现
- 链表实现一元多项式加法
- 链表实现多项式加法和乘法
- 多项式加法,链表方法实现
- 多项式加法运算链表实现
- 链表实现两个多项式的加法
- 链表的多项式加法链表的多项式加法
- 多项式加法 链表方法
- 链表实现多项式的加法和乘法
- 链表实现多项式的加法和乘法
- 多项式加法利用链表实现(c语言)
- 一元多项式加法与乘法运算链表实现
- 多项式乘法与加法运算(链表实现)
- 【数据结构】C++链表实现一元多项式的加法
- C++实现多项式加法
- 多项式加法的实现
- 《Java编程思想第四版》笔记---14章 附:包装类--装箱和拆箱
- 二分查找
- 64. Minimum Path Sum DP经典问题
- 创新科技,只为尊重音乐原声:dFiM睿妙深度剖析
- Java之wait()/sleep()和notify()/notifyAll()
- java 链表实现多项式加法!
- TabBar 的selecedImage无法显示设置图片问题 和item文字颜色选中设置
- 基于netty的网络聊天室
- 浅谈iOS10及Xcode8
- xcoj1227-电梯
- 寻迹小车代码
- 二叉树的创建,递归和非递归遍历
- 聊聊java高并发系统之异步非阻塞
- CentOS设置开机等待时间