Archive for نوامبر, 2007

Vevtor

نوامبر 23, 2007

 

سلام !
با وجود اینکه  جاوا کلاس Vector  را دارا می باشد اما خب پیاده سازی آن به روش دلخواه مفید است .
(زیاد حرف نمی زنم برنامه ببنید :)

/*package Matrix*/
import java.lang.Exception;
import java.lang.NullPointerException;
public class Vector<T> {
    private int size=80;
    protected T [] cont;
     protected int low=0,high=0,pos=0;
 
    //=================init with no parameter===============
   protected void init(){
       low=pos=0;
       high=size;
       high++;
       try{
                cont=(T[])new Object[high];
          }catch(NullPointerException exc){
        System.out.println(“nul pointer Exception”);
      }
        for(Integer i=low;i<high;i++)
            cont[i]=null;
   }
   
   //===================init with low par.=================
    protected void init(int l){
    low=pos=l;
   
    high=size;
    high++;
    if(low>high)
            throw new  NullPointerException();
        try{
         cont=(T[])new Object[high];
           }catch(NullPointerException exc){
        System.out.println(“nul pointer Exception”);
           }
        for(Integer i=low;i<high;i++)
            cont[i]=null;
    }
//===================int with two parameter===================
    protected void init(int l,int h){
       low=pos=l;
      
       high=h;
       high++;
        if(low>high)
            throw new  NullPointerException();
       if(high>size)
           size=high;
    try{
          cont=(T[])new Object[high];
       }catch(NullPointerException exc){
        System.out.println(“nul pointer Exception”);
       }
        for(Integer i=low;i<high;i++)
            cont[i]=null;
    }
//====================add element at pos i=====================   
    public T addelement(T e,int p)
    {
     //  if(p > pos || p < low)
    if(p<low)      
       throw new ArrayIndexOutOfBoundsException(p);
      // else
        //{ 
             cont[p]=e;
             pos++;
        //}
       return e;
       
    }
   
    public int currentsize(){
        return this.size;
    }
   
   
    /** Creates a new instance of Vector */
 
   
   
    public Vector(int l,int h) {
    try{
    init(l,h);
    }catch(NullPointerException exc){
        exc.printStackTrace();
        return ;
    }

    }
    public Vector(int l){
    
     init(l);
    }
   
    //=======================contstructor===============
    public Vector(){
    
         init();
    }
    //=====================constructor====================
   
    public  Vector(Vector<T> ob){
        this.high=ob.high;
        this.low=ob.low;
        for(int i=low;i<=high;i++)
        {
      try{
             this.cont[i]=ob.cont[i];
         }catch(NullPointerException exc){
          exc.printStackTrace();return ;  
         }
        }
       
    }
//——————low and high————————–   
   public  int hi(){
        return this.high;
   }
   public int lo(){
       return this.low;
   }
//——————return element at i pos—————————–
   public T reap(int i) throws ArrayIndexOutOfBoundsException//return element at position i;
   {
      if(i>this.high || i<this.low)
          throw new ArrayIndexOutOfBoundsException();
      return cont[i];
   }
  
    public T reap(int i,T e) throws ArrayIndexOutOfBoundsException//return element at position i;
   {
      if(i>this.high || i<this.low)
          throw new ArrayIndexOutOfBoundsException();
      return cont[i];
   }

   public int size()
   {
       return this.size;
   }

}

class Grater extends Exception{
   public  Grater(){
         System.out.println(“your Domain have Error!\n The low is Grater Than High “);
   }
 
}

4 !

نوامبر 19, 2007

سلام !
اول بگم که پست قبلی برنامه ی اصلی ایراد داشت که من درستش کردم و نوشتم .
حالا :

برنامه ی BST : با شیوه ی جالبی پیاده سازی شده  و با تکنیک Generic  نوشته شده که از کلاس Comparable ارث برده .
متد های insert , remove را اینجا می نویسم . متهد های عمومی insert , remove  برعهده ی خواننده ی متن .

 import java.io.*;
import java.util.NoSuchElementException;
class BinaryNode<T extends Comparable> {
  T data;
  protected int Visited;
   BinaryNode<T> right,left;
  BinaryNode ( ) {
    this.data =null;
    right=left=null;
    Visited=0;
    }
}

class BinaryTree<T extends Comparable> {
 
    private  BinaryNode root;
    public  BinaryTree(){
       root=null;
}
  
  private static<T extends Comparable> 
  BinaryNode<T> insert ( BinaryNode tree, T data ) throws DuplicatItem {
    if ( tree == null ) {
      tree = new BinaryNode<T> ( data,null,null );
   
    }
    else {
      int dir = data.compareTo ( tree.data ) > 0 ? 1 : 0;
     
      if(dir<0)
          tree.left=insert(tree.left,data);
      
      else if(dir>0)
          tree.right=insert(tree.right,data);
      else
          throw new DuplicatItem(“Search Tree : insert”);
    }
    return tree;
 }
 
  private<T extends Comparable>
  BinaryNode<T> remove( BinaryNode tree, T data ) {
    if ( tree == null )
            throw new NoSuchElementException(“” +
                    “remove: No element to remove”);
  if(data.compareTo(tree.data)<0)
      tree.left=remove(tree.left,data);
  else if(data.compareTo(tree.data)>0)
      tree.right=remove(tree.right,data);
  else if(data.compareTo(tree.data)==0){
      if(tree.left==null)
        tree=tree.right;
      else if(tree.right==null)
          tree=tree.left;
  }
  else{
        Comparable replacewithvalue=rightMax(tree.left);
        tree.data = (T) replacewithvalue;
        tree.left= deletetrightMax(tree.left);
  }
    return tree;
   
  }

پیمایش پیش ترتیب !

نوامبر 17, 2007

سلام

اگر ساختمان داده داشته باشيد حتما به به پيمايش هاي پيش ترتيب و پس ترتيب و ميان ترتيب بر خورد كرده ايد . براي نوشتن آنها در صورت استفاده از c/c++ يا Java از كلاس(سی ++ و جاوا) و struct  در سي براي پياده سازي استفاده مي كنيد .
به اين صورت كه يك كلاس TreeIterator (مثلا) استفاده مي كنيد و با استفاده از ارث بري  كلاسی ديگر با عنوان PostOrder یا InOrder یا 
PreOrder ساخته  که با استفاده از یک پشته و یک کلاس و یک شمارنده این عمل را انجام دهیم .
همان طور که می بینید یک پروسه ی طولانی و حتما با اشتباهات فراوان همراه است اما جند روز پیش در یک وبلاگ روشی جالب دیدیم که بدین شرح بود :

نکته : این برنامه به زبان جاواست که با اندکی تغییر برای سی++ قابل استفاده است .

 void nonrecpreorder(BinaryTree tree){
      BinaryNode currnode=tree.root;// یک گره ایجاد و مقدار آن را برابر ریشه ی درخت قرار می دهد
      while(true){                                     // یک حلقه ی بی نهایت
          if(currnode==null)                     // این شرط برای چیست؟
              break;
          //int visited=0 in the BinaryTree defined ! 
          if(currnode.left!=null && currnode.Visited!=1) //
           currnode=currnode.left;
          else if(currnode.right!=null && currnode.right.Visited!=1)
            currnode=currnode.right;
                else if(currnode.Visited==0)
                    {
                    // element defined like :      T element   //in BinaryTree
                     System.out.print(currnode.element+ ” “);
                    currnode.Visited=1;
                    }
          else
          {
                            currnode=currnode.parent;
          
           }
      }          
  }

جالب بود نه !

 

 

 

2 !

نوامبر 5, 2007

سلام !

اولین برنامه ای که می خوام اینجا بذارم یک پشته به زبان جاوا با استفاده از اینترفیسه و با استفاده از یک استک آرایه ای  از اون استفاده کردم و در نهایت اجرای آن .

نکته : این برنامه به طور کلی از اپلت استفاده نمی کند (فقط برای نشان دادن قدرت آن و همچنین زیبایی هرچند  اندک برنامه)و فقط برای گرفتن عدد مورد استفاده قرار گرفته .

نکته 2:هر فایل public در فایل جداگانه و با نام آن کلاس ذخیره شود .(اگر در محیط netbeans اجرا شود که چه بهتر):

this is a java programm that creat a Generic Stack (In Array Model). 

package Data;

import java.io.IOException;

/**
 *
 * @author eprogrammer

*/

public interface Stack {
    public void push(Object x);
    public Object pop() throws UnderFlow;
    public boolean isEmpty();
    public void MakeEmpty();
   
}

//————————————–

/*
 * ArrayStack.java
 *
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.lang.*;

/**
 *
 * @author eprogrammer
 */

public class ArrayStack implements Stack {
   
