Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Solution {
- public static Result calculate(float degreease, double epsilon) {
- final double rad = Math.toRadians(degreease);
- final double sin = Math.sin(rad);
- final double x2 = rad * rad;
- int n = 1;
- double U = rad;
- double sum = rad;
- while (Math.abs(sin - sum) > epsilon && n < 100000 ) {
- U = (-1) * U * x2 / (2 * n * (2 * n + 1 ));
- sum += U;
- n++;
- }
- final Result result = new Result(degreease, epsilon);
- result.sin = sin;
- result.count = n;
- result.sum = sum;
- return result;
- }
- public static void main(String[] args) throws Exception {
- boolean t = false;
- boolean c = false;
- String s = "";
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- while (!t && !c) {
- System.out.println("Полный круг или конекретное значение? R/C");
- s = reader.readLine();
- t = s.equals("R");
- c = s.equals("C");
- }
- if (s.equals("C")) {
- System.out.println("Введите x:");
- final float degreease = Float.parseFloat(reader.readLine());
- double epsilon = 0;
- while (epsilon <= 0) {
- System.out.println("Введите epsiolon:");
- epsilon = Double.parseDouble(reader.readLine());
- }
- final Result result = calculate(degreease, epsilon);
- System.out.println("Число слагаемых: " + result.count);
- System.out.println("Sin(x) = " + result.sin);
- System.out.println("Сумма ряда = " + result.sum);
- }
- else if (s.equals("R")) {
- test();
- }
- }
- protected static final String RECORD_PATTERN = "%1.1f -> %s\n";
- protected static void test() throws Exception {
- final double epsilon = 0.0000001;
- final File file = new File("result.txt");
- if (file.exists()) {
- file.delete();
- }
- file.createNewFile();
- final FileWriter writer = new FileWriter(file);
- for (int d = 0; d <= 3600; d++) {
- final Result result = calculate(d / 10f - 180, epsilon);
- writer.write(String.format(RECORD_PATTERN, result.degreease, result.count));
- }
- writer.close();
- System.out.println(file.getAbsolutePath());
- }
- protected static class Result {
- public final float degreease;
- public final double epsilon;
- public int count;
- public double sin;
- public double sum;
- public Result(float degreease, double epsilon) {
- this.degreease = degreease;
- this.epsilon = epsilon;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement