Monthly Archives: March 2012

C++ || Stack Based Postfix Evaluation (Single Digit)

This page consists of another homework assignment which was presented in a C++ Data Structures course. While the previously discussed program dealt with converting Infix expressions to Postfix, this program will demonstrate exactly how to evaluate them.

NOTE: Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

REQUIRED KNOWLEDGE FOR THIS PROGRAM

What Is Postfix?
How To Convert Infix To Postfix Equations
Stack Data Structure
Cin.getline
How To Evaluate Postfix Expressions
The Order Of Operations
#include "ClassStackType.h"

The title of this page is called – “Stack Based Postfix Evaluation (Single Digit).” Why “single digit?” The program demonstrated on this page has the ability to evaluate a postfix equation, but it only has the ability to evaluate single digit values. What do I mean by that? Consider the infix equation: 5+2. When that expression is converted to postfix, it will come out to be: 52+, and the answer will be 7 (5+2=7). But what if we have an equation like 12+2? When that expression is converted to postfix, it will come out to be: 122+. The postfix conversion is correct, but when you try to evaluate the expression, we do not know if the math operation should be 12+2 or 1+22, it can be read either way.

Question: So why is this program being displayed if it only works for single digits?
Answer: Because it demonstrates the process of evaluating postfix equations very well.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

Before we get into things, here is a helpful algorithm for evaluating a postfix expression in pseudo code:

Once you understand the process of converting from infix to postfix, adding the ability to evaluate multiple digits within this program should be doable.

======= POSTFIX EVALUATION =======

This program uses a custom template.h class. To obtain the code for that class, click here.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

====== RUN 1 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 1 2 + 5 6 + /
The postfix expression = 1 2 + 5 6 + /

Calculations:
1+2 = 3
5+6 = 11
3/11 = 0.272727
Final answer = 0.272727

====== RUN 2 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 35*76^+
The postfix expression = 35*76^+

Calculations:
3*5 = 15
7^6 = 117649
15+117649 = 117664
Final answer = 117664

====== RUN 3 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 45^4*32+$2-/12%24*/*
The postfix expression = 45^4*32+$2-/12%24*/*

Calculations:
4^5 = 1024
1024*4 = 4096
3+2 = 5
√5 = 2.23607
2.23607-2 = 0.236068
4096/0.236068 = 17350.9
1%2 = 1
2*4 = 8
1/8 = 0.125
17350.9*0.125 = 2168.87
Final answer = 2168.87

C++ || Stack Based Infix To Postfix Conversion (Single Digit)

This page consists of another homework assignment which was presented in a C++ Data Structures course. No matter which institution you attend, it seems every instructor assigns a program similar to this at one time or another.

Want to evaluate a postfix expression? Click here.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

REQUIRED KNOWLEDGE FOR THIS PROGRAM

What Is Infix?
What Is Postfix?
Stack Data Structure
Cin.getline
How To Convert To Postfix
The Order Of Operations
#include "ClassStackType.h"

The program demonstrated on this page has the ability to convert a normal infix equation to postfix equation, so for example, if the user enters the infix equation of (1*2)+3, the program will display the postfix result of 12*3+.

Before we get into things, here is a helpful algorithm for converting from infix to postfix in pseudo code:

======= INFIX TO POSTFIX CONVERSION =======

This program uses a custom template.h class. To obtain the code for that class, click here.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Want to evaluate a postfix expression? Click here for sample code.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

====== RUN 1 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: ((a+b)+c)/(d^e)
The Infix expression = ((a+b)+c)/(d^e)
The Postfix expression = ab+c+de^/

====== RUN 2 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: (3*5)+(7^6)
The Infix expression = (3*5)+(7^6)
The Postfix expression = 35*76^+

====== RUN 3 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)
The Infix expression = (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)
The Postfix expression = 45^14*232+$2-/12%24*/*

C++ || Snippet – Array Based Custom Template Stack Sample Code

This page will consist of sample code for a custom array based template stack.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Classes
Template Classes - What Are They?
Stacks
LIFO - What Is It?
#include < stack>

This template class is a custom duplication of the Standard Template Library (STL) stack class. Whether you like building your own data structures, you simply do not like to use any inbuilt functions, opting to build everything yourself, or your homework requires you make your own data structure, this sample code is really useful. I feel its beneficial building functions such as this, that way you better understand the behind the scene processes.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

===== DEMONSTRATION HOW TO USE =====

Use of the above template class is the same as its STL counterpart. Here is a sample program demonstrating its use.

Once compiled, you should get this as your output


charStack has 20 items in it
and contains the text: My Programming Notes
backwards setoN gnimmargorP yM

intStack has 9 items in it.
The sum of the numbers in the stack is: 45

floatStack has 10 items in it.
The sum of the numbers in the stack is: 52.834

C++ || Snippet – How To Reverse An Integer Using Modulus & While Loop

This page will consist of a simple program which demonstrates how to reverse an integer (not an int array) using modulus and a while loop.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Functions
Modulus
While Loops


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compiled three separate times to display different output)

==== RUN #1 ====

Enter a number: 2012
2012 reversed is: 2102

==== RUN #2 ====

Enter a number: 1987
1987 reversed is: 7891

==== RUN #3 ====

Enter a number: 241
241 reversed is: 142

Java || Using If Statements, Char & String Variables

As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.

===== SINGLE CHAR =====

This example will demonstrate a simple program using char, which checks to see if you inputted the correctly predefined letter


Notice in line 11 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 18 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 18 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a') as your if statement, and notice the difference.

The resulting code should give this as output

Please try to guess the letter I am thinking of: A
You have Guessed correctly!

===== CHECK IF LETTER IS UPPER CASE =====

This example is similar to the previous one, and will check if a letter is uppercase


Notice in line 18, an If statement was used, which checked to see if the user inputted data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)

IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')

THEN it is an uppercase letter

The resulting code should give this as output

Please enter an UPPERCASE letter: p
p is not an uppercase letter

===== CHECK IF LETTER IS A VOWEL =====

This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.


This program should be very straight forward, and its basically checking to see if the user inputted data is the letter A, E, I, O, U or Y.

The resulting code should give the following output

Please enter a vowel: o
Correct, o is a vowel!

===== HELLO WORLD v2 =====

This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.


The resulting code should give following output

Please enter a sentence: Hello World!
You entered: Hello World!

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac YOUR_CLASS_NAME.java

*** To run the compiled program, simply type this command:

java YOUR_CLASS_NAME

Java || Simple Math Using Int & Float

This page will display the use of int and float data types. Note: you can (obviously) add as many int’s together as you wish, not just two.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.


Notice in lines 8-10, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 23 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

I obtained data from the user by using the Scanner Class.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Add.java

*** To run the compiled program, simply type this command:

java Add

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32

==== SUBTRACTING TWO NUMBERS ====

Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 23, replace the addition symbol with a subtraction sign, and you should have something like this


Note: In the above example, “cin.nextFloat()” was used on line 20 in place of “nextInt.” The declaration “nextFloat” can be used in place of “nextInt” in case you want to obtain floating point data from the user.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Subtract.java

*** To run the compiled program, simply type this command:

java Subtract

The above code should give you the following output

Please enter the first number: 8
Please enter the second number: 23.99999
The difference between 8 and 23.99999 is: -15.99999

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 23, and replacing the designated math operator with the star symbol “*.”

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Multiply.java

*** To run the compiled program, simply type this command:

java Multiply

The above code should give you the following output

Please enter the first number: 7.999999
Please enter the second number: 24
The product of 7.999999 and 24 is: 191.99997


==== DIVIDING TWO NUMBERS TOGETHER ====

In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where the use of the float data type is mandatory (Note: You can also use the “double” data type here aswell).


===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Divide.java

*** To run the compiled program, simply type this command:

java Divide

The above code should give the following output

Please enter the first number: 7.99999
Please enter the second number: 23.99999
The quotient of 7.99999 and 23.99999 is: 0.33333305

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 23, from division, to modulus.


===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Modulus.java

*** To run the compiled program, simply type this command:

java Modulus

The above code should give the following output

Please enter the first number: 23.99999
Please enter the second number: 8
The remainder of 23.99999 and 8 is: 7.9999905

Java || Hello World!

This page will consist of creating the typical “hello world!” application. First, you will need to create a new project in whichever IDE you are using. I am using a simple text editor and the terminal, but any compiler will do.

When you have everything loaded up, you will start your code off by entering the headers like so into your HelloWorld.java file.


The most basic form of a class definition is shown above. The keyword “class” begins the class definition for the class named “HelloWorld.”

Next, you are going to add the main function. This will generally look something like this:


Every program in Java must contain a main method declaration as shown above. The declaration “public” and “static” can be written in any order (public static or static public), but the custom is to use “public static.”

We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen


The above code uses the “System” class from the core library to print the “Hello World!” message to standard output.

So when you add everything together, your full code will look something like this

===== HOW TO COMPILE CODE USING THE TERMINAL =====

Once you have loaded the terminal, if you have your java source file saved in any directory other than the home folder, you will need to change directories to that specific folder where your source file resides. Many typically save their source files to the desktop.

*** If your files are located on the desktop, in the terminal, type this command:

@ubuntu:~$ cd Desktop

*** Next we will compile the java source file, and this can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

@ubuntu:~/Desktop$ javac HelloWorld.java

*** To run the compiled program, simply type this command:

@ubuntu:~/Desktop$ java HelloWorld

And there you have it! After you compile the above code, you should get this as your output

Hello World!

Java || Which Compiler To Use?

A common question one may wonder is, which compiler should I use? There are a few choices, and your choice should probably be based on what you intend to write with it.

If you intend to program on Windows, a common choice is to download one of the many (free) Integrated Development Environment’s (IDE) which are currently available on the internet. The IDE’s I recommend are:

* NetBeans
* Eclipse

Netbeans and Eclipse are the equivalent of Visual Studios and CodeBlocks for C++. (NOTE: Netbeans and Eclipse is also available for download on Linux).

You will also need to have Java installed on your computer in order to develop Java applications. If you are using Windows, then visit the Oracle website. and download and install either JDK7 or JDK7 with Netbeans. Don’t bother with JRE, as it merely runs Java programs, but does not compile them.

==== LINUX USERS ====

To install Java, open the terminal window and enter the command:

sudo apt-get install openjdk-7-jdk

If you want to add Netbeans to your computer, then use the same terminal window and enter the command below:

sudo apt-get install netbeans

Alternatively, if you want to add Eclipse to your computer, then use the same terminal window and enter the command below:

sudo apt-get install eclipse-platform

Personally, I use a simple text editor (gedit) and the terminal window in Ubuntu to program in Java, and all the source code which is presented on this site was created using that development environment.

C++ || 8 Different Ways To Reverse A String/Character Array In C++

This page will consist of 8 different ways to reverse a character array, and a string literal (std::string).

REQUIRED KNOWLEDGE FOR THE PROGRAMS

Character Arrays
String Literals
Cin.getline - Use For Char Arrays
Getline - Use For std::string
Length
Strlen
Strcpy
While Loops
For Loops
Recursion - What is it?
#include < algorithm>
#include < stack>

The methods on this page will be broken up into sections. This page will list:


(3) methods which reverses string literals (std::string)
(4) methods which reverses character arrays
(1) method which utilizes the stack to "reverse" a character sequence

Some methods listed on this page demonstrates the use of reversing a character sequence without the use of strlen/length.

======= REVERSE AN STD::STRING =======


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM

======= REVERSE A CHARACTER ARRAY =======

The following will demonstrate (4) methods which reverses a character array.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


======= REVERSE A CHARACTER SEQUENCE USING A STACK =======

The following will demonstrate (1) method which reverses a character sequence using the STL stack.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM