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);
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70007877/viewspace-2795075/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 不用加減乘除做加法(Java實現)Java
- MongoDB 中的【加減乘除】運算MongoDB
- 簡單的加減乘除(遞迴)遞迴
- 大資料的運算加減乘除大資料
- 超大整數的加減乘除計算方法
- JS加減乘除位移方法封裝JS封裝
- 位運算實現加減乘除
- 三個數字的加減乘除模運算
- 浮點數的加減乘除運算細節
- SDUST OJ 時間類的加、減法賦值運算賦值
- JS 加減乘除 尤其是減法精度問題JS
- 33:計算分數加減表示式的值
- Verilog實現加減乘除運算
- 一元稀疏多項式加減法計算器
- JavaScript浮點數加減乘除精確計算JavaScript
- 7-2 算術入門之加減乘除
- 二進位制運算加減乘除+快速冪
- Java的Monad和懶賦值Java賦值
- [每日一題] 第六題:不用加減乘除做加法每日一題
- 劍指offer 面試題47:不用加減乘除做加法面試題
- NumPy 簡單算術:加減乘除及其他運算
- 36:計算多項式的值
- [Linux]變數加減賦值以及將String轉intLinux變數賦值
- 用單連結串列實現多項式加,減,乘,簡單微分
- 關於多項式的加和、乘積可用連結串列和陣列陣列
- LeetCode 劍指 Offer 65. 不用加減乘除做加法LeetCode
- 實現多項式的JAVA類Java
- 【OpenCV學習】多通道矩陣的賦值和取值OpenCV矩陣賦值
- java時間加減Java
- C++筆記:輸入輸出、變數、變數加減乘除C++筆記變數
- 常被新手忽略的值賦值和引用賦值(偏redux向)賦值Redux
- jquery取值和賦值(包含部分是原生js的取值和賦值)jQuery賦值JS
- Python 鏈式賦值的坑Python賦值
- 整明白 Golang struct 例項化和賦值GolangStruct賦值
- javascript變數賦值或者重新賦值注意事項JavaScript變數賦值
- 大資料帶來信貸“四則運算”:金融生態需要“加減乘除”大資料
- BigDecimal加減乘除運算,保留2位小數點,初始化,與0的比較Decimal
- golang 實現多型的賦值問題Golang多型賦值