CS计算机代考程序代写 Haskell algorithm 10/10/21, 1:29 AM Project 2

10/10/21, 1:29 AM Project 2

https://canvas.vt.edu/courses/135925/assignments/1312504 1/4

Project 2
10/20/2021

100 Possible Points

Details

Project 2
This project is a continuation from our first project in that the input will be the output from our last
project. I will be giving your program the files it needs so we won’t be calling your project 1 first, but
we could.

Project Information
For this project we are going to be implementing a postfix calculator using the Haskell language. The
input will be processed line by line and the result calculated, assuming there are no errors. If there are
errors that will also be reported. Any error messages that are in the input will simply be passed along
as is.

Basic Algorithm
The basic algorithm is as follows:

For each line in the input
For each token in the line
If the token is an operator
pop two numbers from the stack
perform the operations on the operators
push the result back on the stack
else the token is a number
push the number on the stack

The final number on the stack is the answer for the equation

A few things can go wrong when computing

1. When go go to pop the stack, there are less than 2 elements on the stack
2. When dividing, you could try division by 0.
3. When you are done processing the line there are more than 1 element on the stack.

Each of the above are errors and will have error messages as indicated below.

How to perform the operations

10/10/21, 1:29 AM Project 2

https://canvas.vt.edu/courses/135925/assignments/1312504 2/4

When popping from the stack, the second operand comes off first and the second operand comes off
second. Then the operation is performed as such

operand1 operation operand2

This is not a big deal for the operations of multiplication and addition but it is extremely important for
division and subtraction. So please pay attention to order of popping and application.

Errors
There are 3 errors that this project will detect and report.

1. If you attempt to pop the stack and you cannot pop 2 operands, then you will output “Too few
operands”.

2. If at the end of processing the line, there is more than 1 number on the stack, you will output “Too
few operations”.

3. If you pop the stack for division and operand2 is 0, you will output “Attempted division by 0”.

Starter Code
Functional languages tend to try and avoid input and output since I/O tends to produce side effects
and functional languages try to avoid side effects. You can do I/O in Haskell and main returns an IO
() to allow for this to work. The code below is meant to get your started with the input and output and
with the command line parameters. Since Haskell relies on indentation to know nesting, I encourage
you to download the code from the link under the code rather than copy and paste. If you do copy and
paste make sure you are using spaces not tabs and that you have the same indentation as the
example:

import System.Environment
import System.IO

main :: IO ()
main = do
args <- getArgs let files = get_names args let input = fst files let output = snd files putStrLn input putStrLn output in_handle <- openFile input ReadMode out_handle <- openFile output WriteMode mainloop in_handle out_handle hClose in_handle hClose out_handle mainloop :: Handle -> Handle -> IO ()
mainloop in_handle out_handle =
do in_eof <- hIsEOF in_handle if in_eof then return () else do line <- hGetLine in_handle putStrLn $ "line " ++ line let line_words = words line 10/10/21, 1:29 AM Project 2 https://canvas.vt.edu/courses/135925/assignments/1312504 3/4 putStr "\tWords: " print line_words mainloop in_handle out_handle get_names :: [String] -> (String, String)
get_names (arg1:arg2:_) =
let in_file = arg1
out_file = arg2
in (in_file, out_file)

Input
The input for this project is simply the output from the last project.

Too few closing parenthesis
84 99 60 / 74 * + 35 80 29 * –
Too few closing parenthesis
71 31 70 / 73 * – –
94 13 / 89 5 * 58 * + 64 20 * 22 * + +
70 40 * 83 52 65 / + 59 + *
36 10 + 39 9 / 1 * 79 27 / – +
27 33 / 4 40 / – 1 56 + 62 – 92 – –
Too few closing parenthesis
Too few closing parenthesis
29 46 –
44 68 15 / + 20 –
40 20 98 / – 63 40 / 96 * +
68 57 74 * 42 / +
50 12 * 53 – 44 – 60 38 – 84 41 45 / – – –
83 47 *
Too few opening parenthesis
33 33 * 40 – –
Too few opening parenthesis
71 50 11 74 6 * / – 28 71 / + 14 + /
95 42 * 21 – 58 56 / 38 * 23 * * –
58 56 / 43 – 81 – 58 / –
Too few opening parenthesis
Too few opening parenthesis
98 69 – 85 –
96 56

Output
Sample output will be coming. For now, if the input is an error message simply output that message. If
it is not an error, then output the equation on one line and the result on the next.

Restrictions
1. You may not import any modules.

a. Modules are not needed for this assignment. Use what is in the prelude.
2. You program must get the input and output file names as command line arguments.

a. The first argument will be the input file.
b. The second argument will be the output file.

3. You must write a function for each of the operations.

10/10/21, 1:29 AM Project 2

https://canvas.vt.edu/courses/135925/assignments/1312504 4/4

4. You may not use any non-functional approach, i.e. forcing an imperative style on this solution
5. You must use guards instead of if-else statements.
6. Include the honor code in your work.
7. This is an individual assignment.

Grading
I will use my grading tool to grade code as it is turned in to Canvas. You will have the opportunity for
resubmission before the due date for no penalty.

Late work will be assessed a 10% per day penalty.

After the project is done, TAs will assess your work to see if you met the requirements from above and
points may be deducted if you did not meet them.