Monthly Archives: January 2014

C++ || Multi Digit, Decimal & Negative Number Infix To Postfix Conversion & Evaluation


 

Click Here For Updated Version Of Program


The following is sample code which demonstrates the implementation of a multi digit, decimal, and negative number infix to postfix converter and evaluator using a Finite State Machine

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Convert Infix To Postfix
How To Evaluate A Postfix Expression
What Is A Finite State Machine?

Using a Finite State Machine, the program demonstrated on this page has the ability to convert and evaluate a single digit, multi digit, decimal number, and/or negative number infix equation. So for example, if the the infix equation of (19.87 * -2) was entered into the program, the converted postfix expression of 19.87 ~2* would display to the screen, as well as the final evaluated answer of -39.74.

NOTE: In this program, negative numbers are represented by the “~” symbol on the postfix string. This is used to differentiate between a negative number and a subtraction symbol.

This program has the following flow of control:

• Get an infix expression from the user
• Convert the infix expression to postfix
• Use a Finite State Machine to isolate all of the math operators, multi digit, decimal, negative and single digit numbers that are found in the postfix expression
• Evaluate the postfix expression using the tokens found from the above step
• Display the evaluated answer to the screen

The above steps are implemented below.


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.

The following is sample output.

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: 12/3*9

The Infix expression = 12/3*9
The Postfix expression = 12 3 /9*

Calculations:
12/3 = 4
4*9 = 36

Final answer = 36

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: -150.89996 - 87.56643

The Infix expression = -150.89996 - 87.56643
The Postfix expression = ~150.89996 87.56643-

Calculations:
-150.9-87.5664 = -238.466

Final answer = -238.466

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

The Infix expression = ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))
The Postfix expression = ~4 5^ s1.4* 23 2+ $~2.8-/ 1 2% c7.28 .1987* 23t^/*

Calculations:
-4^5 = -1024
sin(-1024) = 0.158533
0.158533*1.4 = 0.221947
23+2 = 25
√25 = 5
5--2.8 = 7.8
0.221947/7.8 = 0.0284547
1%2 = 1
cos(1) = 0.540302
7.28*0.1987 = 1.44654
tan(23) = 1.58815
1.44654^1.58815 = 1.79733
0.540302/1.79733 = 0.300614
0.0284547*0.300614 = 0.00855389

Final answer = 0.00855389

====== RUN 4 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1987 + 1991) * -1

The Infix expression = (1987 + 1991) * -1
The Postfix expression = 1987 1991+ ~1*

Calculations:
1987+1991 = 3978
3978*-1 = -3978

Final answer = -3978

Python || Find The Average Using A List – Omit Highest And Lowest Scores

This page will consist of a program which calculates the average of a specific amount of numbers using a list.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Lists
For Loops
Arithmetic Operators
Basic Math - How To Find The Average

The following program is fairly simple, and was used to introduce the list concept. This program prompts the user to enter the total amount of numbers they wish to find the average for, then displays the answer to the screen. Using a sort, this program also has the ability to find the average of a list of numbers, omitting the highest and lowest valued items.


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.

The following is sample output.

How many items do you want to place into the list?: 5

Enter item #1: 7
Enter item #2: 7
Enter item #3: 4
Enter item #4: 8
Enter item #5: 7

The current items inside the list are:
Item #1: 7
Item #2: 7
Item #3: 4
Item #4: 8
Item #5: 7

The average of the 5 numbers is 6.60

The average adjusted score omitting the highest and lowest result is 7.00

C++ || Simple Tokenizer Lexer Using A Finite State Machine

The following is sample code which demonstrates the implementation of a simple Lexer using a table driven Finite State Machine.

In its simplest form, a Finite State Machine is a procedure that can: (1) store the status of an event, (2) can operate on new (or existing) input to change the status of an event, and (3) can cause an action to take place (based on the input change) for the particular event being examined.

State machines usually adhere to the following basic principles:

• Has an initial state or record of something stored someplace
• Has a set of possible input events
• Has a set of new states that may result from the input
• Has a set of possible actions or output events that result from a new state

Finite state machines usually have a limited or finite number of possible states. The Finite State Machine implemented on this page has 6 transition states. Those transition states has the ability to isolate tokens from a given input string, based on characteristics defined in the transition state table.

The machine was implemented with compiler construction in mind, resulting in the grammar for the 6 transition states to resemble that of many popular programming languages.

The transition table defined in this program can be further fine tuned by allowing the states to accept or reject different parameters, and/or by adding more transition states. Tuning the parameters in the state transition table allows you to change the behavior of the state machine without having the need to alter the Finite State Machine Algorithm (the “Lexer” function) much at all.

NOTE: The example on this page uses a sample input file. Click here to download the file.

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.

NOTE: The example on this page uses a sample input file. Click here to download the file.

The following is sample output.

C++ || Snippet – How To Send Text Over A Network Using A TCP Connection

The following is sample code which demonstrates the use of the “socket“, “connect“, “bind“, “read“, and “write” function calls for interprocess communication over a network on Unix based systems.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client. This program demonstrates communication between two programs using a TCP connection.

Click here to examine the UDP version.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===

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.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 07:51:53 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 07:51:53 PM" from the server

C++ || Snippet – How To Send Text Over A Network Using A UDP Connection

The following is sample code which demonstrates the use of the “socket“, “bind“, “recvfrom“, and “sendto” function calls for interprocess communication over a network on Unix based systems.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client. This program demonstrates communication between two programs using a UDP connection.

Click here to examine the TCP version.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===


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.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 11:47:12 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 11:47:12 PM" from the server

Java || Snippet – How To Send Text Over A Network Using Socket

The following is sample code which demonstrates the use of the “socket” function call for interprocess communication over a network.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===


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.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

(SRCIP=/127.0.0.1, SRCPORT=46608) (DESTIP=/127.0.0.1, DESTPORT=1234)

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed Jan 08 18:07:15 PST 2014" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed Jan 08 18:07:15 PST 2014" from the server

Python || Snippet – How To Send Text Over A Network Using Socket, Send, & Recv

The following is sample code which demonstrates the use of the “socket“, “connect“, “send“, and “recv” function calls for interprocess communication over a network.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===

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.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 04:02:13 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 04:02:13 PM" from the server