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
 Part 4...2318 
 Linux Kernel 2.6...1766 
 Go Web ...1752 
 William Gropp...1750 
 uOS...1750 
 Ext4 FS...1750 
 Assembler...1749 
 M.Marcu...1749 
 Part 1...1749 
 Linux Inline for x86...1749 
 NASM...1749 
 Ext2 internals...1749 
 ffmpeg->tutorial...1748 
 Stevens-> Глава 4...1748 
 AT&T Tutorial...1748 
 Python Cluster...1748 
 OS-FAQ 2...1748 
 File handling...1748 
 Redis...1748 
 Rust 4...1748 
 
  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