[求助]字符型转成数字型
本人是刚学VFP,现想要把一个字符型字段转换成数字型,字符型字段(SL)内容如下:10*2+5 、 10*2+2*5+3 、 5 、 1*5 等等,加个数字型字段计算出来也行!现手上有一段相关代码不知道能否用上:
PROCEDURE EVA_CH
PARA X,Z
PRIV M,N,I,J,X1
SET EXAC ON
STOR 1 TO M,N
IF PARA()=1
Z=1
ENDIF
X=UPPE(ALLT(X))
IF TYPE(X)="U"
RETURN "0"
ENDIF
IF TYPE(X)="N".AND.AT("*",X)=0
M=LTRI(STR(&X.*Z))
RETURN IIF(VAL(M)>9999999999,"0",M)
ENDIF
J=INT((LEN(X)+1)/2)
DIME A(J,2)
A=""
FOR I=1 TO LEN(X)
X1=SUBS(X,I,1)
DO CASE
CASE X1=" "
*NOTHING
CASE BETW(X1,"0","9") AND N=1
A(M,1)=A(M,1)+X1
CASE X1="+" OR X1="-"
N=1
IF A(1,1)<>""
M=M+1
ENDIF
A(M,1)=X1
CASE X1="*" AND N=1 AND A(M,1)<>""
N=2
CASE N=2 AND X1<>"*"
A(M,2)=A(M,2)+X1
OTHE
RETURN "0"
ENDC
ENDF
=ASORT(A,2,-1,1)
FOR I=1 TO J
A(I,1)=LTRIM(STR(VAL(A(I,1))*Z))
IF I=1
LOOP
ENDIF
IF A(I,2)=A(I-1,2)
A(I,1)=LTRIM(STR(VAL(A(I,1))+VAL(A(I-1,1))))
A(I-1,1)=""
A(I-1,2)=""
ENDIF
ENDF
FOR I=1 TO J
A(I,2)=VAL(A(I,2))
IF A(I,2)=1
A(I,2)=0
ENDIF
ENDF
=ASORT(A,2,-1,1)
FOR I=1 TO J
A(I,2)=LTRI(STR(A(I,2)))
IF A(I,2)="0"
A(I,2)=""
ENDIF
ENDF
X=""
FOR I=1 TO J
IF A(I,1)="" OR A(I,1)="0"
LOOP
ENDIF
X1=LEFT(A(I,1),1)
IF X1<>"-" AND X1<>"+"
A(I,1)="+"+A(I,1)
ENDIF
X=X+A(I,1)
IF A(I,2)<>""
X=X+"*"+A(I,2)
ENDIF
ENDF
IF LEFT(X,1)="+"
X=SUBS(X,2)
ENDIF
IF TRIM(X)=""
X="0"
ENDIF
RETURN IIF(EVAL(X)=0,"0",X)
[[it] 本帖最后由 lsp_2 于 2008-6-1 15:00 编辑 [/it]]
[[it] 本帖最后由 lsp_2 于 2008-6-2 09:07 编辑 [/it]]