PDA

View Full Version : سوال: کمک



harmonika
دوشنبه 29 تیر 1388, 21:23 عصر
با سلام خدمت دوستان گرامی لطفا کمک کنید مشکل این برنامه رو رفع کنم.
این یه برنامه برای محاسبه LCS هستش.تو قسمت LCSDemo دوتا فیلد داریم که دوتا رشته دریافت کرده و تو متغیر های کلاس LCS با استفاده از توابع set ذخیره میکنن تا LCS این دوتا رشته با استفاده از تابع determineLCS محاسبه شده و با استفاده از تابع getLCS به کلاس LCSDemo جهت نمایش ارسال شوند.
مشکل این برنامه از اینجا ناشی میشه که وقتی enter زده میشه فقط LCS دو رشته ای که در قسمت ( final LCS lcshanle = new LCS("BBB","AB درکلاس LCSDemo وارد شده رو حساب میکنه و LCS دو رشته ای که از textField گرفتیم رو حساب نمیکنه!


package lcs;

public class LCS {
private String lFirst = "";
private String lSecond = "";
private String lcs = "";

public LCS(String X,String Y){

lFirst = X;
lSecond = Y;
}

public void setFirst( String first ){

lFirst = first;
}

public void setSecond( String second ){

lSecond = second;
}

public String getLCS(){

return lcs;
}

public void determineLCS(){

int i,j;
int n = lFirst.length();
int m = lSecond.length();
int[][] C = new int[n+1][m+1];
int[][] B = new int[n+1][m+1];
for ( i = 0; i <= n; i++ ){
C[i][0] = 0;
}
for ( j = 0; j <= m; j++ ){
C[0][j] = 0;
}
for ( i = 1; i <= n; i++ ){
for ( j = 1; j <= m; j++ ){
if ( lFirst.charAt(i-1) == lSecond.charAt(j-1) ){
C[i][j]=C[i-1][j-1]+1;
B[i][j]=1;
}
else if (C[i-1][j]>=C[i][j-1]){
C[i][j]=C[i-1][j];
B[i][j] = 2;
}
else{
C[i][j]=C[i][j-1];
B[i][j]=3;
}
}
}
lcs = new String();
i=n;
j=m;
while (i!=0 && j!=0){
if (B[i][j] ==1){
lcs = lFirst.charAt(i-1) + lcs;
i = i - 1;
j = j - 1;
}
if (B[i][j] == 2){
i = i - 1;
}
if (B[i][j] == 3){
j = j - 1;
}
}
}
}




package lcs;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

public class LCSDemo {
public static void main(String [] args){

final LCS lcshanle = new LCS("BBB","AB");
JFrame frame = new JFrame();
FlowLayout layout = new FlowLayout();
layout.setAlignment( FlowLayout.LEFT );
frame.setLayout( layout );

JMenu fileMenu = new JMenu( "File" );
JMenuItem exitItem = new JMenuItem( "Exit" );
fileMenu.add( exitItem );
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
System.exit( 0 );
}
}
);
JMenuItem aboutItem = new JMenuItem( "About..." );
fileMenu.add( aboutItem );
aboutItem.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JOptionPane.showMessageDialog( null,
"this program was developed by saeid heidari",
"About",JOptionPane.PLAIN_MESSAGE );

}
}
);

JMenuBar bar = new JMenuBar();
frame.add(bar);
frame.setJMenuBar( bar );

JLabel label1 = new JLabel("enter first string:");
frame.add(label1);

JTextField text1 = new JTextField(12);
frame.add(text1);
text1.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
String first= event.getActionCommand();
lcshanle.setFirst(first);

}
}
);

JLabel label2 = new JLabel( "enter second string:" );
frame.add( label2 );

JTextField text2 = new JTextField( 12 );
frame.add( text2 );
text2.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event ){
String second = event.getActionCommand();
lcshanle.setSecond(second);
}
}
);


lcshanle.determineLCS();

JButton button = new JButton("Enter");
frame.add( button );
button.addActionListener(
new ActionListener()
{

String lcs = lcshanle.getLCS();
public void actionPerformed( ActionEvent event ){
JOptionPane.showInputDialog( this,"LCS is :" + lcs);
}
}
);

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,150);
frame.setVisible( true );





}

}

java.source.ir
دوشنبه 29 تیر 1388, 22:19 عصر
سلام
کدهای شما رو نگاه کردم. به نظرم اومد که باید به صورت زیر آنها را تغییر دهی تا درست کار کنه, چون آنها را اجرا کردم:

public class LCS {
private String lFirst = "";
private String lSecond = "";
private String lcs = "";

public LCS(){


}

public void setFirst( String first ){

lFirst = first;
}

public void setSecond( String second ){

lSecond = second;
}

public String getLCS(){

return lcs;
}

public void determineLCS(){

int i,j;
int n = lFirst.length();
int m = lSecond.length();
int[][] C = new int[n+1][m+1];
int[][] B = new int[n+1][m+1];
for ( i = 0; i <= n; i++ ){
C[i][0] = 0;
}
for ( j = 0; j <= m; j++ ){
C[0][j] = 0;
}
for ( i = 1; i <= n; i++ ){
for ( j = 1; j <= m; j++ ){
if ( lFirst.charAt(i-1) == lSecond.charAt(j-1) ){
C[i][j]=C[i-1][j-1]+1;
B[i][j]=1;
}
else if (C[i-1][j]>=C[i][j-1]){
C[i][j]=C[i-1][j];
B[i][j] = 2;
}
else{
C[i][j]=C[i][j-1];
B[i][j]=3;
}
}
}
lcs = new String();
i=n;
j=m;
while (i!=0 && j!=0){
if (B[i][j] ==1){
lcs = lFirst.charAt(i-1) + lcs;
i = i - 1;
j = j - 1;
}
if (B[i][j] == 2){
i = i - 1;
}
if (B[i][j] == 3){
j = j - 1;
}
}
}
}


import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

public class LCSDemo implements ActionListener {
JTextField text1 = new JTextField(12);
JTextField text2 = new JTextField( 12 );
final LCS lcshanle = new LCS();

public LCSDemo() {
JFrame frame = new JFrame();
FlowLayout layout = new FlowLayout();
layout.setAlignment( FlowLayout.LEFT );
frame.setLayout( layout );

JMenu fileMenu = new JMenu( "File" );
JMenuItem exitItem = new JMenuItem( "Exit" );
fileMenu.add( exitItem );
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
System.exit( 0 );
}
}
);
JMenuItem aboutItem = new JMenuItem( "About..." );
fileMenu.add( aboutItem );
aboutItem.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JOptionPane.showMessageDialog( null,
"this program was developed by saeid heidari",
"About",JOptionPane.PLAIN_MESSAGE );

}
}
);

JMenuBar bar = new JMenuBar();
frame.add(bar);
frame.setJMenuBar( bar );

JLabel label1 = new JLabel("enter first string:");
frame.add(label1);

frame.add(text1);

JLabel label2 = new JLabel( "enter second string:" );
frame.add( label2 );

frame.add( text2 );




JButton button = new JButton("Enter");
frame.add( button );
button.addActionListener(this);

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,150);
frame.setVisible( true );
}

public static void main(String [] args){



new LCSDemo();




}

public void actionPerformed( ActionEvent event ){
lcshanle.setFirst(text1.getText());
lcshanle.setSecond(text2.getText());
lcshanle.determineLCS();
String lcs = lcshanle.getLCS();
JOptionPane.showInputDialog( this,"LCS is :" + lcs);
}

}

موفق و سربلند باشید. امیدوارم به دردتون بخوره چون فقط ایرادی که سبب می شد تا برنامه کار نکنه را رفع نمودم.

harmonika
سه شنبه 30 تیر 1388, 11:37 صبح
از کمکتون ممنونم برنامه به درستی کار کرد.