如何在QBasic中解析多年的时间

时间:2015-03-28 21:33:34

标签: time basic qbasic

如何解析测试所需的Microsoft QBasic中的时间(月/日/年)。

s = 'PT1H28M26S'

我想得到:

num_mins = 88

2 个答案:

答案 0 :(得分:1)

您可以使用下面的代码解析这样的时间字符串,但真正的问题是:
谁还在2015年使用QBasic!?

CLS
s$ = "PT1H28M26S"

' find the key characters in string
posP = INSTR(s$, "PT")
posH = INSTR(s$, "H")
posM = INSTR(s$, "M")
posS = INSTR(s$, "S")

' if one of values is zero, multiplying all will be zero
IF ((posP * posH * posM * posS) = 0) THEN
  ' one or more key characters are missing
  nummins = -1
  numsecs = -1
ELSE
  ' get values as string
  sHour$ = MID$(s$, posP + 2, (posH - posP - 2))
  sMin$ = MID$(s$, posH + 1, (posM - posH - 1))
  sSec$ = MID$(s$, posM + 1, (posS - posM - 1))

  ' string to integer, so we can calculate
  iHour = VAL(sHour$)
  iMin = VAL(sMin$)
  iSec = VAL(sSec$)

  ' calculate totals
  nummins = (iHour * 60) + iMin
  numsecs = (iHour * 60 * 60) + (iMin * 60) + iSec
END IF

' display results
PRINT "Number of minutes: "; nummins
PRINT "Number of seconds: "; numsecs
PRINT "QBasic in 2015! w00t?!"

答案 1 :(得分:0)

在qbasic

中从字符串中抓取分钟的简单方法
REM Simpler way to grab minutes from string in qbasic
S$ = "PT1H28M26S"
S$ = MID$(S$, 3) ' 1H28M26S
V = INSTR(S$, "H") ' position
H = VAL(LEFT$(S$, V - 1)) ' hours
S$ = MID$(S$, V + 1) ' 28M26S
V = INSTR(S$, "M") ' position
M = VAL(LEFT$(S$, V - 1)) ' minutes
PRINT "num_mins ="; H * 60 + M