Posts

Mapping Words to Line Numbers in Text Files in STL / C++

Image
Following on from the previous post , this example shows an example of how to use an STL multimap to track the line number(s) associated with each word in a text file. This program essentially reads in text line-by-line, while stripping out all occurrences of punctuation and other non-alphanumeric charcters. Each pair is inserted into the multimap container using the insert function. As with the previous posting, which deals with counting the frequency of words in a file, this example also uses the sample Hamlet.txt file . Code listing as follows: [code language="cpp"] #include <iostream> #include <sstream> #include <fstream> #include <map> using namespace std; int main() { const string path = "/home/andy/NetBeansProjects/Hamlet.txt"; //Linux //const string path = "C:\\Dump\\Hamlet.txt"; ifstream input( path.c_str() ); if ( !input ) { cout << "Error opening file." << endl; ...

Counting the Number of Words in a Text File in STL / C++

Image
This post aims to illustrate the power of using STL's associative arrays as a word counter. It reads the entire contents of the text file, word-by-word, and keeps a running total of the number of occurences of each word. All using just a few lines of code, discounting the bits that output the results.

How to Create an Installer with Microsoft Visual Studio

Image
Introduction Visual Studio 2010 contains a package that enables you to create Windows installer files for your applications. Follow these simple steps to build your own setup package for the Visual Studio application you are working on.

Polymorphism in Java

An example: [code language="cpp"] import java.util.*; public class JavaPolymorph { public void Print() { System.out.println( "JavaPolymorph"); } public static void main(String[] args) { JavaPolymorph j1 = new JavaPolymorph(); JavaPolymorph j2 = new SubJavaPolymorph(); j1.Print(); j2.Print(); } } public class SubJavaPolymorph extends JavaPolymorph { public void Print() { System.out.println( "SubJavaPolymorph"); } } [/code] Giving the output: JavaPolymorph SubJavaPolymorph

Java Threads: The Basics

Method 1: Write a class that implements the Runnable interface (i) Put the thread code in the run() method. (ii) Create a thread object by passing a Runnable object as an argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method. Like this: (new Thread(new MyThread())).start(); Simple code sample as follows: [code language="java"] package javathread1; class MyThread implements Runnable { public void run() { //Display info about thread System.out.println(Thread.currentThread()); } } public class JavaThread1 { public static void main(String[] args) { // Create the thread Thread thread1 = new Thread(new MyThread(), "thread 1"); // Start the thread thread1.start(); } } [/code] Method 2: Declare the class to be a Subclass of the Thread class (i) Override the run() method from the Thread class to define the ...

Mathematical Expression Parsers in Java and C++

Image
Basic Expression Parsing Click here for advanced expression parsing When writing your own calculator it is necessary to build a converter that can transform an input mathematical expression such as ( 1 + 8 ) – ( ( 3 * 4 ) / 2 ) , into a format that is more suited for evaluation by computers. When evaluating expressions such as the one above (known as “ infix notation "), that which appears simple and intuitive to us humans, is usually not so straightforward to implement in a programming language. The shunting-yard algorithm is a method for parsing mathematical expressions written in infix notation to Reverse Polish Notation (RPN) . The RPN notation is different to infix notation in that every operator (+, -, * etc) comes after the operands (numbers) and there are no parentheses (brackets). So ( 3 * 4 ) for example becomes 3 4 * . When given an input string in Reverse Polish Notation, it is then possible to employ a simple algorithm based around the use o...

Reading Text Files into String Arrays in Java

Programming Tip: Now you can load your essential programming tools such as emulators and IDE`s into the cloud with high performance citrix vdi from CloudDesktopOnline and access it remotely at your convenience on your preferred device(PC/Mac/android/iOS). If you prefer a gpu dedicated server, Try dedicated gpu hosting from Apps4Rent with 24*7*365 days top-notch tech-support and migration assistance. Some example Java code to read the contents of text file into a string array, line-by-line. Here is the Java class which is used to output the string array after the file location has been passed to it: [code language="java"] // ReadFile.java package javareadtextfile; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ReadFile { public String[] readLines(String filename) throws IOException { FileReader fileReader = n...