python绘制图像程序设计大作业_Python大作业_计算机&作图&单纯形法

1 #-*- coding: cp936 -*-

2 '''

3 使用单纯形法时,采用现在的这种头文件,不能出现from __future__ import division4 使用计算器或画图时,务必把from __future__ import division 前面的#拿掉5 如果不想用复数计算,可以载入math,屏蔽cmath,此时plot里面的sin()不用写出m.sin()6 如果要复数计算,和前一行所说的相反7 屏蔽头文件的方法,加#;载入,则取消#8 ''' #calculator plot SM

9 from __future__ import division #1 1 0

10 from math import* #0(推荐) 1 x

11 #from cmath import* # 1(推荐) 0 x

12

13 from graphics import *

14 from math importfabs,ceil,floor15 importmath as m16 from random importrandom17 from copy importdeepcopy18 from time importsleep19

20

21 defstat(ilist):22 ave=023 s=024 s2=025 n=len(ilist)26 for i inilist:27 ave=ave+i/n28 for i inilist:29 s2=(i-ave)**2/(n-1)30 s=sqrt(s2)31 return 'ave='+str(ave)+'\n'+'s='+str(s)32

33

34 '''________________________________class Rational___________________________'''

35 classRational:36 def __init__(self,num,den):37 if num%1!=0 or den%1!=0:38 exit(1)39 self.num=num40 self.den=den41 self.__RF()42 def __RF(self):43 if self.num==0:44 self.den=1

45 temp=min(abs(self.num),abs(self.den))46 while temp>1:47 if self.num%temp==0 and self.den%temp==0:48 self.num=self.num/temp49 self.den=self.den/temp50 temp=temp-1

51 if self.den<0:52 self.num=-self.num53 self.den=-self.den54 defshow(self):55 if self.den!=1:56 return str(self.num)+'/'+str(self.den)57 else:58 returnstr(self.num)59 def __add__(self,other):60 return Rational(self.num*other.den+self.den*other.num,self.den*other.den)61 def __sub__(self,other):62 return Rational(self.num*other.den-self.den*other.num,self.den*other.den)63 def __mul__(self,other):64 return Rational(self.num*other.num,self.den*other.den)65 def __div__(self,other):66 if other==Rational(0,1):67 exit(1)68 else:69 return Rational(self.num*other.den,self.den*other.num)70 def __pow__(self,n):71 if n%1==0:72 return Rational(self.num**n,self.den**n)73 else:74 print "Number Error"

75 def floatify(self): #for compare

76 return 1.0*self.num/self.den77 def __gt__(self,other):78 return self.floatify() >other.floatify()79 def __lt__(self,other):80 return self.floatify() =other.floatify()83 def __le__(self,other):84 return self.floatify() <=other.floatify()85 def __eq__(self,other):86 return self.floatify() ==other.floatify()87 def __ne__(self,other):88 return not self ==other89 def __iaddr__(self,other):90 self=self+other91 def __isubr__(self,other):92 self=self-other93 def __imulr__(self,other):94 self=self*other95 def __idivr__(self,other):96 self=self/other97 def __ipowr__(self,other):98 self=self**other99 '''_________________________________class iMatrix___________________________'''

100 classiMatrix:101 def __init__(self):102 self.win=GraphWin('simplex method',1095,410) #1095 410

103 self.win.setBackground('#8497b0')104 self.win.setCoords(0,0,61,28)105 self.__createEntrys()106 self.__createButton()107 self.__createList()108 self.__createInfoBar()109 self.__createInfoEntrys()110 self.t=Text(Point(28,26),'')111 self.t.setTextColor('red')112 self.t.setSize(20)113 def __createInfoEntrys(self):114 self.InfoEntrys=[]115 for i in range(1,11,1):116 self.InfoEntrys.append(Entry(Point(-2+5*i,23.5),2))117 for i in range(10):118 self.InfoEntrys[i].draw(self.win)119 def __createInfoBar(self):120 self.InfoBox=Rectangle(Point(1,25),Point(5,27))121 self.InfoBox.setFill('red')122 self.InfoBox.draw(self.win)123 self.InfoBoxText=Text(Point(3,26),'Info')124 self.InfoBoxText.draw(self.win)125 self.InfoBar=Rectangle(Point(6,25),Point(50,27))126 self.InfoBar.setFill('white')127 self.Info=Text(Point(28,26),'Information')128 self.Info.setTextColor('#d0cece')129 self.Info.setSize(25)130 self.InfoBar.draw(self.win)131 self.Info.draw(self.win)132 def __createEntrys(self):133 self.entrys=[]134 for i in range(11):135 self.entrys.append([])136 for j in range(11):137 self.entrys[i].append(Entry(Point(3+5*j,1.5+2*i),8))138 for i in range(10):139 self.entrys[0][i].setTextColor('red')140 for i in range(10):141 self.entrys[i+1][10].setTextColor('purple')142 self.entrys[0][10].setTextColor('blue')143 for i in range(11):144 for j in range(11):145 self.entrys[i][j].setText('0')146 self.entrys[i][j].draw(self.win)147 def __createButton(self):148 self.button=Rectangle(Point(56,1),Point(60,22))149 self.button.setFill('green')150 self.button.draw(self.win)151 def __createList(self):152 self.List=[]153 for i in range(11):154 self.List.append([])155 for j in range(11):156 self.List[i].append(0)157 defgetInfo(self):158 self.InfoList=[]159 for i in range(10):160 if self.InfoEntrys[i].getText()!='':161 self.InfoList.append(eval(self.InfoEntrys[i].getText()))162 else:163 self.InfoList.append(0)164 defprocessInfo(self):165 for i in range(10):166 if self.InfoList[i]>0:167 if self.List[0][i]!=Rational(0,1):168 n_1=0169 n_0=0170 for j in range(1,self.row+1,1):171 if self.List[j][i]==Rational(0,1):172 n_0=n_0+1

