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
 Linux Kernel 2.6...3284 
 Trees...455 
 Clickhouse...437 
 Go Web ...430 
 Ethreal 4...424 
 Максвелл 3...386 
 Ext4 FS...384 
 C++ Patterns 3...375 
 William Gropp...364 
 Rodriguez 6...360 
 Ethreal 1...356 
 Secure Programming for Li...356 
 Gary V.Vaughan-> Libtool...354 
 Steve Pate 1...349 
 Ethreal 3...341 
 Assembler...337 
 DevFS...335 
 Стивенс 9...333 
 Ulrich Drepper...322 
 Стивенс 10...301 
 
  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