Tuesday, February 28, 2012

Data Structure and Algorithm - Stack - stackAsLinkdedlist.h source code

stackAsLinkedlist.h as template


#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
// Stack as Linkedlist

template <class Type>
class Node{
    public:
        Type data;
        Node *link;
        Node *back;
        Node(int data){
            this->data = data;
            link = NULL;
            back = NULL;
        }
};

template<class Type>
class stackType
{
private:
     int stackTop; //variable to point to the top of the stack
     Node<Type> *newnode;
     Node<Type> *tail;
public:
    //Function to initialize the stack to an empty state.
    //Postcondition: stackTop = 0
    void initializeStack(){
        tail = NULL;         
        stackTop = 0;
    }
   
    //Function to determine whether the stack is empty.
    //Postcondition: Returns true if the stack is empty;
    // otherwise, returns false.
    bool isEmptyStack(){
        return (stackTop == 0);
    }
    //Function to remove all the elements from the stack.
    //Postcondition: stackTop = 0
    void destroyStack(){
        for(int i=0;i<stackTop;i++)
           pop();
        stackTop = 0;
    }
   
    //Function to add newItem to the stack.
    //Precondition: The stack exists and is not full.
    //Postcondition: The stack is changed and newItem
    // is added to the top of stack.
    void push(Type newItem){
                newnode = new Node<Type>(newItem);
                if(stackTop > 0){
                    tail->link = newnode;
                    newnode->back = tail;
                    tail = newnode;
                } 
                tail = newnode;
                stackTop++;
    }
   
    //Function to return the top element of the stack.
    //Precondition: The stack exists and is not empty.
    //Postcondition: If the stack is empty, the program
    // terminates; otherwise, the top element
    // of the stack is returned.
    Type top(){
        assert(stackTop!=0);    // if stackTop = 0 then exit program
        return tail->data;
    }
   
    //Function to remove the top element of the stack.
    //Precondition: The stack exists and is not empty.
    //Postcondition: The stack is changed and the top
    // element is removed from the stack.
    void pop(){
        if(isEmptyStack()){
            cout<<"Stack is Empty..."<<endl;
        }else{
                if(stackTop == 1){
                    Node<Type> *delnode;
                    delnode = tail;
                    tail = NULL;
                    delete delnode;
                }else{
                    tail = tail->back;
                    delete tail->link;
                    tail->link = NULL;
                }
            stackTop--;
        }
    }
     // number of stacks
    int Nstack(){
         return stackTop;
     }   
    //constructor
    stackType(){
        stackTop = 0;
        tail = NULL;
    }
 
    //destructor
    ~stackType(){
         destroyStack();
    }
};

Data Structure and Algorithm - Stack - stackAsArray.h source code

stackAsAraay.h as template


#include <iostream>
#include <cassert>
#include <string.h>
using namespace std;

template<class Type>
class stackType
{
private:
    int maxStackSize; //variable to store the maximum stack size
    int stackTop; //variable to point to the top of the stack
    Type *list; //pointer to the array that holds the
    //stack elements
public:
    //Function to initialize the stack to an empty state.
    //Postcondition: stackTop = 0
    void initializeStack(){
        stackTop = 0;
    }
   
    //Function to determine whether the stack is empty.
    //Postcondition: Returns true if the stack is empty;
    // otherwise, returns false.
    bool isEmptyStack(){
        return (stackTop == 0);
    }
 
    //Function to determine whether the stack is full.
    //Postcondition: Returns true if the stack is full;
    // otherwise, returns false.
    bool isFullStack(){
        return (stackTop == maxStackSize);
    }
   
    //Function to remove all the elements from the stack.
    //Postcondition: stackTop = 0
    void destroyStack(){
        for(int i=0;i<stackTop;i++){
            delete list[i];
        }
        stackTop = 0;
    }
   
    //Function to add newItem to the stack.
    //Precondition: The stack exists and is not full.
    //Postcondition: The stack is changed and newItem
    // is added to the top of stack.
    void push(const Type& newItem){
        if(isFullStack()){
            cout<<"Stack is Full....."<<endl;
        }else{
            list[stackTop]=newItem;
            stackTop++;
        }
    }
   
