Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // lab2_1
- //
- // Created by Денис Домашевич on 18.02.2018.
- // Copyright © 2018 Денис Домашевич. All rights reserved.
- //
- #include <stdbool.h>
- #include <stdio.h>
- #include <math.h>
- bool ok = true;
- double AB, AC, CB;
- double coordinatesX[3];
- double coordinatesY[3];
- //Проверка на ввод числа типа Int
- int readInt()
- {
- int res;
- while(true) {
- if(scanf("%d", &res) && getchar() == '\n') {
- return res;
- } else {
- printf("Try again\n");
- while(getchar() != '\n') {}
- }
- }
- }
- //Проверка на ввод числа типа Double
- double readDouble()
- {
- double res;
- while(true) {
- if(scanf("%lf", &res) && getchar() == '\n') {
- return res;
- } else {
- printf("Try again\n");
- while(getchar() != '\n') {}
- }
- }
- }
- //Подсчёт расстония между 2-мя точками
- double calcDist(double x1, double x2, double y1, double y2) {
- return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
- }
- //Ввод кординат треугольника
- void enterCoordinates() {
- for(int i = 0; i < 3; ++i) {
- printf("X%d,", i + 1);
- printf("Y%d = ", i + 1);
- coordinatesX[i] = readDouble();
- coordinatesY[i] = readDouble();
- }
- AB = calcDist(coordinatesX[0] , coordinatesX[1] , coordinatesY[0], coordinatesY[1]);
- AC = calcDist(coordinatesX[1] , coordinatesX[2] , coordinatesY[1], coordinatesY[2]);
- CB = calcDist(coordinatesX[2] , coordinatesX[0] , coordinatesY[2], coordinatesY[0]);
- }
- //Определение типа треугольника
- void getTypeOfTriangle() {
- if(AB == AC && AC == CB) {
- printf("Equilateral triangle\n");
- } else if(AB * AB + AC * AC == CB * CB) {
- printf("Right triangle\n");
- } else if(CB * CB + AC * AC == AB * AB) {
- printf("Right triangle\n");
- } else if(AB * AB + CB * CB == AC * AC) {
- printf("right triangle\n");
- } else if(AB == CB || AB == AC || CB == AC) {
- printf("Isosceles triangle\n");
- } else {
- printf("Arbitrary triangle\n");
- }
- }
- //Подсчёт периметра треугольника
- double calcPerimetr() {
- return AB + AC + CB;
- }
- //Подсчёт площади трегуольника
- double calcArea() {
- double p;
- p = (AB + AC + CB) / 2;
- return sqrt(p * (p - AB) * (p - AC) * (p - CB));
- }
- //Подсчёт вписанного и описанного радиусов треульника
- void calcRadii() {
- double RadCirCircle, RadInsCircle;
- RadCirCircle = (AB * AC * CB) / calcArea();
- RadInsCircle = 2 * calcArea() / calcPerimetr();
- printf("RadCirCircle = %lf\n", RadCirCircle);
- printf("RadInsCircle = %lf\n", RadInsCircle);
- }
- //Вывод на экран типов команд
- void printTypeOfCommand() {
- printf("1.Enter Coordinates\n");
- printf("2.Type of triangle\n");
- printf("3.Calculate the perimeter\n");
- printf("4.Сalculate the area\n");
- printf("5.Calculate radii\n");
- printf("6.Get info about author of the program\n");
- printf("7.EXIT\n\n");
- }
- //Вывод информации об авторе программы
- void InfoAboutAuthor() {
- printf("Created by Denis Domashevich\n");
- printf("Students of the BSUIR, the Faculty of CSaN\n");
- }
- int main() {
- while(ok) {
- printTypeOfCommand();
- fflush(stdout);
- int command = readInt(); // ввод типа команды
- switch (command) {
- case 1:
- enterCoordinates();
- break;
- case 2:
- getTypeOfTriangle();
- break;
- case 3:
- printf("%lf\n", calcPerimetr());
- break;
- case 4 :
- printf("%lf\n", calcArea());
- break;
- case 5:
- calcRadii();
- break;
- case 6:
- InfoAboutAuthor();
- break;
- case 7:
- return 0;
- break;
- default:
- printf("ERROR try again\n");
- break;
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement