Monday, April 19, 2010

Review for OOP344

//An example from class OOP344_Mar_18
#include
int IsOne(unsigned int A, int n){
return A & (1<< (n-1));
}
void prnBits(unsigned int A){
int i;
for(i=sizeof(unsigned int)*8; i>0;i--){
printf("%d", !!IsOne(A, i));
}
putchar('\n');
}
int main(void){
char A = 0x9c;
prnBits(A);
return 0;
}
~

~

output:

11111111111111111111111110011100

//An alternative way of doing this:

#include

void prnBits(unsigned int A){
int i;
for(i=1;i<=sizeof(unsigned int)*8; i++){
printf("%d", (A>>(32-i))&1);
}
putchar('\n');
}
int main(void){
char A = 0x9c;
prnBits(A);
return 0;

output:
11111111111111111111111110011100


// An example for 'Exception Handling' from website

#include
#include
using namespace std;
class NegativeNumber
{
public:
NegativeNumber( );
NegativeNumber(string
take_me_to_your_catch_block);
string get_message( );
private:
string message;
};

class DivideByZero   
{ };

int main( )
{
int jem_hadar, klingons;
double portion;
try
{
cout << "Enter number of Jem Hadar warriors:\n";
cin >> jem_hadar;
if (jem_hadar < 0)
throw NegativeNumber("Jem Hadar");
cout << "How many Klingon warriors do you have?\n";
cin >> klingons;
if (klingons < 0)
throw NegativeNumber("Klingons");
if (klingons != 0)
portion = jem_hadar/double(klingons);
else
throw DivideByZero( );     
cout << "Each Klingon must fight " << portion << " Jem Hadar.\n";
}
catch(NegativeNumber e)
{
cout << "Cannot have a negative number of " << e.get_message( ) <<  endl;
}
catch(DivideByZero)
{
cout << "Today is a good day to die.\n";
}

cout << "End of program.\n";
return 0;
}

catch(...)     //can catch any type of exception
{
cout << "Unexplained exception.\n";
}


Sunday, April 4, 2010

Practice for Queue( Int data)

//int_queue.h
//practice for queue with integer data type
//Apr 4

#ifndef _MO_INT_QUEUE
#define _MO_INT_QUEUE
#include
using namespace std;
class Queue;
class Node{
int _data;
Node* next;
public:
friend class Queue;
Node(int data, Node* next);
};

class Queue{
Node* head;
Node* tail;
public:
Queue();
void Add(int data);
int& remove();
bool IsEmpty();
~Queue();
};


#endif


//int_queue.cpp
// Apr 4

#include "int_queue.h"
#include
using namespace std;
Node::Node(int data, Node *next){
this->_data=data;
this->next=next;
}

Queue::Queue(){
head=tail=(Node*)0;//queue is empty at beginning
}

void Queue::Add(int data){
if(!head){// add the first node
head=new Node(data,tail);

}
else{
if(!tail){//add the 2nd node
tail=new Node(data,(Node*)0);
head->next=tail;
}else{//in case there are more than two nodes
Node* temp=new Node(data,(Node*)0);
tail->next=temp;
tail=temp;
}
}

}


int& Queue::remove(){
int ret=head->_data;
Node* ToDel;
if(head==tail)//in case there is only one node left
head=tail=(Node*)0;
else{
ToDel=head;
head=head->next;

delete ToDel;}
return ret;
}
bool Queue::IsEmpty(){
return(!head)&&(!tail);
}

Queue::~Queue(){
while(!IsEmpty())
remove();
}


int_queue_main.cpp

#include
using namespace std;
#include "int_queue.h"

int main(){
int i=0;
Queue Q;
for(i=0;i<16;i+=3)
Q.Add(i++);


while(!Q.IsEmpty())
cout< getchar();
return 0;

}

Saturday, April 3, 2010

Practice for Stack

//stack.h
//Apr 3

#ifndef _MO_STACK_
#define _MO_STACK_
#include
using namespace std;

class Item{
char _code[40];
int _quantity;
public:
Item(char* code="", int quantity=0);
friend ostream& operator<<(ostream& os, Item it);
};
class Node{
Item _data;
Node* next;
public:
Node(Item data, Node* Next);
friend class Stack;
};
class Stack{
Node* top;
public:
Stack();
~Stack();
void push(Item data);
Item pop();
bool IsEmpty();


};
#endif

//stack.cpp
//Apr 3

#include "stack.h"
#include
#include
using namespace std;

Node::Node(Item data, Node* Next){
this->_data=data;
this->next=Next;
}
Item::Item(char* code, int quantity){
strcpy(this->_code,code);
this->_quantity=quantity;
}
ostream& operator<<(ostream& os, Item it){
os< return os;

}

Stack::Stack(){
top=(Node*)0;
}
bool Stack:: IsEmpty(){
return (!top);
}
void Stack::push(Item data){
top=new Node(data, top);
}
Item Stack::pop(){
Item item=top->_data;
Node* toDel=top;
top=top-> next;
delete toDel;
return item;

}
Stack::~Stack(){
while (!IsEmpty())
pop();

}

//stackMain.cpp

#include
using namespace std;
#include "stack.h"

int main(){
Stack s;
s.push(Item("muffin",2));
s.push(Item("puttin",10));
while(!(s.IsEmpty())){
cout< }
getchar();
return 0;
}

Note: ' friend class Stack; ' is needed, otherwise when a stack object was created, the private data members , that is, '_data' and 'next' cannot be accessed.
Q: why in Stack::pop(), the return is 'Item' instead of 'Item&', I may figure it out later on.

Monday, February 22, 2010

Recursive

The directory tree is recursive. The directories contains sub-directories. the sub-directory itself is a directory. So one directory contains another directory, so on so forth...

So, in other words, a parent contains a child who is also a parent of a child, so on so forth.....

Tuesday, February 9, 2010

This Monday, we are learning " pointer to function".

......

int main(){
int (*fptr[3])(int, int){add, prnHA, sub};
int a = 10;
int b = 20;
int c;
int i;
for(i=0;i<3;i++){
c = (*fptr[i])(a, b);
printf("%d\n", c);
}
return 0;
}

......

To my understanding, (*fptr[3]) is an array of variables which are pointers to functions, as long as the functions have the same signature as defined.
ex. ( int (*fptr)(int, int)) --
refers to any function which receives two integer as parameters and then return one integer.

In the example listed above, with one loop, we can call three different functions, add, prnHA and sub.
It is very cool!

Thursday, January 28, 2010

Today I am glad I understand the meaning of "ifndef .....". It is to prevent the compiling conflict .
#ifndef _344_BIOF_H
#define _344_BIOF_H
..........
......
#enddefine

At the first time,when we execute the code the _344_BIOF_H has not been defined, the code underneath will be executed. When it comes to the second time, since _344_BIOF_H has been defined, the following code will not be executed. In this way, we can prevent the code to be compiled for twice.

Actually, I had the problem when I did my oop244 assignment. I did not know exactly the reason at that time. Now I realize we need to put #ifndefine, #define, #enddefine to every blank header file.

Wednesday, January 13, 2010

testing

This is a testing for my first blog.