欢迎光临散文网 会员登陆 & 注册

v2.2 python 一/二次函数绘图

2022-09-04 18:48 作者:ICE27182  | 我要投稿

横坐标表不下 只能纵坐标了大概

输入本来想改进 然后重写了3边 直到自己被循环绕晕 ABANDON

如果不知道 输出结果的截图我都是在windows的terminal里默认的powershell里截的


#介绍

print('fx可以绘制二次函数图像和一次函数图像\nv2.2更新内容')

print('1 明确了a b c的作用')

print('2 添加了纵坐标显示(准确度受图像精度影响)')

print('3 去掉了让变量下班的功能')

print('4 把更改字符也加进了是否确认')

print('修bug')

print('1 把一行没用的代码删了')


#输入

print("f(x)=ax²+bx+c")

def fxin(n):

    str(n)

    while True:

        try :

            a = input('%s=' % n)

            if a == '' or a == 'q':

                break

            a = float(a)

            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('输入组成图像的字符,默认为 ██ 和 2个空格  直接按回车保持默认')

    dot,space = input('请输入代表点的字符(直接回车则默认██)'),input('请输入代表空的字符(直接回车则默认2个空格)')

    if dot == '':

        dot = '██'

    if space == '':

        space = '  '


    #显示输入的函数

    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))

    print('用%s表示点, 用%s表示空' % (dot,space))


    #确认

    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  


#计算函数值

fx,y_axis,x = [],[],x_min


while x <= x_max :

    fx.append(int(round((a*x**2+b*x+c)*scale,0)))

    y_axis.append(a*x**2+b*x+c)

    x+=(1/scale)


fx_sequence = list(set(fx))

fx_sequence.sort(reverse=True)


y_axis = list(set(y_axis))

y_axis.sort()

y_max,y_min= y_axis[len(y_axis)-1],y_axis[0]

y_distance = (y_max-y_min)/((y_max-y_min)*scale)


#绘图

row = 0


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

            print(round(y_max-row*y_distance,2),'\t\t',end='')

           

            while count2 < count:   #单行

                plen = fx.index(e_fxs,pplen,len(fx))

                print(space*(plen-pplen), end='')

                print(dot, end='')

                pplen = plen+1

                count2+=1

           

            row+=1

            print('')

            count3+=1


    else:

        print(round(y_max-row*y_distance,2),'\t\t',end='')

        while count2 < count:   #最后一行

            plen = fx.index(e_fxs,pplen,len(fx))

            print(space*(plen-pplen), end='')

            print(dot, end='')

            pplen = plen+1

            count2+=1

心力憔悴


v2.2 python 一/二次函数绘图的评论 (共 条)

分享到微博请遵守国家法律