    private int capacity=100;
    private Object []data;
    private int topindex=0;

    private Object []data2;
   
    /** Creates a new instance of ArrayStack */
    public ArrayStack(int capacity) {
        this.capacity=capacity;
        data = new Object[capacity+1];
 
    }
   
    public ArrayStack(){
        this(101);
         }
   
    public void push(Object x){
   /* if(topindex==this.capacity-1)
        {
            this.capacity*=2;
            data2=new Object[this.capacity];
            for(int i=0;i<topindex;i++)
              data2[i]=data[i];
            data=data2;                                       
        } */
          try{
       data[topindex++]=x;
      }catch(ArrayIndexOutOfBoundsException ex){
          System.out.println(“Out of boounds”);
      }
     
    }
   
   
    public Object pop()throws UnderFlow{
        if(this.isEmpty())
             throw  new UnderFlow(“The stack is empty”);
       
      else
          data[topindex--]=null;
     return data[topindex+1];
     }
        public void MakeEmpty(){
        topindex=-1;
    }
    public Object  rtop(){
        return data[topindex];
       
    }
   
    public Object retrieve(int i){
        return data[i];
    }
    public boolean isEmpty(){
        if(topindex==-1)
            return true;
        else
            return false;
       
    }

        
    public int returnsize(){
        return topindex;
    }
   
public static void main(String[] args)
{
  
  //  Scanner in=new Scanner(System.in);
    ArrayStack mystack=new ArrayStack();
   int num=0;
 
 for(;num==0;)
 {
       try{
 
     num=Integer.parseInt(JOptionPane.showInputDialog(null,”Enter size of Stack “)); 
   }catch(NumberFormatException ex){
       JOptionPane.showMessageDialog(null,”Must Enter a Number !”);
      
   }
   
}
   
   
    for(int i=0;i<num;i++)
    {
    String str=JOptionPane.showInputDialog(“Enter “+(i+1) + “th Object : “);
    try{
        mystack.push(new String(str));
        }catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println(“Out Of Bound”);
        }
    }
    String str = null;
        try {
            str=(String) mystack.pop();
            } catch (UnderFlow ex) {
            ex.printStackTrace();
        }catch(ArrayIndexOutOfBoundsException ex)
        {
            JOptionPane.showMessageDialog(null,”Out of bounds “);
        }
    if (!mystack.isEmpty())
    JOptionPane.showConfirmDialog(null,str+” Poped”)     ;
      
    for(int i=0;i<mystack.returnsize();i++)
    System.out.println(mystack.retrieve(i));
       
}

   
}

class UnderFlow extends IOException{
   
    /** Creates a new instance of UnderFlow */
    public UnderFlow() {
    }
    public UnderFlow(String message)
    {
        super(message);
    }
   
}

1 !

نوامبر 3, 2007

خب بعد از مدت ها شروع کردم .

در این مکان درباره همه چیز(فقط برنامه نویسی!)  حرف خواهیم (خواهم   (شما فقط نظر بدهید (((:  ) زد .

  زبانهای  C/C++  ,  java , Assembly  را با هم یاد می گیریم (منم همین طور) خب از کجا شروع کنیم ؟

  ( اگر هم کسی این روزهای اول ندید  یا خدایی نکرده نظر نداد ؛ خودم از یه جایی شروع می کنم (:  )