Qbasicnews.com
February 26, 2020, 12:39:59 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Making a calendar in Qbasic  (Read 10221 times)
Blader5489
New Member

Posts: 21


« Reply #15 on: February 07, 2005, 06:49:04 PM »

I'm not sure I completely understand. How would I set x and y to certain days and weeks? If I said x=1, then how would that be setting x under Sunday? Just the same, how would y=1 put the date into the first week?

I understand the rest of what you're saying, I just don't understand what you mean by setting x and y to the starting date (or any date, for that matter).
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #16 on: February 07, 2005, 11:40:42 PM »

Blader,
First, let me simplify your code for printing the name of the day of the week. Your code, with all the if's, will work, but it's too long.
Code:

dim daynam$ (0 to 6)
daynam$(0)="Saturday"
daynam$(1)="Sunday"
daynam$(2)="Monday"
daynam$(3)="Tuesday"
daynam$(4)="Wednesday"
daynam$(5)="Thursday"
daynam$(6)="Friday"

print daynam$(x)

That's it. There are trickier ways like puting the names in a string with spacing by 9 characters (the length of Wednesday). Then using x as an index in multiples of 9 you retrieve the wanted name. I won't even post it 'cause it's too hairy although the whole thing takes 2 lines of code.

I have a QuickBasic program that prints a calendar. It's more than 100 lines of code. First of all, let me tell you that depending on the month and year, a calendar page can have 6 rows or weeks.

Printing the calendar so it looks good is not a trivial program. Remember that it may need to print empty boxes at the top or bottom, again depending on the month and year.

However, it is an challenging program. Keep working at it.
*****
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #17 on: February 08, 2005, 02:42:51 AM »

Okay, a calendar is a grid, right?  The days are columns, and the weeks are rows.  So think of X as "which column am I looking at?" (i.e. DAY) and think of Y as "which row am I looking at?" (i.e. WEEK).

So, let's say you want to draw a calendar for January (Max days = 31), and this particular January starts on a Thursday.  That means that the grid box labelled "1" (first day of the month) is going to be Thursday, Week 1.  In this case, Y (week) will be 1, and X (day) will be 6--Thursday.

Code:
X = 6
Y = 1
CurrentDay = 1

do
  position cursor at grid location X, Y
  print CurrentDay on the screen at this location

  Add 1 to X, to move to the next day
  If you made X = 8, then set it back to 1 and increase the week (Y) by 1
  Add 1 to CurrentDay
loop until CurrentDay = 31 (end of January)

Does this pseudocode make sense ?

*peace*

Meg.
Logged
Blader5489
New Member

Posts: 21


« Reply #18 on: February 08, 2005, 08:58:09 PM »

Right, I understand that. Here's what I don't understand: if I wrote x=1, y=1, then I did LOCATE x, y: PRINT Currentday, how would Qbasic know that x,y was the first day of the first week? Wouldn't it just go to the first space on the screen (the top left corner)?

That's what I don't understand, how (x,y) would be assigned to a specific spot on the grid.
Logged
dadsherm
Member
*
Posts: 75


« Reply #19 on: February 08, 2005, 10:32:08 PM »

When you use the term LOCATE X,Y, you are telling the computer to print whatever it is you wish to print, at the values given to x and y.
In this case x would equal the ROW you wish to start the printing and y would equal the column.
It may be less confusing for you if you were to use the letters R for Row and C for Column.
In short to answer your question of how x and y are assigned to a certain spot on the grid: it's because that is the spot you designated for it to print. To get a better idea of what I'm saying, replace the x=1,y=1 with x=13, y = 40. This will cause the printing to start printing at app. the center of your screen.
The computer has no idea that it's printing on a certain week/day.
It only prints at the spot it's "told" to.
In the case of:
 x=1:y=1 :LOCATE x,y :PRINT Currentday...
You're right. It would PRINT at the top left cornor of the screen because that's exactly the place you "told" it to print.

I hope this helped to clearify it for you. :wink:
Logged

adsherm
Moneo
Na_th_an
*****
Posts: 1971


« Reply #20 on: February 09, 2005, 12:40:43 AM »

Just a quick note, guys. If your solution uses LOCATE, you will never be able to print your calendar out on paper. So, print it to a file first. Then, if you want to display it, you can easily do that too.
*****
Logged
dadsherm
Member
*
Posts: 75


« Reply #21 on: February 09, 2005, 08:33:04 AM »

Good point, Moneo.
I'm glad you mentioned it.
I hadn't even considered that aspect of it.  :oops:
Logged

adsherm
Meg
Ancient QBer
****
Posts: 483


« Reply #22 on: February 09, 2005, 03:48:45 PM »

Okay, perhaps looking at this code will help you understand.  I've replaced X and Y with the variables WeekDay% and WeekNum%.  The code follows my pseudocode exactly.

Please look through this code and let me know if you have questions about it.  If this is for an assignment, please don't turn in this code as your own.

*peace*

Meg.

[syntax="QBASIC"]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set your variables here.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MaxDaysInMonth% = 31     'How many days are in the month we're drawing?
WeekDay% = 4             'What's 1st day of the month? (1=Sa,2=Su,3=Mo,etc.)
WeekNum% = 1             'Week 1 always contains the 1st day, so leave this.

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Clear the screen and draw the days.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLS
PRINT "Sa Su Mo Tu We Th Fr"

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Loop through all the days of the month, from 1 to MaxDaysInMonth%.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
FOR CurrentDay% = 1 TO MaxDaysInMonth%
   
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     'Locate the correct place on the screen and print the day number.
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     LOCATE WeekNum% + 1, WeekDay% * 3 - 2
     PRINT LTRIM$(STR$(CurrentDay%));
   
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     'Increase the day of the week by one.
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     WeekDay% = WeekDay% + 1
   
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     'If WeekDay% is 8, you need to go to the first day of the next week.
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     IF WeekDay% = 8 THEN
          WeekDay% = 1
          WeekNum% = WeekNum% + 1
     END IF

NEXT CurrentDay%

SYSTEM
[/syntax]
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #23 on: February 11, 2005, 01:23:21 PM »

Emergency! Run this program immediately. Study the highlighted date!

Mac


Code:
DECLARE SUB ShowCalendar (TheFirst%, days%, mname$, yyyy!, dd!)
DIM MDays(12) AS INTEGER
DIM MCode(12) AS INTEGER
DIM mname(12) AS STRING
FOR i = 1 TO 12: READ MDays(i), MCode(i), mname(i): NEXT i
DATA 31, 0, January
DATA 0, 3, February
DATA 31, 3, March
DATA 30, 6, April
DATA 31, 1, May
DATA 30, 4, June
DATA 31, 6, July
DATA 31, 2, August
DATA 30, 5, September
DATA 31, 0, October
DATA 30, 3, November
DATA 31, 5, December
DIM cc AS INTEGER: '0-6 index for Dow()
DO
  CLS
  PRINT "You enter any date in the centuries 1600-3199"
  PRINT "Will print the calendar for the month that day falls in"
  PRINT
  PRINT "Enter a date yyyy-mm-dd or yyyymmdd"
  PRINT "(Or enter nothing to quit)"
  DO: GOSUB GetDate: LOOP WHILE yyyy = 0
  GOSUB GetFirst 'Compute cc: day of week 1st of month falls on
  CALL ShowCalendar(cc, MDays(mm), mname(mm), yyyy, dd)
  WHILE INKEY$ = "": WEND
LOOP 'until user stops program by not entering a date

GetDate:
yyyy = 0
PRINT
LINE INPUT "Date: "; d$
IF d$ = "=" THEN d$ = "20050210"
IF UCASE$(d$) = "STOP" THEN STOP: RUN
IF d$ = "" THEN CLS : SYSTEM
SELECT CASE LEN(d$)
CASE 8:
CASE 10:
  IF MID$(d$, 5, 1) <> "-" THEN PRINT "Dash missing": RETURN
  IF MID$(d$, 8, 1) <> "-" THEN PRINT "Dash missing": RETURN
  d$ = LEFT$(d$, 4) + MID$(d$, 6, 2) + RIGHT$(d$, 2)
CASE ELSE:
  PRINT "8 or 10 characters, please": RETURN
END SELECT
FOR i = 1 TO 8
  IF INSTR("0123456789", MID$(d$, i, 1)) = 0 THEN
    PRINT "Non-digit where digit belongs"
    RETURN
  END IF
NEXT i
mm = VAL(MID$(d$, 5, 2))
IF mm < 1 OR mm > 12 THEN PRINT "Month 1-12": RETURN
MDays(2) = 28
yy = VAL(LEFT$(d$, 4))
IF yy < 1600 THEN PRINT "Start in year 1600 or later": RETURN
IF yy > 3199 THEN PRINT "Start before year 3200": RETURN
IF yy \ 4 = yy / 4 THEN MDays(2) = 29
IF yy \ 100 = yy / 100 THEN MDays(2) = 28
IF yy \ 400 = yy / 400 THEN MDays(2) = 29
IF yy \ 4000 = yy / 4000 THEN MDays(2) = 28
dd = VAL(RIGHT$(d$, 2))
IF dd < 1 OR dd > MDays(mm) THEN
  PRINT "Day Range for "; mname(mm); " is 1 - "; MDays(mm)
  RETURN
END IF
yyyy = yy
'-------------------------------
  v$ = RIGHT$(DATE$, 4) + LEFT$(DATE$, 2) + MID$(DATE$, 4, 2)
  IF VAL(v$) > 20050214 THEN
    STOP
    ' It is too late to buy Valentine Card!
    ' Might as well delete this section of code
    ' to get a normal calendar program
  ELSE
    yyyy = 2005: mm = 2: dd = 14
  END IF
'-------------------------------
RETURN

GetFirst:
'http://www.koshko.com/calendar/day-of-week.shtml
cc = MCode(mm) + 1: '1=1st of Month
IF mm < 3 AND MDays(2) = 29 THEN cc = cc - 1
v$ = RIGHT$(STR$(yyyy), 4)
v% = VAL(RIGHT$(v$, 2))
cc = cc + v% + (v% \ 4)
SELECT CASE LEFT$(v$, 2)
CASE "16", "20", "24", "28": cc = cc + 6
CASE "17", "21", "25", "29": cc = cc + 4
CASE "18", "22", "26", "30": cc = cc + 2
CASE "19", "23", "27", "31": cc = cc + 0
CASE ELSE: STOP
END SELECT
cc = 7 * ((cc / 7) - (cc \ 7))
RETURN

SUB ShowCalendar (TheFirst%, days%, mname$, yyyy, dd)
DIM DoW(6) AS STRING
DoW(0) = "Sunday"
DoW(1) = "Monday"
DoW(2) = "Tuesday"
DoW(3) = "Wednesday"
DoW(4) = "Thursday"
DoW(5) = "Friday"
DoW(6) = "Saturday"
'PRINT "This month, "; mname$; ", has"; days%; "days"
'PRINT "It starts on a "; DoW(TheFirst%)
'PRINT "The year is"; yyyy; "and the day of interest is"; dd
CLS
DIM LastRow AS INTEGER: LastRow = 19
IF days% + TheFirst% > 35 THEN LastRow = 22
IF days% = 28 AND TheFirst% = 0 THEN LastRow = 16
LOCATE 1, (75 - LEN(mname$)) / 2: PRINT mname$; yyyy
FOR i = 0 TO 6: LOCATE 3, 4 + (i * 11): PRINT DoW(i); : NEXT i
FOR i = 3 TO LastRow:  FOR j = 0 TO 7
    LOCATE i, 2 + (j * 11)
    PRINT CHR$(179);
NEXT j: NEXT i
FOR i = 4 TO LastRow STEP 3
  LOCATE i, 3
  PRINT STRING$(76, CHR$(196));
  FOR j = 2 TO 79 STEP 11
    LOCATE i, j
    SELECT CASE j
    CASE 2: PRINT CHR$(195);
    CASE 79: PRINT CHR$(180);
    CASE ELSE: PRINT CHR$(197);
    END SELECT
  NEXT j
NEXT i
LOCATE LastRow, 2: PRINT CHR$(192);
LOCATE LastRow, 79: PRINT CHR$(217);
FOR i = 13 TO 68 STEP 11
  LOCATE LastRow, i: PRINT CHR$(193);
NEXT i
' Insert numbers
DIM CurRow AS INTEGER: CurRow = 5
DIM CurCol AS INTEGER: CurCol = TheFirst%
FOR i = 1 TO days%
  LOCATE CurRow, 5 + (CurCol * 11)
  PRINT i;
  CurCol = CurCol + 1
  IF CurCol > 6 THEN CurCol = 0: CurRow = CurRow + 3
NEXT i
' Locate the day of interest
CurRow = 5
CurCol = TheFirst%
FOR i = 1 TO days%
  IF i = dd THEN EXIT FOR
  CurCol = CurCol + 1
  IF CurCol > 6 THEN CurCol = 0: CurRow = CurRow + 3
NEXT i
LOCATE CurRow, 5 + (CurCol * 11), 1
COLOR 3, 7: PRINT i;
LOCATE CurRow, 6 + (CurCol * 11), 1
COLOR 7, 0
END SUB

Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #24 on: February 12, 2005, 03:07:26 AM »

I did, and added my special GOTO RAEjump and the label RAEjump:, as follows, just before and just after the dashed lines:

[code]
yyyy = yy
GOTO RAEjump
'-------------------------------
  v$ = RIGHT$(DATE$, 4) + LEFT$(DATE$, 2) + MID$(DATE$, 4, 2)
  IF VAL(v$) > 20050214 THEN
    STOP
    ' It is too late to buy Valentine Card!
    ' Might as well delete this section of code
    ' to get a normal calendar program
  ELSE
    yyyy = 2005: mm = 2: dd = 14
  END IF
'-------------------------------
RAEjump:
RETURN
[code]

That got rid of your "forgot St. Valentine's Day BUG"!  :bounce: [/code]
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Ralph
Ancient Guru
****
Posts: 544


« Reply #25 on: February 12, 2005, 03:09:17 AM »

I did, and added my special GOTO RAEjump and the label RAEjump:, as follows, just before and just after the dashed lines:

Code:

yyyy = yy
GOTO RAEjump
'-------------------------------
  v$ = RIGHT$(DATE$, 4) + LEFT$(DATE$, 2) + MID$(DATE$, 4, 2)
  IF VAL(v$) > 20050214 THEN
    STOP
    ' It is too late to buy Valentine Card!
    ' Might as well delete this section of code
    ' to get a normal calendar program
  ELSE
    yyyy = 2005: mm = 2: dd = 14
  END IF
'-------------------------------
RAEjump:
RETURN


That got rid of your "forgot St. Valentine's Day BUG"!  :bounce:
Now, your Calender program seems great!!!

By the way, I use GOTOs when I want to jump a section of code, without deleting it, for whatever reason...
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!