|
||||
Chapter 5 : Putting it all togetherNow that we've seen how to handle variables and save and load a program, we can begin to put it all together to produce a workable program using what we've learnt so far, with a few extra bits. All programs in this guide from this point onwards can be found as Basic files in the accompanying directory called 'Progs' so you won't have to type them in. If you feel you could do with more practice, you could type a program in a bit at a time, saving it as you go. That way, if you made a serious mistake, you could reload the last saved version and start again from the point where you saved it. If you LIST a long file using Basic's command level, you may have trouble seeing it when it grows too long to fit on the screen all at once. If you want to examine the first few lines after you've typed most of it in, you'll find that they scroll straight up and off the top of the screen when you press Return after typing LIST, and you don't get a chance to read them. The solution is to put the screen into page mode. You do this by pressing Ctrl-N (hold down Ctrl while you press and release N). In page mode, the screen will scroll part of the way, then stop. To make it scroll some more, press either of the two Shift keys. If the program is long, you may find that too much of it scrolls up the screen before you can release the Shift key and you miss some. You can prevent this happening by holding down Ctrl while you press and release Shift, which makes it scroll more slowly. While the listing is partly scrolled, you can't do anything else with the machine. To get out of a partly-scrolled program, either hold down Shift to scroll to the end, or press Esc. To get out of page mode, type Ctrl-O. Always save a program before you run it, even if you're using a text editor which allows you to run without saving. It's always possible that your program may contain an obscure bug which will cause the computer to crash and need resetting. If this happens, you will lose whatever is in the memory, including your precious program, if you didn't save it! Our first program will tell you the day of the week of any date between 1900 and 2099. To look at the listing, you can either load the file into your text editor (set it to show the line numbers), or click on its name to see it in a new browser window. The program starts with two REM lines. The first contains the embedded filename and the second tells you what the program does. It is always a good idea to include REMs of this sort. You know what a program does when you've just written it but, if you found it in six months time you might have trouble remembering what it's for. Similarly, you can include REMs throughout the program explaining what various parts of it do. Don't overdo it, though - as well as making the file longer and possibly slowing down the execution speed slightly, it means a lot of extra typing! Watch Out For Idiots!To keep things as simple as possible, we will enter the date as three separate variables for the day of the month, the month number (1 to 12) and the year. (The indentation of the REPEAT ... UNTIL loop between lines 50 and 380 stops at line 100 because the 'UNTIL FALSE' on the end of this line fools the LISTO function.)Lines 80, 90 and 100 are the nearest we'll get in this program to 'idiot-proofing'. It is possible to build in lots of safeguards, for example to reject a month number greater than 12 or to stop you entering 31st February, or indeed 31st April! All these extras add to the complexity of the program, however, and make it more difficult to understand. For the moment, just be careful how you use it. This program deals with dates in the 20th and 21st century. It is quite possible that someone will enter the year without the century number, for example '93' when they mean '1993' or '02' when they mean '2002'. This program makes the assumption that if you enter '40' or higher, you mean '1940' etc. and if you enter '39' or lower, you mean '2039' etc. Lines 80 and 90 deal with this situation by adding 100 to the year number if it's less than 40, then adding 1900 if it's less than 140. Line 100 sees our first use of the keyword OR. Instead of checking if one condition is true following the IF keyword, this line checks two conditions. It checks to see if either year% is less than 1900 or year% is greater than 2099. If either of these is true, we have entered a year number outside the bounds of our program, so the rest of the line is executed, beginning with a warning message telling us that we've gone wrong. If we wanted to do something only if two conditions are both met we would use the AND keyword, for example:
IF x%=2 AND y%=3 PRINT "They are both correct"
This line would print its message if both x%=2 and y%=3. The AND and OR keywords are examples of Boolean algebra, which we will discover more about later in this guide. We've put single quotes (') before and after the apology message to print blank lines on the screen. This is purely to make the screen output look better. Following this, the UNTIL FALSE sends us back to line 40 for another try. Doing the CalculationsAssuming that our year number was correct we get to line 110 and proceed to do a few calculations. First, because we are only interested in years from the beginning of the 20th century, we subtract 1900 from the year number.The year 1900 was not a leap year, but 2000 was, which is why our calculations will be valid over a 200 year period. This method is based on the fact that 1st January 1900 was a Monday. We need to know how many years and leap years there have been since then, add on the day of the month and a special number for each month, divide the whole lot by seven and take the remainder to tell us which day of the week the date was (or will be). Line 120 uses keyword DIV, which is a way of performing whole number division. The result is a whole number and any remainder is discarded. Examples of this are:
7 DIV 4=1
This line tells us the number of leap years since 1900, remembering that 1900 itself wasn't one. If the year is a leap year and the month is January or February, we mustn't include that year when we count the leap years. Line 130 deals with this by checking three conditions. The first part of the line checks whether or not the year is a leap year, i.e. divisible by 4. We do this with the MOD keyword. This is rather like the opposite of DIV, in that it performs whole number division but throws away the result and keeps the remainder. To repeat our earlier examples:
7 MOD 4=3 i.e. 1 remainder 3
Note the use of the brackets round (year% MOD 4). Basic always works out what is inside brackets first before removing them. This removes the risk of it starting by checking to see if 4=0 and applying the value FALSE to it! The second condition to be met is that the month number, mon%, is 1 or 2 for January or February. This is written as 'mon%<3', meaning mon% is less than 3, but it could just as easily have been written 'mon%<=2', meaning mon% is less than or equal to 2. Needless to say, '>=' also means greater than or equal to. The final condition is to ensure that we don't apply this process to 1900, which was not a leap year, which we do by making sure that year% is greater than zero. The two AND keywords ensure that we only reduce the leap year count by 1 if all three conditions are met. Monthly AdjustmentsWe need to add a special number to our calculations to compensate for the fact that the months don't all start on the same day of the week. We will call this number monnum%, and we set its value with lines 140 to 250. There are more elegant ways of doing this which don't take up 12 lines, but we're already seeing plenty of new techniques in this example and we'll leave them for later.Line 260 adds all the necessary numbers together and line 270 divides the result by 7, giving us the remainder by using the MOD keyword. This is a number between zero and 6 which represents the day of the week that we're looking for. All that remains is to turn this into a word that we can print. Lines 280 to 340 do this for us, again in a rather long-winded way. Line 350 provides the answer we're looking for, by printing the text between the quotes followed by the string variable, so that if day$ was, say, Friday, it would print:
That day was a Friday
Line 360 prints a blank line then invites us to have another go. Our reply to this is examined by line 380. This line literally means 'Go back to the REPEAT keyword unless the first character of char$ isn't 'Y' and the first character of char$ isn't 'y'. This means that, as long as we type in something whose first character is either 'Y' or 'y', line 380 sends us back to the REPEAT at line 40. We could get another go by answering 'Yes!!!'. |
||||
|
||||
|