Advertisement
Ne-Biolog

Untitled

Mar 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 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. //Проверка на ввод числа типа Int
  14. int readInt() {
  15. int res;
  16. while(true) {
  17. if(scanf("%d", &res) && getchar() == '\n') {
  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. //Подсчёт расстояния между 2-мя точками
  40. double calcDist(double x1, double x2, double y1, double y2) {
  41. return sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
  42. }
  43.  
  44. //Подсчёт площади трегуольника
  45. double calcArea(double AB, double AC, double CB) {
  46. double p;
  47. p = (AB + AC + CB) / 2;
  48. return sqrt(p * (p - AB) * (p - AC) * (p - CB));
  49. }
  50.  
  51. //Ввод кординат треугольника
  52. void enterCoordinates(double coordinatesX[], double coordinatesY[], double *AB, double *AC, double *CB) {
  53.  
  54. while(true) {
  55. for(int i = 0; i < 3; ++i) {
  56. printf("X%d,", i + 1);
  57. printf("Y%d = \n", 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. if(calcArea(*AB, *AC, *CB) != 0) {
  66. break;
  67. } else {
  68. printf("Enter coordinates again\n");
  69. }
  70. }
  71. }
  72.  
  73. //Определение типа треугольника
  74. void getTypeOfTriangle(double AB, double AC, double CB) {
  75.  
  76. if(AB == AC && AC == CB) {
  77. printf("Equilateral triangle\n");
  78. } else if(AB * AB + AC * AC == CB * CB) {
  79. printf("Right triangle\n");
  80. } else if(CB * CB + AC * AC == AB * AB) {
  81. printf("Right triangle\n");
  82. } else if(AB * AB + CB * CB == AC * AC) {
  83. printf("Right triangle\n");
  84. } else if(AB == CB || AB == AC || CB == AC) {
  85. printf("Isosceles triangle\n");
  86. } else {
  87. printf("Arbitrary triangle\n");
  88. }
  89. }
  90.  
  91. //Подсчёт периметра треугольника
  92. double calcPerimeter(double AB, double AC, double CB) {
  93. return AB + AC + CB;
  94. }
  95.  
  96. //Подсчёт вписанного и описанного радиусов треульника
  97. void calcRadii(double AB, double AC, double CB) {
  98. double RadCirCircle, RadInsCircle;
  99. RadCirCircle = (AB * AC * CB) / calcArea(AB, AC, CB);
  100. RadInsCircle = 2 * calcArea(AB, AC, CB) / calcPerimeter(AB, AC, CB);
  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("Student of the BSUIR, the Faculty of CSaN\n");
  121. }
  122.  
  123. int main() {
  124.  
  125. bool ok = true;
  126. double AB = 0, AC = 0, CB = 0;
  127. double coordinatesX[3];
  128. double coordinatesY[3];
  129.  
  130. while(ok) {
  131. printTypeOfCommand();
  132. fflush(stdout);
  133. int command = readInt(); // ввод типа команды
  134. switch (command) {
  135. case 1:
  136. enterCoordinates(coordinatesX, coordinatesY, &AB, &AC, &CB);
  137. break;
  138. case 2:
  139. getTypeOfTriangle(AB, AC, CB);
  140. break;
  141. case 3:
  142. printf("Perimeter %lf\n", calcPerimeter(AB, AC, CB));
  143. break;
  144. case 4 :
  145. printf("Area %lf\n", calcArea(AB, AC, CB));
  146. break;
  147. case 5:
  148. calcRadii(AB, AC, CB);
  149. break;
  150. case 6:
  151. InfoAboutAuthor();
  152. break;
  153. case 7:
  154. return 0;
  155. break;
  156. default:
  157. printf("ERROR try again\n");
  158. break;
  159. }
  160. printf("\n");
  161. }
  162.  
  163. return 0;
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement