Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.86 KB | None | 0 0
  1. /*!
  2.  
  3. * Trabalho de Programação Estruturada
  4.  
  5. *  Integrantes:
  6.  
  7. * - Anderson Fraga
  8.  
  9. * - Teu Solano
  10.  
  11. * - Thiago Garcia
  12.  
  13. *
  14.  
  15. * Data: 30/05/2008 - Professora Beatriz Lux
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. #include <conio.h>
  22.  
  23. #include <stdlib.h>
  24.  
  25. #include <windows.h>
  26.  
  27.  
  28.  
  29. #define MAX 35 //Define qual é o número máximo de alunos
  30.  
  31.  
  32.  
  33. // Definição da estrutura que receberá os dados dos alunos
  34.  
  35. struct alunos {
  36.  
  37.     int matricula;
  38.  
  39.     char datanasc[10];
  40.  
  41.     float altura;
  42.  
  43.     char nome[50];
  44.  
  45. };
  46.  
  47.  
  48.  
  49. // Definição das funções e seus argumentos
  50.  
  51. void listagem_alunos(struct alunos turma[MAX], int qtd);
  52.  
  53. float media_idades(struct alunos turma[MAX], int qtd, char data_atual[]);
  54.  
  55. float media_alturas(struct alunos turma[MAX], int qtd);
  56.  
  57. void lista_shell(struct alunos _turma[MAX], int qtd);
  58.  
  59. int pesquisa_matricula(int busca, struct alunos turma[MAX]);
  60.  
  61.  
  62.  
  63.  
  64.  
  65. main() {
  66.  
  67.     int busca, op = 9, qtd = 0, m = 1, res = 0;
  68.  
  69.     char data_atual[10];
  70.  
  71.        
  72.  
  73.     // Define a variável que receberá a estrutura de alunos
  74.  
  75.     struct alunos turma[MAX];
  76.  
  77.    
  78.  
  79.     system("color 17");
  80.  
  81.    
  82.  
  83.     // Menu base
  84.  
  85.     puts("Trabalho 1 de Programacao Estruturada");
  86.  
  87.     puts("Integrantes:");
  88.  
  89.     puts("- Anderson Fraga");
  90.  
  91.     puts("- Teu Solano");
  92.  
  93.     puts("- Thiago Garcia");
  94.  
  95.     printf("\n\n");
  96.  
  97.     // Para calcular a idade média dos alunos, precisamos saber qual é a data atual
  98.  
  99.     // Tentamos implementar as funções da biblioteca time.h, mas não obtivemos sucesso :'(
  100.  
  101.     puts("Por favor, insira a data atual (dd/mm/aaaa):");
  102.  
  103.     scanf(" %s", &data_atual);
  104.  
  105.    
  106.  
  107.     // Enquanto o valor do menu for diferente de zero, gera o menu
  108.  
  109.     while( op != 0 ) {
  110.  
  111.         puts("Escolha alguma opcao a seguir:");
  112.  
  113.         puts("1 - Inserir dados da turma de alunos");
  114.  
  115.         puts("2 - Mostrar informacoes dos alunos da turma");
  116.  
  117.         puts("3 - Media de altura dos alunos");
  118.  
  119.         puts("4 - Media de idade da turma");
  120.  
  121.         puts("5 - Listar alunos em ordem decrescente da altura");
  122.  
  123.         puts("6 - Buscar aluno por matricula");
  124.  
  125.         puts("0 - Sair");
  126.  
  127.        
  128.  
  129.         scanf(" %d", &op);
  130.  
  131.        
  132.  
  133.         // Escolhe a opção do menu conforme solicitado pelo usuário
  134.  
  135.         switch(op) {
  136.  
  137.             case 1:
  138.  
  139.                 while(m != 0) {
  140.  
  141.                     for(int x = qtd; x < MAX && m != 0; x++) {
  142.  
  143.                         puts("Para finalizar a insercao de dados, digite 0 para a matricula");
  144.  
  145.                         puts("Informe a matricula:");
  146.  
  147.                         scanf(" %i", &m);
  148.  
  149.                        
  150.  
  151.                         if(m != 0) {
  152.  
  153.                             turma[x].matricula = m;
  154.  
  155.                            
  156.  
  157.                             puts("Informe o nome:");
  158.  
  159.                             scanf(" %s", &turma[x].nome);
  160.  
  161.                            
  162.  
  163.                             puts("Informe a altura (x.xx):");
  164.  
  165.                             scanf(" %f", &turma[x].altura);
  166.  
  167.                            
  168.  
  169.                             puts("Informe a data de nascimento (dd/mm/aaaa):");
  170.  
  171.                             scanf(" %s", &turma[x].datanasc);
  172.  
  173.                            
  174.  
  175.                             qtd = x+1;      
  176.  
  177.                         }
  178.  
  179.                     }
  180.  
  181.                 }
  182.  
  183.                 m = 1;
  184.  
  185.             break;
  186.  
  187.             case 2:
  188.  
  189.                 if( qtd == 0 )
  190.  
  191.                     puts("Nao ha dados para calculo");
  192.  
  193.                 else
  194.  
  195.                     listagem_alunos(turma, qtd);
  196.  
  197.             break;
  198.  
  199.             case 3:
  200.  
  201.                 if( qtd == 0 )
  202.  
  203.                     puts("Nao ha dados para calculo");
  204.  
  205.                 else
  206.  
  207.                     printf("A altura media dos alunos da turma e' de %3.2f m \n\n", media_alturas(turma, qtd));
  208.  
  209.             break;
  210.  
  211.             case 4:
  212.  
  213.                 if( qtd == 0 )
  214.  
  215.                     puts("Nao ha dados para calculo");
  216.  
  217.                 else
  218.  
  219.                     printf("A media de idade da turma e' de %4.2f anos\n", media_idades(turma, qtd, data_atual));  
  220.  
  221.             break;
  222.  
  223.             case 5:
  224.  
  225.                 lista_shell(turma, qtd);
  226.  
  227.             break;
  228.  
  229.             case 6:
  230.  
  231.                 if( qtd == 0 )
  232.  
  233.                     puts("Nao ha dados para calculo");
  234.  
  235.                 else {
  236.  
  237.                     puts("Digite a matricula que deseja localizar");
  238.  
  239.                     scanf(" %i", &busca);
  240.  
  241.                    
  242.  
  243.                     res = pesquisa_matricula(busca, turma);
  244.  
  245.                    
  246.  
  247.                     if( res > 0 ) {
  248.  
  249.                         printf("A matricula %i se encontra na posicao %i do vetor\n", busca, res);
  250.  
  251.                     }
  252.  
  253.                     else {
  254.  
  255.                         puts("A matricula solicitada nao esta cadastrada\n");
  256.  
  257.                     }
  258.  
  259.                 }
  260.  
  261.             break;
  262.  
  263.         }
  264.  
  265.     }
  266.  
  267.     getch();
  268.  
  269. }
  270.  
  271.  
  272.  
  273. // Faz a listagem dos alunos, mostrando os seus dados
  274.  
  275. void listagem_alunos(struct alunos turma[MAX], int qtd) {
  276.  
  277.     printf("Atualmente ha %i alunos cadastrados:\n", qtd);
  278.  
  279.    
  280.  
  281.     for(int x = 0; x < qtd && x < MAX; x++) {
  282.  
  283.        printf("Aluno %i: \n - Matricula: %i \n - Nome: %s \n - Altura: %3.2f \n - Data de Nascimento: %s \n\n", x+1, turma[x].matricula, turma[x].nome, turma[x].altura, turma[x].datanasc);
  284.  
  285.     }
  286.  
  287. }
  288.  
  289.  
  290.  
  291. // Calcula a média das alturas dos alunos, o retorno é de um dado float
  292.  
  293. float media_alturas(struct alunos turma[MAX], int qtd) {
  294.  
  295.     float media = 0;
  296.  
  297.    
  298.  
  299.     for(int x = 0; x < qtd; x++) {
  300.  
  301.         media += turma[x].altura;
  302.  
  303.     }
  304.  
  305.    
  306.  
  307.     return media/qtd;
  308.  
  309. }
  310.  
  311.  
  312.  
  313. // Calcula a idade média dos alunos da turma, retorna float.
  314.  
  315. // Como falado anteriormente, tentamos diversas vezes implementar as funções da LIB time.h, como time, ctime, mktime, asctime dentre outras, infelizmente sem sucesso
  316.  
  317. float media_idades(struct alunos turma[MAX], int qtd, char data[]) {
  318.  
  319.     char ano[4], _data[4];
  320.  
  321.     int _ano, soma = 0, res;
  322.  
  323.    
  324.  
  325.     for(int x = 0; x < qtd && x < MAX; x++) {
  326.  
  327.         sprintf(ano, "%c%c%c%c", turma[x].datanasc[6], turma[x].datanasc[7], turma[x].datanasc[8], turma[x].datanasc[9]);    
  328.  
  329.         _ano = atoi(ano); // atoi transforma uma string em um valor inteiro. Biblioteca stdio
  330.  
  331.         soma += _ano;
  332.  
  333.     }
  334.  
  335.    
  336.  
  337.     res = soma/qtd;
  338.  
  339.     sprintf(_data, "%c%c%c%c", data[6], data[7], data[8], data[9]);
  340.  
  341.        
  342.  
  343.     return atoi(_data) - res;
  344.  
  345. }
  346.  
  347.  
  348.  
  349. // Faz a ordenação dos dados através do método Shell
  350.  
  351. void lista_shell(struct alunos _turma[MAX], int qtd) {
  352.  
  353.     int i, j, gap, k, a[5];
  354.  
  355.     alunos x;
  356.  
  357.    
  358.  
  359.     a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;
  360.  
  361.    
  362.  
  363.     for(k = 0; k < 5; k++) {
  364.  
  365.         gap = a[k];
  366.  
  367.         for(i = gap; i < qtd; ++i) {
  368.  
  369.             x = _turma[i];
  370.  
  371.             for(j = i-gap; x.altura < _turma[j].altura && j >= 0; j = j-gap)
  372.  
  373.                 _turma[j+gap] = _turma[j];
  374.  
  375.             _turma[j+gap] = x;
  376.  
  377.         }
  378.  
  379.     }
  380.  
  381.    
  382.  
  383.     // Imprime estrutura ordenada
  384.  
  385.     for(int x = 0; x < qtd; x++) {
  386.  
  387.         printf("Aluno %i - %3.2f \n", _turma[x].matricula, _turma[x].altura);
  388.  
  389.     }
  390.  
  391. }
  392.  
  393.  
  394.  
  395. // Faz a pesquisa da matrícula pelo método de pesquisa binária
  396.  
  397. int pesquisa_matricula(int busca, struct alunos _turma[MAX]) {
  398.  
  399.     int comeco = 0, meio, fim;
  400.  
  401.     fim = MAX - 1;
  402.  
  403.    
  404.  
  405.     int x,y;
  406.  
  407.     alunos aux;
  408.  
  409.    
  410.  
  411.     // Para correta busca, é necessário a estrutura estar ordenada.
  412.  
  413.     // Abaixo, método bolha foi utilizado
  414.  
  415.     for(int x = 0; x < MAX-1; x++) {
  416.  
  417.         for(int y = x+1; y < MAX; y++) {
  418.  
  419.             if(_turma[x].matricula > _turma[y].matricula) {
  420.  
  421.                 aux = _turma[x];
  422.  
  423.                 _turma[x] = _turma[y];
  424.  
  425.                 _turma[y] = aux;
  426.  
  427.             }
  428.  
  429.         }
  430.  
  431.     }  
  432.  
  433.    
  434.  
  435.     // Inicio do processo de busca do valor
  436.  
  437.     while (comeco <= fim) {
  438.  
  439.         meio = (comeco + fim)/2;
  440.  
  441.        
  442.  
  443.         if(busca == _turma[meio].matricula) {
  444.  
  445.             return(meio); // Valor encontrado
  446.  
  447.         }
  448.  
  449.         else {
  450.  
  451.             if(busca < _turma[meio].matricula) {
  452.  
  453.                 fim = meio-1;
  454.  
  455.             }
  456.  
  457.             else {
  458.  
  459.                 comeco = meio + 1;
  460.  
  461.             }
  462.  
  463.         }
  464.  
  465.     }
  466.  
  467.     return(-1); // Caso não foi encontrado a matricula na estrutura, retorna -1
  468.  
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement