CaioCesar

Fatec - Fixação Aula 09 - Fórmula de Heron com Struct

Oct 27th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include "iostream" // comando cin cout
  2. #include "cstdlib"  // comando system
  3. #include "math.h"   // funções matemáticas
  4. #define n 3
  5.  
  6. using namespace std;
  7.  
  8. typedef struct triangulo tri;
  9. struct triangulo
  10. {   double a[n], b[n], c[n], base[n], altura[n], area[n], semper[n];
  11.     string tipo[n];
  12. };
  13.  
  14. tri t1;
  15.  
  16. double lerA()
  17. { double a;
  18.   system("cls");
  19.   cout << "\nDigite o LADO A: ";
  20.   cin >> a;
  21.   return a;
  22. }
  23.  
  24. double lerB()
  25. { double b;
  26.   system("cls");
  27.   cout << "\nDigite o LADO B: ";
  28.   cin >> b;
  29.   return b;
  30. }
  31.  
  32. double lerC()
  33. { double c;
  34.   system("cls");
  35.   cout << "\nDigite o LADO C: ";
  36.   cin >> c;
  37.   return c;
  38. }
  39.  
  40. string gettipo(double a, double b, double c)
  41. {
  42.        if (a==b && a == c) return "equilatero";
  43.        else if (a!=b && a!=c && b!=c) return "escaleno";
  44.        else return "isoceles";
  45.  
  46.        }
  47.  
  48. double getsemper(double a, double b, double c)
  49. { return ( a+b+c)/2; }
  50.  
  51. double getarea(double a, double b, double c, double s)
  52. {     return sqrt( s*(s-a)* (s-b)* (s-c) );  }
  53.  
  54. double getbase(double a, double b, double c, string tp)
  55. {  if (tp == "equilatero") return a;
  56.   else  if (tp == "escaleno"){
  57.        if (a>b && a > c)  return a;
  58.        else if (b>a && b > c)  return b;
  59.            else return c;}
  60. else{
  61.      if (a==b) return c;
  62.         else if  (a==c) return b;
  63.          else return a;} }
  64.  
  65.  
  66.  
  67. double getaltura(double b, double a)
  68. {  return (2 * a )/b;
  69. }
  70.  
  71. void armazena_struct(double a1, double b1,double c1, string tipo1, int linha)
  72. {
  73.        t1.a[linha] = a1; t1.b[linha] =b1; t1.c[linha] = c1; t1.tipo[linha] = tipo1;
  74. }
  75.  
  76. void processar_struct(int linha)
  77. {
  78.  for (int i=0; i<= linha ;i++)
  79. {   t1.semper[i]  = getsemper(t1.a[i], t1.b[i], t1.c[i] );
  80.     t1.area[i] = getarea(t1.a[i], t1.b[i], t1.c[i], t1.semper[i]  );
  81.    t1.base[i] = getbase(t1.a[i], t1.b[i], t1.c[i], t1.tipo[i]  );
  82.    t1.altura[i] = getaltura(    t1.area[i],    t1.base[i]);
  83. }
  84.  
  85.  
  86. }
  87.  
  88. void telasaida(int linh)
  89. { system("cls");
  90.   for (int i =0; i<= linh; i++)
  91. {
  92.   cout << "\n\nRelatório de Saída " << i+1 << endl;
  93.   cout << "\nValor de LA....: " << t1.a[i];
  94.   cout << "\nValor de LB....: " << t1.b[i];
  95.   cout << "\nValor de LC....: " << t1.c[i];
  96.   cout << "\nValor de TIPO: " << t1.tipo[i];
  97.   cout << "\nValor de BASE...: " << t1.base[i];
  98.   cout << "\nValor de ALTURA...: " << t1.altura[i];
  99.   cout << "\nValor de AREA..: " << t1.area[i];
  100.   cout << "\nValor de SEMPER...: " << t1.semper[i];
  101.   cout << "\n" << endl;
  102.  }
  103.   system("pause");
  104. }
  105.  
  106. int telaentrada() {
  107.     int tecla;
  108.     system("cls");
  109.     cout << "\nTela de Entrada\n";
  110.     cout << "\n1- ler A, B e C";
  111.     cout << "\n2- Calcular ";
  112.     cout << "\n3- exibir ";
  113.    cout << "\n4- Finalizar";
  114.     cout << "\nItem: ";
  115.     cin >> tecla;
  116.     return tecla; }
  117.  
  118. void controlemenu() {
  119.      float a, b, c;
  120.      string tipo;
  121. int tecla = -1, linh = -1, x = -1;
  122.  
  123.  while (tecla != 4)
  124.  { tecla = telaentrada();// chama a tela
  125.    switch (tecla)
  126.     {
  127.         case 1:
  128.             linh ++;
  129.             if(linh<n){
  130.             a = lerA();
  131.             b = lerB();
  132.             c = lerC();
  133.             tipo = gettipo(a,b,c);
  134.             armazena_struct(a,b,c, tipo, linh);
  135.             }
  136.             else{
  137.             system("cls");
  138.             cout << "\nNão há mais espaço para novos dados!\n" << endl;
  139.             linh --;
  140.             system("pause");}
  141.             break;
  142.  
  143.         case 2:
  144.              x++;
  145.              if(x<n){
  146.              system("cls");
  147.              processar_struct(linh);
  148.              cout << "\nCálculo realizado com sucesso!\n" << endl;
  149.              system("pause");
  150.              }
  151.              else{
  152.              system("cls");
  153.              cout << "\nNão há novos dados para cálculo!\n" << endl;
  154.              x--;
  155.              system("pause");}
  156.              break;
  157.  
  158.         case 3:
  159.              telasaida(linh);
  160.             break;
  161.  }}}
  162.  
  163. int main()
  164. {
  165.    setlocale(LC_ALL, "Portuguese");
  166.    controlemenu();
  167.    return 0; }
Advertisement
Add Comment
Please, Sign In to add comment