Beispiel #1
0
def quadratic_results(request):
    context = {}

    if request.method == "GET":
        form = QuadraticForm(request.GET)
        if form.is_valid():
            a = form.cleaned_data['a']
            b = form.cleaned_data['b']
            c = form.cleaned_data['c']

            discriminant = b * b - 4 * a * c

            if discriminant > 0:
                context['x1'] = (-b + sqrt(discriminant)) / (2 * a)
                context['x2'] = (-b - sqrt(discriminant)) / (2 * a)
            elif discriminant == 0:
                context['x1'] = (-b - sqrt(discriminant))/(2 * a)

            context['discriminant'] = discriminant

    else:
        form = QuadraticForm()

    context['form'] = form

    return render(request, 'results.html', context)
Beispiel #2
0
def quadratic_results(request):
        
	disc = {}
	text_result = {}

	if request.method == "GET":
		form = QuadraticForm(request.GET)
		if form.is_valid():
			a = form.cleaned_data['a']
			b = form.cleaned_data['b']
			c = form.cleaned_data['c']

			disc['message'] = "Дискриминант: "
			disc['value'] = b**2-4*a*c

			if disc['value'] < 0:
				text_result['message'] = u"Дискриминант меньше нуля, квадратное уравнение не имеет действительных решений."
			elif disc['value'] == 0:
				x = (-b + disc['value'] ** (1/2.0)) / 2*a
				text_result['message'] = u"Дискриминант равен нулю, квадратное уравнение имеет один действительный корень: x1 = x2 = "
				text_result['value'] = x
			else:
				x1 = (-b + disc['value'] ** (1/2.0)) / 2*a
				x2 = (-b - disc['value'] ** (1/2.0)) / 2*a
				text_result['message'] = u"Квадратное уравнение имеет два действительных корня: " 
				text_result['value'] = u"x1 = %.1f, x2 = %.1f" % (x1, x2)
	else:
		form = QuadraticForm()
	
	out_result =  context = {"disc":disc, "text_result":text_result, "form":form}
	
	return render(request,'results.html', out_result)
Beispiel #3
0
def quadratic_results(request):
	context={}
	if request.method == 'GET' and request.GET.keys():
		form = QuadraticForm(request.GET)
		if form.is_valid():
			a = context['a'] = request.GET.get('a','')
			b = context['b'] = request.GET.get('b','')
			c = context['c'] = request.GET.get('c','')

			context['d'] = float(b)**2 - 4*float(a)*float(c)
			d = context['d']
			if d > 0 and int(a) != 0:
				context['descr'] = 'Дискриминант: %d' %d
				context['x1'] = (-float(b) + float(d) ** (1/2.0)) / 2*float(a)
				context['x2'] = (-float(b) - float(d) ** (1/2.0)) / 2*float(a)
				print context['x2']
				print context['x1']
				context['result'] = "Квадратное уравнение имеет два действительных корня: x1 = %.1f, x2 = %.1f" % (context['x1'] + 0, context['x2'] + 0) 
			elif d < 0:
				context['descr'] = 'Дискриминант: %d' %d
				context['result'] = "Дискриминант меньше нуля, квадратное уравнение не имеет действительных решений."
			elif d == 0 and int(a) != 0:
				context['descr'] = 'Дискриминант: %d' %d
				context['x1'] = (-float(b) + float(d) ** (1/2.0)) / 2.0*float(a)
				context['result'] = "Дискриминант равен нулю, квадратное уравнение имеет один действительный корень: x1 = x2 = %.1f" % (context['x1'] + 0)      
	else:
		form = QuadraticForm()

	context['form'] = form    
	return render(request, 'quadratic/results.html', context)