Files
Python-Programming-Exercise/20210721-第4章.py
2025-09-25 21:23:34 +08:00

281 lines
10 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================================================================
# 通过 D:\文档\电子文档\个人\脚本库\PythonScript\第4章.py + 题号 运行
# 程序名:
# 第4章习题
# 目的:
#
# 修订记录:
# 日期 编程者 改动描述
# =================== ============= =====================================
# 2021-07-21 17:28:01 Sola 4-a 计算终值的例题
# 2021-07-21 17:28:24 Sola 4-b 通过获取三个点绘制三角形的例题
# 2021-07-21 21:53:31 Sola 4-c 击键输入
# 2021-07-21 21:53:54 Sola 4-d 点击移动圆形
# 2021-07-21 21:54:11 Sola 4-1 点击生成正方形
# 2021-07-21 21:54:27 Sola 4-2 生成靶标
# 2021-07-21 22:05:13 Sola 4-3 绘制某种面孔。。。
# 2021-07-21 22:08:23 Sola 4-4 ~ 4-5 略
# 2021-07-21 22:08:35 Sola 4-6 通过Entry输入求终值
# 2021-07-22 00:46:54 Sola 4-7 圆的交点
# 2021-07-22 01:16:26 Sola 4-8 绘制线段并显示相关信息
# 2021-07-22 01:29:07 Sola 4-9 绘制矩形并显示相关信息
# 2021-07-22 01:33:57 Sola 4-10 绘制三角形并显示相关信息
# 2021-07-22 01:59:03 Sola 4-11 绘制小房子
#
# module start
from typing import Sequence
from graphics import *
def test4_a():
# Introduction
print("This program plots the growth of a 10-year investment.")
# Get principal and interest rate
principal = float(input("Enter the initial principal: "))
apr = float(input("Enter the annualized interest rate: "))
# Create a graphics window with labels on left edge
win = GraphWin("Investment Growth Chart", 320, 240)
win.setBackground("white")
Text(Point(20, 230), ' 0.0K').draw(win)
Text(Point(20, 180), ' 2.5K').draw(win)
Text(Point(20, 130), ' 5.0K').draw(win)
Text(Point(20, 80), ' 7.5K').draw(win)
Text(Point(20, 30), '10.0K').draw(win)
# Draw bar for initial principal
height = principal * 0.02
bar = Rectangle(Point(40, 230), Point(65, 230-height))
bar.setFill("green")
bar.setWidth(2)
bar.draw(win)
# Draw bars for successive years
for year in range(1,11):
# calculate value for the next year
principal = principal * (1 + apr)
# draw bar for this value
xll = year * 25 + 40
height = principal * 0.02
bar = Rectangle(Point(xll, 230), Point(xll+25, 230-height)) # 创建柱体
bar.setFill("green") # 设置填充色
bar.setWidth(2) # 设置轮廓宽度
bar.draw(win) # 绘制
input("Press <Enter> to quit")
win.close()
def test4_b():
win = GraphWin("Draw a Triangle")
win.setCoords(0.0, 0.0, 10.0, 10.0)
message = Text(Point(5, 0.5), "Click on three points")
message.draw(win)
# Get and draw three vertices of triangle
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
# Use Polygon object to draw the triangle
triangle = Polygon(p1,p2,p3)
triangle.setFill("peachpuff")
triangle.setOutline("cyan")
triangle.draw(win)
# Wait for another click to exit
message.setText("Click anywhere to quit.")
win.getMouse()
def test4_c():
win = GraphWin("Click and Type", 400, 400)
for i in range(10):
pt = win.getMouse()
key = win.getKey()
label = Text(pt, key)
label.draw(win)
def test4_d():
win = GraphWin()
shape = Circle(Point(50, 50), 20)
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
for i in range(10):
p = win.getMouse()
c = shape.getCenter()
dx = p.getX() - c.getX()
dy = p.getY() - c.getY()
shape.move(dx,dy)
win.close()
def test4_1():
win = GraphWin()
shape = Rectangle(Point(50, 50), Point(70, 70))
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
for i in range(10):
p = win.getMouse()
c = shape.getCenter()
dx = p.getX() - c.getX()
dy = p.getY() - c.getY()
shape.clone().draw(win)
shape.move(dx,dy)
Text(Point(100, 180), "Click again to quit").draw(win)
win.getMouse()
win.close()
def test4_2():
win = GraphWin()
def draw_circle(r, s_fill):
shape = Circle(Point(100, 90), r)
shape.setFill(s_fill)
shape.setOutline("black")
shape.clone().draw(win)
draw_circle(75, "white")
draw_circle(60, "black")
draw_circle(45, "blue")
draw_circle(30, "red")
draw_circle(15, "yellow")
Text(Point(100, 180), "Click again to quit").draw(win)
win.getMouse()
win.close()
def test4_3():
win = GraphWin()
Text(Point(100, 90), "某种面孔").draw(win)
Text(Point(100, 180), "Click again to quit").draw(win)
win.getMouse()
win.close()
def test4_6():
win = GraphWin("Input menu", 400, 130)
Text(Point(200, 10), "This program plots the growth of a 10-year investment.").draw(win)
Text(Point(200, 30), "Enter the initial principal:").draw(win)
Text(Point(200, 70), "Enter the annualized interest rate:").draw(win)
apr_entry = Entry(Point(200, 90), 10)
apr_entry.draw(win)
principal_entry = Entry(Point(200, 50), 10)
principal_entry.draw(win)
Text(Point(200, 115), "Click again to continue").draw(win)
win.getMouse()
principal = float(principal_entry.getText())
apr = float(apr_entry.getText())
win = GraphWin("Investment Growth Chart", 320, 240)
win.setBackground("white")
Text(Point(20, 230), ' 0.0K').draw(win)
Text(Point(20, 180), ' 2.5K').draw(win)
Text(Point(20, 130), ' 5.0K').draw(win)
Text(Point(20, 80), ' 7.5K').draw(win)
Text(Point(20, 30), '10.0K').draw(win)
# Draw bar for initial principal
height = principal * 0.02
bar = Rectangle(Point(40, 230), Point(65, 230-height))
bar.setFill("green")
bar.setWidth(2)
bar.draw(win)
# Draw bars for successive years
for year in range(1,11):
# calculate value for the next year
principal = principal * (1 + apr)
# draw bar for this value
xll = year * 25 + 40
height = principal * 0.02
bar = Rectangle(Point(xll, 230), Point(xll+25, 230-height)) # 创建柱体
bar.setFill("green") # 设置填充色
bar.setWidth(2) # 设置轮廓宽度
bar.draw(win) # 绘制
win.getMouse()
def test4_7():
r = float(input("请输入圆的半径:"))
b = float(input("请属于水平线在y轴方向截距"))
win = GraphWin()
win.setCoords(-10, -10, 10, 10)
Circle(Point(0, 0), r).draw(win)
Line(Point(-10, b), Point(10, b)).draw(win)
if abs(b) > r:
print("没有交点!")
else:
x = round((r ** 2 - b ** 2) ** 0.5, 1)
b = round(b, 1)
def printPoint(x, b):
p = Point(x, b)
p.setFill("red")
p.clone().draw(win)
Text(Point(x, b + 1), "(" + str(x) + ", " + str(b) + ")").draw(win)
printPoint(x, b)
printPoint(-x, b)
Text(Point(0, -9), "Click again to quit").draw(win)
win.getMouse()
def test4_8():
win = GraphWin()
text = Text(Point(100, 190), "请点击两个位置!")
text.draw(win)
p1 = win.getMouse()
p2 = win.getMouse()
l1 = Line(p1, p2)
l1.draw(win)
p3 = l1.getCenter()
p3.setFill("blue")
p3.draw
if (p2.getX() - p1.getX()) == 0:
slope = "inf"
else:
slope = round((p2.getY() - p1.getY()) / (p2.getX() - p1.getX()), 4)
length = round(((p2.getY() - p1.getY()) ** 2 + (p2.getX() - p1.getX()) ** 2) ** 0.5, 4)
print("线段的斜率为:", slope)
print("线段的长度为:", length)
text.setText("Click again to quit")
win.getMouse()
def test4_9():
win = GraphWin()
text = Text(Point(100, 190), "请点击两个位置!")
text.draw(win)
p1 = win.getMouse()
p2 = win.getMouse()
l1 = Rectangle(p1, p2)
l1.draw(win)
cRectangle = round((abs(p2.getY() - p1.getY()) + abs(p2.getX() - p1.getX())) * 2, 4)
sRectangle = round(abs(p2.getY() - p1.getY()) * abs(p2.getX() - p1.getX()), 4)
print("矩形的周长为:", cRectangle)
print("矩形的面积为:", sRectangle)
text.setText("Click again to quit")
win.getMouse()
def test4_10():
win = GraphWin()
text = Text(Point(100, 190), "请点击三个位置!")
text.draw(win)
p1 = win.getMouse()
p2 = win.getMouse()
p3 = win.getMouse()
Polygon(p1, p2, p3).draw(win)
a = ((p2.getY() - p1.getY()) ** 2 + (p2.getX() - p1.getX()) ** 2) ** 0.5
b = ((p3.getY() - p2.getY()) ** 2 + (p3.getX() - p2.getX()) ** 2) ** 0.5
c = ((p3.getY() - p1.getY()) ** 2 + (p3.getX() - p1.getX()) ** 2) ** 0.5
cPolygon = round(a + b + c, 4)
sPolygon = round((cPolygon / 2 * (cPolygon / 2 - a) * (cPolygon / 2 - b) * (cPolygon / 2 - c)) ** 0.5, 4)
print("三角形的周长为:", cPolygon)
print("三角形的面积为:", sPolygon)
text.setText("Click again to quit")
win.getMouse()
def test4_11():
win = GraphWin()
text = Text(Point(100, 190), "请点击五个位置!")
text.draw(win)
p1 = win.getMouse()
p2 = win.getMouse()
p3 = win.getMouse()
p4 = win.getMouse()
p5 = win.getMouse()
d = p2.getX() - p1.getX()
Rectangle(p1, p2).draw(win)
Polygon(p2, Point(p1.getX(), p2.getY()), p5).draw(win)
Rectangle(Point(p3.getX() + d * 0.1, p3.getY()), Point(p3.getX() - d * 0.1, p1.getY())).draw(win)
Rectangle(Point(p4.getX() + d * 0.05, p4.getY() + d * 0.05), Point(p4.getX() - d * 0.05, p4.getY() - d * 0.05)).draw(win)
text.setText("Click again to quit")
win.getMouse()
import sys
def run(num):
eval("test4_" + num + "()")
run(sys.argv[1])