一/二次函数绘图v2.1
#介绍
print('\n\n\n\nfx_2.1可以绘制二次函数图像和一次函数图像',end='')
print('''
2.1更新内容:
1 显示输入内容时会显示x范围和缩放了
2 可以自定义组成图像的字符了
3 缩短了定义列表变量fx和fx_sequence的代码长度
4 使图像从原来的散点变成了相对连续的(大概是因为python计算精度的问题 可能会有一点点小bug导致不连续...)
5 让变量没用之后下班
修bugs
1 把scale的范围限定在了正数
2 防止了x max < x min 导致的死循环
3 去掉了一个多余的变量
''',end='')
#输入
def fxin(n):
str(n)
while True:
try :
a = float(input('%s=' % n))
break
except:
print('请输入数字')
return(a)
while True:
a,b,c,x_min = fxin('a'),fxin('b'),fxin('c'),fxin('x min')
while True:
try :
x_max = float(input('x max='))
if x_max >= x_min:
break
else:
x_max = float('')
except:
print('请输入数字,且x max不应小于x min')
while True:
try :
scale = float(input('图像缩放'))
if scale > 0:
break
else:
scale = float('')
except ValueError:
print('请输入大于0的正数')
#显示输入的函数
print('\nf(x)=',end='')
if a == 1:
print('x²',end='')
elif a == -1:
print('-x²',end='')
elif a != 0:
print('%fx²' % a,end='')
if b == 1 and a == 0:
print('x',end='')
elif b == 1 and a != 0:
print('+x',end='')
elif b == -1:
print('-x',end='')
elif b != 0:
if b > 0 and a != 0:
print('+%fx' % b,end='')
if b > 0 and a == 0:
print('%fx' % b,end='')
if b <= 0:
print('%fx' % b,end='')
if a == 0 and b == 0:
print(c,end = '')
elif a != 0 or b != 0:
if c > 0:
print('+%f' % c,end = '')
if c < 0:
print(c,end = '')
if c == 0:
print('',end = '')
print('\nx的最小值为%f,x的最大值为%f,函数图像缩放为%f' % (x_min,x_max,scale))
#确认
while True:
yrq = input('输入y确认\n输入r重新输入\n输入q退出\n')
if yrq == 'y' or yrq == 'r':
break
elif yrq == 'q':
exit()
else:
print('\n仅限输入 y 或 r 或 q')
if yrq == 'y':
del yrq
break
#输入输出的字符
print('输入组成图像的字符,默认为 ██ 和 2个空格 直接按回车保持默认')
dot,space = input('请输入代表点的字符(直接回车则默认██)'),input('请输入代表空的字符(直接回车则默认2个空格)')
if dot == '':
dot = '██'
if space == '':
space = ' '
#计算函数值
fx = []
while x_min <= x_max :
fx.append(int(round((a*x_min**2+b*x_min+c)*scale,0)))
x_min+=(1/scale)
fx_sequence = list(set(fx))
fx_sequence.sort(reverse=True)
del a,b,c,x_max,x_min,scale
#绘图
for e_fxs in fx_sequence:
count = fx.count(e_fxs)
count2,plen,pplen,count3 = 0,0,0,0
if fx_sequence.index(e_fxs) < (len(fx_sequence)-1):
while count3 < (e_fxs - fx_sequence[fx_sequence.index(e_fxs)+1]): #多行
count2,plen,pplen = 0,0,0
while count2 < count: #单行
#print(pplen,' ',len(fx)-1,' ',fx)
plen = fx.index(e_fxs,pplen,len(fx))
print(space*(plen-pplen), end='')
print(dot, end='')
pplen = plen+1
count2+=1
print('')
count3+=1
else:
while count2 < count: #最后一行
plen = fx.index(e_fxs,pplen,len(fx))
print(space*(plen-pplen), end='')
print(dot, end='')
pplen = plen+1
count2+=1