    //Function to return the top element of the stack.
    //Precondition: The stack exists and is not empty.
    //Postcondition: If the stack is empty, the program
    // terminates; otherwise, the top element
    // of the stack is returned.
    Type top(){
        assert(stackTop!=0);    // if stackTop = 0 then exit program
        return list[stackTop-1];
    }
   
    //Function to remove the top element of the stack.
    //Precondition: The stack exists and is not empty.
    //Postcondition: The stack is changed and the top
    // element is removed from the stack.
    void pop(){
        if(isEmptyStack()){
            cout<<"Stack is Empty..."<<endl;
        }else{
            stackTop--;
        }
    }
     // number of stacks
    int Nstack(){
         return stackTop;
     }   
    //constructor
    //Creates an array of the size stackSize to hold the
    //stack elements. The default stack size is 100.
    //Postcondition: The variable list contains the base
    // address of the array, stackTop = 0, and
    // maxStackSize = stackSize.
    stackType(int stackSize){
        if(stackSize <= 0){
            cout<<"Size of stack can't be negative number ..."<<endl;
            cout<<"Now the program set size = 100 Automatically.."<<endl;
            stackSize = 100;
        }else{
            maxStackSize = stackSize;
        }
        stackTop = 0;
        list = new Type[maxStackSize];
        assert(list!=NULL);         // exit program if list is NULL
    }
 
    //destructor
    //Removes all the elements from the stack.
    //Postcondition: The array (list) holding the stack
    // elements is deleted.
    ~stackType(){
        delete[] list;
    }
    void print(void);
};

Data Structure and Algorithm - Queue - queueAsLinkedlist.h source code

queueAsLinkedlist.h as template


#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

// Class Node
template <class Type>
class Node{
public:
    Type node;
    Node* next;
    Node(Type node){
        this->node = node;
        next = NULL;
    }
};

// Class QueueType
template <class Type>
class queueType{
private:
    int count;
    Node<Type> *first;
    Node<Type> *last;
    Node<Type> *newNode;
public:
    queueType(){
        first = NULL;
        last = NULL;
        count = 0;
    }
    void addQueue(Type newItem){
        newNode = new Node<Type>(newItem);          // create new Node
        if(count == 0){                             // if queue is empty
            first = newNode;
            last = newNode;
        }else{                                      // if queue is not empty
            last->next = newNode;
            last = newNode;
        }
        count++;
    }
    void deleteQueue(){
        if(count == 0){                             // if queue is empty
            cout<<"Queue is empty, can't delete...."<<endl;
        }else{
            if(count > 1){
                Node<Type> *delNode;
                delNode = first;
                first = first->next;
                delete delNode;
                count--;
            }else{                      // if queue contents only one item
                first = NULL;
                last = NULL;
                delete newNode;
                count--;
            }
        }
    }
    int getCount(){
        return count;
    }
    Type front(){                               // return front value of queue
        if(count > 0){      
            return first->node;
        }else{
            return '\0';
        }
    }
    Type back(){                                // return back value of queue
        if(count > 0){      
            return last->node;
        }else{
            return '\0';
        }   
    }
    ~queueType(){
        for(int i =0 ; i<count ; i++){
            Node<Type> *delNode;
            delNode = first;
            first = delNode->next;
            delete delNode;
        }
    }
   
};

Data Structure and Algorithm - Queue - queueAsArray.h source code

queueAsArray.h  as template


#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

template <class Type>
class queueType{
private:
    int maxQueueSize;
    int count;
    int queueFront;
    int queueRear;
    Type* list;
public:
    void initializeQueue(){             // original queue
        queueFront = 0;
        queueRear = maxQueueSize - 1;
        count = 0;
    }
    bool isEmptyQueue(){                // Is queue empty ? [count = 0]
        return (count == 0);
    }
    bool isFullQueue(){                 // Is queue full ? [count = max of queue]
        return (count == maxQueueSize);
    }
    void destroyQueue(){                // delete all queue
        initializeQueue();
    }
    Type front(){                       // return value front of queue
        assert(!isEmptyQueue());
        return list[queueFront];
    }
    Type back(){                        // return value back or rear of queue
        assert(!isEmptyQueue());
        return list[queueRear];
    }
    int size(){                         // return value size of queue present
        return count;
    }
    int nFront(){                       // return value of front
        return queueFront;
    }
    int nRear(){
        return queueRear;               // return value of rear
    }
    Type locationQueue(int loc){        // return value of queue in any loction
        return list[loc];
    }
    int maxSize(){
        return maxQueueSize;
    }
    void addQueue(const Type& newElement){      // add queue from the rear
        if(!isFullQueue()){
            queueRear = (queueRear + 1) % maxQueueSize;
            count++;
            list[queueRear] = newElement;
        }else{
            cout<<"Queue list is Full => can not add more..."<<endl;
        }
    }
    void deleteQueue(){                 // delete queue from the front
        if(!isEmptyQueue()){
            queueFront = (queueFront + 1) % maxQueueSize;
            count--;
        }else{
            cout<<"Queue list is Empty => can not delete more..."<<endl;
        }
    }
    queueType(int queueSize){           // constructor
        if(queueSize <= 0){
            cout<<"Size of array to hold the element must be Positive.."<<endl;
            cout<<"Create a array of size = 100"<<endl;
            maxQueueSize = 100;
        }else
            maxQueueSize = queueSize;
        queueFront = 0;
        queueRear = maxQueueSize -1;
        count = 0;
        list = new Type[maxQueueSize];
        assert(list !=NULL);
    }
    ~queueType(){                       // destructor
        delete []list;
    }
};

Monday, February 27, 2012

Unix Tools - Shell Script Programming - Example - Demo_choice.sh

Demo_choice.sh

# Code :
#! /bin/bash

choice1() {
  printf "Disk usage for your HOME=%s\n" "${HOME}"
  du -sh ${HOME}
  return
}

choice2() {
  { printf "Press 'q' key to terminate viewing this list of files.\n";
    ls -lR ${HOME}; } | less
  return
}

choice3() {
  { printf "Press 'q' key to terminate viewing this list of files.\n";
  find ${HOME} -type f -size +1M -print | xargs ls -lh; } | less
  return
}

choice4() {
  read -p "Enter string for which to search:" STRING
  printf "Working...."
  find ${HOME} -type f ! -name '.seKreT' ! -name '*.flv' ! -name '*.mp4' -exec grep -l -- "${STRING}" {} \; >.seKreT
  printf "Done\n"
  { printf "Press 'q' key to terminate viewing this list of files.\n";
  cat .seKreT; } | less
  rm .seKreT
  return
}

menu() {
   local CHOICE
   local XIT=5 # this is the exit choice

   while true
   do
     printf "    M E N U\n"
     printf "1.  Display Disk Space Usage\n"
     printf "2.  Display File List\n"
     printf "3.  Display files >= 1MB in size\n"
     printf "4.  Display all files containing some string\n"
     printf "5.  Exit\n"
     read -p "Enter your choice:" CHOICE
     if [[ ! "${CHOICE}" =~ ^[0-9]+$ ]]
     then
       printf "You must enter an unsigned integer value between 1 and %d.\n" ${XIT}
       continue
     fi
     if ((CHOICE<1)) || ((CHOICE>XIT))
     then
       printf "Your choice must be between 1 and %d.\n" ${XIT}
       continue
     fi
     break
   done
   return ${CHOICE}
}

# main code
while true
do
  menu
  case ${?} in
    1) choice1 ;;
    2) choice2 ;;
    3) choice3 ;;
    4) choice4 ;;
    5) # do nothing, exit program
       break
       ;;
    *)
       # should never get here
       printf "Impossible choice; contact programmer now....\n"
       ;;
  esac
done
exit 0

Make permission for execution before running program
$chmod 700 Demo_choice.sh
$./Demo_choice.sh

C Language - get formatted data form stdin , scanf()

scanf

function
<cstdio>
int  scanf ( const char * format, ... );
Read formatted data from stdin
Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

Parameters

format
C string that contains one or more of the following items:
  • Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
  • Non-whitespace character, except percentage signs (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from stdin, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of stdin unread.
  • Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from stdin and stored in the locations pointed by the additional arguments. A format specifier follows this prototype:

    %[*][width][modifiers]type

    where:

    *An optional starting asterisk indicates that the data is to be retrieved from stdin but ignored, i.e. it is not stored in the corresponding argument.
    widthSpecifies the maximum number of characters to be read in the current reading operation
    modifiersSpecifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument:
    h : short int (for d, i and n), or unsigned short int (for o, u and x)
    l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g)
    L : long double (for e, f and g)
    typeA character specifying the type of data to be read and how it is expected to be read. See next table.

    scanf type specifiers:
    typeQualifying InputType of argument
    cSingle character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.char *
    dDecimal integer: Number optionally preceded with a + or - sign.int *
    e,E,f,g,GFloating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4float *
    oOctal integer.int *
    sString of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).char *
    uUnsigned decimal integer.unsigned int *
    x,XHexadecimal integer.int *
additional arguments
The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding %-tag within the format string, in the same order. For each format specifier in the format string that retrieves data, an additional argument should be specified. These arguments are expected to be references (pointers): if you want to store the result of a fscanf operation on a regular variable you should precede its identifier with the reference operator, i.e. an ampersand sign (&), like in:
int n; scanf ("%d",&n);

Return Value

On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* scanf example */
#include <stdio.h>

int main ()
{
  char str [80];
  int i;

  printf ("Enter your family name: ");
  scanf ("%s",str);  
  printf ("Enter your age: ");
  scanf ("%d",&i);
  printf ("Mr. %s , %d years old.\n",str,i);
  printf ("Enter a hexadecimal number: ");
  scanf ("%x",&i);
  printf ("You have entered %#x (%d).\n",i,i);
  
  return 0;
}


This example demonstrates some of the types that can be read with scanf:
Enter your family name: Soulie
Enter your age: 29
Mr. Soulie , 29 years old.
Enter a hexadecimal number: ff
You have entered 0xff (255).

Sunday, February 26, 2012

