Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Solution {
  4. public static Result calculate(float degreease, double epsilon) {
  5. final double rad = Math.toRadians(degreease);
  6. final double sin = Math.sin(rad);
  7. final double x2 = rad * rad;
  8.  
  9. int n = 1;
  10. double U = rad;
  11. double sum = rad;
  12.  
  13. while (Math.abs(sin - sum) > epsilon && n < 100000 ) {
  14. U = (-1) * U * x2 / (2 * n * (2 * n + 1 ));
  15. sum += U;
  16. n++;
  17. }
  18.  
  19.  
  20. final Result result = new Result(degreease, epsilon);
  21. result.sin = sin;
  22. result.count = n;
  23. result.sum = sum;
  24.  
  25. return result;
  26. }
  27.  
  28. public static void main(String[] args) throws Exception {
  29. boolean t = false;
  30. boolean c = false;
  31. String s = "";
  32. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  33. while (!t && !c) {
  34. System.out.println("Полный круг или конекретное значение? R/C");
  35. s = reader.readLine();
  36. t = s.equals("R");
  37. c = s.equals("C");
  38. }
  39. if (s.equals("C")) {
  40. System.out.println("Введите x:");
  41.  
  42.  
  43. final float degreease = Float.parseFloat(reader.readLine());
  44.  
  45. double epsilon = 0;
  46. while (epsilon <= 0) {
  47. System.out.println("Введите epsiolon:");
  48. epsilon = Double.parseDouble(reader.readLine());
  49. }
  50.  
  51. final Result result = calculate(degreease, epsilon);
  52.  
  53. System.out.println("Число слагаемых: " + result.count);
  54. System.out.println("Sin(x) = " + result.sin);
  55. System.out.println("Сумма ряда = " + result.sum);
  56. }
  57. else if (s.equals("R")) {
  58. test();
  59. }
  60. }
  61.  
  62. protected static final String RECORD_PATTERN = "%1.1f -> %s\n";
  63.  
  64. protected static void test() throws Exception {
  65. final double epsilon = 0.0000001;
  66.  
  67. final File file = new File("result.txt");
  68. if (file.exists()) {
  69. file.delete();
  70. }
  71. file.createNewFile();
  72.  
  73. final FileWriter writer = new FileWriter(file);
  74. for (int d = 0; d <= 3600; d++) {
  75. final Result result = calculate(d / 10f - 180, epsilon);
  76. writer.write(String.format(RECORD_PATTERN, result.degreease, result.count));
  77. }
  78.  
  79. writer.close();
  80.  
  81. System.out.println(file.getAbsolutePath());
  82. }
  83.  
  84.  
  85. protected static class Result {
  86. public final float degreease;
  87. public final double epsilon;
  88.  
  89. public int count;
  90. public double sin;
  91. public double sum;
  92.  
  93. public Result(float degreease, double epsilon) {
  94. this.degreease = degreease;
  95. this.epsilon = epsilon;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement