Python大作业_计算机&作图&单纯形法
2013-06-24 22:39
344 查看
先看代码
程序分为三个部分,打开程序后先进入Calculator界面,然后可以选择进入另外两个
l Calculator(科学计算器)
l Plot(作图)
l Simplex Method(单纯形法解决运筹学问题)
具体的程序说明见http://wl.ibox.sjtu.edu.cn/w/8967O6X0p47978y3/ReadMe.docx
2013-06-2422:38:08
# -*- coding: cp936 -*- ''' 使用单纯形法时,采用现在的这种头文件,不能出现from __future__ import division 使用计算器或画图时,务必把from __future__ import division 前面的#拿掉 如果不想用复数计算,可以载入math,屏蔽cmath,此时plot里面的sin()不用写出m.sin() 如果要复数计算,和前一行所说的相反 屏蔽头文件的方法,加#;载入,则取消# ''' #calculator plot SM from __future__ import division # 1 1 0 from math import* # 0(推荐) 1 x #from cmath import* # 1(推荐) 0 x from graphics import * from math import fabs,ceil,floor import math as m from random import random from copy import deepcopy from time import sleep def stat(ilist): ave=0 s=0 s2=0 n=len(ilist) for i in ilist: ave=ave+i/n for i in ilist: s2=(i-ave)**2/(n-1) s=sqrt(s2) return 'ave='+str(ave)+'\n'+'s='+str(s) '''________________________________class Rational___________________________''' class Rational: def __init__(self,num,den): if num%1!=0 or den%1!=0: exit(1) self.num=num self.den=den self.__RF() def __RF(self): if self.num==0: self.den=1 temp=min(abs(self.num),abs(self.den)) while temp>1: if self.num%temp==0 and self.den%temp==0: self.num=self.num/temp self.den=self.den/temp temp=temp-1 if self.den<0: self.num=-self.num self.den=-self.den def show(self): if self.den!=1: return str(self.num)+'/'+str(self.den) else: return str(self.num) def __add__(self,other): return Rational(self.num*other.den+self.den*other.num,self.den*other.den) def __sub__(self,other): return Rational(self.num*other.den-self.den*other.num,self.den*other.den) def __mul__(self,other): return Rational(self.num*other.num,self.den*other.den) def __div__(self,other): if other==Rational(0,1): exit(1) else: return Rational(self.num*other.den,self.den*other.num) def __pow__(self,n): if n%1==0: return Rational(self.num**n,self.den**n) else: print "Number Error" def floatify(self): #for compare return 1.0*self.num/self.den def __gt__(self,other): return self.floatify() > other.floatify() def __lt__(self,other): return self.floatify() < other.floatify() def __ge__(self,other): return self.floatify() >= other.floatify() def __le__(self,other): return self.floatify() <= other.floatify() def __eq__(self,other): return self.floatify() == other.floatify() def __ne__(self,other): return not self == other def __iaddr__(self,other): self=self+other def __isubr__(self,other): self=self-other def __imulr__(self,other): self=self*other def __idivr__(self,other): self=self/other def __ipowr__(self,other): self=self**other '''_________________________________class iMatrix___________________________''' class iMatrix: def __init__(self): self.win=GraphWin('simplex method',1095,410) #1095 410 self.win.setBackground('#8497b0') self.win.setCoords(0,0,61,28) self.__createEntrys() self.__createButton() self.__createList() self.__createInfoBar() self.__createInfoEntrys() self.t=Text(Point(28,26),'') self.t.setTextColor('red') self.t.setSize(20) def __createInfoEntrys(self): self.InfoEntrys=[] for i in range(1,11,1): self.InfoEntrys.append(Entry(Point(-2+5*i,23.5),2)) for i in range(10): self.InfoEntrys[i].draw(self.win) def __createInfoBar(self): self.InfoBox=Rectangle(Point(1,25),Point(5,27)) self.InfoBox.setFill('red') self.InfoBox.draw(self.win) self.InfoBoxText=Text(Point(3,26),'Info') self.InfoBoxText.draw(self.win) self.InfoBar=Rectangle(Point(6,25),Point(50,27)) self.InfoBar.setFill('white') self.Info=Text(Point(28,26),'Information') self.Info.setTextColor('#d0cece') self.Info.setSize(25) self.InfoBar.draw(self.win) self.Info.draw(self.win) def __createEntrys(self): self.entrys=[] for i in range(11): self.entrys.append([]) for j in range(11): self.entrys[i].append(Entry(Point(3+5*j,1.5+2*i),8)) for i in range(10): self.entrys[0][i].setTextColor('red') for i in range(10): self.entrys[i+1][10].setTextColor('purple') self.entrys[0][10].setTextColor('blue') for i in range(11): for j in range(11): self.entrys[i][j].setText('0') self.entrys[i][j].draw(self.win) def __createButton(self): self.button=Rectangle(Point(56,1),Point(60,22)) self.button.setFill('green') self.button.draw(self.win) def __createList(self): self.List=[] for i in range(11): self.List.append([]) for j in range(11): self.List[i].append(0) def getInfo(self): self.InfoList=[] for i in range(10): if self.InfoEntrys[i].getText()!='': self.InfoList.append(eval(self.InfoEntrys[i].getText())) else: self.InfoList.append(0) def processInfo(self): for i in range(10): if self.InfoList[i]>0: if self.List[0][i]!=Rational(0,1): n_1=0 n_0=0 for j in range(1,self.row+1,1): if self.List[j][i]==Rational(0,1): n_0=n_0+1 if self.List[j][i]==Rational(1,1): n_1=n_1+1 if n_1==1 and n_0==self.row-1: self.t.setText('No solution!') self.t.draw(self.win) sleep(2) self.t.undraw(win) break flag=0 for i in range(10): if self.List[0][i]==Rational(0,1) and (not(i in self.BlankColumn)): n_1=0 n_0=0 for j in range(1,self.row+1,1): if self.List[j][i]==Rational(0,1): n_0=n_0+1 if self.List[j][i]==Rational(1,1): n_1=n_1+1 if n_1!=1 or n_0!=self.row-1: self.t.setText('Infinity solutions!') self.t.draw(self.win) sleep(2) self.t.undraw() flag=1 break if flag==0: self.t.setText('One solution') self.t.draw(self.win) sleep(2) self.t.undraw() def getNum(self): for i in range(11): for j in range(11): temp=self.entrys[i][j].getText() if temp=='': self.List[i][j]=Rational(0,1) elif '/' in temp: n=temp.index('/') a=eval(temp[:n]) b=eval(temp[n+1:]) self.List[i][j]=Rational(a,b) else: self.List[i][j]=Rational(eval(temp),1) self.row=10 for i in range(10,0,-1): if max(self.List[i])==Rational(0,1) and min(self.List[i])==Rational(0,1): self.row=i-1 else: break if self.row==0: exit(1) self.BlankColumn=[] for j in range(10): flag=0 for i in range(11): if self.List[i][j]!=Rational(0,1): flag=flag+1 if flag==0: self.BlankColumn.append(j) #self.column=len(self.BlankColumn) def printList(self): for i in range(self.row,-1,-1): print '\n' for j in range(11): if not (j in self.BlankColumn): print '%8s' %(self.List[i][j].show()), def whether_0(self): temp=max(self.List[0][0:10]) self.centerX=self.List[0].index(temp) return(temp) def findXY(self,delta): self.centerX=self.List[0].index(delta) for i in range(1,self.row+1,1): if self.List[i][self.centerX]>Rational(0,1): self.centerY=i temp1=self.List[i][10]/self.List[i][self.centerX] break for i in range(1,self.row+1,1): if self.List[i][self.centerX]>Rational(0,1): temp2=self.List[i][10]/self.List[i][self.centerX] if temp2<temp1: self.centerY=i temp1=temp2 def unitify(self,row,column): # row ~ list center=self.List[row][column] for i in range(11): self.List[row][i]/=center def step(self,list1,list2,column): rate=self.List[list2][column]/self.List[list1][column] for i in range(11): self.List[list2][i]-=rate*self.List[list1][i] def show(self): for i in range(self.row+1): for j in range(11): self.entrys[i][j].setText(self.List[i][j].show()) def runOneTime(self): p=self.win.getMouse() if 56<p.getX()<60 and 1<p.getY()<22: self.getNum() while 1: temp=self.whether_0() if temp>Rational(0,1): flag=0 for i in range(1,1+self.row,1): if self.List[i][self.centerX]>Rational(0,1): flag=1 break if flag==0: self.show() #change numbers in the self.Entrys self.t.setText('Unbounded Solution') self.t.draw(self.win) sleep(2) self.t.undraw() break else: self.findXY(temp) self.unitify(self.centerY,self.centerX) for i in range(0,self.row+1,1): if i!=self.centerY: self.step(self.centerY,i,self.centerX) #self.printList() else: self.show() self.getInfo() self.processInfo() #change sentence in the InfoBar break def run(self): while 1: self.runOneTime() '''__________________________________class Button___________________________''' class Button: def __init__(self, win, center, width, height, label,color='lightgray'): w,h = width/2.0, height/2.0 x,y = center.getX(), center.getY() self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.color=color self.rect = Rectangle(p1,p2) self.rect.setFill(self.color) self.rect.draw(win) self.label = Text(center, label) self.label.draw(win) self.deactivate() def clicked(self, p): return self.active and \ self.xmin <= p.getX() <= self.xmax and \ self.ymin <= p.getY() <= self.ymax def getLabel(self): return self.label.getText() def activate(self): self.label.setFill('black') self.rect.setWidth(2) self.active = 1 def deactivate(self): self.label.setFill('slategrey') self.rect.setWidth(1) self.active = 0 '''_____________________________class Painting_______________________________''' class Painting: def __init__(self): self.win=GraphWin("Painting here ~~",600,600) self.win.setCoords(-1,-0.6,8,8) self.entry=Entry(Point(4.5,7.5),28) self.entry.setTextColor('purple') self.entry.setStyle('italic') self.entry.setSize(20) self.entry.draw(self.win) self.t=Text(Point(0.8,7.5),"f(x)=") self.t.setSize(20) self.t.draw(self.win) self.t1=Text(Point(1,6.5),"x from") self.t1.setSize(20) self.t1.draw(self.win) self.t1_entry=Entry(Point(2.5,6.5),6) self.t1_entry.setTextColor('purple') self.t1_entry.setSize(20) self.t1_entry.draw(self.win) self.t2=Text(Point(3.7,6.5),'to') self.t2.setSize(20) self.t2.draw(self.win) self.t2_entry=Entry(Point(5,6.5),6) self.t2_entry.setTextColor('purple') self.t2_entry.setSize(20) self.t2_entry.draw(self.win) self.l=Line(Point(0,6),Point(8,6)) self.l.draw(self.win) self.button=Rectangle(Point(6,6.2),Point(7.6,6.8)) self.button.setFill('pink') self.button.draw(self.win) self.button_text=Text(Point(6.8,6.5),'Plot') self.button_text.draw(self.win) line1=Line(Point(0,0),Point(0,5.9)) line1.setArrow('last') line1.draw(self.win) line2=Line(Point(0,0),Point(7.9,0)) line2.setArrow('last') line2.draw(self.win) for i in range(10): t=Text(Point(i*0.8,0),'|') t.draw(self.win) for i in range(10): t=Text(Point(0,0.6*i),'-') t.draw(self.win) def getNum(self): self.s=self.entry.getText() self.lhs=eval(self.t1_entry.getText()) self.rhs=eval(self.t2_entry.getText()) def processNum(self): self.d=self.rhs-self.lhs self.list1=[] self.list2=[] for i in range(1000): self.list1.append(self.lhs+self.d*i/1000) for i in range(1000): x=self.list1[i] self.list2.append(eval(self.s)) self.min=min(self.list2) self.max=max(self.list2) rate1=8/(self.rhs-self.lhs) rate2=6/(self.max-self.min) self.list11=[] self.list22=[] for i in self.list1: self.list11.append((i-self.lhs)*rate1) for i in self.list2: self.list22.append((i-self.min)*rate2) def paintNum(self): for i in range(1000): p=Point(self.list11[i],self.list22[i]) p.draw(self.win) for i in range(10): t=Text(Point(i*0.8,-0.25),str(round(self.lhs+(self.rhs-self.lhs)*i/10,3))) t.setSize(11) t.draw(self.win) for i in range(10): t=Text(Point(-0.4,0.6*i),str(round(self.min+(self.max-self.min)*i/10,3))) t.setSize(11) t.draw(self.win) def run(self): while 1: pp=self.win.getMouse() if 6<pp.getX()<7.6 and 6.2<pp.getY()<6.8: '''self.getNum() self.processNum() self.paintNum()''' try: self.getNum() self.processNum() self.paintNum() except: t=Text(Point(4,3),'SomeThing Error') t.setSize(20) t.setTextColor('red') t.draw(self.win) sleep(2) t.undraw() '''___________________________class Calculator_______________________________''' class Calculator: def __init__(self): win = GraphWin("calculator",440,600) win.setCoords(0,-2,7,11) win.setBackground("slategray") self.win = win self.__createButtons() self.__createDisplay() global ans def setAns(a): ans=deepcopy(a) def __createButtons(self): buttonSpecs = [(1,1,'0','#c65911'),(2,1,'.','#c65911'),(3,1,',','#c65911'),(4,1,'pi','#ffe699'),(5,1,'e','#ffe699'),(6,1,'j','#ffe699'), (1,2,'1','#c65911'),(2,2,'2','#c65911'),(3,2,'3','#c65911'),(4,2,'DEL','#ffe699'),(5,2,'AC','#ffe699'),(6,2,'ans','#ffe699'), (1,3,'4','#c65911'),(2,3,'5','#c65911'),(3,3,'6','#c65911'),(4,3,'+','#aeaaaa'),(5,3,'-','#aeaaaa'),(6,3,'/','#aeaaaa'), (1,4,'7','#c65911'),(2,4,'8','#c65911'),(3,4,'9','#c65911'),(4,4,'*','#aeaaaa'),(5,4,'**','#aeaaaa'),(6,4,'%','#aeaaaa'), (1,5,'fabs','#a9d08e'),(2,5,'ceil','#a9d08e'),(3,5,'floor','#a9d08e'),(4,5,'sqrt','#a9d08e'),(5,5,'(','#a9d08e'),(6,5,')','#a9d08e'), (1,6,'rand','#a9d08e'),(2,6,'stat','#a9d08e'),(3,6,'max','#a9d08e'),(4,6,'ln','#a9d08e'),(5,6,'lg','#a9d08e'),(6,6,']','#a9d08e'), (1,7,'sin','#00b0f0'),(2,7,'cos','#00b0f0'),(3,7,'tan','#00b0f0'),(4,7,'sinh','#00b050'),(5,7,'cosh','#00b050'),(6,7,'tanh','#00b050'), (1,8,'asin','#00b0f0'),(2,8,'acos','#00b0f0'),(3,8,'atan','#00b0f0'),(4,8,'ash','#00b050'),(5,8,'ach','#00b050'),(6,8,'ath','#00b050') ] self.buttons = [] for cx,cy,label,color in buttonSpecs: self.buttons.append(Button(self.win,Point(cx,cy),1,1,label,color)) self.buttons.append(Button(self.win,Point(3.5,0),6,1,"=",'#806000')) self.buttons.append(Button(self.win,Point(2,-1),3,1,"plot",'pink')) self.buttons.append(Button(self.win,Point(5,-1),3,1,"Simplex Method",'orange')) for b in self.buttons: b.activate() def __createDisplay(self): bg = Rectangle(Point(0.5,8.5), Point(6.5,10.5)) bg.setFill('white') bg.draw(self.win) text = Text(Point(3.5,9.5), "") text.draw(self.win) text.setFace("courier") text.setStyle("bold") text.setSize(16) self.display = text def getKeyPress(self): while 1: p = self.win.getMouse() for b in self.buttons: if b.clicked(p): return b.getLabel() def processKey(self, key): global ans text = self.display.getText() if key == 'AC': self.display.setText("") elif key in ['fabs','ceil','floor','sqrt','max','sin','cos','tan','sinh','cosh','tanh','asin','acos','atan']: self.display.setText(text+key+'(') elif key == 'stat': self.display.setText(text+'stat([') elif key == 'rand': self.display.setText(text+'random()') elif key == 'ln': self.display.setText(text+'log(') elif key == 'lg': self.display.setText(text+'log10(') elif key == 'ash': self.display.setText(text+'asinh(') elif key == 'ach': self.display.setText(text+'acosh(') elif key == 'ath': self.display.setText(text+'atanh(') elif key == 'DEL': self.display.setText(text[:-1]) elif key == 'plot': self.win.close() paintor=Painting() paintor.run() elif key == 'Simplex Method': self.win.close() ms=iMatrix() ms.run() elif key == '=': try: result = eval(text) ans=result except: result = 'ERROR' self.display.setText(str(result)) else: self.display.setText(text+key) def run(self): while 1: key = self.getKeyPress() self.processKey(key) ''' def main(): theCalc = Calculator() theCalc.run() main() ''' theCalc=Calculator() theCalc.run()
程序分为三个部分,打开程序后先进入Calculator界面,然后可以选择进入另外两个
l Calculator(科学计算器)
l Plot(作图)
l Simplex Method(单纯形法解决运筹学问题)
具体的程序说明见http://wl.ibox.sjtu.edu.cn/w/8967O6X0p47978y3/ReadMe.docx
2013-06-2422:38:08
相关文章推荐
- python & 机器学习 & 计算机视觉 & 深度学习
- CS231n 课后作业 虚拟环境的建立及使用(win10 & python 3)
- 第1章 Python基础-Python介绍&循环语句 练习题&作业
- 计算机图形学作业 - 运用Python-OpenGL设计一个带键盘控制的三维模型
- 计算机图形学作业-运用数值微分法和中点作圆法使用Python的OpenGL库画出时钟
- 利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)
- 第2章 Python基础-字符编码&数据类型 购物车&多级菜单 作业
- 计算机视觉经典论文集&此资源批量分类下载的Python程序
- &lt;&lt;深入理解计算机系统&gt;&gt;家庭作业3.38, 分析全过程
- Windows下Python Web开发环境搭建 (Notepad++ & Web.py)
- Python cx_Oracle & Oracle Client for Mac OS
- 【剑指Offer】二维数组中的查找 解题报告(Java & Python)
- python中if __name__ == '__main__': 的解析
- python笔记 :模拟登录、三级菜单、购物车作业
- python-&gt;FTP上传下载文件
- 使用python numpy进行SVD分解
- Python 迭代器 & __iter__方法
- Python第五天课后作业
- Python & PyCharm & Django 搭建web开发环境
- urllib&urllib2库--python2.X(学习日记2)