Part 1: Prime Factorization
Write a program that uses a stack to print the prime factors of a positive integer in descending order.
Prime factorization is a way of expressing a number as a product of its prime factors. A prime number is a number that has exactly two factors, 1 and the number itself. For example, if we take the number 30. We know that 30 = 5 × 6, but 6 is not a prime number. The number 6 can further be factorized as 2 × 3, where 2 and 3 are prime numbers. Therefore, the prime factorization of 30 = 2 × 3 × 5, where all the factors are prime numbers.
The prime factorization of 72 = 3 x 3 x 2 x 2 x 2 = 3^2 x 2^3
Let the user enter the number and program prints out the prime factors in descending order. Repeated factors must be printed. For example, if the input is 72, then the output should be 3^2*2^3.
You will need to use a stack to solve this problem.
Approach: The idea is to use the Stack data structure to store all the prime factors of N and in the end, print all the values in the Stack. Follow the steps below to solve the problem:
- Initialize a stack, say st.
- Run a loop while N != 1. From i = 2, for each value of i, run a loop until N % i == 0 and push i into the stack st and update N to N/i.
- Finally, print all the values from top to bottom of stack st.
Finally: Write a test program that generate 1000 random numbers and produces the fatorizations of each, then tess the results. Sent the output to a file, which you include in the project.
Part 2: Infix to Postfix Conversion
Step 1
Write a program that converts an infix expression into an equivalent postfix expression.
The rules to convert an infix expression into an equivalent postfix expression
are as follows:
Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows:
a. Initialize pfx to an empty expression and also initialize the stack.
b. Get the next symbol, sym, from infx.
b.1. If sym is an operand, append sym to pfx.
b.2. If sym is (, push sym into the stack.
b.3. If sym is ), pop and append all the symbols from the stack until the most recent left parenthesis.
Pop and discard the left parenthesis.
b.4. If sym is an operator:
b.4.1. Pop and append all the operators from the stack to pfx that
are above the most recent left parenthesis and have precedence greater than or equal to sym.
b.4.2. Push sym onto the stack.
c. After processing infx, some operators might be left in the stack. Pop and append to pfx everything from the stack.
In this program, you will consider the following (binary) arithmetic operators: +, -, *, and /. You may assume that the expressions you will process are error free.
Design a class that stores the infix and postfix strings. The class must include
the following operations:
• getInfix—Stores the infix expression
• showInfix—Outputs the infix expression
• showPostfix—Outputs the postfix expression
Some other operations that you might need are the following:
• convertToPostfix—Converts the infix expression into a postfix
expression. The resulting postfix expression is stored in pfx.
• precedence—Determines the precedence between two operators. If the
first operator is of higher or equal precedence than the second operator, it
returns the value true; otherwise, it returns the value false.
You should use the C++ stack STL
Step 2
Test your program on the following five expressions:
A + B – C;
(A + B ) * C;
(A + B) * (C – D);
A + ((B + C) * (E – F) – G) / (H – I);
A + B * (C + D ) – E / F * G + H;
For each expression, your answer must be in the following form:Infix Expression: A + B - C;
Postfix Expression: A B + C -
Step 3
Use AI (ChatGPT) to write a function that generates random expressions. The function should be given the min and max number of variables, and the min and max depth of paraenthses.
Create a loop that tests your system by creating 5,000 test cases and then converting them all the postfix.
Output the results to a test file and include in project code.
Final program notes.
Read the expressions from a file and write the results to the screen. Your input file should contain all the expressions given in the problem but you may add more. For simplicity, you may assume that the expressions only contain uppercase letters, white spaces, paranthesis, and the arithmetic operations +, -, * and / .
Grading
| Requirements | Grading Comments | Points | Score |
|---|---|---|---|
| Good Code Design Part 1 | 10 | ||
| All operations work as specified | 15 | ||
| Complete Test Code Part 1 | 15 | ||
| Good Code Design Part 2 | 10 | ||
| All operations work as specified | 15 | ||
| Complete Test code Part2 | 15 | ||
| Test output form both projects | 20 | ||
| Total | 100 |
Groups
Lab5-Pair-01: Trang Nguyen, Leo Xie
Lab5-Pair-02: Joey (Jiayin) Liu, Calvin Deka
Lab5-Pair-03: Khanh Mai, Ella Rigoli
Lab5-Pair-04: Eden Cohen, Peter Dunson
Lab5-Pair-05: Lucas Waite, Davelle Ampofotwumasi
Lab5-Pair-06: Westley (Kelly) Kailus, Sariyah Sintayehu
Lab5-Pair-07: Yaw Oppongkrampah, Moe Belghith
Lab5-Pair-08: Shadia Amado-Aguad, Hoan Nguyen
Lab5-Pair-09: Luke Galik, Cloris (Zongyu) Liu
Lab5-Pair-10: Nora Archer, Djordje Dragojlovic
Lab5-Solo-11: Adam Khan
Lab5-Solo-12: Kuba Kopczuk
