本文主要是介绍多项式除法输出整个竖式(java实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.算法思想:
1.1 对多项式进行拆分
1.1.1 拆为单项式并存储于数组zDAxs中(拆成单项式方便后面获取系数和幂):
关键方法:1.fzs.length():获取多项式“fzs”的长度(所含字符的个数);
2.fzs.charAt(i):获取多项式的第i的字符;
3. String.valuef(item):将字符‘item’转为字符串
for(int i=0;i<10;i++) {zDAxs[i]="";}for(int i=0;i<fzs.length();i++) {char item=fzs.charAt(i);String item1=String.valueOf(item);if(item=='+'||item=='-') {n+=1;}zDAxs[n]=zDAxs[n]+item1;}
1.1.2 获取系数
关键方法:1.Integer.parseInt(STR):将字符串STR转为整型;
2.Double.valueOf(str):将字符串str转为double型;
for(int i=0;i<=n;i++) {String zXS1="";for(int m=0;m<zDAxs[i].length();m++) {if(zDAxs[i].charAt(0)=='+') {if(zDAxs[i].charAt(1)>='a'&&zDAxs[i].charAt(1)<='z') {zXS1="1";break;}if(zDAxs[i].charAt(m)>='a'&&zDAxs[i].charAt(m)<='z') break;zXS1=zXS1+String.valueOf(zDAxs[i].charAt(m));}if(zDAxs[i].charAt(0)=='-') {if(zDAxs[i].charAt(1)>='a'&&zDAxs[1].charAt(0)<='z') {zXS1="-1";break;}if(zDAxs[i].charAt(m)>='a'&&zDAxs[i].charAt(m)<='z') break;zXS1=zXS1+String.valueOf(zDAxs[i].charAt(m));}if(zDAxs[i].charAt(0)>='a'&&zDAxs[i].charAt(0)<='z') {zXS1="1";}if(zDAxs[i].charAt(0)>='0'&&zDAxs[i].charAt(0)<='9') {if(zDAxs[i].charAt(m)>='a'&&zDAxs[i].charAt(m)<='z') break;zXS1=zXS1+String.valueOf(zDAxs[i].charAt(m));}}
1.1.1 获取幂
for(int i=0;i<=n;i++) {int c=0;for(int m=1;m<zDAxs[i].length();m++) {if(zDAxs[i].charAt(m-1)=='^') M[i]=Integer.parseInt(String.valueOf(zDAxs[i].charAt(m)));}for(int m=0;m<zDAxs[i].length();m++) {if(zDAxs[i].charAt(m)=='^'||(zDAxs[i].charAt(m)>='a'&&zDAxs[i].charAt(m)<='z')) c+=1;}if(c==0) M[i]=0;if(c==1) M[i]=1;System.out.print(M[i]);System.out.print(" ");}
1.2 将幂和系数按幂从高到低排序:
int[] D=new int[n+1];for(int i=0;i<=n;i++) {D[i]=M[i];}M[n+1]=1;for(int i=0;i<=n;i++) {if(D[i]==0) d=i;M[n+1]=0;}for(int i=0;i<=n;i++){int max=0;int b=0;for(int m=0;m<=n;m++) {if(max<=D[m]&&D[m]>0){max=D[m];b=m;}}M[i]=D[b];XH[i]=b;D[b]=0;}if(M[n+1]==0) {M[n]=0;XH[n]=d;}
1.2.1 将除项按幂从高到低重新排列为一串字符方便后续输出:
for(int i=0;i<=n;i++) {if(i==0&&XH[i]!=0&&zDAxs[XH[i]].charAt(0)=='+') {zDAxs[XH[i]]=zDAxs[XH[i]].substring(1);}if(i!=0&&XH[i]==0&&zDAxs[XH[i]].charAt(0)!='+'&&zDAxs[XH[i]].charAt(0)!='-') {zDAxs[XH[i]]="+"+zDAxs[XH[i]];}if(XH[i]!=0&&zDAxs[XH[i]].charAt(0)!='+'&&zDAxs[XH[i]].charAt(0)!='-') {zDAxs[XH[i]]="+"+zDAxs[XH[i]];}CS=CS+zDAxs[XH[i]];}
1.2.2 对系数进行计算:
算法思想:商项的第i项系数等于第i排被除项第一项系数除以除数的第一项系数;
减项的项数等于除数的项数,i排减项的第n项系数等于商的第i项系数乘以除项的第n项系数;
i+1排被除项的系数等于i-1排被除项的系数减减项对应项的系数;(这里需要注意:将被除项的系数向前移一个位置,要不然会与减项的对应项错开)即:
XBCS1[i+1][n]=XBCS1[i+1][n+1];
对被除项的第FMS.n(除数的项数)进行赋值:XBCS1[i+1][FMS.n]=XBCS1[0][FMS.n+i+1];
for(int i=0;i<=FZS.n;i++) {XBCS[i]=FZS.ZXS[i];MBCS[i]=FZS.M[i];}for(int i=0;i<=FMS.n;i++) {XCS[i]=FMS.ZXS[i];MCS[i]=FMS.M[i];}System.out.print("\n");for(int i=0;i<=FZS.n;i++) {XBCS1[0][i]=XBCS[i];mXBCS[i]=MBCS[i];}for(int i=0;i<=FMS.n;i++) {XCS1[0][i]=XCS[i];mXCS[i]=MCS[i];}for(int i=0;i<=FZS.M[0]-FMS.M[0]+1;i++) {XS[i]=XBCS1[i][0]/XCS[0];for(int n=0;n<=FMS.n;n++) {XJS[i][n]=XS[i]*XCS[n];XBCS1[i+1][n]=XBCS1[i][n]-XJS[i][n];}for(int n=0;n<=FZS.n;n++) {XBCS1[i+1][n]=XBCS1[i+1][n+1];}XBCS1[i+1][FMS.n]=XBCS1[0][FMS.n+i+1];}
1.2.3 将商项重新排列:
for(int i=0;i<=FMS.M[0]-FMS.M[0]+1;i++) {if(i==0&&XS[i]>0) {if(FZS.M[0]-FMS.M[0]>0) {Shang=Shang+String.valueOf(XS[i])+"x^"+String.valueOf(FZS.M[0]-FMS.M[0]);}if(FZS.M[0]-FMS.M[0]==0) {Shang=Shang+String.valueOf(XS[i]);}if(FZS.M[0]-FMS.M[0]==1) {Shang=Shang+"+"+String.valueOf(XS[i])+"x";}}if(i>0&&XS[i]>0) {if(FZS.M[i]-FMS.M[0]==0) {Shang=Shang+"+"+String.valueOf(XS[i]);}if(FZS.M[i]-FMS.M[0]==1) {Shang=Shang+"+"+String.valueOf(XS[i])+"x";}if(FZS.M[i]-FMS.M[0]>1) {Shang=Shang+"+"+String.valueOf(XS[i])+"x^"+String.valueOf(FZS.M[i]-FMS.M[0]);}}if(XS[i]<0) {if(FZS.M[i]-FMS.M[0]==0) {Shang=Shang+String.valueOf(XS[i]);}if(FZS.M[i]-FMS.M[0]==1) {Shang=Shang+String.valueOf(XS[i])+"x";}if(FZS.M[i]-FMS.M[0]>1) {Shang=Shang+String.valueOf(XS[i])+"x^"+String.valueOf(FZS.M[i]-FMS.M[0]);}}if(XS[i]==0) {Shang=Shang+"";} }if(XS[FMS.n-1]>0) Shang=Shang+"+"+String.valueOf(XS[FMS.n-1]);if(XS[FMS.n-1]<0) Shang=Shang+String.valueOf(XS[FMS.n-1]);
1.2.4 将被除项与除项重新排列:
for(int i=0;i<=FMS.n;i++) {for(int n=0;n<=FMS.n;n++) {if(mXJS[i][n]>0&&n!=0) {if(mXBCS[n+i]==1) {JS[i]=JS[i]+"+"+String.valueOf(mXJS[i][n])+"x";}if(mXBCS[n+i]==0) {JS[i]=JS[i]+"+"+String.valueOf(mXJS[i][n]);}if(mXBCS[n+i]>1) {JS[i]=JS[i]+"+"+String.valueOf(mXJS[i][n])+"x^"+String.valueOf(mXBCS[n+i]);}}if((mXJS[i][n]>0&&n==0)||(mXJS[i][n]<0)){JS[i]=JS[i]+String.valueOf(mXJS[i][n])+"x^"+String.valueOf(mXBCS[n+i]);}if(mXJS[i][n]==0){JS[i]=JS[i]+"";}}}for(int i=0;i<FMS.n+1;i++) {for(int n=0;n<FMS.n+1;n++) {if(mBCS[i][n]>0&&n!=0) {if(mXBCS[n+i]==1) {BCS[i]=BCS[i]+"+"+String.valueOf(mBCS[i][n])+"x";}if(mXBCS[n+i]==0) {BCS[i]=BCS[i]+"+"+String.valueOf(mBCS[i][n]);}if(mXBCS[n+i]>1) {BCS[i]=BCS[i]+"+"+String.valueOf(mBCS[i][n])+"x^"+String.valueOf(mXBCS[n+i]);}}if((mBCS[i][n]>0&&n==0)||(mBCS[i][n]<0)){BCS[i]=BCS[i]+String.valueOf(mBCS[i][n])+"x^"+String.valueOf(mXBCS[n+i]);}if(mBCS[i][n]==0){BCS[i]=BCS[i]+"";}}if(BCS[i].equals("")) {BCS[i]="0";}}
2.1 使用Swing GUI用户界面设计写一个窗口,用于输入多项式:
public class DxsCf {private static String className="com.mysql.jdbc.Driver";private Class<?> forName;private static int read2;public static String FS;public static String FzS;public static String FmS;DxsCf() throws IOException{JFrame f=new JFrame("计算多项式除法");JLabel label3=new JLabel("请勿输入空格!");f.getContentPane().add(label3);label3.setOpaque(true);Font font4=new Font("宋体",Font.PLAIN,24);label3.setFont(font4);label3.setBorder(BorderFactory.createLineBorder(Color.black));label3.setBackground(Color.white);label3.setForeground(Color.black);label3.setBounds(10,10,250,40);label3.setOpaque(true);JLabel label4=new JLabel("在控制台输入“回车”后,点击计算开始计算");f.getContentPane().add(label4);label4.setOpaque(true);label4.setFont(font4);label4.setBorder(BorderFactory.createLineBorder(Color.black));label4.setBackground(Color.white);label4.setForeground(Color.black);label4.setBounds(10,60,500,40);label4.setOpaque(true);JLabel label=new JLabel("请输入被除数");f.getContentPane().add(label);label.setOpaque(true);Font font=new Font("宋体",Font.PLAIN,25);label.setFont(font);label.setBorder(BorderFactory.createLineBorder(Color.black));label.setBackground(Color.white);label.setForeground(Color.black);label.setBounds(10,200,200,40);label.setOpaque(true);JTextField textField = new JTextField(); // 创建一个单行输入框textField.setEditable(true);textField.setColumns(50); Font font1=new Font("宋体",Font.PLAIN,25);textField.setFont(font1);f.getContentPane().add(textField);textField.setBounds(260,200,400,40);JLabel label2=new JLabel("请输入除数");f.getContentPane().add(label2);label2.setOpaque(true);Font font2=new Font("宋体",Font.PLAIN,25);label2.setFont(font2);label2.setBorder(BorderFactory.createLineBorder(Color.black));label2.setBackground(Color.white);label2.setForeground(Color.black);label2.setBounds(10,250,200,40);label2.setOpaque(true);JTextField textField2= new JTextField(); // 创建一个单行输入框textField2.setEditable(true); // 设置输入框允许编辑textField2.setColumns(50); // 设置输入框的长度为11个字符Font font3=new Font("宋体",Font.PLAIN,25);textField2.setFont(font3);f.getContentPane().add(textField2);textField2.setBounds(260,250,400,40);JPanel j=(JPanel)f.getContentPane();j.setOpaque(false);JPanel panel=new JPanel();JButton button1=new JButton("计算");button1.setFont(font);button1.setBounds(300,300,100,50);f.getContentPane().add(button1);panel.setOpaque(false);JTextPane tp=new JTextPane(); f.setSize(800,600);f.add(panel);f.setVisible(true);//这里有一个bug,若没有这句,将会在输入之前获取输入框的值,就会获取到空值System.in.read();FmS=textField2.getText();FzS=textField.getText();button1.addActionListener(new ButtonHandler1());}public static void main(String[]args) throws IOException{new DxsCf();}
}
2.2 绑定button事件类:
public class ButtonHandler1 implements ActionListener {public static String FzS=DxsCf.FzS;public static String FmS=DxsCf.FmS;public char[] A=new char[5];public char[] B=new char[5];public char[] C=new char[5];@Overridepublic void actionPerformed(ActionEvent e) {JFrame f=new JFrame("计算计算结果:");JPanel panel=new JPanel();Font font=new Font("宋体",Font.PLAIN,25);new JSuan(); JTextPane tp=new JTextPane();f.getContentPane().add(tp);Document doc=tp.getDocument();tp.setFont(font);tp.setBounds(600,10,450,50);try {doc.insertString(doc.getLength(),JSuan.Shang, null);}catch(BadLocationException ble){System.err.println(JSuan.Shang);}JLabel label9=new JLabel("商");f.getContentPane().add(label9);label9.setFont(font);label9.setBorder(BorderFactory.createLineBorder(Color.black));label9.setBackground(Color.white);label9.setForeground(Color.black);label9.setBounds(1110,10,80,50);label9.setOpaque(true);Font font5=new Font("宋体",Font.PLAIN,25);JTextPane tp1=new JTextPane();f.getContentPane().add(tp1);Document doc1=tp1.getDocument();tp1.setFont(font5);tp1.setBounds(600,80,450,50);try {doc1.insertString(doc1.getLength(),JSuan.BCS[0], null);}catch(BadLocationException ble){System.err.println(JSuan.BCS[0]);}JLabel label2=new JLabel("被除数");f.getContentPane().add(label2);label2.setOpaque(true);Font font8=new Font("宋体",Font.PLAIN,25);label2.setFont(font8);label2.setBorder(BorderFactory.createLineBorder(Color.black));label2.setBackground(Color.white);label2.setForeground(Color.black);label2.setBounds(1110,80,80,50);label2.setOpaque(true);JTextPane tp6=new JTextPane();f.getContentPane().add(tp6);Document doc6=tp6.getDocument();tp6.setFont(font5);tp6.setBounds(600,220,450,50);try {doc6.insertString(doc6.getLength(),JSuan.BCS[1], null);}catch(BadLocationException ble){System.err.println(JSuan.BCS[1]);}JLabel label4=new JLabel("被除数");f.getContentPane().add(label4);label4.setOpaque(true);label4.setFont(font8);label4.setBorder(BorderFactory.createLineBorder(Color.black));label4.setBackground(Color.white);label4.setForeground(Color.black);label4.setBounds(1110,220,80,50);label4.setOpaque(true);JLabel label6=new JLabel("被除数");f.getContentPane().add(label6);label6.setOpaque(true);label6.setFont(font8);label6.setBorder(BorderFactory.createLineBorder(Color.black));label6.setBackground(Color.white);label6.setForeground(Color.black);label6.setBounds(1110,360,80,50);label6.setOpaque(true); JTextPane tp8=new JTextPane();f.getContentPane().add(tp8);Document doc8=tp8.getDocument();tp8.setFont(font5);tp8.setBounds(600,360,450,50);try {doc8.insertString(doc8.getLength(),JSuan.BCS[2], null);}catch(BadLocationException ble){System.err.println(JSuan.BCS[2]);}Font font6=new Font("宋体",Font.PLAIN,25);JTextPane tp3=new JTextPane();f.getContentPane().add(tp3);Document doc3=tp3.getDocument();tp3.setFont(font6);tp3.setBounds(100,80,450,50);try {doc3.insertString(doc3.getLength(),FMS.CS, null);}catch(BadLocationException ble){System.err.println(FMS.CS);}JLabel label=new JLabel("除数");f.getContentPane().add(label);label.setOpaque(true);Font font7=new Font("宋体",Font.PLAIN,25);label.setFont(font7);label.setBorder(BorderFactory.createLineBorder(Color.black));label.setBackground(Color.white);label.setForeground(Color.black);label.setBounds(10,80,80,50);label.setOpaque(true);JTextPane tp5=new JTextPane();f.getContentPane().add(tp5);Document doc5=tp5.getDocument();tp5.setFont(font5);tp5.setBounds(600,150,450,50);try {doc5.insertString(doc5.getLength(),JSuan.JS[0], null);}catch(BadLocationException ble){System.err.println(JSuan.JS[0]);}JLabel label3=new JLabel("减数");f.getContentPane().add(label3);label3.setOpaque(true);label3.setFont(font8);label3.setBorder(BorderFactory.createLineBorder(Color.black));label3.setBackground(Color.white);label3.setForeground(Color.black);label3.setBounds(1110,150,80,50);label3.setOpaque(true);JTextPane tp7=new JTextPane();f.getContentPane().add(tp7);Document doc7=tp7.getDocument();tp7.setFont(font5);tp7.setBounds(600,290,450,50);try {doc7.insertString(doc7.getLength(),JSuan.JS[1], null);}catch(BadLocationException ble){System.err.println(JSuan.JS[1]);}JLabel label5=new JLabel("减数");f.getContentPane().add(label5);label5.setOpaque(true);label5.setFont(font8);label5.setBorder(BorderFactory.createLineBorder(Color.black));label5.setBackground(Color.white);label5.setForeground(Color.black);label5.setBounds(1110,290,80,50);label5.setOpaque(true);JTextPane tp9=new JTextPane();f.getContentPane().add(tp9);Document doc9=tp9.getDocument();tp9.setFont(font5);tp9.setBounds(600,430,450,50);try {doc9.insertString(doc9.getLength(),JSuan.JS[2], null);}catch(BadLocationException ble){System.err.println(JSuan.JS[2]);}JLabel label7=new JLabel("减数");f.getContentPane().add(label7);label7.setOpaque(true);label7.setFont(font8);label7.setBorder(BorderFactory.createLineBorder(Color.black));label7.setBackground(Color.white);label7.setForeground(Color.black);label7.setBounds(1110,430,80,50);label7.setOpaque(true);JTextPane tp10=new JTextPane();f.getContentPane().add(tp10);Document doc10=tp10.getDocument();tp10.setFont(font5);tp10.setBounds(600,500,450,50);try {doc10.insertString(doc10.getLength(),JSuan.BCS[FZS.n-FMS.n+1], null);}catch(BadLocationException ble){System.err.println(JSuan.BCS[FZS.n-FMS.n+1]);}JLabel label8=new JLabel("余数");f.getContentPane().add(label8);label8.setOpaque(true);label8.setFont(font8);label8.setBorder(BorderFactory.createLineBorder(Color.black));label8.setBackground(Color.white);label8.setForeground(Color.black);label8.setBounds(1110,500,80,50);label8.setOpaque(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(1200,900);f.add(panel);f.setVisible(true);// TODO Auto-generated method stub
}
}
3.1 实现效果图:
注:此代码还可能存在少量bug,望各位大佬指正!希望能够帮助到大家!
一起学习,一起进步!
这篇关于多项式除法输出整个竖式(java实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!