Advertisement
MenddGabriel

Circle Calculator

Oct 23rd, 2021
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.76 KB | None | 0 0
  1. /* Circle Calculator */
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <math.h>
  7.  
  8. #define PI 3.1415
  9.  
  10. void main_menu();
  11. void clear();
  12. void circler();
  13. void triangle();
  14. void rectangle();
  15. void proceed(int instruction);
  16. void(*f[3])() = { circler, triangle, rectangle };
  17.  
  18. int main() {
  19.  
  20.     main_menu();
  21.  
  22.     return 0;
  23. }
  24.  
  25.  
  26. void proceed(int instruction)
  27. {
  28.     int menu;
  29.     printf("\n\n 0 Continue\n"
  30.         " 1 Main menu...\n"
  31.         " Instruction: ");
  32.     scanf("%d", &menu);
  33.     if (menu == 0) {
  34.         (*f[instruction])();
  35.     }
  36.     else {
  37.         main_menu();
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. void rectangle()
  44. {
  45.     clear();
  46.     printf("Rectangle\n");
  47.     proceed(2);
  48. }
  49.  
  50.  
  51.  
  52. void triangle()
  53. {
  54.     clear();
  55.     printf("Triangle\n");
  56.     proceed(1);
  57. }
  58.  
  59.  
  60. void circler()
  61. {
  62.     void calc_cicler(double r, double d, double U, double A);
  63.  
  64.     clear();
  65.     int instruction;
  66.     printf("\t\tCircles\n\n"
  67.         "       *  *        0\tRadius\n"
  68.         "    *        *     1\tDiameter\n"
  69.         "   *          *    2\tPerimeter\n"
  70.         "   *          *    3\tArea\n"
  71.         "    *        *     4\tMain menu\n"
  72.         "       *  *        \n"
  73.         "                   Instruction: ");
  74.  
  75.     scanf("%d", &instruction);
  76.  
  77.  
  78.     double radius = 0;
  79.     double diameter = 0;
  80.     double perimeter = 0;
  81.     double area = 0;
  82.  
  83.     switch (instruction) {
  84.     case 0:
  85.         printf("\n\n\tInform the radius: ");
  86.         scanf("%lf", &radius);
  87.         break;
  88.     case 1:
  89.         printf("\n\n\tInform the diameter: ");
  90.         scanf("%lf", &diameter);
  91.         break;
  92.     case 2:
  93.         printf("\n\n\tInform the perimeter: ");
  94.         scanf("%lf", &perimeter);
  95.         break;
  96.     case 3:
  97.         printf("\n\n\tInform the area: ");
  98.         scanf("%lf", &area);
  99.         break;
  100.     default:
  101.         main_menu();
  102.     }
  103.  
  104.     calc_cicler(radius, diameter, perimeter, area);
  105.     proceed(0);
  106. }
  107.  
  108. void calc_cicler(double r, double d, double U, double A) {
  109.     clear();
  110.    
  111.     if (d != 0) {
  112.         r = d / 2;
  113.         printf("\n Radius:"
  114.             "\t\tr = d : 2\n"
  115.             "\t\tr = %.2lf : 2\n"
  116.             "\t\tr = %.2f\n", d, r);
  117.     }
  118.     else if (U != 0) {
  119.         r = U / (2 * PI);
  120.         printf("\n Radius:"
  121.             "\t\tr = U : (2pi)\n"
  122.             "\t\tr = %.2lf : (2pi)\n"
  123.             "\t\tr = %.2f\n", U, r);
  124.     }
  125.     else if (A != 0) {
  126.         r = sqrt(A / PI);
  127.         printf("\n Radius:"
  128.             "\t\tr^2 = A : pi\n"
  129.             "\t\tr^2 = %.2lf : pi\n"
  130.             "\t\tr = %.2f\n", A, r);
  131.     }
  132.  
  133.     A = r * r * PI;
  134.     printf("\n Area:\t\tA = pi * r^2\n"
  135.         "\t\tA = pi * %.2lf^2\n"
  136.         "\t\tA = %.2f\n", r, A);
  137.  
  138.     U = r * PI * 2;
  139.     printf("\n Perimeter:"
  140.         "\tU = 2 * pi * r\n"
  141.         "\t\tU = 2 * pi * %.2lf\n"
  142.         "\t\tU = %.2f\n", r, U);
  143.  
  144.     d = r * 2;
  145.     printf("\n Diameter:"
  146.         "\td = 2 * r\n"
  147.         "\t\td = 2 * %.2lf\n"
  148.         "\t\td = %.2f\n", r, d);
  149.  
  150.     printf("\n\n Radius = %.2lf\n"
  151.         " Diameter = %.2lf\n"
  152.         " Perimeter = %.2lf\n"
  153.         " Area = %.2lf\n", r, d, U, A);
  154.  
  155. }
  156.  
  157.  
  158. void clear() {
  159.  
  160.     DWORD n;                        /* Número de caracteres escritos */
  161.     DWORD size;                     /* número de caracteres visíveis */
  162.     COORD coord = { 0, 0 };         /* Posição superior esquerda da tela */
  163.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  164.  
  165.     /* Obter um identificador para o console */
  166.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  167.  
  168.     GetConsoleScreenBufferInfo(h, &csbi);
  169.  
  170.     /* Encontre o número de caracteres a serem sobrescritos */
  171.     size = csbi.dwSize.X * csbi.dwSize.Y;
  172.  
  173.     /* Sobrescrever o buffer de tela com espaços em branco */
  174.     FillConsoleOutputCharacter(h, TEXT(' '), size, coord, &n);
  175.     GetConsoleScreenBufferInfo(h, &csbi);
  176.     FillConsoleOutputAttribute(h, csbi.wAttributes, size, coord, &n);
  177.  
  178.     /* Redefine o cursor para a posição superior esquerda */
  179.     SetConsoleCursorPosition(h, coord);
  180.  
  181. }
  182.  
  183. void main_menu() {
  184.  
  185.     clear();
  186.     int instruction;
  187.  
  188.     printf("\tCalculator:\n\n"
  189.         "\t0 \tCircle\n"
  190.         "\t1 \tTriangle\n"
  191.         "\t2 \tRectangle\n"
  192.         "\t3 \tExit\n\n"
  193.         "\tInstruction: ");
  194.  
  195.     scanf("%d", &instruction);
  196.  
  197.     if (instruction >= 0 && instruction < 3) {
  198.         (*f[instruction])();
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement