الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سوال للبارعين

مغلق
بدأه Ham_ghailandi في 24 مارس 2003 · 10 رد · 1,115 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله و بركاته

ارجو مساعدتي لتصميم برنامج بامكانه المقارنه بين ملفين ثم عرظ أوجه التشابه بيناهما, للتبسيط الملفات من نوع txt.

Brothers and sisters i would like any of you to help me completing the following project which required, comparing to text files and returning the diffrences and the matches between the compared files.

Thank you very much

جزاكم الله خبرا

#2

وعليكم السلام ورحمه الله وبركاته

اخي في الله

ماذا فعلت في حل سوالك الي الان؟ هل قمت بكتابه الكود تبعك وتريد المساعده فيه؟

في امان الله

#3

رد على الأخ bawazeem.

إبدأ يا أخي و عندما تحتاج المساعدة, كلنا هنا من أجل ذلك.

بالمناسبة سلامي الى الأخوين bawazeem و javaman.

#4

وعليك السلام ورحمه الله وبركاته يا علاء الدين.

#5

السلام عليكم

اولا اشكركم يا اخوتي علي ردودكم نعم لقد بدات و استطعت ان اخزن الملف الاول في VECTOR و الان احاول ان اجعل البرنامج يقوم بفتح الملف الثاني و تخزينه على شكل كلمات ومن ثم تخزينها في VECTOR اخرى ثم المقارنة بين الاثنين. و عندما نجد التشابه نقوم بتخزين اوجه التشابه في VECTOR ثالثه حتي يتم تقديمها بطريقة مناسبه.

نص الكود حتى الان هو كل ألات:

import java.awt.*;

import java.awt.event.*;

import java.text.*;

import java.util.*;

import javax.swing.*;

import java.util.Vector;

import java.lang.*;

import java.io.*;

import javax.swing.plaf.basic.*;

public class IteratorTest2 extends JPanel {

protected JComboBox localeButton;

protected JTextArea textArea;

protected JRadioButton charButton;

protected JRadioButton wordButton;

protected JRadioButton lineButton;

protected JRadioButton sentButton;

protected JLabel countLabel;

protected JButton refreshButton;

protected JList itemList;

// Create a window for the Iterator test

// and make sure that later components will fit

public static void main(String[] args) {

JFrame f = new JFrame("Iterator Test");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(new IteratorTest2());

f.pack();

f.setVisible(true);

}

public IteratorTest2() {

buildLayout();

refreshDisplay();

}

protected void buildLayout() {

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

// Set up the contents of the Locale combo box

gbc.gridx = 0;

gbc.gridy = 0;

localeButton = new JComboBox(Locale.getAvailableLocales());

localeButton.setRenderer(new LocaleListCellRenderer());

localeButton.setSelectedItem(Locale.getDefault());

add(localeButton, gbc);

gbc.gridwidth = GridBagConstraints.REMAINDER;

gbc.fill = GridBagConstraints.BOTH;

gbc.weightx = 1;

// Set up the input area panel

gbc.gridy++;

gbc.weighty = 1;

textArea = new JTextArea(5, 20);

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

JScrollPane jsp = new JScrollPane(textArea,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

add(jsp, gbc);

// Add a panel for the choice buttons

gbc.gridy++;

gbc.weighty = 0;

add(getTypePanel(), gbc);

// Add a panel for the refresh button and the count label

gbc.gridy++;

add(getCountPanel(), gbc);

// Add a panel for the parsed output

gbc.gridy++;

gbc.weighty = 1;

itemList = new JList();

add(new JScrollPane(itemList), gbc);

}

// Create the panel for the choice buttons

protected JPanel getTypePanel() {

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(2, 2, 20, 0));

charButton = new JRadioButton("Character", true);

panel.add(charButton);

wordButton = new JRadioButton("Word");

panel.add(wordButton);

lineButton = new JRadioButton("Line");

panel.add(lineButton);

sentButton = new JRadioButton("Sentence");

panel.add(sentButton);

// Add the buttons to a group

ButtonGroup group = new ButtonGroup();

group.add(charButton);

group.add(wordButton);

group.add(lineButton);

group.add(sentButton);

return panel;

}

// Create a panel for the refresh button and the count label

protected JPanel getCountPanel() {

JPanel panel = new JPanel();

JLabel label = new JLabel("Count:", JLabel.RIGHT);

panel.add(label);

countLabel = new JLabel("", JLabel.LEFT);

Dimension size = panel.getPreferredSize();

size.width = Math.min(size.width, 100);

countLabel.setPreferredSize(size);

panel.add(countLabel);

// Add the refresh button

refreshButton = new JButton("Refresh");

refreshButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

refreshDisplay();

}

});

panel.add(refreshButton);

//return panel;

/////////////////////////////////////////////////////////////////////////////////

try

{

String fileName = "myfile.txt";

// Open a file of the given name.

File file = new File(fileName);

// Get the size of the opened file.

int fsize = (int)file.length();

// Set to zero a counter for counting the number of

// characters that have been read from the file.

int chars_read = 0;

// Create an input reader based on the file, so we can read its data.

// FileReader handles international character encoding conversions.

FileReader in = new FileReader(file);

// Create a character array of the size of the file,

// to use as a data buffer, into which we will read

// the text data.

char[] data = new char[fsize];

// Read all available characters into the buffer.

while(in.ready()) {

// Increment the count for each character read,

// and accumulate them in the data buffer.

chars_read += in.read(data, chars_read, fsize - chars_read);

}

in.close();

// Create a temporary string containing the data,

// and set the string into the JTextArea.

textArea.setText(new String(data, 0, chars_read));

}

catch (IOException e)

{

}

return panel;

}

protected void refreshDisplay() {

int startIndex, nextIndex;

Vector items = new Vector();

// BasicDirectoryModel sor =new BasicDirectoryModel();

String lowText;

// Get the input text

String msgText = textArea.getText();

// Set the locale and prepare the iterator

Locale locale = (Locale)(localeButton.getSelectedItem());

lowText = msgText.toLowerCase(locale);

BreakIterator iterator = null;

// Work out which button is selected and set the iterator

if (charButton.isSelected()) {

iterator = BreakIterator.getCharacterInstance(locale);

}

else if (wordButton.isSelected()) {

iterator = BreakIterator.getWordInstance(locale);

}

else if (lineButton.isSelected()) {

iterator = BreakIterator.getLineInstance(locale);

}

else if (sentButton.isSelected()) {

iterator = BreakIterator.getSentenceInstance(locale);

}

iterator.setText(lowText);

startIndex = iterator.first();

nextIndex = iterator.next();

// Find the breaks in the input text

// and add the substrings for output

while (nextIndex != BreakIterator.DONE) {

items.addElement(lowText.substring(startIndex, nextIndex));

startIndex = nextIndex;

nextIndex = iterator.next();

}

String s= null;

//TO GET RID OF THE CHARACTERS SO THE DONT STORED IN THE VECTOR

String [] symbolDictionary = {"n",".",",","'",""","'","","£","..",";",":","-","_",null,"{","}",

"(",")","/","","+","=","[","]","<",">","n"," n"," n", " n",

" ", "nn", "n ","nnn"," "," "," "," "," ","n "," ",

" n "," n","n "};

//EXECLUDE COMMON WORD FROM THE COMPARISON

String [] wordDictionary = {"a","the","i","to","you","i","and","can","he","she","they","do",

"does","are","have","has","me","mr","mrs","ms","me","my","assignment"};

Collections.sort(items);

for (int d =0; d < symbolDictionary.length; d++)

Collections.replaceAll(items,symbolDictionary[d],"*");

for (int d=0 ; d< wordDictionary.length;d++)

Collections.replaceAll(items,wordDictionary[d],"*");

for (int i=0; i < (items.size()*2); i++)

{

items.remove("*");

}

countLabel.setText(Integer.toString(items.size()));

itemList.setListData(items); // Output the parsed input

System.out.println(items);

}

// Combo box to select the available locales

class LocaleListCellRenderer extends DefaultListCellRenderer {

public Component getListCellRendererComponent(

JList list, Object value, int index,

boolean isSelected, boolean hasFocus) {

Locale locale = (Locale)(value);

return super.getListCellRendererComponent(

list, locale.getDisplayName(),

index, isSelected, hasFocus);

}

}

}

iteratortest2.zip

#6

السلام عليكم

اخي العزيز

لقد شاهدت برنامجك ورايت انك تخزن الملف في Vector ولكن ما هو غرض البرنامج تبعك. لقد رايت انك انشئت 2 text areas و عده Buttons ولكن ما هو غرض البرنامج الاساسي. بمعني هل تريد ان تقارن ملفين سطر بسطر او كلمه بكلمه او حرف بحرف. حتي لو اردت ان تقارن الملفين كلمه بكلمه هل تريد ان تبني المقارنه علي اساس سطر بسطر.

هناك سوال اخر وهو ما طبيعه ناتج البرنامج او بمعني كيف تريد ان تخبر المستخدم انك انتهيت من عمل البرنامج وتريد ان تقدم النتيجه الي المستخدم؟

من اقتراحي لك اولا ان تنشا برنامج او كلاس يقوم بمهمه المقارنه ونتيجته ترد الى المستخدم. المستخدم ممكن يكون البرنامج هذا او اي كلاس صغير يقوم بارسال الطلب الي الكلاس المقارن.

#7

ليه ما تستخدم StringTokenizer ؟

الفكره هي ،، تاخذ الملف الاول تضعه في String و تستخدم StringTokenizer و نفس الطريقه للملف الثاني و بعدين تاخذ اول token للملفين و تقارنهم مع بعض .. الى اخر كلمه بالملف الاطول :) و بعدين يكون عندك عدادين .. الاول يعد جميع الكلمات و الثاني يعد الكلمات المتشابهه.. و بعد تطلع النسبه المؤيه (شكل الاملاء غلط :rolleyes: ) للكلمات المتشابهه ( تقريبا هذي هي الفكره اللي وصلت لها )

و اذا تبغى تكتب كود معين ...

ضع الكود في الرد و بعدين حدد الكود كامل و اضغط على الزر # او php لكي يصبح من الجهه الشمال و بشكل صحيح :)

انا حاولت اعدل ردك لكن ما اعرف ليه ما قدرت :D

#8

و عليك السلام اخي علاء الدين

بالنسبة للموضوع اظن ان اخي صلاح كفى و اوفى

لانه عندما عمل برنامج المفكرة فيه مقارنة و بحث و هذا قريب على سؤالك اخي Ham_ghailandi

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#9

السلام عليكم

اخي العزيز

هولاء كلاسات كل منهم يعمل عمل خاص

اولا كلاس يقوم بقراة اي ملف ويمكنك استعماله في كل مره تريد ان نقراء ملف ويقوم بقراة الملف ويعيد لك سطر سطر او كلمه كلمه او حرف حرف او رقم رقم

وهو كذلك يستطيع قراءه المستخدم:

import java.io.*;



public class LineReader {

  private final StreamTokenizer st;

  private final InputStream stream;

  private boolean eofReached;

  private FileReader r;

  private BufferedReader bf;







  public LineReader (String filename) {

    r = null;

    InputStream tmpStream = null;     

    try {

         tmpStream = new FileInputStream(filename);

    } 

    catch (FileNotFoundException e) {

      System.out.println ("File : " + filename + " not found.nExitting.");

      System.exit (1);

    }

    this.stream = tmpStream;

    bf = new BufferedReader(new InputStreamReader(stream));

    st = new StreamTokenizer (bf); 

    boolean eofReached = false;

  }







  public LineReader (InputStream stream) {

	Reader r = new BufferedReader(new InputStreamReader(stream));

    st = new StreamTokenizer (r); 

    this.stream = stream;

    boolean eofReached = false;

  }







  public int readInt () {

    try {

      eofReached = (st.nextToken() == StreamTokenizer.TT_EOF);

    } catch (IOException e) {System.err.println ("Error in readInt().");}



    if (eofReached) {

      System.err.println ("EOF reached. No readInt() possible. Returning -1.");

      return (-1);

    }



    return ((int) st.nval);

  }







  public double readDouble () {

    try {

      eofReached = (st.nextToken() == StreamTokenizer.TT_EOF);

    } catch (IOException e) {System.err.println ("Error in readDouble().");}



    if (eofReached) {

      System.err.println ("EOF reached. No readDouble() possible. Returning -1.0.");

      return (-1.0);

    }



    return (st.nval);

  }







  public char readChar () {

    try {

      eofReached = (st.nextToken() == StreamTokenizer.TT_EOF);

    } catch (IOException e) {System.err.println ("Error in readChar().");}



    if (eofReached) {

      System.err.println ("EOF reached. No readChar() possible. Returning -1.");

      return ((char)(-1));

    }



    return (st.sval.charAt(0));

  }







  public String readString () {

    String currentString = new String(); 

    try {

      for (int ch=stream.read(); ((ch!='n') && (ch!=-1)); ch=stream.read())

       currentString += (char) ch;

    } catch (IOException e) {System.out.println ("ERROR in readString().");}

    return currentString;

  }

  

  public String readline(){

     String currentString = null;

     try{

         return currentString = bf.readLine();

      }

     catch(IOException io ){

      io.printStackTrace();

      return null;

     }

     

  }

     

}
#10

هذا كلاس خاص بك ويقوم بارسال ملفين الي الكلاس السابق ومن ثم يخزن المعلومات في فكتورز

/*

 *  åÐÇ ÇáßáÇÓ íÞÑÇ ÇáãáÝíä æíÑÏåã ßá ÇáÓØæÑ Ýí ÝßÊæÑ

 *  æãä Ëã íãßäß ÊÓÊÚãá ßá ÏæÇá åäÇ áßí ÊÍÕá Úáì ÝßÊæÑÒ 

 *  

 */







import java.io.*;

import java.util.*;

import java.lang.*;







public class CompareFile{



    private String file1 = null;

    private String file2 = null;

    private Vector file1Vector = null;

    private Vector file2Vector = null;

    

    // constructor takes two files

    public CompareFile(String f1, String f2){

       this.file1 = f1;

       this.file2 = f2;

       file1Vector = new Vector();

       file2Vector = new Vector();

      }

   	 

   	 // íÎÒä ÇáãáÝ 1 ÓØÑ ÈÓØÑ 

   	 public void storeFile1ByLine(){

      LineReader lr = new LineReader( file1);

      String line = null;

      while ( (line = lr.readline()) != null ){

        file1Vector.addElement(line);

         }

   	 } 

   	 

   	 // íÎÒä ÇáãáÝ 2 ÓØÑ ÈÓØÑ 

   	 public void storeFile2ByLine(){

      LineReader lr = new LineReader( file2);

      String line = null;

      while ( (line = lr.readline()) != null ){

        file2Vector.addElement(line);

         }

   	 }

   	 

   	 // íÎÒä ÇáãáÝ 1 ÍÑÝ ÈÍÑÝ

   	 public void storeFile1ByChar(){

      LineReader lr = new LineReader( file1);

      char line;

      while ( (line = lr.readChar()) != ((char)(-1)) ){

        file1Vector.addElement(new Character(line));

         }

      }

      

      // íÎÒä ÇáãáÝ 2 ÍÑÝ ÈÍÑÝ

   	 public void storeFile2ByChar(){

      LineReader lr = new LineReader( file1);

      char line;

      while ( (line = lr.readChar()) != ((char)(-1)) ){

        file2Vector.addElement(new Character(line));

         }

      }   

   	 

      // íÑÌÚ Ýß澄 ÇáãáÝ 1 

     public Vector getFile1Vector(){

      return file1Vector;

     }

     

     //  íÑÌÚ Ýß澄 ÇáãáÝ 2

     public Vector getFile2Vector(){

      return file2Vector;

     }

     

     

 }
#11

الكلاس السابق الكتابه بالعربي ما طلع الانكود تبعها

المعذره

هذا كلاس يقوم باختبار الكلاسات السابقه حيث يمكنك استبداله بالبرنامج تبعك:

import java.lang.*;

import java.util.*;

import java.io.*;



class TestCompare{



	private static String file1 = "file1.txt";

	private static String file2 = "file2.txt";

	private static CompareFile cf;

	private static Vector v1 = new Vector();

	private static Vector v2 = new Vector();



	public static void main(String[] args){

  

  cf = new CompareFile(file1, file2);

  cf.storeFile1ByLine();

  cf.storeFile2ByLine();

  

  v1 = cf.getFile1Vector();

  v2 = cf.getFile2Vector();

  int i=0;

  for ( i=0; i<v1.size(); i++){

 	 

 	 String tmp1 = (String) v1.elementAt(i);

 	 System.out.println( "file1 line " +i+"t"+ tmp1 );

 	 String tmp2 = (String) v2.elementAt(i);

 	 System.out.println( "file2 line " +i+"t"+ tmp2);

  }

  for ( int j=i; j<v2.size(); j++){

 	 

 	 String tmp2 = (String) v2.elementAt(j);

 	 System.out.println( "file2 line " +j+"t"+ tmp2);

 	 }	

	}

}

هذا الموضوع مغلق.

مواضيع مشابهة