Практикум по программированию. Основы. Ветвление. Корни уравнения
From AsIsWiki
(Difference between revisions)
(Created page with "<div style='max-width:700px;text-align:justify;'> Ветвление __FORCETOC...") |
|||
(8 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<div style='max-width:700px;text-align:justify;'> | <div style='max-width:700px;text-align:justify;'> | ||
− | [[Практикум по программированию. Основы. Ветвление|Ветвление]] | + | [[Практикум по программированию. Основы. Ветвление|Задачи]] · |
+ | [[Практикум по программированию. Основы. Ветвление. Запуск ракеты|Дальше]] | ||
__FORCETOC__ | __FORCETOC__ | ||
Line 6: | Line 7: | ||
==Java== | ==Java== | ||
− | < | + | <source lang="java"> |
import java.util.Scanner; | import java.util.Scanner; | ||
import java.lang.Math; | import java.lang.Math; | ||
Line 13: | Line 14: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | + | ||
Scanner in = new Scanner(System.in); | Scanner in = new Scanner(System.in); | ||
− | + | ||
− | + | System.out.println(); | |
− | System.out.println(" Поиск корней уравнения A * X^2 + B * X + C = 0 "); | + | System.out.println(" Поиск корней уравнения A * X^2 + B * X + C = 0"); |
System.out.println("------------------------------------------------"); | System.out.println("------------------------------------------------"); | ||
− | System.out.print(" Введите A: "); | + | System.out.print(" Введите A: "); |
− | + | double a = in.nextDouble(); | |
− | System.out.print(" Введите B: "); | + | System.out.print(" Введите B: "); |
− | + | double b = in.nextDouble(); | |
− | System.out.print(" Введите C: "); | + | System.out.print(" Введите C: "); |
− | + | double c = in.nextDouble(); | |
System.out.println("------------------------------------------------"); | System.out.println("------------------------------------------------"); | ||
− | |||
− | |||
− | |||
− | + | double d = b * b - 4 * a * c; | |
+ | double x1, x2; | ||
− | + | if (d > 0) { | |
− | + | ||
− | + | x1 = (-b + Math.sqrt(d)) / (2 * a); | |
− | + | x2 = (-b - Math.sqrt(d)) / (2 * a); | |
− | + | System.out.printf(" X1 = %.3f\n", x1); | |
− | + | System.out.printf(" X2 = %.3f\n", x2); | |
− | + | } else if (d == 0) { | |
− | + | x1 = -b / (2 * a); | |
− | + | System.out.printf(" X = %.3f\n", x1); | |
− | + | } else { | |
− | + | System.out.println(" Уравнение корней не имеет"); | |
− | + | ||
+ | } | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
==C++== | ==C++== | ||
− | < | + | <source lang="cpp"> |
− | </ | + | // g++ 4.2 |
+ | |||
+ | #include <iostream> | ||
+ | #include <math.h> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | double a, b, c; | ||
+ | |||
+ | cout << "\n Поиск корней уравнения A * X^2 + B * X + C = 0\n"; | ||
+ | cout << "------------------------------------------------\n"; | ||
+ | |||
+ | cout << " Введите A: "; | ||
+ | cin >> a; | ||
+ | |||
+ | cout << " Введите B: "; | ||
+ | cin >> b; | ||
+ | |||
+ | cout << " Введите C: "; | ||
+ | cin >> c; | ||
+ | |||
+ | cout << "------------------------------------------------\n"; | ||
+ | |||
+ | double d = b * b - 4 * a * c; | ||
+ | double x1, x2; | ||
+ | |||
+ | if (d > 0) { | ||
+ | |||
+ | x1 = (-b + sqrt(d)) / (2 * a); | ||
+ | x2 = (-b - sqrt(d)) / (2 * a); | ||
+ | |||
+ | printf(" X1 = %.3f\n", x1); | ||
+ | printf(" X2 = %.3f\n", x2); | ||
+ | |||
+ | } else if (d == 0) { | ||
+ | |||
+ | x1 = -b / (2 * a); | ||
+ | |||
+ | printf(" X = %.3f\n", x1); | ||
+ | |||
+ | } else { | ||
+ | |||
+ | cout << " Уравнение корней не имеет\n"; | ||
+ | |||
+ | } | ||
+ | |||
+ | cout << "\n"; | ||
+ | return 0; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | ==Python== | ||
+ | |||
+ | <source lang="cpp"> | ||
+ | # Python 3 | ||
+ | |||
+ | import math | ||
+ | |||
+ | print('\n Поиск корней уравнения A * X^2 + B * X + C = 0') | ||
+ | print('------------------------------------------------') | ||
+ | |||
+ | a = float(input(' Введите A: ')) | ||
+ | b = float(input(' Введите B: ')) | ||
+ | c = float(input(' Введите C: ')) | ||
+ | |||
+ | print('------------------------------------------------') | ||
+ | |||
+ | d = b * b - 4 * a * c | ||
+ | |||
+ | if d > 0: | ||
+ | |||
+ | x1 = (-b + math.sqrt(d)) / (2 * a) | ||
+ | x2 = (-b - math.sqrt(d)) / (2 * a) | ||
+ | |||
+ | print(' X1 = %.3f' % x1) | ||
+ | print(' X2 = %.3f' % x2) | ||
+ | |||
+ | elif d == 0: | ||
+ | |||
+ | x1 = -b / (2 * a) | ||
+ | |||
+ | print(' X = %.3f' % x1) | ||
+ | |||
+ | else: | ||
+ | |||
+ | print(' Уравнение корней не имеет') | ||
+ | </source> | ||
==Pascal== | ==Pascal== | ||
− | < | + | <source lang="delphi"> |
− | </ | + | </source> |
+ | |||
+ | |||
+ | ==JavaScript== | ||
+ | |||
+ | <source lang="js"> | ||
+ | <html lang="ru"> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <script> | ||
+ | function calc() { | ||
+ | var a = document.getElementById("aId").value; | ||
+ | var b = document.getElementById("bId").value; | ||
+ | var c = document.getElementById("cId").value; | ||
+ | |||
+ | var d = b * b - 4 * a * c; | ||
+ | var x1, x2; | ||
+ | var result = ""; | ||
+ | |||
+ | if (d > 0) { | ||
+ | |||
+ | x1 = (-b + Math.sqrt(d)) / (2 * a); | ||
+ | x2 = (-b - Math.sqrt(d)) / (2 * a); | ||
+ | |||
+ | result = "X1 = " + x1.toFixed(3) + "<br>" + | ||
+ | "X2 = " + x2.toFixed(3); | ||
+ | |||
+ | } else if (d == 0) { | ||
+ | |||
+ | x1 = -b / (2 * a); | ||
+ | |||
+ | result = "X = " + x1.toFixed(3); | ||
+ | |||
+ | } else { | ||
+ | |||
+ | result = "Уравнение корней не имеет"; | ||
+ | } | ||
+ | |||
+ | document.getElementById("resultId").innerHTML = result; | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
+ | <body> | ||
+ | |||
+ | <p>Поиск корней уравнения A * X^2 + B * X + C = 0</p> | ||
+ | <hr> | ||
+ | <p>Введите A: <input id="aId" size="5"></p> | ||
+ | <p>Введите B: <input id="bId" size="5"></p> | ||
+ | <p>Введите C: <input id="cId" size="5"></p> | ||
+ | <hr> | ||
+ | <p id="resultId"></p> | ||
+ | |||
+ | <button onclick="calc()">Рассчитать</button> | ||
+ | |||
+ | </body> | ||
+ | </html> | ||
+ | </source> | ||
---- | ---- | ||
− | [[Практикум по программированию. Основы. Ветвление|Ветвление]] | + | [[Практикум по программированию. Основы. Ветвление|Задачи]] · |
+ | [[Практикум по программированию. Основы. Ветвление. Запуск ракеты|Дальше]] | ||
</div> | </div> |
Latest revision as of 20:09, 19 November 2017
Contents |
[edit] Java
import java.util.Scanner; import java.lang.Math; public class Task01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(); System.out.println(" Поиск корней уравнения A * X^2 + B * X + C = 0"); System.out.println("------------------------------------------------"); System.out.print(" Введите A: "); double a = in.nextDouble(); System.out.print(" Введите B: "); double b = in.nextDouble(); System.out.print(" Введите C: "); double c = in.nextDouble(); System.out.println("------------------------------------------------"); double d = b * b - 4 * a * c; double x1, x2; if (d > 0) { x1 = (-b + Math.sqrt(d)) / (2 * a); x2 = (-b - Math.sqrt(d)) / (2 * a); System.out.printf(" X1 = %.3f\n", x1); System.out.printf(" X2 = %.3f\n", x2); } else if (d == 0) { x1 = -b / (2 * a); System.out.printf(" X = %.3f\n", x1); } else { System.out.println(" Уравнение корней не имеет"); } } }
[edit] C++
// g++ 4.2 #include <iostream> #include <math.h> using namespace std; int main() { double a, b, c; cout << "\n Поиск корней уравнения A * X^2 + B * X + C = 0\n"; cout << "------------------------------------------------\n"; cout << " Введите A: "; cin >> a; cout << " Введите B: "; cin >> b; cout << " Введите C: "; cin >> c; cout << "------------------------------------------------\n"; double d = b * b - 4 * a * c; double x1, x2; if (d > 0) { x1 = (-b + sqrt(d)) / (2 * a); x2 = (-b - sqrt(d)) / (2 * a); printf(" X1 = %.3f\n", x1); printf(" X2 = %.3f\n", x2); } else if (d == 0) { x1 = -b / (2 * a); printf(" X = %.3f\n", x1); } else { cout << " Уравнение корней не имеет\n"; } cout << "\n"; return 0; }
[edit] Python
# Python 3 import math print('\n Поиск корней уравнения A * X^2 + B * X + C = 0') print('------------------------------------------------') a = float(input(' Введите A: ')) b = float(input(' Введите B: ')) c = float(input(' Введите C: ')) print('------------------------------------------------') d = b * b - 4 * a * c if d > 0: x1 = (-b + math.sqrt(d)) / (2 * a) x2 = (-b - math.sqrt(d)) / (2 * a) print(' X1 = %.3f' % x1) print(' X2 = %.3f' % x2) elif d == 0: x1 = -b / (2 * a) print(' X = %.3f' % x1) else: print(' Уравнение корней не имеет')
[edit] Pascal
[edit] JavaScript
<html lang="ru"> <head> <meta charset="UTF-8"> <script> function calc() { var a = document.getElementById("aId").value; var b = document.getElementById("bId").value; var c = document.getElementById("cId").value; var d = b * b - 4 * a * c; var x1, x2; var result = ""; if (d > 0) { x1 = (-b + Math.sqrt(d)) / (2 * a); x2 = (-b - Math.sqrt(d)) / (2 * a); result = "X1 = " + x1.toFixed(3) + "<br>" + "X2 = " + x2.toFixed(3); } else if (d == 0) { x1 = -b / (2 * a); result = "X = " + x1.toFixed(3); } else { result = "Уравнение корней не имеет"; } document.getElementById("resultId").innerHTML = result; } </script> </head> <body> <p>Поиск корней уравнения A * X^2 + B * X + C = 0</p> <hr> <p>Введите A: <input id="aId" size="5"></p> <p>Введите B: <input id="bId" size="5"></p> <p>Введите C: <input id="cId" size="5"></p> <hr> <p id="resultId"></p> <button onclick="calc()">Рассчитать</button> </body> </html>