Tuesday, November 26, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 9C


9.3 Strings
“Alright, everybody! Show me what you have!”. With that opening statement, the class began. The teacher have them filled this table:
L0 D C D C
L1 D D C C
A A A=AopB A A=AopB
B B=(B*10)+C B C B
C D 0 D 0
OP OP op OP op
What that table means is that L0 and L1 denote the type of commands that is entered. D for digits and C for aritmetic operation. L0 is the last keypress, while L1 is the previous keypress (what was previously L0). A,B,C stands for variables, where A and B are variables for calculation and C is the digit being entered. OP is the variable holding the aritmetic operator, where 'op' lowercase is the actual operator.
“Now, that you have learned string operation, L0 and L1 are a piece of cake!” said the teacher. “Here is the modified main program. You see how simple this can be.”
REM CALCULATOR
CLS:CLEAR
DIM OP$[5]
@INIT
A=0:B=0:C=0:OP=0
OP$[0]=”NOP”:OP$[1]=”ADD”
OP$[2]=”SUB”:OP$[3]=”MUL”
OP$[4]=”DIV”
L0=0:L1=0

@MAIN
VSYNC 1:CLS
?”A= “;A:?”B= “;B:?”C= “;C
?”OP= “;OP$[OP]
?”L0= “;L0”?”L1= “;L1
@MAIN1
CH$=INKEY$:IF CH$==”” GOTO @MAIN1
IF CH$==”C” GOTO @INIT
IF CH$==”E” THEN B=0:GOTO @MAIN
L1=L0:L0=0:'0=D,1=C
IF INSTR(“0123456789”,CH$)<0 THEN L0=1
ON (L1*2+L0) GOTO @ADD,@ADC,@ACD,@ACC
@ADD
GOTO @MAIN
@ADC
GOTO @MAIN
@ACD
GOTO @MAIN
@ACC
GOTO @MAIN
“And there you go. All you have to do is fill in the blanks. Now that you have all the table set up, it's easy! Of course, if you fill in the table wrong, then you will have trouble doing it. Do you understand now why I'm not grading your homework? I'd rather see you suffer all the bugs you yourself created!
“And then I will see you try to overcome the bugs. The way you approach your debugging will reveal your character. If you blame yourself, then you'll be alright. If you blame the tool, then you will never make it! Gya ha ha ha!”
@ADD
C=INSTR(“0123456789”,CH$)
B=(B*10)+C
GOTO @MAIN
@ADC
IF OP==0 THEN A=B
IF OP==1 THEN A=A+B
IF OP==2 THEN A=A-B
IF OP==3 THEN A=A*B
IF OP==4 THEN A=A/B
C=0
IF INSTR(“=+-*/”,CH$)>0 THEN OP=INSTR(“=+-*/”,CH$)
GOTO @MAIN
@ACD
C=INSTR(“0123456789”,CH$)
B=C
GOTO @MAIN
@ACC
GOTO @ADC
GOTO @MAIN
Filling the routines then becomes a simple exercise. The students need to be careful regarding some order or execution, but otherwise, it's not a problem. The problem, of course, is in design, and with bad design, comes bad program. Peter, the smart one in the class, has actually programmed in the routines just to be sure what he has actually works. In fact, that's the best way to do it. The other students, being beginners, never thought to do it that way. They still think that coding remains the most difficult phase of computer programming.
The whole process of filling in data takes less than 10 minutes. Testing, of course, takes longer. One by one, however, the students finally managed to get their program going. There are two special commands involved: All Clear (AC) and Clear Entry (CE). These corresponded with character 'C' and 'E' respectively. If you see the code, they are very simple. 'C' will simply shuffle the program execution to @INIT function, whereas 'E' will simply reset B to 0.
“Alright.” the teacher said. “Now that everybody got it, the next step is to create a graphical calculator. Now, don't worry about it because it doesn't involve anything you haven't done. In fact, if you remember your piano playing program, then it's the same exact thing. The only thing different is the key or button arrangement. So, what I want you to do now is to draw some character text on screen and shape that as calculators. So, get out your journal and start drawing!
“Remember, program design starts on paper! Those of you who tries to shortcut the step will find out that the whole process will take 2 to 3 times longer. If you don't believe me, just try it! Gya ha ha ha!”
The students dutifully drew the design on their journal. A 4 by 4 design emerged. In the end, there was no place to put the equal sign command, and the function is placed on the display instead. The final code is listed in Appendix G.
And so it goes. The students finally managed to draw a rough draft of the calculator, and it did start to look like a real calculator. Certainly there are some features that are missing, and some of the calculation behavior isn't quite right, but these are just little nits. The fact is, even beginning students are perfectly capable of doing a calculator program that is not dependent upon INPUT commands.
“Right, guys. Now for homework, I want you to think very carefully about how strings work because I will take the character patterns away from you. You will have to use character arrays for your on-screen keyboard input.”
The students all moaned their displeasure.
The teacher banged on their desk. “What's this? You're all complaining on such a little task? Well, well, why don't I add this to your homework, then: Memory button. Remember, you have 4 different operations on it. Memory Add, Memory Clear, Memory Reset, and Memory Substract.”
The students regretted their outburst already, but the teacher isn't finished yet. “Come to think of it, that's still too easy. So, not only you will have to come up with 8 memory banks, but also a way to read a script so you can do the operation automatically.”
“Teacher, what do you mean a 'script'?” asked a student.
“What I mean is that you have automatic input. Coming from a string. You need to parse the string and automatically give the answer.”
Peter raised his hand. “Parse the string? Isn't parsing a difficult thing to do? As in Graduate Student level?”
The teacher waved his hand, dismissing Peter's concern. “Nah. I learned it in College Senior level. So, should be fine.”
All the students looked at one another. College Level? Yikes! What kind of trouble have their teacher got them into? They couldn't believe it!
“Will this be graded?” asked Peter. There is hope yet at salvation.
“Of course. If you managed to do this, you will be amply rewarded.” The teacher nodded his head. “Of course, if you fail miserably, then you will be severely punished, considering that it will have the highest scoring criteria. So, good luck on working on that problem. Gya ha ha ha!” The bell rang, and there's nothing more to say on the subject.
On the next day, the students compared their notes. The first criteria turns out to be easy. Instead of using PRINT and CHKCHR, the solution is simply use a string array instead of PRINT for the row, and use MID$ for the column. That's it. It's very trivial to do.
The second solution is a bit harder. However, the AC/CE button provided a hint on the solution. It's a simple matter to just add more command to it. The most important function is READ/WRITE command equivalent. What about this 8 bank memory? Another array solves the problem. Simply by setting aside 8 characters for each bank, the memory bank problem is trivially solved. What remains are the task of Memory Add, Sub, Read, and Clear. An extension of AC/CE buttons solved that one also.
Of course, the students only had so much time in a day, and they were unable to solve the last problem. Parsing! A whole book can, and have been, written on the subject. In the end, they gave up and on the next day, they entered the class with fear, just waiting for the hammer to fall.
There was nothing they could do. How can mere high school students compete with University Level students? How can they, as beginner, do what 4 year college students are expected to do? Impossible!
Of course, this doesn't sit well with the teacher. “What do you mean you have no idea what to do? Well, well, it's lucky that I have my favorite stamp with me, today.” The students coiled in terror!
“So everybody got the first problem? Of course, it only scored 5 points. That's so easy. All you have to do is create a string array, and then check against it. Like this:”
DIM SC$[25]
@DIS
CLS:PNLTYPE “OFF”
SC$[0]=”====================”
SC$[1]=SC$[0]:SC$[2]=SC$[0]
SC$[3]=”111112222233333+++++”
SC$[4]=SC$[3]:SC$[5]=SC$[3]:SC$[6]=3:SC$[7]=3
SC$[8]=”444445555566666-----”
and so on...
'Checking
CH$=MID$(SC$[TCHY/8],TCHX/8,1)
For memory bank problem, simply set aside a memory array. Then set some keys to select them. “C” and “E” are taken, but “a”-”h” are not. As for Memory key operations, “M”-”P” are available. This simply lends itself to this implementation:
DIM MM[8]:'MEMORY BANK STORAGE
MI=0:'MEMORY BANK INDEX

IF INSTR('abcdefgh',CH$)>=0 THEN MI=INSTR('abcdefgh',CH$):GOTO MAIN
IF CH$==”M” THEN MM[MI]=MM[MI]+B:GOTO @MAIN
IF CH$==”N” THEN MM[MI]=MM[MI]-B:GOTO @MAIN
IF CH$==”M” THEN MM[MI]=0:GOTO @MAIN
IF CH$==”M” THEN B=MM[MI]:GOTO @MAIN
IF CH$==”C” GOTO @INIT
“Does everybody got that? They are all so easy, if you cannot do it, you should be ashamed of yourself! Well, now that you've got 15 points out of 100, ready to get the other 85 points?” the teacher asked. Of course, this is where none of the students have any solution whatsoever. The teacher gleefully put 15 points for everybody. “What a bunch of losers! Gya ha ha ha!”
All the students are just so frustrated. They simply do not know what to do. Instead of resuming teaching, however, the teacher simply went back to his desk, put his feet up, and went to sleep. Snoozing. At first, the student were patiently waiting for the teacher to wake up. After a while, it's clear that the students are left to fend for themselves.
None of the students know what to do. Some started doodling. Others decided to take a snooze as well. Only Peter was alert. He knew that the teacher did this for a reason. What could that be?
What was the assignment? To have automated entry? That's parsing. Parsing is difficult. But since the teacher said that it isn't impossible, that means it's possible, and within the limits of the lectures so far. So, how can that be?
It was not after Peter stared at the problem for 20 minutes that he looked at this line one more time:
CH$=MID$(SC$[TCHY/8],TCHX/8,1)
The key isn't in TCHX/TCHY. The key is in CH$! That's one single character. What's a single character? LEFT$(S$,1). If he can find a way to pull a single character each time...
IS$=”1+2+3+4+5=”
@LOOP
IF LEN(I$) THEN CH$=LEFT$(IS$,1) ELSE END
IS$=RIGHT$(IS$,LEN(IS$)-1)
?CH$:GOTO @LOOP
Peter confidently raised his hand. “Teacher! I got it!” All the rest of the students couldn't believe it!

No comments:

Post a Comment