zoukankan      html  css  js  c++  java
  • 多项式计算器

    /**

    完成多项式的计算器,可以进行多项式的加法,减法,乘法三种运算。

    */

    import java.util.ArrayList;

    import java.util.List;
    import java.util.Scanner;
    import java.util.Stack;

    public class ExpressionMain {

    public static void main(String[] args) {
    expressionCalculator();
    }

    private static void expressionCalculator() {

    System.out.println("This is a polynomials program. " +
    "Please enter a valid command: " +
    "[?] Read a Polynomial. " +
    "[=] Return a Polynomial. " +
    "[+] Sum two Polynomial. " +
    "[-] Difference two Polynomial。 " +
    "[*] Mult one Polynomial with a term Polynomial. " +
    "[Q] Quit. ");

    Scanner scanner = new Scanner(System.in);
    boolean isTheWorldAlive = true;

    Stack<Term> stack = new Stack<Term>();

    while (isTheWorldAlive) {
    System.out.println("Select command and presss <Enter>:");
    String command = scanner.next();
    if ("?".equals(command)) {
    boolean isCurrentExpressionInProgress = true;

    Term current = new Term();
    boolean firstTime = true;
    while (isCurrentExpressionInProgress) {
    System.out.println("coefficient?");
    int xishu = scanner.nextInt();
    if (xishu != 0) {
    System.out.println("exponent?");
    int zhishu = scanner.nextInt();
    if (zhishu != 0) {
    if(firstTime){
    current.setXishu(xishu);
    current.setZhishu(zhishu);
    firstTime = false;
    }else{
    Term temp = current;

    while(temp.next != null){
    temp = temp.next;
    }
    temp.next = new Term(xishu, zhishu);

    }

    } else {
    System.out.println("The following has been pushed to Stack:");
    System.out.println(current.toString());
    stack.push(current);
    isCurrentExpressionInProgress = false;
    }

    } else {
    System.out.println("The following has been pushed to Stack:");
    System.out.println(current.toString());
    stack.push(current);
    isCurrentExpressionInProgress = false;
    }
    }

    } else if("+".equals(command)){
    System.out.println(Expression.add(stack.pop(), stack.pop()));

    }else if("-".equals(command)){
    System.out.println(Expression.minus(stack.pop(), stack.pop()));

    }else if("*".equals(command)){
    System.out.println(Expression.mutiple(stack.pop(), stack.pop()));
    }
    else if ("Q".equalsIgnoreCase(command)) {
    isTheWorldAlive = false;
    }


    }
    }

    }

    class Term {
    private int xishu;
    private int zhishu;

    Term next = null;

    Term(){}
    Term(int xishu, int zhishu){
    this.xishu = xishu;
    this.zhishu = zhishu;
    this.next = null;
    }
    public int getXishu() {
    return xishu;
    }

    public void setXishu(int xishu) {
    this.xishu = xishu;
    }

    public int getZhishu() {
    return zhishu;
    }

    public void setZhishu(int zhishu) {
    this.zhishu = zhishu;
    }

    public String toString(){
    String toString = xishu+"X^"+zhishu;
    Term term = this.next;
    while (term != null){
    int nextXishu = term.getXishu();
    toString = toString + ((nextXishu>0)?"+"+nextXishu:nextXishu)+"X^"+term.getZhishu();
    term = term.next;
    }

    return toString;
    }
    }

    class Expression {
    public static Term add(Term t1, Term t2){
    if(t1 == null){
    return t2;
    }else if(t2 == null){
    return t1;
    }else{
    Term term = new Term();
    if(t1.getZhishu() > t2.getZhishu()){
    term.setZhishu(t1.getZhishu());
    term.setXishu(t1.getXishu());
    t1 = t1.next;
    }else if(t1.getZhishu() == t2.getZhishu()){
    term.setZhishu(t1.getZhishu());
    term.setXishu(t1.getXishu() + t2.getXishu());
    t1 = t1.next;
    t2 = t2.next;
    }else{
    term.setZhishu(t2.getZhishu());
    term.setXishu(t2.getXishu());
    t2 = t2.next;
    }
    term.next = add(t1, t2);

    return term;
    }

    }

    public static Term minus(Term t1, Term t2){
    if(t1 == null){
    return t2;
    }else if(t2 == null){
    return t1;
    }else{
    Term term = new Term();
    if(t1.getZhishu() > t2.getZhishu()){
    term.setZhishu(t1.getZhishu());
    term.setXishu(t1.getXishu());
    t1 = t1.next;
    }else if(t1.getZhishu() == t2.getZhishu()){
    term.setZhishu(t1.getZhishu());
    term.setXishu(t2.getXishu() - t1.getXishu());
    t1 = t1.next;
    t2 = t2.next;
    }else{
    term.setZhishu(t2.getZhishu());
    term.setXishu(t2.getXishu() * -1);
    t2 = t2.next;
    }

    term.next = minus(t1, t2);

    return term;
    }


    }

    public static Term mutiple(Term t1, Term t2){
    Term temTerm;
    if(t1.getXishu() < t2.getXishu()){
    temTerm = t1;
    t1 = t2;
    t2 = temTerm;
    }

    List<Term> expressions = new ArrayList<Term>();
    expressions.add(mutipleSingleTermToWholeTerm(t1, t2));
    while (t2.next != null){
    t2 = t2.next;
    expressions.add(mutipleSingleTermToWholeTerm(t1, t2));
    }

    Term term = expressions.get(0);
    for(int i=1; i<expressions.size(); i++){
    term = add(term, expressions.get(i));
    }

    return term;
    }

    private static Term mutipleSingleTermToWholeTerm(Term t1, Term t2){
    Term term = new Term();
    term.setZhishu(t1.getZhishu() + t2.getZhishu());
    term.setXishu(t1.getXishu() * t2.getXishu());
    while (t1.next != null){
    t1 = t1.next;
    term.next = mutipleSingleTermToWholeTerm(t1, t2);
    }

    return term;
    }
    }

    //运行效果:(展示实现两个多项式的加法)

  • 相关阅读:
    JQueryMobile开发必须的知道的知识
    15款很棒的 JavaScript 开发工具
    浅谈 JavaScript 编程语言的编码规范
    也谈谈js的压缩,jquery压缩。【转】
    jQuery实现点击单选按钮切换选中状态效果
    JavaScript入门学习书籍的阶段选择
    试读《基于MVC的JavaScript Web富应用开发》— 不一样的JavaScript
    javaScript之function定义
    利用Powershell自动部署asp.net mvc网站项目 (一)
    【好文收藏】javascript中event对象详解
  • 原文地址:https://www.cnblogs.com/cdlyy/p/10200953.html
Copyright © 2011-2022 走看看