| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1573 人关注过本帖
标题:java的多项式的加减乘除和赋值错误问题
只看楼主 加入收藏
s1303352590
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-8-24
收藏
 问题点数:0 回复次数:0 
java的多项式的加减乘除和赋值错误问题
package calculator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import java.awt.Font;

public class Test {

    private JFrame frame;
    private JTextField duoxiangshi1;
    private JTextField duoxiangshi2;
    private JTextField Jieguo;
    String xishu1[]=new String[10000];
    String zhishu1[]=new String[10000];
    String xishu2[]=new String[10000];
    String zhishu2[]=new String[10000];
    double dxishu1[]=new double[10000];
    double dzhishu1[]=new double[10000];
    double dxishu2[]=new double[10000];
    double dzhishu2[]=new double[10000];
    double dxishu3[]=new double[10000];
    double dzhishu3[]=new double[10000];
    private JTextField fuzhi1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test window = new Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("\u4E00\u5143\u591A\u9879\u5F0F\u8BA1\u7B97\u5668");
        frame.setBounds(100, 100, 646, 514);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        duoxiangshi1 = new JTextField();
        duoxiangshi1.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();               
                if((keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)||keyChar==KeyEvent.VK_MINUS||
                        keyChar==KeyEvent.VK_PERIOD||keyChar==KeyEvent.VK_X||keyChar==KeyEvent.VK_CIRCUMFLEX){
                }else{
                    e.consume();
                }
            }
        });//输入控制
        duoxiangshi1.setText("1*x^2+3*x^4+5*x^6");
        duoxiangshi1.setBounds(114, 33, 402, 21);
        frame.getContentPane().add(duoxiangshi1);
        duoxiangshi1.setColumns(10);
        

        
        JButton queren1 = new JButton("\u786E\u5B9A");
        queren1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s1=duoxiangshi1.getText();
                String str1 = new String();
               
                String[] xiang=s1.split("\\+");//分离每一个项
                int xiangshu=xiang.length;

                for (int i=0;i<xiangshu;i++)
                {
                    str1 +=(xiang[i]+"*x^");    //为分离指数 和系数做准备
                }
                String[] x=str1.split("\\*x\\^");  //分离指数和系数
               
               
                for (int i=0;i<x.length;i+=2)
                {
                    xishu1[i/2]=x[i];
                }
                for (int i=1;i<x.length;i+=2)
                {
                    zhishu1[(i-1)/2]=x[i];
                }
               

            }
        });
        queren1.setBounds(533, 32, 70, 23);
        frame.getContentPane().add(queren1);
        
        duoxiangshi2 = new JTextField();
        duoxiangshi2.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();               
                if((keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)||keyChar==KeyEvent.VK_MINUS||
                        keyChar==KeyEvent.VK_PERIOD||keyChar==KeyEvent.VK_X||keyChar==KeyEvent.VK_CIRCUMFLEX){
                }else{
                    e.consume();
                }
            }
        });//输入控制
        duoxiangshi2.setText("1*x^2+4*x^5+3*x^4");
        duoxiangshi2.setColumns(10);
        duoxiangshi2.setBounds(114, 64, 402, 21);
        frame.getContentPane().add(duoxiangshi2);
        
        JButton queren2 = new JButton("\u786E\u5B9A");
        queren2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String s1=duoxiangshi1.getText();
                String str1 = new String();
               
                String[] xiang=s1.split("\\+");//分离每一个项
                int xiangshu=xiang.length;

                for (int i=0;i<xiangshu;i++)
                {
                    str1 +=(xiang[i]+"*x^");    //为分离指数 和系数做准备
                }
                String[] x=str1.split("\\*x\\^");  //分离指数和系数
               
               
                for (int i=0;i<x.length;i+=2)
                {
                    xishu2[i/2]=x[i];
                }
                for (int i=1;i<x.length;i+=2)
                {
                    zhishu2[(i-1)/2]=x[i];
                }
            }
        });
        queren2.setBounds(533, 63, 70, 23);
        frame.getContentPane().add(queren2);
        
        Jieguo = new JTextField();
        Jieguo.setEditable(false);//设置开始不能输入文本
        Jieguo.setColumns(10);
        Jieguo.setBounds(114, 122, 402, 21);
        frame.getContentPane().add(Jieguo);
        
        //多项式加法
        JButton jia = new JButton("+");
        jia.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String jieguo=new String();
                for(int i=0;i<10000;i++)//将string数组转化成double
                {
                    if(xishu1[i]!=null||zhishu1[i]!=null)
                    {
                        dxishu1[i]=new Double(xishu1[i]);
                        dzhishu1[i]=new Double(zhishu1[i]);
                    }
                    if(xishu2[i]!=null||zhishu2[i]!=null)
                    {
                        dxishu2[i]=new Double(xishu2[i]);
                        dzhishu2[i]=new Double(zhishu2[i]);
                    }
                }
                for(int i=0;i<10000;i++)//录入
                {
                    dzhishu3[i]=dzhishu1[i];
                    dxishu3[i]=dxishu1[i];
                }
                for(int i=0;i<10000;i++)//计算
                {
                    int k=1,queren=0;
                    if(dzhishu3[i]==dzhishu2[i])//如果指数相等,系数相加
                    {
                    dxishu3[i]+=dxishu2[i];
                    }
                    if(dzhishu3[i]!=dzhishu2[i])//如果指数不等
                    {
                        for (int j=0;j<10000;j++)
                        {
                            if (dzhishu2[i]==dzhishu3[j])//如果该项的指数与其他项的指数相等
                            {
                                dxishu3[j]+=dxishu2[i];
                                queren=1;
                                break;
                            }
                        }
                        if(queren==0)//如果该项的指数与其他项的指数都不等,新建一项
                        {
                            dxishu3[dzhishu3.length+k]+=dxishu2[i];
                            dzhishu3[dzhishu3.length+k]+=dzhishu2[i];
                            k++;
                        }
                    }
                    queren=0;
                }
                for(int i=0;i<10000;i++)//删除系数为零的项
                {
                    if(dxishu3[i]==0)
                    {
                        dzhishu3[i]=0;
                    }
                }
                for(int i=0;i<10000;i++)//输出结果
                {
                    if(dxishu3[i]!=0||dzhishu3[i]!=0) {
                    jieguo+=dxishu3[i]+"*x^"+dzhishu3[i]+"+";
                    }
                }
                jieguo = jieguo.substring(0,jieguo.length()-1);
                Jieguo.setText(jieguo);
            }
        });
        jia.setBounds(114, 89, 93, 23);
        frame.getContentPane().add(jia);
        
        //多项式减法
        JButton jian = new JButton("-");
        jian.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String jieguo=new String();
                for(int i=0;i<10000;i++)//将string数组转化成double
                {
                    if(xishu1[i]!=null||zhishu1[i]!=null)
                    {
                        dxishu1[i]=new Double(xishu1[i]);
                        dzhishu1[i]=new Double(zhishu1[i]);
                    }
                    if(xishu2[i]!=null||zhishu2[i]!=null)
                    {
                        dxishu2[i]=new Double(xishu2[i]);
                        dzhishu2[i]=new Double(zhishu2[i]);
                    }
                }
                for(int i=0;i<10000;i++)//录入
                {
                    dzhishu3[i]=dzhishu1[i];
                    dxishu3[i]=dxishu1[i];
                }
                for(int i=0;i<10000;i++)//计算
                {
                    int k=1,queren=0;
                    if(dzhishu3[i]==dzhishu2[i])//如果指数相等,系数相加
                    {
                    dxishu3[i]-=dxishu2[i];
                    }
                    if(dzhishu3[i]!=dzhishu2[i])//如果指数不等
                    {
                        for (int j=0;j<10000;j++)
                        {
                            if (dzhishu2[i]==dzhishu3[j])//如果该项的指数与其他项的指数相等
                            {
                                dxishu3[j]-=dxishu2[i];
                                queren=1;
                                break;
                            }
                        }
                        if(queren==0)//如果该项的指数与其他项的指数都不等,新建一项
                        {
                            dxishu3[dzhishu3.length+k]-=dxishu2[i];
                            dzhishu3[dzhishu3.length+k]+=dzhishu2[i];
                            k++;
                        }
                    }
                    queren=0;
                }
                for(int i=0;i<10000;i++)//删除系数为零的项
                {
                    if(dxishu3[i]==0)
                    {
                        dzhishu3[i]=0;
                    }
                }
                for(int i=0;i<10000;i++)//输出结果
                {
                    if(dxishu3[i]!=0||dzhishu3[i]!=0) {
                    jieguo+=dxishu3[i]+"*x^"+dzhishu3[i]+"+";
                    }
                }
                jieguo = jieguo.substring(0,jieguo.length()-1);
                Jieguo.setText(jieguo);
               
            }
        });
        jian.setBounds(217, 89, 93, 23);
        frame.getContentPane().add(jian);
        
        JButton cheng = new JButton("*");
        cheng.setBounds(320, 89, 93, 23);
        frame.getContentPane().add(cheng);

        JButton chu = new JButton("/");
        chu.setBounds(423, 89, 93, 23);
        frame.getContentPane().add(chu);
        
        JLabel label = new JLabel("\u7B2C\u4E00\u4E2A\u591A\u9879\u5F0F\uFF1A");
        label.setBounds(26, 36, 93, 15);
        frame.getContentPane().add(label);
        
        JLabel label_1 = new JLabel("\u7B2C\u4E8C\u4E2A\u591A\u9879\u5F0F\uFF1A");
        label_1.setBounds(26, 67, 93, 15);
        frame.getContentPane().add(label_1);
        
        JLabel label_2 = new JLabel("\u64CD\u4F5C\u9009\u62E9\uFF1A");
        label_2.setBounds(26, 93, 93, 15);
        frame.getContentPane().add(label_2);
        
        JLabel label_3 = new JLabel("\u8BA1\u7B97\u7ED3\u679C\uFF1A");
        label_3.setBounds(26, 125, 93, 15);
        frame.getContentPane().add(label_3);
        
        JSeparator separator = new JSeparator();
        separator.setBounds(8, 153, 612, 15);
        frame.getContentPane().add(separator);
        
        JLabel label_4 = new JLabel("\u591A\u9879\u5F0F\u8D4B\u503C");
        label_4.setFont(new Font("楷体", Font.PLAIN, 16));
        label_4.setBounds(26, 161, 112, 28);
        frame.getContentPane().add(label_4);
        
        fuzhi1 = new JTextField();
        fuzhi1.setBounds(114, 192, 402, 21);
        frame.getContentPane().add(fuzhi1);
        fuzhi1.setColumns(10);
    }
}
搜索更多相关主题的帖子: frame String new double int 
2018-10-26 18:43
快速回复:java的多项式的加减乘除和赋值错误问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019764 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved