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
 Rodriguez 3...2371 
 Linux Kernel 2.6...1779 
 Trees...1409 
 William Gropp...1406 
 Clickhouse...1405 
 Rodriguez 7...1404 
 Стивенс 9...1404 
 M.Pilgrim->Часть 2...1403 
 Part 3...1402 
 Httpd-> История Ap...1402 
 Mod_perl 2...1402 
 Rodriguez 8...1401 
 OS ->Intel Manual 1...1400 
 ground Up...1400 
 Rodriguez 6...1400 
 Cluster 2...1399 
 FAQ...1399 
 Stewens -> IPC 9...1399 
 Ext4 FS...1398 
 Robert Love 2...1398 
 
  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