Despite all the glamour that has been given to the metric system in the past few years, there are still some areas of endeavor in which the English system shows no sign of being replaced, at least in America. As an engineer working in the building industry, I am involved in one of those. A few of us are lucky enough to still have an old-fashioned calculator filled with cams and levers, but I have spent untold hours with pencil and paper adding feet and inches and eighths. So I have been looking for a way to automate that is simple and cost-effective, and I have found it in the ZX81.
The first requirement for such a program is that it respond quickly enough that the user does not become frustrated waiting for the answer to flash up on the screen. I also wanted the program to be simple and comprehensible, because I want to be able to add to it the ability to do further processing on the dimensions that have been keyed in. It turns out that with a little attention paid to program layout, Basic does the job.
The program assumes the following conventions: dimensions are entered in feet, inches and eighths. Fractions involving sixteenths and beyond are not used. A dimension 12″ or larger is expressed in terms of feet and inchies. For example, 16″ is expressed as 1’4″. However, this last can be changed, as noted later in the article.
The way of entering dimensions is best shown by the examples in Figure 1. Feet are separated from inches by a period. Whole inches and then the fraction are keyed without a hyphen or slash; 5-1/2″ becomes simple .512. If there are no whole inches, only the fraction need be entered. Thus .14 is understood to be 1/4″ (not 14″). If the dimension is to be subtracted, it is preceded by M. (A minus sign could have been used, but minus is a shifted character and therefore less convenient.)
To start the program simply hit RUN; the two double quotation marks with inverse L cursor between will appear to receive the first dimension. As each dimension is entered it will be interpreted and printed on the screen. To see the total hit ENTER (a null string). The total is really a subtotal; it is not cleared, and additional dimensions may be added or subtracted. The total is printed with an asterisk and may be negative. Enter C to clear the total and the screen. To stop the program delete the left double quotation mark and substitute STOP. If Basic stops the program due to faulty input, it may be restarted without losing the total by entering GOTO 140.
Figure 1.
ENTER UNDERSTOOD
TO
MEAN
3.4 3'4"
4.12 4'0-1/2"
6.10 6'10"
1.738 1'7-3/8"
2.1114 2'11-1/4"
.18 0'0-1/8"
154 154'-0"
M12.6 -12'6"
M.958 -0'9-5/8"
Since Basic locates a given line by searching from the top, the subroutines that interpret the dimensions have been placed first so as to minimize the time spent in the search. (RETURN is done directly without a search.) There are also no REM statements to slow things down. The program actually begins at line 110—Line 5 could be eliminated with a very small increase in speed if one remembers to always start with RUN 110. Personally, I have trouble with this, so I have included line 5.
Because of line 310, lines 10, 20, 30 and 40 must have those numbers and no other. Line 150 is the only input instruction in the program. If it is desired to process dimensions in a new program segment, the test for the new instruction should be placed around lines 170 and 180.
Lines 240 through 260 locate the period, if any. If there is no period, then the dimension is all feet and we are ready to print. If a period is found, the feet are placed in X and the feet and period are chopped off the string at line 300. This leaves from 1 to 4 digits remaining.
If there is one digit, it is whole inches 0-9. If there are three digits, they are whole inches 0-9 plus a fraction of two digits. If there are four digits, they are whole inches 10-11 plus a fraction of two digits. If there are two digits, they may be whole inches 10-11 or a fraction with no whole inches. This separation is made at line 20. 10 or 11 will lead to line 10 whereas 18, 14, 38, 12, 58, 34, or 78 will lead to line 21. If it is desired to be able to input numbers of inches larger than 11, then things can be rearranged so that two digits always lead to line 10. In this case, a dimension of 1/2″, for example, will have to be entered .012 which requires an extra keystroke.
RETURNing to line 320, we are now ready to PRINT. Since the ZX81 character set does not include an apostrophe, the graphic on the 1 key has been used instead. I find that this reads very well. The extra PRINT statement at line 350 is required to end the line should there be no fraction; accordingly there is a semicolon at the end of line 80 also. The dimension is added to the total at line 380, and we are ready for the next command.
Line 400 sets W equal to the total and calls the PRINT routine which begins at line 440. This has been made a subroutine so that future program segments can use it to PRINT dimensions in the standard format. To use the subroutine, set W equal to the desired dimension in decimal inches and GOSUB 440. The asterisk could be turned on and off in a string variable if it were not appropriate for all calls to 440.
PRINTing of the fraction associated with the total is done by pointing to the appropriate characters in B$. The pointer is generated in line 510. The .1 is added to 32*Z to overcome roundoff error that occurs in the decimal to binary conversions that are going on within Basic. If the .1 is left out, part of the time N will be very slightly less than the correct value, and the wrong characters will be PRINTed in line 520.
The contents of the program variables are shown in Figure 2. After each addition or total, W contains the last PRINTed dimension in decimal inches, and is available for use elsewhere.
Figure 2.
VARIABLES
A$ INPUT STRING
B$ IMAGES OF FRACTIONS
X FEET
Y WHOLE INCHES; DECIMAL INCHES
N NUMERATOR; 4*NUMBER OF EIGHTHS
D DENOMINATOR
Z DIMENSION IN INCHES;
FRACTION EXPRESSED AS DECIMAL
W LAST DIMENSION DISPLAYED
(DECIMAL INCHES)
S SIGN; 0 = +; 1 = -
T TOTAL (DECIMAL INCHES)
I COUNTER
Figure 3. ([g1] = the graphic on the 1 key.)
1 REM ENG
5 GOTO 110
10 LET Y=VAL A$
11 RETURN
20 IF VAL A$<12 THEN GOTO 10
21 LET N=VAL A$(1)
22 LET D=VAL A$(2)
23 RETURN
30 LET Y=VAL A$(1)
31 LET N=VAL A$(2)
32 LET D=VAL A$(3)
33 RETURN
40 LET Y=VAL A$( TO 2)
41 LET N=VAL A$(3)
42 LET D=VAL A$(4)
43 RETURN
50 LET S=1
60 LET A$=A$(2 TO )
70 RETURN
80 PRINT "-";N;"/";D;
90 LET Y=Y+N/D
100 RETURN
110 LET B$="-1/8-1/4-3/8-1/2-5/8-3/4-7/8"
120 LET T=0
130 CLS
140 SLOW
150 INPUT A$
160 FAST
170 IF A$="" THEN GOTO 400
180 IF A$="C" THEN GOTO 120
190 LET S=0
200 IF A$(1)="M" THEN GOSUB 50
210 LET X=0
220 LET Y=0
230 LET N=0
240 FOR I=1 TO LEN A$
250 IF A$(I)="." THEN GOTO 290
260 NEXT I
270 LET X=VAL A$
280 GOTO 320
290 IF I>1 THEN LET X=VAL A$( TO I-1)
300 LET A$=A$(I+1 TO )
310 GOSUB 10*LEN A$
320 IF S THEN PRINT "-";
330 PRINT X;"[g1]";Y;
340 IF N THEN GOSUB 80
350 PRINT
360 LET W=12*X+Y
370 IF S THEN LET W=-W
380 LET T=T+W
390 GOTO 140
400 LET W=T
420 GOSUB 440
430 GOTO 140
440 LET Z=ABS W
450 LET X=INT (Z/12)
460 LET Y=Z-12*X
470 LET Z=Y-INT Y
480 IF W<0 THEN PRINT "-";
490 PRINT X;"[g1]";INT Y;
500 IF NOT Z THEN GOTO 540
510 LET N=INT (32*Z+.1)
520 PRINT B$(N-3 TO N);" *"
530 RETURN
540 PRINT " *"
550 RETURN
Line notes:
10: Whole inches only. 20: 2 digits to right of period. 21: Fractional inches only. 30: 3 digits: 0-9 inches plus fraction. 40: 4 digits: 10 or 11 inches plus fraction 50: Set up for subtraction. 80: Print fraction. 110: Initialize. 150: Next command. 170: Does user want total? 180: Does user want to clear? 200: Is this a subtraction? 240: Find location of period. 270: No period; whole feet only. 290: Extract feet. 300: Chop off feet and period. 310: Len A$ equals 1, 2, 3, or 4. 330: Graphic on 1. 340: Print fraction if any. 400: Print total. 440: Subroutine to PRINT W. 490: Graphic on 1. 510: PRINT the fraction. 540: No fraction.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.