一/二次函數(shù)繪圖v2.1
#介紹
print('\n\n\n\nfx_2.1可以繪制二次函數(shù)圖像和一次函數(shù)圖像',end='')
print('''
2.1更新內(nèi)容:
1 顯示輸入內(nèi)容時(shí)會(huì)顯示x范圍和縮放了
2 可以自定義組成圖像的字符了
3 縮短了定義列表變量fx和fx_sequence的代碼長(zhǎng)度
4 使圖像從原來(lái)的散點(diǎn)變成了相對(duì)連續(xù)的(大概是因?yàn)閜ython計(jì)算精度的問(wèn)題 可能會(huì)有一點(diǎn)點(diǎn)小bug導(dǎo)致不連續(xù)...)
5 讓變量沒(méi)用之后下班
修bugs
? ? 1 把scale的范圍限定在了正數(shù)
? ? 2 防止了x max < x min 導(dǎo)致的死循環(huán)
? ? 3 去掉了一個(gè)多余的變量
''',end='')
#輸入
def fxin(n):
? ? str(n)
? ? while True:
? ? ? ? try :
? ? ? ? ? ? a = float(input('%s=' % n))
? ? ? ? ? ? break
? ? ? ? except:
? ? ? ? ? ? print('請(qǐng)輸入數(shù)字')
? ? ? ?
? ? 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('請(qǐng)輸入數(shù)字,且x max不應(yīng)小于x min')
? ?
? ? while True:
? ? ? ? try :
? ? ? ? ? ? scale = float(input('圖像縮放'))
? ? ? ? ? ? if scale > 0:
? ? ? ? ? ? ? ? break
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? scale = float('')
? ? ? ? except ValueError:
? ? ? ? ? ? print('請(qǐng)輸入大于0的正數(shù)')
? ? #顯示輸入的函數(shù)
? ? print('\nf(x)=',end='')
? ? if a == 1:
? ? ? ? ? ? print('x2',end='')
? ? elif a == -1:
? ? ? ? ? ? print('-x2',end='')
? ? elif a != 0:
? ? ? ? ? ? print('%fx2' % 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,函數(shù)圖像縮放為%f' % (x_min,x_max,scale))
? ? #確認(rèn)
? ? while True:
? ? ? ? yrq = input('輸入y確認(rèn)\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('輸入組成圖像的字符,默認(rèn)為 ██ 和 2個(gè)空格 ?直接按回車(chē)保持默認(rèn)')
dot,space = input('請(qǐng)輸入代表點(diǎn)的字符(直接回車(chē)則默認(rèn)██)'),input('請(qǐng)輸入代表空的字符(直接回車(chē)則默認(rèn)2個(gè)空格)')
if dot == '':
? ? dot = '██'
if space == '':
? ? space = ' ?'
#計(jì)算函數(shù)值
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


