Archive

Archive for July, 2008

Simple Application, ListBox and Item

Simple ListBox exercise…..  create two list box , one button and one label as following picture.

Select button and set the properties text  as  “Add Item” , and ID as “addItem”
For the First list box click the upper right icon and “Edit item”

null

And then , show the ListItem Collection Editor

To add the item –> Click Add button and Input the Text properties  anything you want.


After finish Adding item Click ok.    You will get the list of all items in the first Listbox
To add the code, Double click on  “Add button”  and put the following code.

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void adItem_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1) // to check, is there any item in the first listbox is selected.
{
Label1.Text = “Move -> ” + ListBox1.SelectedItem.Value; // show, the moved item
ListBox2.Items.Add(ListBox1.SelectedItem.Text); // add the selected item to the second listbox.
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex); // Remove the selected item from the first listbox
}
else
{
Label1.Text = “Error , Please item you want to move “;
}
}
}

Categories: DOT NET

The game of life, the classic problem and unbeliverable.

What is the game of life ????

The game of life ( or simply life ) is not a game in convenional sense. There are no players , and no winning or losing. One the “piece” are placed in the starting position, the rules determine everything that happens later.  Nevertheless , life is full of surprise! In most cases, it is impossible to look at starting position ( or pattern ) and see what will happen in the future. The only way to find out is to follow the rules of the game…..

There is a good reference site for the rule of this game go to this link : http://www.math.com/students/wonders/life/life.html or if you want to see some online example go : http://www.bitstorm.org/gameoflife/ and this is my code for the game of life in C++ version ( and very simple one )

#include

using namespace std;

int main(int argc, char *argv[])
{
const int row = 34 ;
const int col = 34 ;
// this table for displaying the result
int myTable[row+1][col+1] = {{0}};
// this table for caculating …
int cTable[row+1][col+1]=
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} } ;
int count = 1; ;
int Pcount ;
while(count < 1000) {//----------------------Start of while ----------------------------------- cout << "Round number : " << count << endl ; for ( int i = 0; i < row ; i++) { for (int j = 0; j < col; j++) { Pcount = 0; //------------------------------------------------------------------ // Determine amount of neihbore cell.... // ----------------------------------------------------------------- if (( i == 0 )&& ( j == 0 )) // Upper left corner { if (cTable[i+1][j] == 1) Pcount++; if (cTable[i+1][j+1] == 1) Pcount++; if (cTable[i][j+1] == 1) Pcount++; } else if (( i == 0 ) && ( j > 0 )) // Upper border …..
{
if (cTable[i][j-1] == 1 )
Pcount++;
if (cTable[i+1][j-1] == 1)
Pcount++;
if (cTable[i+1][j] == 1)
Pcount++;
if (cTable[i+1][j+1] == 1)
Pcount++ ;
if (cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == 0 ) && ( j == col )) // Upper right cornet
{
if (cTable[i+1][j] == 1)
Pcount++;
if (cTable[i+1][j-1] == 1)
Pcount++;
if (cTable[i][j-1] == 1)
Pcount++;
}
else if ( ( j == 0 ) && ( i > 0 ) ) // left border …………
{
if (cTable[i-1][j] == 1)
Pcount++;
if (cTable[i-1][j+1] == 1)
Pcount++;
if (cTable[i][j+1] == 1)
Pcount++;
if (cTable[i+1][j+1] ==1)
Pcount++;
if (cTable[i+1][j] == 1)
Pcount++;
}
else if ( ( j == 0 ) && ( i == row )) // lower left
{
if(cTable[i-1][j] == 1 )
Pcount++;
if(cTable[i-1][j+1] == 1)
Pcount++;
if(cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == row ) && ( j > 0 ) ) // lower border …….
{
if(cTable[i][j-1] == 1)
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] == 1)
Pcount++;
if(cTable[i-1][j+1] == 1)
Pcount++;
if(cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == row ) && ( j == col )) // lower right
{
if(cTable[i][j-1] == 1 )
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] == 1)
Pcount++;
}
else if ( ( i > 0 ) && ( j == col )) // right border …..
{
if(cTable[i+1][j] == 1)
Pcount++;
if(cTable[i+1][j-1] == 1)
Pcount++;
if(cTable[i][j-1] == 1)
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] ==1)
Pcount++;
}
else if ( (( i > 0 ) && ( i < row )) && ( ( j > 0 )&&( j < col )) ) // inside the box ...... { if(cTable[i-1][j-1]==1) Pcount++; if(cTable[i-1][j] == 1) Pcount++; if(cTable[i-1][j+1] == 1) Pcount++; if(cTable[i][j-1] == 1) Pcount++; if(cTable[i][j+1] == 1) Pcount++; if(cTable[i+1][j-1] == 1) Pcount++; if(cTable[i+1][j] == 1) Pcount++; if(cTable[i+1][j+1] == 1) Pcount++; } else { Pcount = 0; } // ----------------------------------------------------------------- // Determine and plot to myTable... // This part apply to all i,j // ----------------------------------------------------------------- if ( cTable[i][j] == 0 ) // dead cell { if ( Pcount == 3 ) { myTable[i][j] = 1; } } else // life cell { if ( ( Pcount == 2 ) || ( Pcount == 3) ) { myTable[i][j] = 1; } else { myTable[i][j] = 0 ; } } } } // ------------------------------------------------------------------------- // -------- Copy all from myTable to cTable , for the next calculation ----- // ------------------------------------------------------------------------- for (int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { cTable[i][j]= myTable[i][j]; } } // -------------------------------------------------------------------------- // ------- Display the result ----------------------------------------------- // -------------------------------------------------------------------------- for ( int i = 0; i < row; i++ ) { for(int j= 0; j < col; j++) { //cout << myTable[i][j] << " " ; if (myTable[i][j] == 1 ) { cout << "*" << "" ; } else { cout << "-" << ""; } } //cout << endl; cout << endl; } //-------------------------------------------------------------------------- // Clear The display Table.... for ( int i = 0; i < row; i++ ) { for(int j= 0; j < col; j++) { myTable[i][j] = 0; } } // delay form some amount of time.. Time_Delay(); // clear the console... system("cls"); // Add one to count ... count++; }// end of while system("PAUSE"); return EXIT_SUCCESS; } //---------------------------------------------------------------------------------------------- // ---- Function Library ----------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- void Time_Delay(void) { //----------------------------------------------------------------------------------------- // delay for some amount of times... don't know how many , but just some amount of time.. //----------------------------------------------------------------------------------------- for ( int i = 0; i < 10000 ; i++) { for ( int j = 0; j < 20000 ; j++) { ; } } } [/sourcecode]

