Tuesday, May 31, 2011

C++ Lambdas.

lambda functions enable us to not only write functions in the middle of others, but to more importantly pass expressions as arguments to a function.

The documentation for lambdas on the internet is sparse, so bear with me if there is a mistake here or there.

Some simple examples of their use:



#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;



bool IsEqualWithoutCase( string first, string second ){
auto upper = [] ( string::value_type &ch ) { ch = toupper(ch) }; //<-- lambda
for_each( first.begin(), first.end(), upper );
for_each( second.begin(), second.end(), upper );
return first == second;
}


int main()
{
string input, comp = "Hello World";
getline(cin,input);
cout << (IsEqualWithoutCase(input,comp) ? "That is correct." : "Wrong answer.") << endl;
}

Saturday, May 28, 2011

Project: "EasyScreen2011"

So since I feel like this feature should be built into Windows anyway, I've decided to make a small application that allows me to select a region of the screen and take a screenshot of it.  For this project I have chosen C# as my language, and it uses a windows low-level keyboard/mouse hook library located here.

I am very pleased with C# libraries for now, they are much less of a hassle to use than many C++ libraries (which are often gigantic).  The library is used because of the amount of pInvoking required to use the Windows API functions to add hooks.

For this project I have chosen to use the library in a DLL.  It also features error notification via a balloon tip from the system tray where it resides.



The current build isn't without it's flaws, but they are for the most part, at least, obvious.




Who knows, you may find it more useful than you ever thought you would ;)
I have uploaded my program for download from here.  
Currently to use the application you hold Ctrl + Shift and select an area of the screen with your mouse.

Please let me know what you think !

Wednesday, May 25, 2011

CS ADTs and fun fun fun!

Of utmost importance to a C/C++ programmer are (you guessed it) pointers!  Pointers enable programmers to create interesting (and useful) data structures that are likely more commonplace in performance applications.

A list of data structures is available on Wikipedia, http://en.wikipedia.org/wiki/List_of_data_structures  for now this post will is concerned with the abstract data types.

To name a few, there exists the stack, queue, deque, list, vector, set, map, and tree.

Perhaps the simplest, can be the "list", aka the "Linked List."

Typically the linked list will feature a node type commonly represented by a structure, in which each structure contains a pointer to the next (and in the case of a doubly linked list, the previous).

http://en.wikipedia.org/wiki/Linked_list

For reference a simple node type can be in C++ as follows:


[code]
template<class NodeData>
struct Node
{
NodeData data;
Node *next;
Node() : next(nullptr) {}
};
[/code]

When creating the linked list, the first node is created with it's "next" member pointing to nothing.  Appending a node to the end will cause the first node's next pointer to point to the second node.

With this structure, one may only have a pointer to the first node and be able to access the entire list's content by "traversing" the list.

Diagram of a linked list:
[Node] -> [Node] -> [Node] -> NULL

to traverse the list is pretty simple,
Node *current = firstNode;
while( current->next != NULL )
{
current = current->next;
}
the loop ends with current pointing to the last node in the list, commonly one will see traversing the list in this manner in the destructor or a clear() member function of the class.

For now, the implementation of many of these Computer Science data structures is left to you, there is a whole "list" of them.

Thursday, May 19, 2011

C# screen capture!

Among the many good things about C# is it's amazing library, the .NET framework.  It may seem almost alien to native programmers, and I'm thoroughly convinced trained monkeys can write C# programs--but here is a small sample of the level of abstraction provided by C# and the rich .NET framework classes.

GetScreen.cs
[file]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;


namespace ScreenCap
{
 
    public class GetScreen
    {
        private Graphics gfx;
        private Bitmap bmp;
        private Point zero;
        private Size resolution;


        public GetScreen()
        {
            zero = new Point(0, 0);
            resolution = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height,
                PixelFormat.Format32bppArgb);
            gfx = Graphics.FromImage(bmp);
        }
        ~GetScreen()
        {
            bmp.Dispose();
            gfx.Dispose();
        }
        public Bitmap GetImage()
        {
            gfx.CopyFromScreen(zero, zero, resolution);
            return new Bitmap(bmp);
        }
    }
}


[/file]

Form1.cs
[file]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ScreenCap
{
    public partial class Form1 : Form
    {
        public GetScreen capture;
        public Form1()
        {
            InitializeComponent();
            capture = new GetScreen();
            //Assigning a snapshot to the window content.
            Graphics gfx = this.CreateGraphics();
            this.Show();//Make sure to sure the window first.
            gfx.DrawImage(capture.GetImage(), this.ClientRectangle);
        }
    }
}
[/file]

If built properly you should see a snapshot of your screen in the window, and a glimpse into the life of the army of trained monkeys writing software for Microsoft Windows.

Wednesday, May 18, 2011

STL = a pure computation library for C++ (amazing)

The C++ STL or Standard Template Library can easily be divided into two sections, algorithms and containers.
The version you will find accompanying most modern C++ compilers is based off of the SGI version of the STL which can be used alternatively.

Many programmers can find some utility in using the STL as it can make computation as easy as pie.  For example, every permutation of "abcdefghijklmnopqrstuvwxyz"




#include <iostream>
#include <string>
#include <algorithm>



int main()
{
   using namespace std;
   string input = "abcdefghijklmnopqrstuvwxyz";
   do
   {
       cout << input << "\n";
   }while( std::next_permutation(input.begin(), input.end()) );
}

Tuesday, May 17, 2011

C++ Loop construction for file input.

The following loop setup will yield the best results for file input and avoids many common mistakes that have to do with the eof() member returning true only after the eof() has been reached.




int main()
{
using namespace std;
ifstream inFile("somefile.txt");
//check for errors, etc.

string input;
while( inFile >> input ) //< -- loop
{
//Processing.
}

inFile.close();
}

Monday, May 16, 2011

DreamSpark provides free training to students.

Microsoft Dreamspark provides free training to currently enrolled students!

Among Dreamspark's free software for students, such as Visual Studio 2010 Professional, there exists now a Pluralsight-training.net trial membership for ~80 days of free training videos.

www.dreamspark.com