Advertisement
Ne-Biolog

Untitled

Mar 3rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. //
  2. // main.c
  3. // lab2_1
  4. //
  5. // Created by Денис Домашевич on 18.02.2018.
  6. // Copyright © 2018 Денис Домашевич. All rights reserved.
  7. //
  8.  
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12.  
  13. bool ok = true;
  14. double AB, AC, CB;
  15. double coordinatesX[3];
  16. double coordinatesY[3];
  17.  
  18. //Проверка на ввод числа типа Int
  19. int readInt()
  20. {
  21. int res;
  22.  
  23. while(true) {
  24. if(scanf("%d", &res) && getchar() == '\n') {
  25. return res;
  26. } else {
  27. printf("Try again\n");
  28. while(getchar() != '\n') {}
  29. }
  30. }
  31. }
  32.  
  33. //Проверка на ввод числа типа Double
  34. double readDouble()
  35. {
  36. double res;
  37.  
  38. while(true) {
  39. if(scanf("%lf", &res) && getchar() == '\n') {
  40. return res;
  41. } else {
  42. printf("Try again\n");
  43. while(getchar() != '\n') {}
  44. }
  45. }
  46. }
  47.  
  48. //Подсчёт расстония между 2-мя точками
  49. double calcDist(double x1, double x2, double y1, double y2) {
  50. return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
  51. }
  52.  
  53. //Ввод кординат треугольника
  54. void enterCoordinates() {
  55. for(int i = 0; i < 3; ++i) {
  56. printf("X%d,", i + 1);
  57. printf("Y%d = ", i + 1);
  58. coordinatesX[i] = readDouble();
  59. coordinatesY[i] = readDouble();
  60. }
  61. AB = calcDist(coordinatesX[0] , coordinatesX[1] , coordinatesY[0], coordinatesY[1]);
  62. AC = calcDist(coordinatesX[1] , coordinatesX[2] , coordinatesY[1], coordinatesY[2]);
  63. CB = calcDist(coordinatesX[2] , coordinatesX[0] , coordinatesY[2], coordinatesY[0]);
  64. }
  65.  
  66. //Определение типа треугольника
  67. void getTypeOfTriangle() {
  68. if(AB == AC && AC == CB) {
  69. printf("Equilateral triangle\n");
  70. } else if(AB * AB + AC * AC == CB * CB) {
  71. printf("Right triangle\n");
  72. } else if(CB * CB + AC * AC == AB * AB) {
  73. printf("Right triangle\n");
  74. } else if(AB * AB + CB * CB == AC * AC) {
  75. printf("right triangle\n");
  76. } else if(AB == CB || AB == AC || CB == AC) {
  77. printf("Isosceles triangle\n");
  78. } else {
  79. printf("Arbitrary triangle\n");
  80. }
  81. }
  82.  
  83. //Подсчёт периметра треугольника
  84. double calcPerimetr() {
  85. return AB + AC + CB;
  86. }
  87.  
  88. //Подсчёт площади трегуольника
  89. double calcArea() {
  90. double p;
  91. p = (AB + AC + CB) / 2;
  92. return sqrt(p * (p - AB) * (p - AC) * (p - CB));
  93. }
  94.  
  95. //Подсчёт вписанного и описанного радиусов треульника
  96. void calcRadii() {
  97. double RadCirCircle, RadInsCircle;
  98.  
  99. RadCirCircle = (AB * AC * CB) / calcArea();
  100. RadInsCircle = 2 * calcArea() / calcPerimetr();
  101. printf("RadCirCircle = %lf\n", RadCirCircle);
  102. printf("RadInsCircle = %lf\n", RadInsCircle);
  103. }
  104.  
  105. //Вывод на экран типов команд
  106. void printTypeOfCommand() {
  107. printf("1.Enter Coordinates\n");
  108. printf("2.Type of triangle\n");
  109. printf("3.Calculate the perimeter\n");
  110. printf("4.Сalculate the area\n");
  111. printf("5.Calculate radii\n");
  112. printf("6.Get info about author of the program\n");
  113. printf("7.EXIT\n\n");
  114.  
  115. }
  116.  
  117. //Вывод информации об авторе программы
  118. void InfoAboutAuthor() {
  119. printf("Created by Denis Domashevich\n");
  120. printf("Students of the BSUIR, the Faculty of CSaN\n");
  121. }
  122.  
  123. int main() {
  124. while(ok) {
  125. printTypeOfCommand();
  126. fflush(stdout);
  127. int command = readInt(); // ввод типа команды
  128. switch (command) {
  129. case 1:
  130. enterCoordinates();
  131. break;
  132. case 2:
  133. getTypeOfTriangle();
  134. break;
  135. case 3:
  136. printf("%lf\n", calcPerimetr());
  137. break;
  138. case 4 :
  139. printf("%lf\n", calcArea());
  140. break;
  141. case 5:
  142. calcRadii();
  143. break;
  144. case 6:
  145. InfoAboutAuthor();
  146. break;
  147. case 7:
  148. return 0;
  149. break;
  150. default:
  151. printf("ERROR try again\n");
  152. break;
  153. }
  154. printf("\n");
  155. }
  156.  
  157. return 0;
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement