Qbasicnews.com
October 18, 2019, 01:52:21 PM *
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: Challenge: International version of PRINT USING  (Read 7599 times)
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #15 on: May 23, 2004, 10:10:44 AM »

I know it will take longer... But I currently can't put together even 20 minutes... imagine a longer period of time! Sad

Anyhow using a FSM it would be easier, at least the first interpretation part.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #16 on: May 23, 2004, 05:46:27 PM »

Here's my solution. It simply changes the required symbols in QB's internal routine. This works with PRINT USING, PRINT # USING and LPRINT USING.

Code:
'===============================================================
' SetUsing by Plasma
'---------------------------------------------------------------
' International support for PRINT USING, PRINT # USING and
' LPRINT USING. Created for Moneo's challenge.
'===============================================================

DECLARE SUB SetUsing (Currency$, Thousand$, Fractional$)

' Example...
CLS
' European (if you use cp858, you can use CHR$(213) for the real Euro)
SetUsing "E", ".", ","
PRINT USING "$$#.#######,##"; 1983745.34
' USA
SetUsing "$", ",", "."
PRINT USING "$$#,#######.##"; 1983745.34


DEFINT A-Z
SUB SetUsing (Currency$, Thousand$, Fractional$)

  '===============================================================
  ' SetUsing by Plasma
  '---------------------------------------------------------------
  ' International support for PRINT USING, PRINT # USING and
  ' LPRINT USING. Created for Moneo's challenge.
  '---------------------------------------------------------------
  ' Currency$ - New currency symbol, null to keep old symbol
  '             (default is "$")
  '
  ' Thousand$ - New separator character for thousands, null to
  '             keep old character (default is ",")
  '
  ' Fractional$ - New delimiter for fractional values, null to
  '               keep old delimiter (default is ".")
  '---------------------------------------------------------------
  ' * Compatible with:      QBasic 1.x
  '                     QuickBasic 4.x (IDE & compiled)
  '                         QB PDS 7.x (IDE & compiled)
  '                          VBDOS 1.0 (IDE & compiled)
  '
  '   Note: If you compile your program, it must be compiled as
  '         a stand-alone EXE!
  '===============================================================

  ' Due to Moneo's challenge rules, $ needs to be used in the
  ' format string no matter what you set the currency symbol
  ' to. If you would like to use your specified currency symbol
  ' in the format string, change the constant below to 1.
  CONST FormatCurrency = 0

  STATIC UsingSeg1&, UsingOff1
  STATIC UsingSeg2&, UsingOff2
  STATIC Version

  ' Find the segment and offset of the symbols in B$PUFOUT
  IF UsingSeg1& = 0 THEN
    UsingSeg1& = VARSEG(DefSeg$) - &H400
    DO WHILE UsingSeg1& > 0
      DEF SEG = UsingSeg1&
      FOR i = &H0 TO &H3FF4
        IF PEEK(i) = &HB0 AND PEEK(i + 2) = &HAA THEN
        IF PEEK(i + 3) = &H8A AND PEEK(i + 4) = &HCE THEN
        IF PEEK(i + 5) = &HF6 AND PEEK(i + 6) = &HDE THEN
        IF PEEK(i + 7) = &H7C AND PEEK(i + 8) = &H12 THEN
        IF PEEK(i + 9) = &HFE AND PEEK(i + 10) = &HCF THEN
        IF PEEK(i + 11) = &H75 THEN
          UsingOff1 = i
          EXIT DO
        END IF
        END IF
        END IF
        END IF
        END IF
        END IF
      NEXT
      UsingSeg1& = UsingSeg1& - &H3F0
    LOOP
    IF i = &H3FF5 THEN ERROR 5
  END IF

  ' Find the segment and offset of the symbols in PUSCAN
  IF UsingSeg2& = 0 THEN
    UsingSeg2& = UsingSeg1&
    DO WHILE UsingSeg2& > 0
      DEF SEG = UsingSeg2&
      FOR i = &H0 TO &H3FF4
        IF PEEK(i) = &H3C AND PEEK(i + 2) = &H74 THEN
          IF PEEK(i + 3) = &H45 THEN
            IF PEEK(i + 4) = &H3C AND PEEK(i + 5) = &H5F THEN
            IF PEEK(i + 6) = &H74 AND PEEK(i + 7) = &H94 THEN
            IF PEEK(i + 8) = &H3C AND PEEK(i + 9) = &H5C THEN
            IF PEEK(i + 10) = &H74 AND PEEK(i + 11) = &HB1 THEN
              UsingOff2 = i
              Version = 45
              EXIT DO
            END IF
            END IF
            END IF
            END IF
          ELSEIF PEEK(i + 3) = &H48 THEN
            IF PEEK(i + 4) = &H3C AND PEEK(i + 5) = &H5F THEN
            IF PEEK(i + 6) = &H74 AND PEEK(i + 7) = &H8C THEN
            IF PEEK(i + 8) = &H3C AND PEEK(i + 9) = &H5C THEN
            IF PEEK(i + 10) = &H74 AND PEEK(i + 11) = &HAC THEN
              UsingOff2 = i
              Version = 71
              EXIT DO
            END IF
            END IF
            END IF
            END IF
          END IF
        END IF
      NEXT
      UsingSeg2& = UsingSeg2& - &H3F0
    LOOP
    IF i = &H3FF5 THEN ERROR 5
  END IF

  ' Change the symbols in B$PUFOUT
  DEF SEG = UsingSeg1&
  IF Currency$ <> "" THEN POKE UsingOff1 + &H1, ASC(Currency$)
  IF Thousand$ <> "" THEN POKE UsingOff1 + &H17, ASC(Thousand$)
  IF Fractional$ <> "" THEN POKE UsingOff1 + &H2D, ASC(Fractional$)

  ' Change the symbols in PUSCAN
  DEF SEG = UsingSeg2&
  IF Version = 45 THEN
    IF FormatCurrency AND Currency$ <> "" THEN
      POKE UsingOff2 + &H11, ASC(Currency$)
      POKE UsingOff2 + &H24, ASC(Currency$)
    END IF
    IF Thousand$ <> "" THEN POKE UsingOff2 + &H41, ASC(Thousand$)
    IF Fractional$ <> "" THEN
      POKE UsingOff2 + &H1, ASC(Fractional$)
      POKE UsingOff2 + &H39, ASC(Fractional$)
      POKE UsingOff2 + &H4E, ASC(Fractional$)
    END IF
  ELSE
    IF FormatCurrency AND Currency$ <> "" THEN
      POKE UsingOff2 + &H12, ASC(Currency$)
      POKE UsingOff2 + &H26, ASC(Currency$)
    END IF
    IF Thousand$ <> "" THEN POKE UsingOff2 + &H44, ASC(Thousand$)
    IF Fractional$ <> "" THEN
      POKE UsingOff2 + &H1, ASC(Fractional$)
      POKE UsingOff2 + &H3C, ASC(Fractional$)
      POKE UsingOff2 + &H52, ASC(Fractional$)
    END IF
  END IF

  EXIT SUB
  PRINT USING ""; 0   ' Force the compiler to include PRINT USING

END SUB
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #17 on: May 23, 2004, 08:57:19 PM »

Plasma, utterly amazing! Except for the business of specifying the currency symbol which is a bit shaky, all the rest works just fine.

I can't figure out what you're doing because I've never used peek and poke. How do you figure out where the data is in memory in order to get in and modify it?

Unless we have a miracle entry at the last minute which performs better than yours, you will be the winner. At the end, I'll post my simplistic solution.

Thanks.
*****
Logged
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #18 on: May 23, 2004, 10:51:38 PM »

Hrmm...how is the currency symbol shaky? It'll use whatever character you pass to it. Neither codepage 437 nor 850 have a euro symbol, so there's not much I can do there.

About modifying the memory...well, it helps to have the source code to QBasic. Smiley
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #19 on: May 24, 2004, 08:33:40 AM »

All 7 MB of it. :*(

Lucky you. :*)
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Moneo
Na_th_an
*****
Posts: 1971


« Reply #20 on: May 25, 2004, 11:16:44 PM »

Plasma, you're too much! "It helps to have the source code to Qbasic" :rotfl:

About the currency symbol shakiness, I can't remember now what it was.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #21 on: May 27, 2004, 11:48:47 PM »

*Well, this challenge is getting as cold as yesterday's mashed potatoes.

Except for Plasma, no one really gives a diddley about the problem.

So, I'm naming Plasma the winner for a brilliant, though different, solution.

Now I'm going to post my simplistic solution.
I'm tired so I'll just walk you through the logic.

I got the idea from a function in Ethan Winer's QuickPak library which allows you to assign the results of a PRINT USING to a variable. So, without using this QuickPak function:

OPEN A WORK FILE FOR OUTPUT TO #2.
USING USING A NORMAL MASK, DO A PRINT USING #2.
Now on file 2 you have the result formatted according to the normal mask.
CLOSE THE WORK FILE, AND READ IN THE FORMATTED RESULT.

NOW, IF YOU WANT TO CONVERT THE RESULT TO EUROPEAN FORMAT,  CONTINUE.
USE AN INSTR TO CONVERT THE DECIMAL POINT TO AN "X" CHARACTER, AND USE AN INSTR TO CONVERT THE COMMAS TO A "Y".
This is to distinguish original decimal point and commas.
THEN CONVERT THE X'S TO COMMAS AND THE Y'S TO DECIMAL POINTS.
THEN IF YOU WANT TO DISPLAY SOME OTHER CURRENCY CHARACTER, CONVERT THE DOLLAR-SIGN TO THAT CHARACTER.

Simple, yes. Elegant, well that's a matter of opinion. Many people don't like to use work files. In this case, the work file is 95% of the solution.

Opinions?
*****
Logged
SCM
Wandering Guru
***
Posts: 311



« Reply #22 on: May 28, 2004, 01:33:21 AM »

Moneo,
It isn't that no one was interested in the challenge, we just weren't looking at it as having a simple solution.  nath and I were both considering it as a rewrite of the PRINT USING function.  

I like yours.  An output and an input isn't real fast, but most of the time it wouldn't be noticable.
Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #23 on: May 29, 2004, 12:52:30 AM »

Thanks, SCM, those are exactly my feelings too.
*****
Logged
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!