CaioCesar

Fatec - Struct - Média Alunos com exclusão de itens do struc

Nov 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <windows.h>
  4. #include <string>
  5. #include <math.h>
  6. #define n 3
  7.  
  8. using namespace std;
  9.  
  10. double const pes1 = 0.15;
  11. double const pes2 = 0.5;
  12. double const pes3 = 0.35;
  13. int linh = -1, aux = 0;
  14.  
  15. typedef struct aluno alu;
  16.  
  17. struct aluno{
  18. string nome[n];
  19. double p1[n], p2[n], tr[n];
  20. string sta[n];};
  21. alu TB1;
  22.  
  23. //Inicia funções de leitura
  24. string lernome(){
  25. system("cls");
  26. string N;
  27. cout << "\nDigite o Nome: ";
  28. cin >> N;
  29. return N;}
  30.  
  31. double lerp1(){
  32. system("cls");
  33. double P1;
  34. cout << "\nDigite a nota da P1: ";
  35. cin >> P1;
  36. return P1;}
  37.  
  38. double lerp2(){
  39. system("cls");
  40. double P2;
  41. cout << "\nDigite a nota da P2: ";
  42. cin >> P2;
  43. return P2;}
  44.  
  45. double lertr(){
  46. system("cls");
  47. double TR;
  48. cout << "\nDigite a nota do trabalho: ";
  49. cin >> TR;
  50. return TR;}
  51.  
  52. //Inicia funções de cálculo
  53. string getStatus(double P1, double P2, double TRAB){
  54. double media = (P1*pes3)+(P2*pes2)+(TRAB*pes1);
  55. if(media>=6) return "Aprovado";
  56. else if(media<6) return "Reprovado";}
  57.  
  58. void inserirstruct(int linha, string NOM, double N1, double N2, double Ntrab){
  59. TB1.nome[linha] = NOM;
  60. TB1.p1[linha] = N1;
  61. TB1.p2[linha] = N2;
  62. TB1.tr[linha] = Ntrab;}
  63.  
  64. void processarstruct(int linha){
  65.     for(int i=0; i<=linha;i++){
  66.         TB1.sta[i] = getStatus(TB1.p1[i], TB1.p2[i], TB1.tr[i]);
  67.     }
  68.     cout << "\n*** Dados armazenados com sucesso! ***\n\n" << endl;
  69.     system("pause");
  70.     }
  71.  
  72. bool remover(string nome, int lin){
  73.  
  74.         if(TB1.nome[lin]==nome || (lin==0 && TB1.nome[0]==nome)) return true;
  75.         else{
  76.  
  77.             for(int i=0; i<=lin; i++){
  78.                 if(TB1.nome[i]==nome){
  79.  
  80.                     for(int j=i; j<lin; j++){ // atualiza todos os campos
  81.                         TB1.nome[j]=TB1.nome[j+1];
  82.                         TB1.p1[j]=TB1.p1[j+1];
  83.                         TB1.p2[j]=TB1.p2[j+1];
  84.                         TB1.tr[j]=TB1.tr[j+1];
  85.                         TB1.sta[j]=TB1.sta[j+1];
  86.                         }
  87.                     return true;
  88.  
  89.                 }else{
  90.                     cout << "\n*** Nome não encontrado! ***\n\n" << endl;
  91.                     system("pause");
  92.                     return false;}
  93.             }
  94.         }
  95. }
  96.  
  97. void exibir(int UL){
  98.     for(int i=0;i<=UL;i++){
  99.         cout << "\nNome: " << TB1.nome[i];
  100.         cout << "\nP1: " << TB1.p1[i];
  101.         cout << "\nP2: " << TB1.p2[i];
  102.         cout << "\nTrabalho: " << TB1.tr[i];
  103.         cout << "\nStatus: " << TB1.sta[i];
  104.         cout << "\n" << endl;}
  105.     system("pause");
  106.     }
  107.  
  108. int tela(){
  109. system("cls");
  110. int T;
  111. cout << "\n *** Média de alunos ***\n" << endl;
  112. cout << "\n1 - Incluir aluno \
  113.         \n2 - Exibir informações\
  114.         \n3 - Excluir aluno\
  115.         \n4 - Sair\
  116.         \n\nItem: ";
  117. cin >> T;
  118. return T;}
  119.  
  120. void menu(){
  121. double nota1, nota2, notatr;
  122. string nomealuno, statusaluno, delnome;
  123. int tecla = -1;
  124. bool excluir;
  125.     while(tecla!=4){//Inicio Menu
  126.         tecla = tela();
  127.             if(tecla==1){
  128.                     aux++;
  129.                     nomealuno = lernome();
  130.                     nota1 = lerp1();
  131.                     nota2 = lerp2();
  132.                     notatr = lertr();
  133.                     linh ++;
  134.                     inserirstruct(linh, nomealuno, nota1, nota2, notatr);
  135.                     Sleep(500);
  136.                     system("cls");
  137.                     cout << "\nSalvando informações..." << endl;
  138.                     processarstruct(linh);
  139.                     Sleep(500);
  140.             }
  141.  
  142.             else if(tecla==2){
  143.                 system("cls");
  144.                 if (aux == 0) cout << "\n *** Sem registros ***\n" << endl;
  145.                 else cout << "\n *** Alunos cadastrados ***\n" << endl;
  146.                 exibir(linh);
  147.             }
  148.  
  149.             else if(tecla==3){
  150.                 aux--;
  151.                 system("cls");
  152.                 cout << "\nDigite o nome que deseja excluir: ";
  153.                 cin >> delnome;
  154.                 if(remover(delnome, linh)){
  155.                         linh--;
  156.                         cout << "\n*** Nome excluído com sucesso! ***\n\n" << endl;
  157.                         system("pause");
  158.                     }
  159.             }
  160.  
  161.             else if(tecla==4){
  162.                 system("cls");
  163.                 cout << "\n\n *** Bye Bye ***\n\n" << endl;
  164.             }
  165.     }//Fim Menu
  166. }
  167.  
  168. int main(){
  169.     setlocale(LC_ALL, "Portuguese");
  170.     system("cls");
  171.     cout << "\n *** Média de alunos ***\n" << endl;
  172.     menu();
  173.     system("pause");
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment