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

248 lines
8.9 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\第5章.py + 题号 运行
# 程序名:
# 第5章习题
# 目的:
#
# 修订记录:
# 日期 编程者 改动描述
# =================== ============= =====================================
# 2021-07-23 15:52:52 Sola 5-1 简化dateconvert2格式化时间输出
# 2021-07-23 15:57:01 Sola 5-2 分数等级转化
# 2021-07-23 16:29:47 Sola 5-3 百分制分数等级转化
# 2021-07-23 16:30:00 Sola 5-4 获取短语缩写
# 2021-07-23 16:44:47 Sola 5-5 计算输入单个名字的数值
# 2021-07-23 16:49:19 Sola 5-6 计算所有名字的数字之和
# 2021-07-23 17:02:26 Sola 5-7 简单基于Unicode的凯撒密码
# 2021-07-23 17:15:16 Sola 5-8 真正的凯撒密码
# 2021-07-23 17:53:11 Sola 5-9 计算用户输入句子的单词数
# 2021-07-23 21:59:33 Sola 5-10 计算输入句子中单词的平均长度
# 2021-07-23 22:04:15 Sola 5-11 第1章 chaos.py 改进版本
# 2021-07-23 22:22:38 Sola 5-12 第二章算终值改进版本
# 2021-07-23 22:23:03 Sola 5-13
# 2021-07-23 22:23:15 Sola 5-14 统计文件的行数、单词数和字符数
# 2021-07-23 23:21:10 Sola 5-15 学生成绩统计表
# 2021-07-23 23:21:28 Sola 5-16 数字频数统计表
#
# module start
# from graphics import * # 引入图形库
from os import times
def test5_1():
# get the date
dateStr = input("Enter a date (mm/dd/yyyy): ")
# split into components
monthStr, dayStr, yearStr = dateStr.split("/")
# convert monthStr to the month name
months = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
monthStr = months[int(monthStr)-1]
# output result in month day, year format
print("The converted date is: {0} {1}, {2}".format(monthStr, dayStr, yearStr))
def test5_2():
score = int(input("请输入你的分数:"))
grade = ["F", "E", "D", "C", "B", "A"]
result = grade[score]
print("你的分数对应的等级为:{0}".format(result))
def test5_3():
score = int(float(input("请输入你的分数:")) / 10)
grade = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A"]
result = grade[score]
print("你的分数对应的等级为:{0}".format(result))
def test5_4():
inputStr = input("请输入一个短语:")
shortName = ""
for i in inputStr.split():
shortName = shortName + i[0].upper()
print("'{0}' 的缩写为:{1}".format(inputStr, shortName))
def test5_5():
name = input("请输入单个名字:").lower()
sum = 0
for i in name:
sum = sum + ord(i) - 96
print("{0} 的数值为:{1}".format(name, sum))
def test5_6():
name = input("请输入你的名字:").lower()
sum = 0
for i in name.split():
for j in i:
sum = sum + ord(j) - 96
print("{0} 的数值为:{1}".format(name, sum))
def test5_7():
char = input("请输入需要加密的密文:")
key = int(input("请输入密文的偏移位数:"))
def code(char, key):
coding = ""
for ch in char:
coding = coding + chr(ord(ch) + key)
return coding
coding = code(char, key)
print("{0} 编码后的密文为:{1}".format(char, coding))
key = int(input("请输入密钥:"))
decoding = code(coding, -key)
print("{0} 解码后的明文为:{1}".format(coding, decoding))
def test5_8():
char = input("请输入需要加密的密文:")
key = int(input("请输入密文的偏移位数:"))
def code(char, key):
keyList = "abcdefghijklmnopqrstuvwxyz"
coding = ""
for ch in char:
if ord(ch) <= ord("z") and ord(ch) >= ord("a"):
coding = coding + keyList[(ord(ch) + key - ord("a")) % 26]
elif ord(ch) <= ord("Z") and ord(ch) >= ord("A"):
coding = coding + keyList[(ord(ch) + key - ord("A")) % 26].upper()
else:
coding = coding + ch
return coding
coding = code(char, key)
print("{0} 编码后的密文为:{1}".format(char, coding))
key = int(input("请输入密钥:"))
decoding = code(coding, -key)
print("{0} 解码后的明文为:{1}".format(coding, decoding))
def test5_9():
inputSentence = input("请输入需要统计的句子:")
num = 0
for ch in inputSentence.title():
if ord(ch) <= ord("Z") and ord(ch) >= ord("A"):
num = num + 1
print("输入语句 {0} 中有 {1} 个单词".format(inputSentence, num))
def test5_10():
inputSentence = input("请输入需要统计的句子:")
num = 0
long = 0
for ch in inputSentence.title():
if ord(ch) <= ord("Z") and ord(ch) >= ord("A"):
num = num + 1
for ch in inputSentence.lower():
if ord(ch) <= ord("z") and ord(ch) >= ord("a"):
long = long + 1
print("输入语句 {0} 中每个单词的平均长度为 {1:2.1f}".format(inputSentence, long / num))
def test5_11():
inputList = input("请输入两个初始值(用逗号分隔):").split(",")
init1, init2 = float(inputList[0]), float(inputList[1])
times = int(input("请输入迭代次数:"))
print("index {0:^8.2f} {1:^8.2f}\n------------------------------".format(init1, init2))
for i in range(times):
init1 = 3.9 * init1 * (1 - init1)
init2 = 3.9 * init2 * (1 - init2)
print("{0:<5} {1:8.6f} {2:8.6f}".format(int(i) + 1, init1, init2))
def test5_12():
print("This program calculates the future value of a investment.")
principal = eval(input("Enter the principal: "))
years = eval(input("Enter the number of years of your investment: "))
apr = eval(input("Enter the annual interest rate: "))
print("Year {0:^8}\n----------------".format("Value"))
print("{0:<4} ${1:>7.2f}".format(0, principal))
for i in range(years):
principal = principal * (1 + apr)
print("{0:<4} ${1:>7.2f}".format(i + 1, principal))
def test5_13():
print("???不玩了不玩了,没意思,再见吧您嘞!")
def test5_14():
from tkinter.filedialog import askopenfilename
print("请选择需要统计的文件:")
inputFileName = askopenfilename()
inputFile = open(inputFileName, 'r')
print("成功打开文件:{0}".format(inputFileName))
numLines = len(inputFile.readlines())
print("文件中共有句子 {0}".format(numLines))
numWords = 0
numChs = 0
inputFile = open(inputFileName, 'r')
for ch in inputFile.read().title():
if ord(ch) <= ord("Z") and ord(ch) >= ord("A"):
numWords = numWords + 1
numChs = numChs + 1
print("文件中共有单词 {0}".format(numWords))
print("文件中共有字符 {0}".format(numChs))
def test5_15():
# 4
# Computewell, 90
# Dibblebit, 60
# Jones, 80
# Smith, 70
from tkinter.filedialog import askopenfilename
from graphics import GraphWin, Text, Rectangle, Point
print("请选择输入的文件记录:")
inputFile = open(askopenfilename(), 'r')
print("成功打开文件!")
numStudents = int(inputFile.readline())
win = GraphWin("学生考试成绩", 800, numStudents * 30)
for i in range(numStudents):
infoList = inputFile.readline().split(",")
Text(Point(95, 15 + 30 * i), infoList[0]).draw(win)
Rectangle(Point(200, 7 + 30 * i), Point(200 + 590 * float(infoList[1]) / 100, 23 + 30 * i)).draw(win)
win.getMouse()
def test5_16():
# 0
# 1
# 2
# 2
# 4
# 4
# 4
# 5
# 5
# 5
# 5
# 6
# 6
# 6
# 6
# 6
# 7
# 7
# 7
# 7
# 7
# 7
# 7
# 8
# 8
# 8
# 9
# 9
# 9
# 10
# 10
from tkinter.filedialog import askopenfilename
from graphics import GraphWin, Text, Rectangle, Point
print("请选择输入的文件记录:")
numList = open(askopenfilename(), 'r').readlines()
print("成功打开文件!")
timesList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in numList:
timesList[int(i)] = timesList[int(i)] + 1
win = GraphWin("数字统计", 440, 300)
numMax = max(timesList)
num = 0
for i in timesList:
Text(Point(20 + 40 * num, 285), num).draw(win)
Rectangle(Point(10 + 40 * num, 270), Point(30 + 40 * num, 270 - 260 * i / numMax)).draw(win)
num = num + 1
win.getMouse()
import sys
def run(num):
eval("test5_" + num + "()")
run(sys.argv[1])