Computer Organization - signed bit number (2's complement and Magnitude) Algorithm

Flow Chart
 Processing Running of Program

Computer Organization - signed bit number (2's complement and Magnitude)

>> Algorithm and FlowChart 
multiSigned.h

#include <iostream>
#include <math.h>
using namespace std;
/*
 multiple by 2 numbers as in 2's complement
 */

class multiSigned {
private:
    /*
     A >>
     Q1 >>
     Q >> holding multiplicant value
     M >> holding multiplier value
     Bit >> bit number
     */
    int A,Q1,Q,M,maxRang,maxBit,Count,minRang,result;
public:

    /*  Constructor :   */
    multiSigned(int Bit , int multiplicant , int multiplier){
        minRang = -pow(2,Bit-1);   // rang lowest
        maxRang = pow(2,Bit-1)-1; // rang highest
        maxBit = pow(2,Bit-2);  // value of MSB
        Count = Bit;
        M = multiplicant;
        Q = multiplier;
        result = M*Q;
        Q1 = 0;
        A = 0;
    }
    int getResult(){
        return result;
    }
    /* Function printAsBit(int number) 
     print decimal to bianry 2's complement in 8bit   
     range { -128 ~ 127 }
     1   1  1  1  1 1 1 1
     -128 64 32 16 8 4 2 1
     */
    void printBitOf(int number){
        if(number<0)
            cout<<"1";
        else
            cout<<"0";
        for(int i=maxBit; i>0 ; i=i/2){
            if((number & i)>0){
                cout<<"1";
            }else{
                cout<<"0";
            }
        }
    }
    // Print Q only without thinking of signed of integer
    void printQ(int number){
        for(int i=maxBit<<1; i>0 ; i=i/2){
            if((number & i)>0){
                cout<<"1";
            }else{
                cout<<"0";
            }
        }
    }
    void calculate(){
        if(Count<7)
            cout<<"A\tQ\tQ[-1]\tM\n\n";
        else
            cout<<"A\t\tQ\t\tQ[-1]\tM\n\n";
        printBitOf(A);
        cout<<"\t";
        printQ(Q);
        cout<<"\t";
        cout<<Q1;
        cout<<"\t";
        printBitOf(M);
        cout<<"\tInitial Value\n\n";
        do{
            // if 1,0 => A <- A + M
            if((Q&1)==0 && Q1==1){
                A= A + M;
                printBitOf(A);
                cout<<"\t";
                printQ(Q);
                cout<<"\t";
                cout<<Q1;
                cout<<"\t";
                printBitOf(M);
                cout<<"\tA <- A + M\n";
            }
            // if 1,0 => A <- A - M
            else if((Q&1)==1 && Q1==0){
                A = A - M;
                printBitOf(A);
                cout<<"\t";
                printQ(Q);
                cout<<"\t";
                cout<<Q1;
                cout<<"\t";
                printBitOf(M);
                cout<<"\tA <- A - M\n";
            }
           
            // Arithmetic Shift-Right A,Q,Q[-1] Count-1
            // Q = xxx1 => Q[-1] = 1
            if((Q&1)==1)
                Q1=1;
            else // Q = xxx0 => Q[-1] = 0
                Q1=0;  
            // Shift-Right Q 1 bit
            Q>>=1;
            // Postcondition : A = xxx1
            if((A&1)==1){
                if(Q>=0)
                    // to set Q = 1xxx
                    Q = abs(Q)|(maxBit*2);  
            }else
                if(Q<0)
                    // to set Q = 0xxx
                    Q = Q & (maxBit*2-1);
            // Shift-Right A 1 bit
            A>>=1;
            // Print A , Q , Q1 , M
            printBitOf(A);
            cout<<"\t";
            printQ(Q);
            cout<<"\t";
            cout<<Q1;
            cout<<"\t";
            printBitOf(M);
            cout<<"\tShift\n\n";
            // decrease count --
            Count--;
        }while(Count != 0);  // count still not Zero ?
        // Print Twice bit of result from A:Q
        cout<<"A:Q :";
        printBitOf(A);
        cout<<" ";
        printQ(Q);
        // Print Result in decimal number
        cout<<" : "<<getResult()<<endl;
    }
};



Tuesday, February 21, 2012

XCODE - Source Code + GUI "Calculator v1.0"

View Controller :
calViewController.h

#import <UIKit/UIKit.h>

@interface calViewController : UIViewController
{   /*  Declare variable as private   */
    double currentNumber;   // store current value
    double storeNumber;     // store old value
    char Operator;          // store sign of operator
    bool point;             // condition to floating point
    int pointCount;         // condition to number of floating point
}
// Prototype Function to use
// getCurrentNumber
- (void) getCurrentNumber:(double)number;
- (void) showCurrentNumber;
- (void) showStoreNumber;
/*  Label for Showing number    */
@property (weak, nonatomic) IBOutlet UIButton *bEqualOutlet;
@property (weak, nonatomic) IBOutlet UILabel *labelShow;
/*  Button for all operand and operator */
- (IBAction)b:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *ac;

- (IBAction)bPlus:(id)sender;
- (IBAction)bMinus:(id)sender;
- (IBAction)bMulti:(id)sender;
- (IBAction)bDivid:(id)sender;
- (IBAction)bClear:(id)sender;
- (IBAction)bPower:(id)sender;
- (IBAction)bEqual:(id)sender;
- (IBAction)bPoint:(id)sender;

@end


calViewController.m

#import "calViewController.h"

@implementation calViewController
@synthesize ac;
@synthesize bEqualOutlet;
@synthesize labelShow;
/*  Start Function    */
//Function getCurrentNumber
- (void) getCurrentNumber:(double)number{
    if(point == false){
        currentNumber = currentNumber*10 + number;
    }else{
        currentNumber += number/pow(10,++pointCount);
    }
}
//Function getLastStore
- (void) getLastStore{
    if(Operator == '+'){
        storeNumber += currentNumber;
    }else if(Operator == '-'){
        storeNumber -= currentNumber;
    }else if(Operator == '*'){
        storeNumber *= currentNumber;
    }else if(Operator == '/'){
        storeNumber /= currentNumber;
    }else if(Operator == '^'){
        storeNumber = pow(storeNumber,currentNumber);
    }
    currentNumber = 0;
}
//Function showCurrentNumber
- (void) showCurrentNumber{
    self.labelShow.text = [NSString stringWithFormat:@"%g",currentNumber];
}

//Function showStoreNumber
-(void) showStoreNumber{
    self.labelShow.text = [NSString stringWithFormat:@"%g",storeNumber];
}
/*  End Function   */
- (IBAction)b:(id)sender{  // button 0
    double n = [sender tag];
    [self getCurrentNumber:n];
    [self showCurrentNumber];
}
- (IBAction)bPlus:(id)sender{          // button Plus +
    [self getLastStore];
    [self showStoreNumber];
    Operator = '+';
    point = false;
    pointCount = 0;
}
- (IBAction)bMinus:(id)sender{          // button Minus -
    [self getLastStore];
    [self showStoreNumber];
    Operator = '-';
    point = false;
    pointCount = 0;

}
- (IBAction)bMulti:(id)sender{          // button Multi *
    [self getLastStore];
    [self showStoreNumber];
    Operator = '*';
    point = false;
    pointCount = 0;

}
- (IBAction)bDivid:(id)sender{          // button Divid /
    [self getLastStore];
    [self showStoreNumber];
    Operator = '/';
    point = false;
    pointCount = 0;

}
- (IBAction)bClear:(id)sender{          // button Clear AC
    currentNumber = 0;
    storeNumber = 0;
    point = false;
    pointCount = 0;
    Operator = '+';
    [self showStoreNumber];
}
- (IBAction)bPower:(id)sender{          // button Power ^
    [self getLastStore];
    [self showStoreNumber];
    Operator = '^';
    point = false;
    pointCount = 0;

}
- (IBAction)bEqual:(id)sender{          // button Equal =
    [self getLastStore];
    [self showStoreNumber];
    point = true;
    pointCount = 0;
    Operator = '+';
}
- (IBAction)bPoint:(id)sender{          // button Point .
    point = true;
    pointCount = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    /*  Constructure */
    currentNumber = 0;
    storeNumber = 0;
    Operator = '+';
    point = false;
    pointCount = 0;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setLabelShow:nil];
    [self setBEqualOutlet:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


Data Structure and Algorithm - Binary Tree Alogrithm PrintTree()

Function printTree() in by level of each tree
.....
class bSearchTreeType{
.......
printTree(); 
.......
};


void bSearchTreeType::printTree(){ 
        int level = 0; // initialize of level of tree
/*  Condition for root is NULL ? */
        if(root == NULL)
            return; /*  Exit function if root is NULL */
/*  Create 2 Queues for store pointer of nodeType */
        queueType<nodeType*> currentlevel,nextlevel;
/*  add pointer root into currentLevel */
        currentlevel.addQueue(root);
/*  Condition currentlevel queue is not yet Empty then
    add pointer llink and rlink of each pointer in front queue
    to other nextlevel queue and delete currentlevel queue front
 */
         int x = level; /* is technique to print Level number */
         while(!currentlevel.isEmptyQueue()){
/*  create pointer currNode of nodeType points to
    pointer of currentlevel queue front
    then delete queuefront of currentlevel queue
 */
             nodeType *currNode = currentlevel.front();
             currentlevel.deleteQueue();
             if(currNode){
                 if(x++ == level) // technique to show only once for level
                     cout<<"L["<<level<<"]\t";
                 cout<<currNode->info<<"\t";
                 nextlevel.addQueue(currNode->llink);
                 nextlevel.addQueue(currNode->rlink);
             }
/* end if */
             if(currentlevel.isEmptyQueue()){
                 cout << endl;
                 level++;
                 x = level;
/*  Swap object queue currentlevel and nextlevel */
                 queueType<nodeType*> temp;
                 temp = currentlevel;
                 currentlevel = nextlevel;
                 nextlevel = temp;
             }
/* end if */
         }
/* end while */
    }
/* end if */
}

Monday, February 20, 2012

Unix Tools - Socket Clien/Server [Simsimi]

Running
.Actually you have to use Unix machine to run these program .
.I recommand to you a Makefile for easy using :
     vi Makefile:
.To compile program just $make
.To run server just $make runs ( do this first )
.To run client just $make runc ( do this after run server in other windows )
.To clean executed program just $make clean
.See this example for this program :
client process simsimi



client.c
/* C library need to include */
#include <string.h>  #include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>

#define MYBUFERSIZE 256


 
server.c
/* C library need to include */
#include <string.h>  
#include <sys/types.h>
#include <sys/socket.h> 
#include <stdio.h> 
#include <sys/un.h> 
#include <unistd.h> 
#include <stdlib.h>
#include <stdbool.h>

#define MYBUFERSIZE 256