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.