Advertisement
Ne-Biolog

Untitled

Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. //
  2. // main.c
  3. // lab2_8
  4. //
  5. // Created by Денис Домашевич on 23.02.2018.
  6. // Copyright © 2018 Денис Домашевич. All rights reserved.
  7. //
  8.  
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12.  
  13. //Проверка на ввод числа тип LongLong
  14. long long readLongLong() {
  15. long long res;
  16. while(true) {
  17. if(scanf("%lld", &res) && getchar() == '\n' && res >= 0) {
  18. return res;
  19. } else {
  20. printf("Try again\n");
  21. while(getchar() != '\n') {}
  22. }
  23. }
  24. }
  25.  
  26. //Проверка на ввод числа типа Double
  27. double readDouble() {
  28. double res;
  29. while(true) {
  30. if(scanf("%lf", &res) && getchar() == '\n') {
  31. return res;
  32. } else {
  33. printf("Try again\n");
  34. while(getchar() != '\n') {}
  35. }
  36. }
  37. }
  38.  
  39. void solve(long long x, double eps, double currY) {
  40. if(x == 0) {
  41. printf("Result of my function %d\n", 0);
  42. printf("Number of iterations %d\n", 0);
  43. printf("Result of standart function %lf\n", sqrt((double)x));
  44. return ;
  45. }
  46.  
  47. long long iter = 0;
  48. while(true) {
  49. iter++; // Подсчёт количества итераций
  50. double newY = (currY + (x / currY)) / 2;
  51. if(eps + newY >= currY) {
  52. break;
  53. }
  54. currY = newY;
  55. }
  56.  
  57. printf("Result of my function %lf\n", currY);
  58. printf("Number of iterations %lld\n", iter);
  59. printf("Result of standart function %lf\n", sqrt((double)x));
  60. }
  61.  
  62. int main() {
  63.  
  64. printf("Enter x = \n");
  65. long long x = readLongLong();
  66. printf("Enter eps = \n");
  67. double eps = readDouble(), Y = (double)x;
  68.  
  69. solve(x, eps, Y);
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement