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
 Advanced Bash Scripting G...1857 
 Ethreal 4...1283 
 Secure Programming for Li...1058 
 CPAN-> FAQ...935 
 Intel 386...628 
 Тренажёр...496 
 Go Web ...479 
 Ethreal 1...413 
 Trees...412 
 Максвелл 3...381 
 Alg1...362 
 Rust...356 
 C + UNIX...336 
 Ext4 FS...334 
 Assembler...328 
 William Gropp...327 
 Mod_parrot...322 
 2.0-> Linux IP Networking...321 
 Benchmark...314 
 Rodriguez 6...314 
 
  01.01.2025 : 3803065 посещений 

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