This is a micro project made by Aaditya Thakkar.
The main function is the entry point of the program and coordinates the overall flow of the program.
The algorithm works as follows:
- Takes an input string from the user, and stores it in a variable.
- Calls the
tokenize
function, passing the input string as an argument. - The
tokenize
function tokenizes the input string into an array of tokens, where each token is either an operator or an operand. - The main function calls the
eval1
function passing the number of tokens as an argument. - The
eval1
function evaluates the '^' and '!' operators by iterating through thetokens
array from left to right and performs the power and factorial operation respectively on the operands that come before and after the operator. It also evaluates the brackets with the help of a temp array. - The main function calls the
eval2
function passing the number of tokens as an argument. - The
eval2
function evaluates the '*','/' and '%' operators by iterating through thetokens
array from left to right and performs the multiplication, division and modulus operation respectively on the operands that come before and after the operator. - The main function calls the
eval3
function passing the number of tokens as an argument. - The
eval3
function evaluates the '+' and '-' operators by iterating through thetokens
array from left to right and performs the addition and subtraction operation respectively on the operands that come before and after the operator. - Finally, the main function prints the final result of the evaluation.
The algorithm used in the tokenize
function is a simple tokenizing algorithm that separates the input string into an array of tokens, where each token is either an operator or an operand.
The algorithm works as follows:
- Initialize two variables,
i
andnumBracks
, to 0.i
is used as an index for the tokens array, andnumBracks
is used to keep track of the number of brackets in the input string. - Use a
while
loop to iterate through the input string character by character. - Within the
while
loop, use theisdigit
function to check if the current character is a digit. If it is, use thestrtol
function to convert the string of digits to a long integer and store the result in thetokens
array. - If the current character is an operator (
+
,-
,*
,/
,^
,!
,%
,(
, or)
), store the operator in thetokens
array. Also, increment thenumBracks
variable if the operator is(
. - If the current character is a space, ignore it and continue to the next character.
- If the current character is any other type of character, print an error message and exit the program.
- Continue the
while
loop until the end of the input string is reached. - After the
while
loop, check if thenumBracks
variable is greater than 1. If it is, print an error message and exit the program. - Finally, return the number of tokens in the
tokens
array.
This function returns the number of tokens present in the expression
The algorithm used in the eval1, eval2, and eval3 functions is a simple left-to-right evaluation algorithm with operator precedence.
The algorithm works as follows:
- eval1() function:
- Iterates through the
tokens
array from left to right. - Looks for '^' and '!' operators.
- When it finds an operator, it performs the power and factorial operation respectively on the operands that come before and after the operator.
- The
tokens
array is updated by removing the operator and the right operand, and storing the result in the left operand. - Additionally, this function also evaluates the brackets present in the expression first and then stores the result in the same array.
- eval2() function:
- Iterates through the
tokens
array from left to right. - Looks for '*','/' and '%' operators.
- When it finds an operator, it performs the multiplication, division and modulus operation respectively on the operands that come before and after the operator.
- The
tokens
array is updated by removing the operator and the right operand, and storing the result in the left operand.
- eval3() function:
- Iterates through the
tokens
array from left to right. - Looks for '+' and '-' operators.
- When it finds an operator, it performs the addition and subtraction operation respectively on the operands that come before and after the operator.
- The
tokens
array is updated by removing the operator and the right operand, and storing the result in the left operand.
The functions then print the updated
tokens
array and return the number of tokens. All the functions return the number of tokens left at the end of the process.