Browse Source

python打印阶乘及所有组合

duh 6 years ago
parent
commit
9e2edbc0ce
4 changed files with 85 additions and 0 deletions
  1. 14 0
      phxb/elab/utils/GetAllModule.py
  2. 5 0
      phxb/elab/utils/GetArray.py
  3. 41 0
      phxb/elab/utils/GetArray2.py
  4. 25 0
      phxb/test.py

+ 14 - 0
phxb/elab/utils/GetAllModule.py

@@ -0,0 +1,14 @@
+# -*- coding: UTF-8 -*-
+from pyautocad import Autocad, APoint
+
+#这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
+acad = Autocad(create_if_not_exists = True)
+acad.prompt("Hello, Autocad from Python\n")
+print(acad.doc.Name)
+
+#遍历cad图形对象
+for obj in acad.iter_objects():
+    print(obj.ObjectName)
+#按类型查找出所有某种图元
+for text in acad.iter_objects('Text'):
+    print(text.TextString, text.InsertionPoint)

+ 5 - 0
phxb/elab/utils/GetArray.py

@@ -0,0 +1,5 @@
+def getArray(n):
+  if(n<=1):
+    return n;
+  else:
+    return n*getArray(n-1);

+ 41 - 0
phxb/elab/utils/GetArray2.py

@@ -0,0 +1,41 @@
+import math
+def jieChen(n):
+    myArray = {};
+    myArray[0] = 1;
+    i = 1;
+    while i<n :
+        myArray[i] = (i+1)*myArray[i-1];
+        i = i+1
+    return myArray;
+#辗转相除法进行进制转化
+def changeInt2Str(i,m):
+    if(not isinstance(m,int)):
+        print("m is not int",m)
+        return
+    if(m*i == 0):
+        return "0";
+    result = "";i
+    myarray = {};
+    while(i>=m):
+        myarray[myarray.__len__()] = i%m
+        i=int(i/m)
+    if(i!=0):
+        myarray[myarray.__len__()] = i
+    for item in myarray :
+        result=str(myarray[item])+result
+    return result
+#获取组合数
+def getAllArray(m,n):
+    allArray = {}
+    max = math.pow(m,n)
+    index = 0;
+    while (index < max):
+        itemIndex = changeInt2Str(index,m)
+        while(itemIndex.__len__()<n):
+            itemIndex = "0"+itemIndex
+        allArray[index]=itemIndex
+        index = index+1
+    print(allArray)
+# result = changeInt2Str(14,3);
+# print(result)
+getAllArray(2,3)

+ 25 - 0
phxb/test.py

@@ -0,0 +1,25 @@
+# -*- coding: UTF-8 -*-
+from pyautocad import Autocad, APoint
+
+#这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
+acad = Autocad(create_if_not_exists = True)
+acad.prompt("Hello, Autocad from Python\n")
+print(acad.doc.Name)
+
+p1 = APoint(0, 0)#点的位置坐标
+p2 = APoint(50, 25)
+for i in range(5):
+    text = acad.model.AddText('大家好 %s!' % i, p1, 1.5)#添加文本
+    acad.model.AddLine(p1, p2)#添加线
+    acad.model.AddCircle(p1, 10)#添加圆
+    p1.y += 10
+
+dp = APoint(10, 0)
+#打印点信息
+for text in acad.iter_objects('Text'):
+    print('text: %s at: %s' % (text.TextString, text.InsertionPoint))
+    text.InsertionPoint = APoint(text.InsertionPoint) + dp
+
+#打印圆,线名称
+for obj in acad.iter_objects(['Circle', 'Line']):
+    print(obj.ObjectName)