大家好,小编为大家解答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<255 self.centery="i256" temp1="temp2257</p">
258 def unitify(self,row,column): #row ~ list
259 center=self.List[row][column]260 for i in range(11):261 self.List[row][i]/=center262 defstep(self,list1,list2,column):263 rate=self.List[list2][column]/self.List[list1][column]264 for i in range(11):265 self.List[list2][i]-=rate*self.List[list1][i]266 defshow(self):267 for i in range(self.row+1):268 for j in range(11):269 self.entrys[i][j].setText(self.List[i][j].show())270 defrunOneTime(self):271 p=self.win.getMouse()272 if 56
Rational(0,1):277 flag=0278 for i in range(1,1+self.row,1):279 if self.List[i][self.centerX]>Rational(0,1):280 flag=1255>281 break
282 if flag==0:283 self.show() #change numbers in the self.Entrys
284 self.t.setText('Unbounded Solution')285 self.t.draw(self.win)286 sleep(2)287 self.t.undraw()288 break
289 else:290 self.findXY(temp)291 self.unitify(self.centerY,self.centerX)292 for i in range(0,self.row+1,1):293 if i!=self.centerY:294 self.step(self.centerY,i,self.centerX)295 #self.printList()
296 else:297 self.show()298 self.getInfo()299 self.processInfo() #change sentence in the InfoBar
300 break
301 defrun(self):302 while 1:303 self.runOneTime()304
305 '''__________________________________class Button___________________________'''
306 classButton:307 def __init__(self, win, center, width, height, label,color='lightgray'):308 w,h = width/2.0, height/2.0
309 x,y =center.getX(), center.getY()310 self.xmax, self.xmin = x+w, x-w311 self.ymax, self.ymin = y+h, y-h312 p1 =Point(self.xmin, self.ymin)313 p2 =Point(self.xmax, self.ymax)314 self.color=color315 self.rect =Rectangle(p1,p2)316 self.rect.setFill(self.color)317 self.rect.draw(win)318 self.label =Text(center, label)319 self.label.draw(win)320 self.deactivate()321 defclicked(self, p):322 return self.active and\323 self.xmin <= p.getX() <= self.xmax and\324 self.ymin <= p.getY() <=self.ymax325
326 defgetLabel(self):327 returnself.label.getText()328
329 defactivate(self):330 self.label.setFill('black')331 self.rect.setWidth(2)332 self.active = 1
333
334 defdeactivate(self):335 self.label.setFill('slategrey')336 self.rect.setWidth(1)337 self.active =0338
339 '''_____________________________class Painting_______________________________'''
340 classPainting:341 def __init__(self):342 self.win=GraphWin("Painting here ~~",600,600)343 self.win.setCoords(-1,-0.6,8,8)344 self.entry=Entry(Point(4.5,7.5),28)345 self.entry.setTextColor('purple')346 self.entry.setStyle('italic')347 self.entry.setSize(20)348 self.entry.draw(self.win)349 self.t=Text(Point(0.8,7.5),"f(x)=")350 self.t.setSize(20)351 self.t.draw(self.win)352 self.t1=Text(Point(1,6.5),"x from")353 self.t1.setSize(20)354 self.t1.draw(self.win)355 self.t1_entry=Entry(Point(2.5,6.5),6)356 self.t1_entry.setTextColor('purple')357 self.t1_entry.setSize(20)358 self.t1_entry.draw(self.win)359 self.t2=Text(Point(3.7,6.5),'to')360 self.t2.setSize(20)361 self.t2.draw(self.win)362 self.t2_entry=Entry(Point(5,6.5),6)363 self.t2_entry.setTextColor('purple')364 self.t2_entry.setSize(20)365 self.t2_entry.draw(self.win)366
367 self.l=Line(Point(0,6),Point(8,6))368 self.l.draw(self.win)369 self.button=Rectangle(Point(6,6.2),Point(7.6,6.8))370 self.button.setFill('pink')371 self.button.draw(self.win)372 self.button_text=Text(Point(6.8,6.5),'Plot')373 self.button_text.draw(self.win)374
375 line1=Line(Point(0,0),Point(0,5.9))376 line1.setArrow('last')377 line1.draw(self.win)378 line2=Line(Point(0,0),Point(7.9,0))379 line2.setArrow('last')380 line2.draw(self.win)381 for i in range(10):382 t=Text(Point(i*0.8,0),'|')383 t.draw(self.win)384 for i in range(10):385 t=Text(Point(0,0.6*i),'-')386 t.draw(self.win)387
388 defgetNum(self):389 self.s=self.entry.getText()390 self.lhs=eval(self.t1_entry.getText())391 self.rhs=eval(self.t2_entry.getText())392 defprocessNum(self):393 self.d=self.rhs-self.lhs394 self.list1=[]395 self.list2=[]396 for i in range(1000):397 self.list1.append(self.lhs+self.d*i/1000)398 for i in range(1000):399 x=self.list1[i]400 self.list2.append(eval(self.s))401 self.min=min(self.list2)402 self.max=max(self.list2)403 rate1=8/(self.rhs-self.lhs)404 rate2=6/(self.max-self.min)405 self.list11=[]406 self.list22=[]407 for i inself.list1:408 self.list11.append((i-self.lhs)*rate1)409 for i inself.list2:410 self.list22.append((i-self.min)*rate2)411 defpaintNum(self):412 for i in range(1000):413 p=Point(self.list11[i],self.list22[i])414 p.draw(self.win)415 for i in range(10):416 t=Text(Point(i*0.8,-0.25),str(round(self.lhs+(self.rhs-self.lhs)*i/10,3)))417 t.setSize(11)418 t.draw(self.win)419 for i in range(10):420 t=Text(Point(-0.4,0.6*i),str(round(self.min+(self.max-self.min)*i/10,3)))421 t.setSize(11)422 t.draw(self.win)423
424 defrun(self):425 while 1:426 pp=self.win.getMouse()427 if 6
431 try:432 self.getNum()433 self.processNum()434 self.paintNum()435 except:436 t=Text(Point(4,3),'SomeThing Error')437 t.setSize(20)438 t.setTextColor('red')439 t.draw(self.win)440 sleep(2)441 t.undraw()442
443 '''___________________________class Calculator_______________________________'''
444 classCalculator:445 def __init__(self):446 win = GraphWin("calculator",440,600)447 win.setCoords(0,-2,7,11)448 win.setBackground("slategray")449 self.win =win450 self.__createButtons()451 self.__createDisplay()452 globalans453 defsetAns(a):454 ans=deepcopy(a)455 def __createButtons(self):456 buttonSpecs = [(1,1,'0','#c65911'),(2,1,'.','#c65911'),(3,1,',','#c65911'),(4,1,'pi','#ffe699'),(5,1,'e','#ffe699'),(6,1,'j','#ffe699'),457 (1,2,'1','#c65911'),(2,2,'2','#c65911'),(3,2,'3','#c65911'),(4,2,'DEL','#ffe699'),(5,2,'AC','#ffe699'),(6,2,'ans','#ffe699'),458 (1,3,'4','#c65911'),(2,3,'5','#c65911'),(3,3,'6','#c65911'),(4,3,'+','#aeaaaa'),(5,3,'-','#aeaaaa'),(6,3,'/','#aeaaaa'),459 (1,4,'7','#c65911'),(2,4,'8','#c65911'),(3,4,'9','#c65911'),(4,4,'*','#aeaaaa'),(5,4,'**','#aeaaaa'),(6,4,'%','#aeaaaa'),460 (1,5,'fabs','#a9d08e'),(2,5,'ceil','#a9d08e'),(3,5,'floor','#a9d08e'),(4,5,'sqrt','#a9d08e'),(5,5,'(','#a9d08e'),(6,5,')','#a9d08e'),461 (1,6,'rand','#a9d08e'),(2,6,'stat','#a9d08e'),(3,6,'max','#a9d08e'),(4,6,'ln','#a9d08e'),(5,6,'lg','#a9d08e'),(6,6,']','#a9d08e'),462 (1,7,'sin','#00b0f0'),(2,7,'cos','#00b0f0'),(3,7,'tan','#00b0f0'),(4,7,'sinh','#00b050'),(5,7,'cosh','#00b050'),(6,7,'tanh','#00b050'),463 (1,8,'asin','#00b0f0'),(2,8,'acos','#00b0f0'),(3,8,'atan','#00b0f0'),(4,8,'ash','#00b050'),(5,8,'ach','#00b050'),(6,8,'ath','#00b050')464 ]465 self.buttons =[]466 for cx,cy,label,color inbuttonSpecs:467 self.buttons.append(Button(self.win,Point(cx,cy),1,1,label,color))468 self.buttons.append(Button(self.win,Point(3.5,0),6,1,"=",'#806000'))469 self.buttons.append(Button(self.win,Point(2,-1),3,1,"plot",'pink'))470 self.buttons.append(Button(self.win,Point(5,-1),3,1,"Simplex Method",'orange'))471 for b inself.buttons:472 b.activate()473
474 def __createDisplay(self):475 bg = Rectangle(Point(0.5,8.5), Point(6.5,10.5))476 bg.setFill('white')477 bg.draw(self.win)478 text = Text(Point(3.5,9.5), "")479 text.draw(self.win)480 text.setFace("courier")481 text.setStyle("bold")482 text.setSize(16)483 self.display =text484
485 defgetKeyPress(self):486 while 1:487 p =self.win.getMouse()488 for b inself.buttons:489 ifb.clicked(p):490 returnb.getLabel()491 defprocessKey(self, key):492 globalans493 text =self.display.getText()494 if key == 'AC':495 self.display.setText("")496 elif key in ['fabs','ceil','floor','sqrt','max','sin','cos','tan','sinh','cosh','tanh','asin','acos','atan']:497 self.display.setText(text+key+'(')498 elif key == 'stat':499 self.display.setText(text+'stat([')500 elif key == 'rand':501 self.display.setText(text+'random()')502 elif key == 'ln':503 self.display.setText(text+'log(')504 elif key == 'lg':505 self.display.setText(text+'log10(')506 elif key == 'ash':507 self.display.setText(text+'asinh(')508 elif key == 'ach':509 self.display.setText(text+'acosh(')510 elif key == 'ath':511 self.display.setText(text+'atanh(')512 elif key == 'DEL':513 self.display.setText(text[:-1])514 elif key == 'plot':515 self.win.close()516 paintor=Painting()517 paintor.run()518 elif key == 'Simplex Method':519 self.win.close()520 ms=iMatrix()521 ms.run()522 elif key == '=':523 try:524 result =eval(text)525 ans=result526 except:527 result = 'ERROR'
528 self.display.setText(str(result))529 else:530 self.display.setText(text+key)531
532 defrun(self):533 while 1:534 key =self.getKeyPress()535 self.processKey(key)536 '''
537 def main():538 theCalc = Calculator()539 theCalc.run()540 main()541 '''
542 theCalc=Calculator()543 theCalc.run()
1563

被折叠的 条评论
为什么被折叠?



