C语言经典100例(Python版本)061-065


               
               
                '''
【程序61】
题目:打印出杨辉三角形(要求打印出10行如下图)   
1.程序分析:
'''
if __name__ == '__main__':
    a = []
    for i in range(10):
        a.append([])
        for j in range(10):
            a.append(0)
    for i in range(10):
        a[0] = 1
        a = 1
    for i in range(2,10):
        for j in range(1,i):
            a[j] = a[i - 1][j-1] + a[i - 1][j]
    from sys import stdout
    for i in range(10):
        for j in range(i + 1):
            stdout.write(a[j])
            stdout.write(' ')
        print
        
'''
【程序62】
题目:学习putpixel画点。
1.程序分析:            
2.程序源代码:
#include "stdio.h"
#include "graphics.h"
main()
{
    int i,j,driver=VGA,mode=VGAHI;
    initgraph(&driver,&mode,"");
    setbkcolor(YELLOW);
    for(i=50;i''
'''
题目:画椭圆ellipse   
1.程序分析:
2.程序源代码:
'''
if __name__ == '__main__':
    from Tkinter import *
    x = 360
    y = 160
    top = y - 30
    bottom = y - 30
   
    canvas = Canvas(width = 400,height = 600,bg = 'white')
    for i in range(20):
        canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom)
        top -= 5
        bottom += 5
    canvas.pack()
    mainloop()
'''
题目:利用ellipse and rectangle 画图。
1.程序分析:
2.程序源代码:
'''
if __name__ == '__main__':
    from Tkinter import *
    canvas = Canvas(width = 400,height = 600,bg = 'white')
    left = 20
    right = 50
    top = 50
    num = 15
    for i in range(num):
        canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
        canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
        canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
        right += 5
        left += 5
        top += 10
    canvas.pack()
    mainloop()
        
'''
题目:利用ellipse and rectangle 画图。
1.程序分析:
2.程序源代码:
'''
if __name__ == '__main__':
    from Tkinter import *
    canvas = Canvas(width = 400,height = 600,bg = 'white')
    left = 20
    right = 50
    top = 50
    num = 15
    for i in range(num):
        canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
        canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
        canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
        right += 5
        left += 5
        top += 10
    canvas.pack()
    mainloop()
        
'''
【程序65】
题目:一个最优美的图案。   
1.程序分析:
2.程序源代码:
'''
import math
class PTS:
    def __init__(self):
        self.x = 0
        self.y = 0
points = []
def LineToDemo():
    from Tkinter import *
    screenx = 400
    screeny = 400
    canvas = Canvas(width = screenx,height = screeny,bg = 'white')
    AspectRatio = 0.85
    MAXPTS = 15
    h = screeny
    w = screenx
    xcenter = w / 2
    ycenter = h / 2
    radius = (h - 30) / (AspectRatio * 2) - 20
    step = 360 / MAXPTS
    angle = 0.0
    for i in range(MAXPTS):
        rads = angle * math.pi / 180.0
        p = PTS()
        p.x = xcenter + int(math.cos(rads) * radius)
        p.y = ycenter - int(math.sin(rads) * radius * AspectRatio)
        angle += step
        points.append(p)
    canvas.create_oval(xcenter - radius,ycenter - radius,
                       xcenter + radius,ycenter + radius)
    for i in range(MAXPTS):
        for j in range(i,MAXPTS):
            canvas.create_line(points.x,points.y,points[j].x,points[j].y)
    canvas.pack()
    mainloop()
if __name__ == '__main__':
    LineToDemo()