Guest User

Untitled

a guest
Mar 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. /*
  6. copiar:
  7. 1
  8. ola
  9. 3
  10. 4
  11. 3
  12. 4
  13. 2
  14. ole
  15. 5
  16. 6
  17. 5
  18. 6
  19. 0
  20.  
  21. */
  22. typedef struct eq{
  23.     char nome[40];
  24.     int vit,der,set_g,set_p;
  25.     struct eq *link;
  26. } equipa;
  27. equipa *inicio=NULL, *temp=NULL,*ant=NULL,*aux=NULL;
  28. FILE *fx;
  29. struct fich{
  30.     char nome[40];
  31.     int vit,der,set_g,set_p;
  32. } expe;
  33.  
  34. void inserir(){
  35.     //temp=inicio;
  36.     system("cls");
  37.     cout << "EX 1\n\nPrima 0 para parar\n";
  38.     int n=1;
  39.     char s[40];
  40.     while(n!=0){
  41.         temp=(equipa*) malloc(sizeof(equipa));
  42.         cout << "Nome: "; cin >> s;
  43.         //verificar nome
  44.         if(inicio!=NULL){
  45.             aux=inicio;
  46.             while(strcmp(aux->nome,s) and aux->link!=NULL){
  47.                 aux=aux->link;
  48.             }
  49.             //cout << "depois";
  50.             if(!strcmp(aux->nome,s)){
  51.                 //cout << "Encontramos o nome " << temp->nome << ", com o numero " << temp->numero << endl;
  52.                 //strcpy(temp->nome,str);
  53.                 cout << "Por favor repita o processo\n";
  54.                 continue;
  55.             }
  56.         }
  57.         strcpy(temp->nome,s);
  58.        
  59.         cout << "Vit: "; cin >> temp->vit;
  60.         cout << "Derrotas:"; cin >> temp->der;
  61.         cout << "SET G: "; cin >> temp->set_g;
  62.         cout << "SET P: "; cin >> temp->set_p;
  63.        
  64.         if(inicio==NULL){
  65.            inicio = temp;
  66.            inicio->link=NULL;
  67.         }
  68.         //se for < inserir antes se nao continuo a verificar a lista
  69.         else{
  70.             if(temp->vit<=inicio->vit){
  71.                 temp->link=inicio;
  72.                 inicio=temp;
  73.             }
  74.             else{
  75.                 ant=inicio;
  76.                 aux=inicio;
  77.                 while(aux->vit<temp->vit && aux->link!=NULL){
  78.                     ant=aux;
  79.                     aux=aux->link;
  80.                 }
  81.                 if(aux->vit>temp->vit){
  82.                     ant->link=temp;
  83.                     temp->link=aux;
  84.                 }
  85.                 else if(aux->vit<temp->vit){
  86.                     temp->link=aux->link;
  87.                     aux->link=temp;
  88.                 }
  89.                 //se forem iguais, dividir pelos set's ganhos
  90.                 else{
  91.                     if((aux->set_g - aux->set_p)<(temp->set_g - aux->set_p)){
  92.                         ant->link=temp;
  93.                         temp->link=aux;
  94.                     }
  95.                     else{
  96.                         temp->link=aux->link;
  97.                         aux->link=temp;
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.         cout << "Continuar? "; cin >> n;
  103.     }
  104. }
  105.  
  106. void listar(){
  107.     system("cls");
  108.     cout << "LISTAGEM CARALHOOO\n";
  109.     //cout << "Esta vazia :(\n";
  110.     temp=inicio;
  111.     if(inicio!=NULL){
  112.         while(temp!=NULL){
  113.             cout<<"Nome: "<<temp->nome<<"\nVit: "<< temp->vit<<"\nDerr: "<< temp->der<<"\nSet G: "<<temp->set_g<<"\nSet P: "<<temp->set_p<<"____"<<endl;
  114.             temp=temp->link;
  115.         }
  116.     }
  117.     else cout << "Esta vazia\n";
  118. }
  119. void eliminar(){
  120.     system("cls");
  121.     cout << "ELIMINAR CENA";
  122.     cout << "Insira o numero a pesquisar: ";
  123.     int n; cin >> n;
  124.     temp=inicio;
  125.     if(inicio!=NULL){
  126.         if((inicio->vit + inicio->der)==n){
  127.             cout << "Eliminada a equipa: " << inicio->nome << endl;
  128.             inicio=inicio->link;
  129.         }
  130.         else{
  131.             while((temp->vit + temp->der)!=n and temp->link!=NULL){
  132.                 //cout << "Numero: " << temp->numero << " e o Nome: " << (*temp).nome << endl;
  133.                 //cout << "ola" << endl;
  134.                 aux=temp;
  135.                 temp=temp->link;
  136.             }
  137.             if((temp->vit + temp->der) == n){
  138.                 cout << "Eliminada a equipa: " << temp->nome << endl;
  139.                 //temp=temp->link;
  140.                 aux->link=temp->link;
  141.             }
  142.             else cout << "Nao encontrado\n";
  143.         }
  144.     }
  145.     else cout << "Esta vazia\n";
  146.    
  147. }
  148. //1- ver quantos há - x
  149. //2- os melhores vão ser os das posições (x-n) até ao fim
  150. void exportar(){
  151.     system("cls");
  152.     cout << "expeORTAR\n";
  153.     cout << "Quantos classificados quer? ";
  154.     int n,q=0; cin >> n;
  155.     temp=inicio;
  156.     if(inicio!=NULL){
  157.         while(temp!=NULL){
  158.             q++;
  159.             temp=temp->link;
  160.         }
  161.     }
  162.     temp=inicio;
  163.     int i = 0;
  164.     while((q-n)!=i){
  165.         temp=temp->link;
  166.         i++;
  167.     }
  168.     while(temp!=NULL){
  169.         if(!(fx=fopen("dados.dat","a"))) cout << ("Erro 404\n");
  170.         else{
  171.             expe.vit=temp->vit;
  172.             expe.der=temp->der;
  173.             expe.set_g=temp->set_g;
  174.             expe.set_p=temp->set_p;
  175.             strcpy(expe.nome,temp->nome);
  176.             fwrite(&expe,sizeof(expe),1,fx);
  177.             fclose(fx);
  178.         }
  179.         temp=temp->link;
  180.     }
  181.    
  182. }
  183. void ler(){
  184.     system("cls");
  185.     cout << "LER O CARALHO DO FICHEIRO\n";
  186.     fx=fopen("dados.dat", "r");
  187.     fread(&expe, sizeof(expe), 1, fx);
  188.     while(!feof(fx)){
  189.         cout<<"\nNome: "<<expe.nome<<"\nVit: "<<expe.vit<<"\nDer: "<<expe.der<<"\nSet G: "<<expe.set_g<<"\nSet P: "<<expe.set_p<<"\n_____\n";
  190.         fread(&expe, sizeof(expe),1, fx);
  191.     }
  192.     fclose(fx);
  193. }
  194. void asneiras(){
  195.     temp=inicio;
  196.     if(inicio!=NULL){
  197.         if(!(inicio->set_g)){
  198.             inicio=inicio->link;
  199.         }
  200.         else{
  201.             while(!(temp->set_g) and temp->link!=NULL){
  202.                 aux=temp;
  203.                 temp=temp->link;
  204.             }
  205.             if(!(temp->set_g)){
  206.                 aux->link=temp->link;
  207.             }
  208.         }
  209.     }
  210. }
  211.  
  212. void set0(){
  213.     system("cls");
  214.     cout << "ELIMINAR SET 0\n";
  215.     temp=inicio;
  216.     if(inicio!=NULL){
  217.         if(!(inicio->set_g)){
  218.             //cout << "Eliminada a equipa: " << inicio->nome << endl;
  219.             inicio=inicio->link;
  220.             cout << "Eliminado com sucesso!\n";
  221.         }
  222.         else{
  223.             while(!(temp->set_g) and temp->link!=NULL){
  224.                 //cout << "Numero: " << temp->numero << " e o Nome: " << (*temp).nome << endl;
  225.                 //cout << "ola" << endl;
  226.                 aux=temp;
  227.                 temp=temp->link;
  228.             }
  229.             if(!(temp->set_g)){
  230.                 //cout << "Eliminada a equipa: " << temp->nome << endl;
  231.                 //temp=temp->link;
  232.                 aux->link=temp->link;
  233.                 cout << "Eliminado com sucesso!\n";
  234.             }
  235.             else cout << "Nao encontrado\n";
  236.         }
  237.     }
  238.     else cout << "Esta vazia\n";
  239.     for(int i=0;i<20;i++) asneiras();
  240. }
  241. int main(){
  242.     string s;
  243.     while(true){
  244.         cout << "\n1. Inserir Equipas\n2. Listar\n3. Eliminar\n4. expeortar\n5. Ler Ficheiro\n6. Eliminar Set0\n->";
  245.         cin >> s;
  246.         if(s=="0") break;
  247.         else if(s=="1") inserir();
  248.         else if(s=="2") listar();
  249.         else if(s=="3") eliminar();
  250.         else if(s=="4") exportar();
  251.         else if(s=="5") ler();
  252.         else if(s=="6") set0();
  253.         else {
  254.             system("cls");
  255.             cout << "errozinho\n";
  256.         }
  257.     }
  258.     return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment