Практикум по программированию. Основы. Ветвление. Корни уравнения
From AsIsWiki
(Difference between revisions)
Line 136: | Line 136: | ||
var d = b * b - 4 * a * c; | var d = b * b - 4 * a * c; | ||
var x1, x2; | var x1, x2; | ||
+ | var result1 = "", result2 = ""; | ||
if (d > 0) { | if (d > 0) { | ||
Line 142: | Line 143: | ||
x2 = (-b - Math.sqrt(d)) / (2 * a); | x2 = (-b - Math.sqrt(d)) / (2 * a); | ||
− | + | result1 = "X1 = " + x1.toFixed(3); | |
− | + | result2 = "X2 = " + x2.toFixed(3); | |
− | + | ||
− | + | ||
} else if (d == 0) { | } else if (d == 0) { | ||
Line 151: | Line 150: | ||
x1 = -b / (2 * a); | x1 = -b / (2 * a); | ||
− | + | result1 = "X = " + x1.toFixed(3); | |
− | + | result2 = ""; | |
+ | |||
} else { | } else { | ||
− | + | result1 = "Уравнение корней не имеет"; | |
− | + | result2 = ""; | |
} | } | ||
+ | |||
+ | document.getElementById("result1Id").innerHTML = result1; | ||
+ | document.getElementById("result2Id").innerHTML = result2; | ||
} | } | ||
</script> | </script> |
Revision as of 08:17, 16 March 2016
Contents[hide] |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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( " Уравнение корней не имеет" ); } } } |
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | // 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; } |
Pascal
1 |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <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 result1 = "" , result2 = "" ; if (d > 0) { x1 = (-b + Math.sqrt(d)) / (2 * a); x2 = (-b - Math.sqrt(d)) / (2 * a); result1 = "X1 = " + x1.toFixed(3); result2 = "X2 = " + x2.toFixed(3); } else if (d == 0) { x1 = -b / (2 * a); result1 = "X = " + x1.toFixed(3); result2 = "" ; } else { result1 = "Уравнение корней не имеет" ; result2 = "" ; } document.getElementById( "result1Id" ).innerHTML = result1; document.getElementById( "result2Id" ).innerHTML = result2; } </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= "result1Id" ></p> <p id= "result2Id" ></p> <button onclick= "calc()" >Рассчитать</button> </body> </html> |