Thursday, October 27, 2011

Conditional operator (if operator)

conditional operator (also called "if operator"), if in the form of

(condition ? statements if condition is true : statements if condition is false)

It's equal to:
if(condition){
statements if condition is true;
}else{
statements if condition is false;
}

#include <iostream>

using namespace std;

int main()
{
cout << "conditional operator (if operator)" << endl << endl;

cout << (true ? "It's TRUE" : "It's FALSE") << endl;
cout << (false ? "It's TRUE" : "It's FALSE") << endl;

cout << "3 > 5: " << ((3 > 5) ? "Yes" : "No") << endl;
cout << "'Z' > 'X': " << (('Z' > 'X') ? "Yes" : "No") << endl;

return 0;
}


Conditional operator (if operator)



Wednesday, October 26, 2011

The C++/CX Episode

GoingNative 3: The C++/CX Episode with Marian Luparu




Tuesday, October 25, 2011

Generate random number with srand()

In C/C++, the rand() function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. This function return a pseudo-random integer, not true.

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand have been made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

In this exercise, current date and time, time(0), is used as seek for srand(). Such that the generated pseudo-random numbers will be a more random.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
cout << "Random number generated by rand()" << endl;
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;

cout << "Random number generated with srand()" << endl;
srand(static_cast<unsigned int>(time(0)));
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;

return 0;
}


Generate random number with srand()

Friday, October 21, 2011

Comparison of Java and C++

The differences between the C++ and Java programming languages can be traced to their heritage, as they have different design goals.
  • C++ was designed for systems and applications programming, extending the C programming language. To this procedural programming language designed for efficient execution, C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library which includes generic containers and algorithms.
  • Java was created initially as an interpreter for printing systems but grew to support network computing. Sun Microsystems used it for the basis of their "HotJava" thin client system. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically-typed object-oriented language that uses a syntax similar to C++, but is not compatible with it. It was designed from scratch with the goal of being easy to use and accessible to a wider audience.


Wikipedia have a article, Comparison of Java and C++, it's list the difference in more details.

It's a sample chapter of the book Java in a Nutshell, describe How Java Differs from C.

Tuesday, October 11, 2011

Exercise of STL stack

#include <iostream>
#include <stack>

using namespace std;

int main()
{
cout << "STL stack exercise" << endl << endl;

stack<int> MyStack;
cout << "init without anything" << endl;
cout <<"MyStack.size(): " << MyStack.size() << endl << endl;

cout << "push something in" << endl;
MyStack.push(1);
MyStack.push(2);
MyStack.push(3);
MyStack.push(4);
cout <<"MyStack.size(): " << MyStack.size() << endl << endl;

cout << "pop all out" << endl;
while (!MyStack.empty())
{
cout << MyStack.top() << endl;
MyStack.pop();
}

cout << endl << endl;

return 0;
}


Exercise of STL stack


Saturday, October 8, 2011

Doubly Linked List

#include <iostream>

using namespace std;

class Node {
private:
Node();
Node(string s, Node* p, Node* n);
string body;
Node* prev;
Node* next;
friend class DoublyLinkedList;
};

Node::Node(){
};

Node::Node(string s, Node* p, Node* n){
body = s;
prev = p;
next = n;
};

class DoublyLinkedList{
public:
DoublyLinkedList();
~DoublyLinkedList();
void AddInFront(string s);
void RemoveInFront();
void AddInBack(string s);
void RemoveInBack();
void ShowAll();
private:
Node* head;
Node* trail;
};

DoublyLinkedList::DoublyLinkedList(){
head = new Node();
trail = new Node();
head->next = trail;
trail->prev = head;
};

DoublyLinkedList::~DoublyLinkedList(){
while (head->next != trail){
RemoveInFront();
}
delete head;
delete trail;
};

void DoublyLinkedList::AddInFront(string s){
Node* nxFrontNode = head->next;
Node* n = new Node(s, head, head->next);
head->next = n;
nxFrontNode->prev = n;
};

void DoublyLinkedList::RemoveInFront(){
if(head->next != trail){
Node* nxFrontNode = head->next;
Node* nnxFrontNode = nxFrontNode->next;
head->next = nnxFrontNode;
nnxFrontNode->prev = head;
delete nxFrontNode;
}
};

void DoublyLinkedList::AddInBack(string s){
Node* nxBackNode = trail->prev;
Node* n = new Node(s, trail->prev, trail);
trail->prev = n;
nxBackNode->next = n;
};

void DoublyLinkedList::RemoveInBack(){
if(trail->prev != head){
Node* nxBackNode = trail->prev;
Node* nnxBackNode = nxBackNode->prev;
trail->prev = nnxBackNode;
nnxBackNode->next = trail;
delete nxBackNode;
}
};

void DoublyLinkedList::ShowAll(){
Node* n = head->next;
while(n != trail){
cout << n -> body << endl;
n = n -> next;
}
}

int main()
{
cout << "- Doubly Linked List exercise -" << endl;

cout << "Create a new DoublyLinkedList: " << endl;
DoublyLinkedList myDoublyLinkedList;
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove nothing in a empty Doubly Linked List: " << endl;
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Insert something in Front: " << endl;
myDoublyLinkedList.AddInFront("ABC");
myDoublyLinkedList.AddInFront("DEFGH");
myDoublyLinkedList.AddInFront("I");
myDoublyLinkedList.AddInFront("JK");
myDoublyLinkedList.AddInFront("LMN");
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove something in Front: " << endl;
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Insert something in Back: " << endl;
myDoublyLinkedList.AddInBack("RST");
myDoublyLinkedList.AddInBack("UVW");
myDoublyLinkedList.AddInBack("XYZ");
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove something in Back: " << endl;
myDoublyLinkedList.RemoveInBack();
myDoublyLinkedList.RemoveInBack();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "end!" << endl;
return 0;
}


Doubly Linked List


Monday, October 3, 2011

C++11 Features in Visual C++ 11

There's a new C++ Standard and a new version of Visual C++, and it's time to reveal what features from the former we're implementing in the latter!

Here is a article from MSDN Blogs talking about the new C++11 Features in Visual C++ 11.