資源簡介
C語言-遺傳算法的排課源碼,遺傳算法、使用c語言開發(fā),源碼
代碼片段和文件信息
#!/usr/bin/python
#?This?program?is?free?software;?you?can?redistribute?it?and/or
#?modify?it?under?the?terms?of?the?GNU?General?Public?License
#?as?published?by?the?Free?Software?Foundation;?either?version?2
#?of?the?License?or?any?later?version.
#
#?This?program?is?distributed?in?the?hope?that?it?will?be?useful
#?but?WITHOUT?ANY?WARRANTY;?without?even?the?implied?warranty?of
#?MERCHANTABILITY?or?FITNESS?FOR?A?PARTICULAR?PURPOSE.??See?the
#?GNU?General?Public?License?for?more?details.
#Received?from?Vimal?Joseph?on?7?June?2004
import?xml.parsers.expat?sys?
class?Myxml:?
????Parser?=?““?
????timetable={}
????day=““
????hour=““
????hours=[]
????days=[]
????which_tt=““
????#?Prepare?for?parsing?
????def?__init__(self?xml_filename):?
????????assert?xml_filename?!=?““?
????????
????????self.xml_filename?=?xml_filename
????????self.Parser?=?xml.parsers.expat.ParserCreate(??)?
?
????????self.Parser.CharacterDataHandler?=?self.handleCharData?
????????self.Parser.StartElementHandler?=?self.handleStartElement?
????????self.Parser.EndElementHandler?=?self.handleEndElement?
?
????#?Parse?the?xml?file?
????def?parse(self):?
????????try:?
????????????xml_file?=?open(self.xml_filename?“r“)
????????except:?
????????????print?“ERROR:?Can‘t?open?xml?file?%s“%self.xml_filename?
????????????raise?
????????else:?
????????????try:?self.Parser.ParseFile(xml_file)?
????????????finally:?xml_file.close(??)?
?
????#?to?be?overridden?by?implementation-specific?methods?
????def?handleCharData(self?data):?pass
????
????def?handleStartElement(self?name?attrs):
????????if?(name==“Students_Timetable“)?or?(name==“Teachers_Timetable“):
????????????self.which_tt=name
????????????print?“\\documentclass[a4paper12pt]{article}“
????????????print?“\\usepackage{anysize}“
????????????print?“\\marginsize{1in}{1in}{1in}{1in}“
????????????print?“\\begin{document}“
????????????
????????if?(name==“Subgroup“)?and?(self.which_tt==“Students_Timetable“):
????????????self.timetable[“class“]=attrs[‘name‘]
????????????self.days=[]
????????if?(name==“Teacher“)?and?(self.which_tt==“Teachers_Timetable“):
????????????self.timetable[“teacher“]=attrs[‘name‘]
????????????self.days=[]
????????????
????????if?name==“Day“?:
????????????self.day=attrs[‘name‘]
????????????self.days.append(self.day)
????????????self.hours=[]
????????if?name==“Hour“?:
????????????self.hour=attrs[‘name‘]
????????????self.hours.append(self.hour)
????????if?name==“Subject“?:
????????????self.timetable[self.day+self.hour]=attrs[‘name‘]
????????if?name==“Students“?:
????????????self.timetable[self.day+self.hour]=attrs[‘name‘]
????????????
????def?handleEndElement(self?name):
????????if?(name==“Subgroup“)?and?(self.which_tt==“Students_Timetable“):
????????????
????????????print?“\\begin{tabular}{|c|“
????????????for?i?in?self.days:
????????????????print?“c|“?
????????????print?“}“
????????????print?“\\hline“
????????????print?“\\multicolumn{“l(fā)en(self.days)+1“}{|c|}{“?self.timetable[“class“]“}\\\\“
????????????print?
- 上一篇:即時檢測USB設(shè)備插拔
- 下一篇:用C++求龍貝格積分
評論
共有 條評論