Categories: C++

The publication, book and tape classes , more advance….

Add another base class sales that holds and array of three floats it can record the sales of a particular publication for the last three months. Include getdata() function to get three sales amounts from the user , and a putdata() function to display the sales figures. Alter the book and tape classes so that they are derived from both publication and sales. An object of class book or tape should input or output sales along with its other data.

#include
using namespace std;

class sale
{
private:
float salerec[3];
public:
void getdata()
{
cout << " Enter the sales for the last three months :" << endl; for (int i = 0; i < 3 ; i++) { cout << " Enter sales for month : " << i+1 << " > “;
cin >> salerec[i];
}
}
void putdata()
{
cout << " The sale of the last three month " << endl; for(int i=0; i < 3; i++) { cout << " Month no : " << i+1 << " > ” << salerec[i] << endl; } } }; class publication { private: char title[50] ; float price; public: void getdata() { cout << " Enter publication title : " ; cin >> title;
cout << " Enter publication price : " ; cin >> price;
}

void putdata()
{
cout << " Publication title : " << title << endl; cout << " Publication price : " << price << endl; } }; // end of class publication class book : public publication, public sale { private: int pageCount ; public: void getdata() { publication::getdata(); cout << " Enter page count : " ; cin >> pageCount;
sale::getdata();
}
void putdata()
{
publication::putdata();
cout << " Page count : " << pageCount ; cout << endl; sale::putdata(); } }; class tape : public publication , public sale { private: int playingtime; public: void getdata() { publication::getdata(); cout << " Enter page count : "; cin >> playingtime;
sale::getdata();
}
void putdata()
{
publication::putdata();
cout << " Playing time : " << playingtime; cout << endl; sale::putdata(); } }; int main(int argc, char** argv) { book mybook; //tape mytape; // get the book data mybook.getdata(); //mybook.sale::getdata(); mybook.putdata(); //mybook.sale::putdata(); // get the tape data //mytape.getdata(); //mytape.putdata(); return (EXIT_SUCCESS); } [/sourcecode]

Categories: C++

Inheritance , Classroom Exercise, NIIT student

Create a class called publication that store the title ( a string ) and price ( type float ) of a publication. From this class, derive two class: book which adds a page count ( type int ); and tape , which adds a playing time (type int ) in minutes. Each of these classes should have a getdata() function to get its data from the user, and a putdata() to display its data.

//———————————————————

#include
using namespace std;
class publication
{
private:
char title[50] ;
float price;
public:
void getdata()
{
cout << " Enter publication title : " ; cin >> title;
cout << " Enter publication price : " ; cin >> price;
}

void putdata()
{
cout << " Publication title : " << title << endl; cout << " Publication price : " << price << endl; } }; // end of class publication class book : public publication { private: int pageCount ; public: void getdata() { publication::getdata(); cout << " Enter page count : " ; cin >> pageCount;
}
void putdata()
{
publication::putdata();
cout << " Page count : " << pageCount ; cout << endl; } }; class tape : public publication { private: int playingtime; public: void getdata() { publication::getdata(); cout << " Enter page count : "; cin >> playingtime;
}
void putdata()
{
publication::putdata();
cout << " Playing time : " << playingtime; cout << endl; } }; int main(int argc, char** argv) { book mybook; tape mytape; // get the book data mybook.getdata(); mybook.putdata(); // get the tape data mytape.getdata(); mytape.putdata(); return (EXIT_SUCCESS); } [/sourcecode] .. That what i've done with the exercise ...

Categories: C++

Filled dimon or UnFilled dimon, OOPs Technique..

#include <iostream.h>

using namespace std;

class dimon
{
private:
    int size;
public:
    dimon()
    {
        size = 0;
    } 
    void setSize(int isize)
    {
        size = isize;
    }
    int getsize()
    {
        return size;
    }
    void pWhiteSpace(int num ) // draw the white space
    {
        for(int i=0; i<num ; i++)
        {
            cout << " " ;
        }        
    }
    void pStar(int num) // draw the star "*"
    {
        for(int i=0; i<num; i++)
        {
            cout << "*" ;
        }
    }
    
};

class FillDimon : public dimon
{
public:
    void show()
    {
        int size = getsize() ;
        int middle = size / 2;
        int j = middle ;
        for(int i=0; i < size ; i++)
        {
            if(i<=middle)
            {
                pWhiteSpace(j--);
                pStar(i);
                pStar(i+1);
            }
            else
            {
                pWhiteSpace(++j+1);
                pStar(size - i);
                pStar(size - i -1 );
            }
            cout << endl;
        }
    }
};

class UnFillDimon : public dimon
{
private:
    void pSW(int num)
    {
        int whitenum;
        if(num==1)
        {
            cout << "*";
        }
        else if(num >1 )
        {
            whitenum = num - 2;
            cout << "*";
            pWhiteSpace(whitenum);
            pWhiteSpace(whitenum + 1);
            cout << "*";
        }
    }
public:
    void show()
    {
        int size = getsize();
        int middle = size/2;
        int j = middle;
        for(int i = 0; i<size; i++)
        {
            if(i <= middle)
            {
                pWhiteSpace(j--);
                pSW(i+1);
            }
            else
            {
                pWhiteSpace(++j+1);
                pSW(size-i);            
            }
            cout << endl;
        }
    }
};

int main(int argc, char** argv) {
    int ch;
    FillDimon mydimon;
    UnFillDimon myudimon;
    cout << " Enter number 1 or 2 to display Filled Dimon or UnFilled Dimon :" << endl;
    cout << " >> " ;
    cin >> ch ;
    if ( ch == 1 )
    {
        mydimon.setSize(25);
        mydimon.show();
    }
    else
    {
        myudimon.setSize(25);
        myudimon.show();
    }
    
    return (EXIT_SUCCESS);
}

This is another version of Filled / Unfilled Dimon , using OOP.
and try to use inheritance , base class , derive class. . . . have fun

Categories: C++