Docsity

Programming - Homework Assignment 4 | CSC 246, Assignments of Computer Science

Toggle sidebar
bg1
CSC201, Section 002,Fall 2000: HW#4 file:///C:/Reeves/My%20Stuff/website-new/Classes/csc246-...
1 of 4 11/8/2003 7:35 PM
CSC201, SECTION 002, Fall 2000:
Homework Assignment #4 Program
(Note: the homework problem descriptions are in a separate part of this assignment.)
DUE DATE
November 17 for the program, in class.
INSTRUCTIONS FOR PROGRAMMING
Print off your program and hand in, folded lengthwise, stapled, with your name, course
number, assignment number, and date on the outside.
Submit your program electronically also.
In your program, include the statement ".include ../pmacros.inc" (note the ".." to find
the file in the parent directory). Do NOT include the sasmacros.inc file; you will not
use these macros.
Keep all your program files strictly in your EOS locker space, with no access given to
other users. Do not put your files on the local machine, such as in temp space!
Be sure your programs include a header comment indicating the assignment, date, and
your name. Include a reasonable number of comments in your code as well.
Thanks for your help.
PROGRAMMING
Write a program called "hw4.asm" (note: don't use "hw4a.asm" this time, since there is
only one program). This program reads a sequence of lines from the standard input.
Each line is processed, and an output produced, before reading the next line. The
program terminates when end of input is reached, or a line containing nothing (just a
carriage return) is entered.
Each input line consists of an arithmetic expression. A valid expression contains some
combinations of numbers and operators (with no spaces). A number is a 1-digit
1.
pf3
pf4

Partial preview of the text

Download Programming - Homework Assignment 4 | CSC 246 and more Assignments Computer Science in PDF only on Docsity!

CSC201, SECTION 002, Fall 2000:

Homework Assignment #4 Program

(Note: the homework problem descriptions are in a separate part of this assignment.)

DUE DATE

November 17 for the program, in class.

INSTRUCTIONS FOR PROGRAMMING

Print off your program and hand in, folded lengthwise, stapled, with your name, course number, assignment number, and date on the outside. Submit your program electronically also. In your program, include the statement ".include ../pmacros.inc" (note the ".." to find the file in the parent directory). Do NOT include the sasmacros.inc file; you will not use these macros. Keep all your program files strictly in your EOS locker space, with no access given to other users. Do not put your files on the local machine, such as in temp space! Be sure your programs include a header comment indicating the assignment, date, and your name. Include a reasonable number of comments in your code as well.

Thanks for your help.

PROGRAMMING

Write a program called "hw4.asm" (note: don't use "hw4a.asm" this time, since there is only one program). This program reads a sequence of lines from the standard input. Each line is processed, and an output produced, before reading the next line. The program terminates when end of input is reached, or a line containing nothing (just a carriage return) is entered.

Each input line consists of an arithmetic expression. A valid expression contains some combinations of numbers and operators (with no spaces). A number is a 1-digit

unsigned decimal value (in ASCII) (ex.: 0, 8, 4, ...). An operator is '', '/' (integer divide), '-', or '+'. As usual, '' and '/' have higher precedence than '+' and '-'.

As each line is read, it is evaluated by your program and the value of the expression is printed as the output, in decimal. The only error condition you need to check for is a divide by zero, which produces an error message. For outputting an integer value, you are allowed to use the "put_i" macro of SASM (but no other macros from SASM), or you may write your own code.

As an example, for the input

12+3- 3+12/2+ 1-2-0* 8/0-2+ 999* 3

the output should be

Value = 0 Value = 12 Value = - Error: divide by zero Value = 6561 Value = 3

To evaluate these expressions, you will need to do stack-based evaluation. The main routine is as follows:

define a number_stack, and an operator_stack; char = getchar(); while (char != -1) { initialize number_stack to empty; initialize operator_stack to empty; while (char != 0ah) { if (char is a digit) { convert char to a number and push on the number_stack; } else { while ((operator_stack^ not^ empty^ )^ &&^ (precedence(char)^ <= precede evaluate top of operator_stack; } push(operator_stack, char); } char = getchar(); } while (operator_stack not empty) { evaluate top of operator_stack; } print the top of the number_stack; char = getchar(); }

Problems will be graded according to the following interpretation:

All right = full credit halfway to mostly right = half credit Not much or none right = no credit