Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168759
Image: ubuntu2004
#this is a little converter tool(v0.01) which helps to convert from Python to Java, made by Patrick Hammer #It is useful for simple numeric functions, cause they can be used with just one click in #processing(that's why i made this tool), a powerful Java API for simulations, #sometimes you still need to correct a bit, especially variable declarations.
Source=""" def durchmesser(u): return u/pi def lulu(aba,test): if aba>0: aba=1 for i in range(0,10) test+=i return test def planetenoffset(ubase,uadd): uadd+=ubase return (durchmesser(uadd)-durchmesser(ubase))/2.0 #plapla """
src="" lastlen=1 arr=Source.split("\n") for i in range(0,len(arr)): if arr[i]!="": lp="" ####################{ }########################### leng=len(arr[i].split(" ")) if(leng<lastlen): for j in range(1,leng): src+=" " src+="}\n" if(leng>lastlen): for j in range(1,lastlen): src+=" " src+="{\n" for j in range(1,leng): lp+=" " lastlen=leng ####################for########################### if "for" in arr[i]: mini=0 maxi="0" VariableStr=arr[i].split("for")[1].split("in")[0].replace(" ","") CommaArr=arr[i].split("range(")[1].split(")")[0].split(",") maxi=CommaArr[0] if len(CommaArr)>1: mini=CommaArr[0] maxi=CommaArr[1] if "len(" in maxi: maxi=maxi.replace("len(","")+".lenght" arr[i]=lp+"for(int "+VariableStr+"="+str(mini)+";"+VariableStr+"<"+str(maxi)+";"+VariableStr+"++)" ####################if########################### if "if" in arr[i]: ArgStr=arr[i].replace(":","").split("if ")[1].replace(" and "," && ").replace(" or "," || ") arr[i]=lp+"if("+ArgStr+")" ####################def########################## if "def" in arr[i]: argsstr=arr[i].split("(")[1].split(")")[0].split(",") FuncNameStr=arr[i].split("(")[0].split("def ")[1] ResStr="def "+FuncNameStr+"(" for j in range(0,len(argsstr)): ResStr+="float "+argsstr[j] if j<len(argsstr)-1: ResStr+="," ResStr+=")" arr[i]=ResStr ###################array allocation############## if "[" in arr[i] and "]" in arr[i] and "," in arr[i]: arr[i]=arr[i].replace("]","}").replace("[","new float[]{") ####################comments##################### arr[i]=arr[i].replace("#","//") src+=arr[i] ####################### ; ###################### listargs=["+","-","*","/","=","return"] listnotargs=["for","def","if","//"] for j in range(0,len(listargs)): if not listargs[j] in arr[i]: BreakIt=0 for k in range(0,len(listnotargs)): if listnotargs[k] in arr[i]: BreakIt=1 break if BreakIt==1: break else: src+=";" break ################################################ src+="\n" ##########post replacing############ src=src.replace("def","float") ############################# print src
float durchmesser(float u) { return u/pi; } float lulu(float aba,float test) { if(aba>0) { aba=1; } for(int i=0;i<10;i++) { test+=i; } return test; } float planetenoffset(float ubase,float uadd) { uadd+=ubase; return (durchmesser(uadd)-durchmesser(ubase))/2.0; } //plapla