D
dread92
Здравствуйте!Есть программа которая расскладывает число на множители.Проблема в том что в textField можно вводить только числа до 10^6,а надо что вычисляло намного большие числа.
Вот весь код программы.
<!--shcode--><pre><code class='java'>import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class NewClass1 extends JFrame implements ActionListener{
private FlowLayout layout;
protected JPanel controlPanel;
protected JPanel historyPanel;
protected JTextField expressionField;
protected JButton solveButton;
protected JButton solveButton1;
protected JList historyList;
protected JScrollPane historyScroller;
protected DefaultListModel listModel;
int I, k, d, A, B, t, dd,q,p=0;
int N; int c;int c1;
private final static String newline = "\n";
public NewClass1(){
super("Алгоритм Шермана-Лемана");
Font font = null;
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Файл");
fileMenu.setFont(font);
JMenu fileMenu1 = new JMenu("Справка");
fileMenu.setFont(font);
JMenuItem txtFileItem = new JMenuItem("Выполнить");
txtFileItem.setFont(font);
fileMenu.add(txtFileItem);
txtFileItem.addActionListener((ActionListener) this);
JMenuItem imgFileItem = new JMenuItem("Стереть");
imgFileItem.setFont(font);
fileMenu.add(imgFileItem);
imgFileItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listModel.clear();
expressionField.selectAll();
expressionField.cut();
}
});
JMenuItem imgFileItem1 = new JMenuItem("О программе");
imgFileItem.setFont(font);
fileMenu1.add(imgFileItem1);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setFont(font);
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
imgFileItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getNewText();
}
private void getNewText() {
JOptionPane pane = new JOptionPane(
"Программа предназначенна для разложения натурального числа на множители"
);
JDialog dialog = pane.createDialog("Информация о программе");
dialog.setModalityType(
Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);
}
});
menuBar.add(fileMenu); menuBar.add(fileMenu1);
setJMenuBar(menuBar);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
controlPanel = new JPanel(new GridBagLayout());
controlPanel.setBorder(BorderFactory.createTitledBorder("Введите число"));
GridBagConstraints cc = new GridBagConstraints();
historyPanel = new JPanel(new BorderLayout());
historyPanel.setBorder(BorderFactory.createTitledBorder("Результат"));
expressionField = new JTextField(20);
expressionField.setHorizontalAlignment(JTextField.RIGHT);
solveButton = new JButton(" Вычислить ");
solveButton.addActionListener((ActionListener) this);
solveButton1 = new JButton(" Стереть ");
solveButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listModel.clear();
expressionField.selectAll();
expressionField.cut();
}
});
listModel = new DefaultListModel();
historyList = new JList(listModel);
historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
historyList.setVisibleRowCount(20);
historyScroller = new JScrollPane(historyList);
cc.gridx = 0;
cc.gridy = 0;
cc.fill = GridBagConstraints.HORIZONTAL;
cc.weightx = 1;
controlPanel.add(expressionField, cc);
cc.gridx = 1;
cc.gridy = 0;
cc.fill = GridBagConstraints.NONE;
cc.weightx = 0;
controlPanel.add(solveButton, cc);
cc.gridx = 2;
cc.gridy = 0;
cc.fill = GridBagConstraints.NONE;
cc.weightx = 0;
controlPanel.add(solveButton1, cc);
historyPanel.add(historyScroller, BorderLayout.CENTER);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 0;
add(controlPanel, c);
c.gridy = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(historyPanel, c);
setSize(410, 220);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
static int sqrt1(double x, double n)
{
return (int) Math.pow(x, 1/n);
}
static int sqrt2(double x,double x1)
{
return (int) ((int) (Math.pow(x, 1/6))/(4*(Math.pow(x1, 1/2)))+1);
}
static int sqrt3(int x1, double n1)
{ while(x1!=n1){
if(x1>n1)
x1=(int) (x1-n1);
else n1=n1-x1;
}
return x1;
}
@Override
public void actionPerformed(ActionEvent e) {
String text = expressionField.getText();
N = Integer.parseInt(text);
if(N>8){
if(N%2==0){ listModel.addElement(N+"=2*"+N/2+newline);
expressionField.selectAll();}
else{
int Result=-1;
for(I=2;I<=sqrt1(N,3);I++){
if((N%I)==0){
Result=I;
p=(int) (N/I);
}
}
if(Result<0){
for(k=1;k<=sqrt1(N,3);k++){
for(d=0;d<=sqrt2(N,k);d++){
q=(int) (4*k*N);
A=sqrt1(q,2)+d;
int q2=A*A-q;
B=sqrt1(q2,2);
if(B>0){
dd = sqrt3(A-B, N);
if (1 < dd)
{if(dd < N){
Result=dd;
p=(int) (N/dd);}}
}
}
}
} if(Result>0){
listModel.addElement(N+"="+Result+"*"+p+newline);
expressionField.selectAll();}
else listModel.addElement(N+" "+"Простое число"+newline);
expressionField.selectAll();
}
}
else{
listModel.addElement("Натуральное число должно быть больше 8"+newline);
expressionField.selectAll();}
}
public JButton getButton() {
return solveButton;
}
public static void main(String[] args){
new NewClass1();
}[/CODE]
Вот здесь сам метод вычисления:<!--shcode--><pre><code class='java'>
@Override
public void actionPerformed(ActionEvent e) {
String text = expressionField.getText();
N = Integer.parseInt(text);
if(N>8){
if(N%2==0){ listModel.addElement(N+"=2*"+N/2+newline);
expressionField.selectAll();}
else{
int Result=-1;
for(I=2;I<=sqrt1(N,3);I++){
if((N%I)==0){
Result=I;
p=(int) (N/I);
}
}
if(Result<0){
for(k=1;k<=sqrt1(N,3);k++){
for(d=0;d<=sqrt2(N,k);d++){
q=(int) (4*k*N);
A=sqrt1(q,2)+d;
int q2=A*A-q;
B=sqrt1(q2,2);
if(B>0){
dd = sqrt3(A-B, N);
if (1 < dd)
{if(dd < N){
Result=dd;
p=(int) (N/dd);}}
}
}
}
} if(Result>0){
listModel.addElement(N+"="+Result+"*"+p+newline);
expressionField.selectAll();}
else listModel.addElement(N+" "+"Простое число"+newline);
expressionField.selectAll();
}
}
else{
listModel.addElement("Натуральное число должно быть больше 8"+newline);
expressionField.selectAll();}[/CODE]
Вот весь код программы.
<!--shcode--><pre><code class='java'>import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class NewClass1 extends JFrame implements ActionListener{
private FlowLayout layout;
protected JPanel controlPanel;
protected JPanel historyPanel;
protected JTextField expressionField;
protected JButton solveButton;
protected JButton solveButton1;
protected JList historyList;
protected JScrollPane historyScroller;
protected DefaultListModel listModel;
int I, k, d, A, B, t, dd,q,p=0;
int N; int c;int c1;
private final static String newline = "\n";
public NewClass1(){
super("Алгоритм Шермана-Лемана");
Font font = null;
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Файл");
fileMenu.setFont(font);
JMenu fileMenu1 = new JMenu("Справка");
fileMenu.setFont(font);
JMenuItem txtFileItem = new JMenuItem("Выполнить");
txtFileItem.setFont(font);
fileMenu.add(txtFileItem);
txtFileItem.addActionListener((ActionListener) this);
JMenuItem imgFileItem = new JMenuItem("Стереть");
imgFileItem.setFont(font);
fileMenu.add(imgFileItem);
imgFileItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listModel.clear();
expressionField.selectAll();
expressionField.cut();
}
});
JMenuItem imgFileItem1 = new JMenuItem("О программе");
imgFileItem.setFont(font);
fileMenu1.add(imgFileItem1);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setFont(font);
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
imgFileItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getNewText();
}
private void getNewText() {
JOptionPane pane = new JOptionPane(
"Программа предназначенна для разложения натурального числа на множители"
);
JDialog dialog = pane.createDialog("Информация о программе");
dialog.setModalityType(
Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);
}
});
menuBar.add(fileMenu); menuBar.add(fileMenu1);
setJMenuBar(menuBar);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
controlPanel = new JPanel(new GridBagLayout());
controlPanel.setBorder(BorderFactory.createTitledBorder("Введите число"));
GridBagConstraints cc = new GridBagConstraints();
historyPanel = new JPanel(new BorderLayout());
historyPanel.setBorder(BorderFactory.createTitledBorder("Результат"));
expressionField = new JTextField(20);
expressionField.setHorizontalAlignment(JTextField.RIGHT);
solveButton = new JButton(" Вычислить ");
solveButton.addActionListener((ActionListener) this);
solveButton1 = new JButton(" Стереть ");
solveButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listModel.clear();
expressionField.selectAll();
expressionField.cut();
}
});
listModel = new DefaultListModel();
historyList = new JList(listModel);
historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
historyList.setVisibleRowCount(20);
historyScroller = new JScrollPane(historyList);
cc.gridx = 0;
cc.gridy = 0;
cc.fill = GridBagConstraints.HORIZONTAL;
cc.weightx = 1;
controlPanel.add(expressionField, cc);
cc.gridx = 1;
cc.gridy = 0;
cc.fill = GridBagConstraints.NONE;
cc.weightx = 0;
controlPanel.add(solveButton, cc);
cc.gridx = 2;
cc.gridy = 0;
cc.fill = GridBagConstraints.NONE;
cc.weightx = 0;
controlPanel.add(solveButton1, cc);
historyPanel.add(historyScroller, BorderLayout.CENTER);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 0;
add(controlPanel, c);
c.gridy = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(historyPanel, c);
setSize(410, 220);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
static int sqrt1(double x, double n)
{
return (int) Math.pow(x, 1/n);
}
static int sqrt2(double x,double x1)
{
return (int) ((int) (Math.pow(x, 1/6))/(4*(Math.pow(x1, 1/2)))+1);
}
static int sqrt3(int x1, double n1)
{ while(x1!=n1){
if(x1>n1)
x1=(int) (x1-n1);
else n1=n1-x1;
}
return x1;
}
@Override
public void actionPerformed(ActionEvent e) {
String text = expressionField.getText();
N = Integer.parseInt(text);
if(N>8){
if(N%2==0){ listModel.addElement(N+"=2*"+N/2+newline);
expressionField.selectAll();}
else{
int Result=-1;
for(I=2;I<=sqrt1(N,3);I++){
if((N%I)==0){
Result=I;
p=(int) (N/I);
}
}
if(Result<0){
for(k=1;k<=sqrt1(N,3);k++){
for(d=0;d<=sqrt2(N,k);d++){
q=(int) (4*k*N);
A=sqrt1(q,2)+d;
int q2=A*A-q;
B=sqrt1(q2,2);
if(B>0){
dd = sqrt3(A-B, N);
if (1 < dd)
{if(dd < N){
Result=dd;
p=(int) (N/dd);}}
}
}
}
} if(Result>0){
listModel.addElement(N+"="+Result+"*"+p+newline);
expressionField.selectAll();}
else listModel.addElement(N+" "+"Простое число"+newline);
expressionField.selectAll();
}
}
else{
listModel.addElement("Натуральное число должно быть больше 8"+newline);
expressionField.selectAll();}
}
public JButton getButton() {
return solveButton;
}
public static void main(String[] args){
new NewClass1();
}[/CODE]
Вот здесь сам метод вычисления:<!--shcode--><pre><code class='java'>
@Override
public void actionPerformed(ActionEvent e) {
String text = expressionField.getText();
N = Integer.parseInt(text);
if(N>8){
if(N%2==0){ listModel.addElement(N+"=2*"+N/2+newline);
expressionField.selectAll();}
else{
int Result=-1;
for(I=2;I<=sqrt1(N,3);I++){
if((N%I)==0){
Result=I;
p=(int) (N/I);
}
}
if(Result<0){
for(k=1;k<=sqrt1(N,3);k++){
for(d=0;d<=sqrt2(N,k);d++){
q=(int) (4*k*N);
A=sqrt1(q,2)+d;
int q2=A*A-q;
B=sqrt1(q2,2);
if(B>0){
dd = sqrt3(A-B, N);
if (1 < dd)
{if(dd < N){
Result=dd;
p=(int) (N/dd);}}
}
}
}
} if(Result>0){
listModel.addElement(N+"="+Result+"*"+p+newline);
expressionField.selectAll();}
else listModel.addElement(N+" "+"Простое число"+newline);
expressionField.selectAll();
}
}
else{
listModel.addElement("Натуральное число должно быть больше 8"+newline);
expressionField.selectAll();}[/CODE]