U E D R , A S I H C RSS

구구단/임인택

구구단을 짜놓은 페이지입니다.

첫 프로그램(루프 이용)


~cpp 
# gugudan with loop.

for dan in range(2, 10):
	for op in range(1, 10):
		print ('%d x %d = %d')%(dan, op, dan*op)
 

약간 구조적으로 리팩토링


~cpp 
# gugudan with slightly structured programming methodology.
def dan(d):
	for op in range(1, 10):
		print ('%d x %d = %d')%(d,op,d*op)

def gugudan():
	for d in range(2, 10):
		dan(d)

if __name__=='__main__':
	gugudan()
 

recursion 사용


~cpp 
# gugudan with recursion.

def recurGugu(dan, op, sum):
	if op < 10 :
		sum += dan
		print ('%d x %d = %d') % (dan, op, sum)
		recurGugu(dan, op+1, sum)
	else:
		if dan < 9 :
			recurGugu(dan+1, 1, 0)
		else:
			return
	return

if __name__=='__main__':
	recurGugu(2, 1, 0)
 

객체지향(이라고 보기는 힘든면도..)


~cpp 
# gugudan with object-oriented programming (just express elements as objects)

class Gugudan:
	def __init__(self):
		for d in range(2, 10):
			Dan(d)
class Dan:
	def __init__(self, dan):
		self.dan = dan
		for op in range(1, 10):
			opDan = Operand(dan)
			opMul = Operand(op)
			print DanLine(opDan, opMul)

class DanLine:
	def __init__(self, op1, op2):
		self.op1 = op1.value()
		self.op2 = op2.value()

	def __str__(self):
		return ('%d x %d = %d')%(self.op1, self.op2, self.op1*self.op2)
	
class Operand:
	def __init__(self, value):
		self.val = value
	def value(self):
		return self.val

if __name__=='__main__':
	Gugudan()

 

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0896 sec