Search     or:     and:
 LINUX 
 Language 
 Kernel 
 Package 
 Book 
 Test 
 OS 
 Forum 
 iakovlev.org 
 Languages
 С
 GNU С Library 
 Qt 
 STL 
 Threads 
 C++ 
 Samples 
 stanford.edu 
 ANSI C
 Libs
 LD
 Socket
 Pusher
 Pipes
 Encryption
 Plugin
 Inter-Process
 Errors
 Deep C Secrets
 C + UNIX
 Linked Lists / Trees
 Asm
 Perl
 Python
 Shell
 Erlang
 Go
 Rust
 Алгоритмы
NEWS
Последние статьи :
  Тренажёр 16.01   
  Эльбрус 05.12   
  Алгоритмы 12.04   
  Rust 07.11   
  Go 25.12   
  EXT4 10.11   
  FS benchmark 15.09   
  Сетунь 23.07   
  Trees 25.06   
  Apache 03.02   
 
TOP 20
 MINIX...3057 
 Solaris...2933 
 LD...2906 
 Linux Kernel 2.6...2482 
 William Gropp...2182 
 Rodriguez 6...2016 
 C++ Templates 3...1945 
 Trees...1938 
 Kamran Husain...1866 
 Secure Programming for Li...1792 
 Максвелл 5...1711 
 DevFS...1695 
 Part 3...1684 
 Stein-MacEachern-> Час...1632 
 Go Web ...1627 
 Ethreal 4...1619 
 Стивенс 9...1607 
 Arrays...1607 
 Максвелл 1...1593 
 FAQ...1539 
 
  01.01.2024 : 3621733 посещений 

iakovlev.org

Калькулятор

 В следующем примере программа ожидает от пользователя набора 
 простого арифметического выражения  вида "1+2-3*4+6/2", 
 после чего выводит результат :
 	import re, string
 	Inputbuf = []
 
 	# A token is either a number or an operator symbol.
 	# The main program reads a line from the input and
 	# stores it in an array called Inputbuf. The function
 	# gettoken() returns individual tokens from this array.
 
 	def gettoken():
 		global Inputbuf
 		p = re.search('^\W*[\+\-\*/]|^\W*[0-9]+', Inputbuf)
 		token = p.string[p.regs[0][0]:p.regs[0][1]]
 		token = string.strip(token)
 		if token not in ['+', '-', '*', '/']:
 			token = int(token)
 		Inputbuf = Inputbuf[p.regs[0][1]:]
 		return token
 
 
 	# lookahead() peeks into the input stream and tells you what
 	# the next input token is
 
 	def lookahead():
 		global Inputbuf
 		try:
 	++/	p = re.search('^\W*[\+\-\*/]|^\W*[0-9]+', Inputbuf)
 			token = p.string[p.regs[0][0]:p.regs[0][1]]
 			token = string.strip(token)
 			if token not in ['+', '-', '*', '/']:
 				token = int(token)
 			return token
 		except:
 			return None
 
 
 	def factor():
 		return gettoken()
 
 
 	def term():
 		e1 = factor()
 		tmp = lookahead()
 		while (tmp in ['*', '/']):
 			gettoken()
 			if (tmp == '*'):
 				e1 = e1 * factor()
 			else:
 				e1 = e1 / factor()
 			tmp = lookahead()
 
 		return e1
 
 
 	def expression():
 		e1 = term()
 		tmp = lookahead()
 		while (tmp in ['+', '-']):
 			gettoken()
 			if (tmp == '+'):
 				e1 = e1 + term()
 			else:
 				e1 = e1 - term()
 			tmp = lookahead()
 
 		return e1
 
 
 	def main():
 		global Inputbuf
 		Inputbuf = raw_input()
 		print expression()
 
 
 	if __name__=='__main__':
 		main() 
 
 
 
 
  В следующем примере определяется расширение файла :
 	#!/usr/bin/python
 	from string import find
 	from sys import argv
 
 	headers = [("GIF8",0), ("PNG",1), ("JFIF",6)]
 	filepath = "my.file"
 	if len(argv)>1: filepath = argv[1]
 
 	fh = open(filepath )
 	dat = fh.read()
 	fh.close()
 
 	for kw,off in headers:
 	    x = 0
 	    while 1:
 	        x = find(dat,kw,x+1)
 	        if x<0: break
 	        print kw,"file begins at byte",x - off
 
 
 
 
 
Оставьте свой комментарий !

Ваше имя:
Комментарий:
Оба поля являются обязательными

 Автор  Комментарий к данной статье
jjj
  k
2006-06-06 17:48:16
Бил
  Спасибо
2012-10-30 13:23:14