Практикум по программированию. Основы. Ветвление. Корни уравнения

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
Line 7: Line 7:
 
==Java==
 
==Java==
  
<pre>
+
<source lang="java">
 
import java.util.Scanner;
 
import java.util.Scanner;
 
import java.lang.Math;
 
import java.lang.Math;
Line 14: 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();
 
         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();
+
        double a = in.nextDouble();
  
         System.out.print(" Введите B: ");  
+
         System.out.print(" Введите B: ");
double b = in.nextDouble();
+
        double b = in.nextDouble();
  
         System.out.print(" Введите C: ");  
+
         System.out.print(" Введите C: ");
double c = in.nextDouble();
+
        double c = in.nextDouble();
  
 
         System.out.println("------------------------------------------------");
 
         System.out.println("------------------------------------------------");
                           
 
double d = b * b - 4 * a * c;
 
double x1, x2;
 
  
if (d > 0) {
+
        double d = b * b - 4 * a * c;
 +
        double x1, x2;
  
    x1 = (-b + Math.sqrt(d)) / (2 * a);
+
        if (d > 0) {
    x2 = (-b - Math.sqrt(d)) / (2 * a);
+
  
    System.out.printf(" X1 = %.3f\n", x1);
+
            x1 = (-b + Math.sqrt(d)) / (2 * a);
    System.out.printf(" X2 = %.3f\n", x2);
+
            x2 = (-b - Math.sqrt(d)) / (2 * a);
  
} else {
+
            System.out.printf(" X1 = %.3f\n", x1);
    if (d == 0) {
+
            System.out.printf(" X2 = %.3f\n", x2);
  
x1 = -b / (2 * a);
+
        } else if (d == 0) {
  
System.out.printf(" X = %.3f\n", x1);
+
            x1 = -b / (2 * a);
  
    } else {
+
            System.out.printf(" X = %.3f\n", x1);
  
System.out.println(" Уравнение корней не имеет");
+
        } else {
  
    }
+
            System.out.println(" Уравнение корней не имеет");
}
+
 
 +
        }
 
     }
 
     }
 
}
 
}
</pre>
+
</source>
  
  
 
==C++==
 
==C++==
  
<pre>
+
<source lang="cpp">
 
// g++ 4.2
 
// g++ 4.2
  
Line 100: Line 98:
 
         printf(" X2 = %.3f\n", x2);
 
         printf(" X2 = %.3f\n", x2);
 
          
 
          
     } else {
+
     } else if (d == 0) {
        if (d == 0) {
+
 
              
 
              
            x1 = -b / (2 * a);
+
        x1 = -b / (2 * a);
 
              
 
              
            printf(" X = %.3f\n", x1);
+
        printf(" X = %.3f\n", x1);
 
              
 
              
        } else {
+
    } else {
 
              
 
              
            cout << " Уравнение корней не имеет\n";
+
        cout << " Уравнение корней не имеет\n";
 
              
 
              
        }
 
 
     }
 
     }
 
      
 
      
Line 117: Line 113:
 
     return 0;
 
     return 0;
 
}
 
}
</pre>
+
</source>
  
  
 
==Pascal==
 
==Pascal==
  
<pre>
+
<source lang="delphi">
</pre>
+
</source>
  
  

Revision as of 14:16, 12 March 2016

Задачи · Дальше


Contents

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(" Уравнение корней не имеет");

        }
    }
}


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;
}


Pascal




Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools