def strToComplex(xStrIn): #By D@CC #On 2021 E May 19 #Article: 164.html selectCode 10 #uSD: none # #Functions Used: # type() # .index # int() # complex() #Limitation: only works with integers numErr="3.14+3.14j" sType=type(xStrIn) #print("sType:",sType) #isLeft=False try: posLeft=xStrIn.index("(") if posLeft>0 : print("err 09: ( not first char") return numErr else: isLeft=True #end if except: #no Left ( isLeft=False #end try if isLeft : #get right try: posRight=xStrIn.index(")") isRight=True except: #no Left ( isRight=False #end try if isRight==False : print("err 28:left ( but no Right )") return numErr else: #elim both parens xStr=xStrIn[1:len(xStrIn)-1] #print("new xStr:",xStr) #end if else: xStr=xStrIn #end if # print("xStr used as is") try: posPlus=xStr.index("+") except: isPlus=False posPlus=0 #end try #print("posPlus:", posPlus) #find any "j" posJ=0 try: posJ=xStr.index("j") isFoundJ=True except: isFoundJ=False #end try if isFoundJ==False: try: posCapJ=xStr.index("J") posJ=posCapJ isFoundJ=True except: isFoundJ=False #end try if isFoundJ==False: print("warning: no j") #return Null posJ=0 #end if #print("posJ:",posJ) #return #posPlus is 0 if no + #posJ is 0 if no J realStr="" if posPlus>0: realStr=xStr[0:posPlus] else: # later we must do this #if there is no J : #realStr=xStr[0:len(xStr)] #err #is there no J realStr="0" #end if #print("realStr:",realStr) imagStr="" if posJ>0: #print("check indices here") #print("posPlus:",posPlus) if posPlus>0: imagStr=xStr[posPlus+1:len(xStr)-1] #print("imagStr:",imagStr) else: imagStr=xStr[0:len(xStr)-1] #print("imagStr:",imagStr) #end if else: #posJ=0 so there is no imaginary component imagStr="0" # we can now get the value of realStr realStr=xStr[0:len(xStr)] #end if #print("imagStr:",imagStr) #print("fut: allow for float complex") #return try: real=int(realStr) except: print("err 107:bad input format eg ) not last char") return numErr #end try #print("real:",real) try: imag=int(imagStr) except: print("err 114: bad input format eg ) not last char") return numErr #end try #print("imag:",imag) xComplex=complex(real,imag) cType=type(xComplex) #print("cType:",cType) #xComplex=1+xComplex return xComplex #/strToComplex_pico.py