173 if self.List[j][i]==Rational(1,1):174 n_1=n_1+1

175 if n_1==1 and n_0==self.row-1:176 self.t.setText('No solution!')177 self.t.draw(self.win)178 sleep(2)179 self.t.undraw(win)180 break

181 flag=0182 for i in range(10):183 if self.List[0][i]==Rational(0,1) and (not(i inself.BlankColumn)):184 n_1=0185 n_0=0186 for j in range(1,self.row+1,1):187 if self.List[j][i]==Rational(0,1):188 n_0=n_0+1

189 if self.List[j][i]==Rational(1,1):190 n_1=n_1+1

191 if n_1!=1 or n_0!=self.row-1:192 self.t.setText('Infinity solutions!')193 self.t.draw(self.win)194 sleep(2)195 self.t.undraw()196 flag=1

197 break

198 if flag==0:199 self.t.setText('One solution')200 self.t.draw(self.win)201 sleep(2)202 self.t.undraw()203 defgetNum(self):204 for i in range(11):205 for j in range(11):206 temp=self.entrys[i][j].getText()207 if temp=='':208 self.List[i][j]=Rational(0,1)209 elif '/' intemp:210 n=temp.index('/')211 a=eval(temp[:n])212 b=eval(temp[n+1:])213 self.List[i][j]=Rational(a,b)214 else:215 self.List[i][j]=Rational(eval(temp),1)216 self.row=10

217 for i in range(10,0,-1):218 if max(self.List[i])==Rational(0,1) and min(self.List[i])==Rational(0,1):219 self.row=i-1

220 else:221 break

222 if self.row==0:223 exit(1)224 self.BlankColumn=[]225 for j in range(10):226 flag=0227 for i in range(11):228 if self.List[i][j]!=Rational(0,1):229 flag=flag+1

230 if flag==0:231 self.BlankColumn.append(j)232 #self.column=len(self.BlankColumn)

233 defprintList(self):234 for i in range(self.row,-1,-1):235 print '\n'

236 for j in range(11):237 if not (j inself.BlankColumn):238 print '%8s' %(self.List[i][j].show()),239

240 defwhether_0(self):241 temp=max(self.List[0][0:10])242 self.centerX=self.List[0].index(temp)243 return(temp)244 deffindXY(self,delta):245 self.centerX=self.List[0].index(delta)246 for i in range(1,self.row+1,1):247 if self.List[i][self.centerX]>Rational(0,1):248 self.centerY=i249 temp1=self.List[i][10]/self.List[i][self.centerX]250 break

251 for i in range(1,self.row+1,1):252 if self.List[i][self.centerX]>Rational(0,1):253 temp2=self.List[i][10]/self.List[i][self.centerX]254 if temp2

<<<<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python大作业 一、Turtle创意大PK 自拟题目,完成一个利用Python程序的创意绘图,采用turtle库绘图为主,不少于50行代码,可选采用其他库。 (滑稽绘制) 二、程序练习 2.1 问题描述(10分) 人们常常提到"一万小时定律",就是不管你做什么事情,只要坚持一万小时,应该都可以成为该领域的专家。那么,10000小时是多少年多少天呢? 2.2 问题描述(10分)0380031003800341590145037657 编写计算从n到m和的函数‬,函数名为sum(n,m)‬,函数返回值为n到m所有数据的和‬,使用该函数计算输入数据x,y之间所有数据的和。 2.3 问题描述(15分) 编写函数judgeTri(a,b,c),判断以参数a,b,c的值为边长能否构成三角形并判断三角形的形状;若是锐角三角形,返回R;若是直角三角形,返回Z;若是钝角三角形,返回D;若三边长不能构成三角形,返回ERROR。 2.4 问题描述(15分) 用户输入一个字符串,分别统计其中小写字母、大写字母、数字、空格和其他字符的个数,并在一行内输出小写字母、大写字母、数字、空格和其他字符的个数。 2.5 问题描述(20分) 程序的功能: (1) 使用随机库功能,生成一个包含10个不重复且小于200的正整数列表ls1,输出ls1。‬ (2) 使用列表排序方法,对ls1按奇数在前偶数在后,并且奇数之间的相对顺序不变,偶数之间的相对顺序也不变进行排序,再输出ls1。‬ (3) 使用列表排序方法,对ls1按元素字符长度降序进行排序,输出ls1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值