import java.io.*; import java.util.*; public class Polynoom2{ public static void main(String[] arg) throws IOException{ BufferedReader lugeja=new BufferedReader(new FileReader("poly2.txt")); String rida=lugeja.readLine(); int[] p1=tykeldaPolynoom(rida); System.out.println(Arrays.toString(p1)); rida=lugeja.readLine(); int[] p2=tykeldaPolynoom(rida); System.out.println(Arrays.toString(p2)); int[] p3=liidaPolynoomid(p1, p2); trykiPolynoom(p3); } static int[] liidaPolynoomid(int[] p1, int[] p2){ int[] vastusp=new int[10]; for(int aste=0; aste1; aste--){ if(p[aste]!=0){ if(p[aste]>0){System.out.print("+");} System.out.print(p[aste]+"x^"+aste); } } if(p[1]!=0){ if(p[1]>0){System.out.print("+");} System.out.print(p[1]+"x"); } if(p[0]!=0){ if(p[0]>0){System.out.print("+");} System.out.print(p[0]); } } public static boolean kasMark(String tykk){ return tykk.equals("+") || tykk.equals("-"); } public static int[] tykeldaPolynoom(String rida){ int[] kordajad=new int[10]; if(!rida.startsWith("-")){ rida="+"+rida; } StringTokenizer stk=new StringTokenizer(rida, "x+-^", true); String mark=stk.nextToken(); while(stk.hasMoreTokens()){ int kordaja=1; int aste=0; String tykk=stk.nextToken(); if(tykk.equals("x")){ kordaja=1; } else { kordaja=Integer.parseInt(tykk); if(stk.hasMoreTokens()){ tykk=stk.nextToken(); //x voi mark } } if(mark.equals("-")){ kordaja=-kordaja; } if(!kasMark(tykk) && stk.hasMoreTokens()){ tykk=stk.nextToken(); aste=1; if(kasMark(tykk)){ mark=tykk; } else { aste=Integer.parseInt(stk.nextToken()); if(stk.hasMoreTokens()){ mark=stk.nextToken(); } } } System.out.println("kordaja "+kordaja + " aste "+aste); kordajad[aste]=kordaja; } return kordajad; } }