<!--寫在代碼之前-->
詳細思路在另外一篇PTA1-3作業總結里
import java.math.BigInteger; import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; /* * 此類用於對多項式表達式求導 * by@kaix2021/03/23 */ //Derivation 求導 public class Main { public static void main(String[] args){ Scanner in=new Scanner(System.in); String temp=in.nextLine(); DvForString dv=new DvForString(); dv.setPolynthic(temp);//預處理輸入的字符串 dv.print(); //根據合法性作出不同輸出 // System.out.println(); // dv.printEveryElement(); } } class DvForString{ private static LinkedList<String> item;//存儲各項,此題並不需要 private static String sign,number,regex,regex1,regex2,regex3,concat,concatend,specialnum,end;//各項正則 //靜態區,字符串 static { //靜態區只在類被加載時被執行一次 item=new LinkedList<>(); sign="(?:[+|-])"; number="(?:([1-9](\\s*\\d*\\s*)*))"; //全是常數項時的特判 specialnum="((?:(\\s*([+|-]?[1-9]+\\s*\\d*)\\s*))*)"; //帶x項,允許系數中間內有空格 //有不為0的系數和指數 regex="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; //x只有指數 regex1="(?:(x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; //x只有系數 regex2="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x))"; //x前面系數,指數都沒有 regex3="(?:x)"; concat="("+regex+"|"+regex1+"|"+regex2+"|"+regex3+")"; //數字和帶x項或在一起構成多項式中的一項 concatend="("+concat+"|"+number+")"; //多項式表達式 ,首項可以無+,-, 后續項必須有 end="(?:("+"\\s*"+sign+"?\\s*"+concatend+"(?:\\s*"+sign+"\\s*"+concatend+"\\s*)*"+"))"; } //Polynthic 多項式 private String Polynthic=null; public LinkedList<String> getItem() { return item; } //無參構造 public DvForString(){} //有參構造,可以直接通過字符串數組名賦值另一個相同大小和類型的數組 public DvForString(String polynthic) { this.Polynthic = polynthic; } //對私有變量獲取和,賦值的方法 public String getPolynthic() { return Polynthic; } public void setPolynthic(String polynthic) { this.Polynthic = polynthic; } //合法性檢驗 private boolean isLegal() { boolean flag=true; String s=Polynthic; //全常數檢驗 if(s.matches(specialnum)) return true; //復雜多項式檢驗 if(!s.matches(end)) flag=false; return flag; } //打印合法輸入中每一項 public void printEveryElement() { System.out.println(); if(isLegal()) { for(String e: item) System.out.println(e);} else System.out.println("Wrong Format"); } //打印 public void print() { if(isLegal()) printLegal();/合法結果打印 else printNotLegal();//不合法結果打印 } //打印不合法輸出 private void printNotLegal() { System.out.println("Wrong Format"); } //打印合法輸出 private void printLegal() { //拷貝一份給字符串s String s=Polynthic; s=s.replace(" ",""); char[] t=s.toCharArray(); for(int i=0;i<s.length()-1;i++) if(t[i]=='^'&&t[i+1]=='-') t[i+1]='#'; //直接通過tostring轉換字符數組會出錯,所以一個個字符拼接 String s1=""; for(int i=0;i<t.length;i++) s1=s1+t[i]; //再來整體替換- s1=s1.replace("^+","^"); s1=s1.replace("-","+-"); //如果+出現在第一個位置會報異常,要用\\+ String [] id=s1.split("\\+"); //加號輸出標記 int flag=0; //無項時輸出標記,標記若最終為0,最后輸出0 int lazy=0; //所有常數直接打印不需要特意放+,帶x項,對於大於0項,前面有其他項要打印出來 for(String e :id) { //切開后對每個元素進行分析處理,#要替換成- e=e.replace("#","-"); if(e!=null) item.addLast(e); int start=e.indexOf("x"); int mid=e.indexOf("*"); int end=e.indexOf("^"); //x,^都有 if(start!=-1&&end!=-1) { //系數不為1,存在* if(mid!=-1) { BigInteger pro=new BigInteger(e.substring(0,mid)); BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pro.multiply(pos);//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//減法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } lazy=1; if(flag==0) flag=1; } //沒有*,系數為負 else { if(e.charAt(0)=='-') { BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pos.multiply(new BigInteger(-1+""));//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//減法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } } //沒*,系數不為負 else { BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pos.multiply(new BigInteger(1+""));//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//減法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } } lazy=1; if(flag==0) flag=1; } }//^,x都存在的分界線 //有x,沒有指數 else if(start!=-1) { //有* if(mid!=-1) { BigInteger num=new BigInteger(e.substring(0, mid)); // if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(num); } //沒有* else { BigInteger num=null; if(e.charAt(0)=='x') num=new BigInteger("1"); else if(e.charAt(0)=='-') num=new BigInteger("-1"); else num=new BigInteger(e.substring(0, start)); // if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(num); } lazy=1; if(flag==0) flag=1; } //常數 else System.out.print(""); }//大for結尾 if(lazy==0) System.out.println(0); } }
貼一張結果圖,不同屆的同學,測試點的提示可能不同~
此題最大的天坑點,是求導得到的正數時不需要+的,如3*x+4*x 應該輸出34而不是3+4