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;
    }
    }

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

  • 相关阅读:
    第二节,神经网络中反向传播四个基本公式证明——BackPropagation
    第一节,windows和ubuntu下深度学习theano环境搭建
    oracle和SQLserver数据库中select into 的区别
    Mysql与Oracle区别
    SQLserver 设置自增为显式插入
    SQL 存储过程入门(事务)(四)
    SQLSqlserver中如何将一列数据,不重复的拼接成一个字符串
    SQL命令优化(积累)
    手机游戏运营主要的指标是什么? 7天活跃, 14天活跃 ARPU ?如何提升游戏 app 的虚拟道具的收入?
    从用户心理看游戏运营和推广
  • 原文地址:https://www.cnblogs.com/cdlyy/p/10200953.html
Copyright © 2011-2022 走看看