Baixe o app para aproveitar ainda mais
Prévia do material em texto
P2 – 2014/1 – Gabarito 1.a int compara(Bolsista * bol, char * tipo, int matricula) { int resp; resp = strcmp(bol->tipoDaBolsa,tipo); if (resp !=0) return resp; if (bol->matricula < matricula) return -1; if (bol->matricula > matricula) return 1; return 0; } int EhBolsista(Bolsista ** v, int n, char * tipo, int matricula) { int ini=0,fim=n-1,meio,resp; while(ini<=fim) { meio=(ini+fim)/2; resp = compara(v[meio],tipo,matricula); if (resp <0) ini=meio+1; else if (resp >0) fim=meio-1; else return 1; } return 0; } � 1.B) Pedido * eliminaPedidosRepetidos(Bolsista ** v, int n, Pedido * lst) { Pedido *p, *aux, *ant=NULL; p=lst; while (p!=NULL) { if (EhBolsista(v,n,p->tipoDoPedido,p->matricula)) { if (ant==NULL) { aux=lst; lst=lst->prox; free(aux); p=lst; } else { ant->prox=p->prox; free(p); p=ant->prox; } } else { ant = p; p=p->prox; } } return lst; } � 2) int compData(Data d1,Data d2) { if (d1.ano < d2.ano) return -1; if (d1.ano > d2.ano) return 1; if (d1.mes < d2.mes) return -1; if (d1.mes > d2.mes) return 1; return 0; } int menor (Bolsista * b, Bolsista * x) { int resp = compData(b->dataInicio, x->dataInicio); return resp >0 || (resp == 0 && strcmp(b->nome,x->nome)>0); } void ordenaDataInicio(Bolsista ** v, int n) { int a = 1, b = n-1; Bolsista * x; Bolsista * temp; if (n<=1) return; x=v[0]; do { while (a<n && !menor(v[a],x)) a++; while (menor(v[b],x)) b--; if (a<b) { temp = v[a]; v[a] = v[b]; v[b] = temp; a++; b--; } } while (a<=b); v[0]=v[b]; v[b]=x; ordenaDataInicio(v,b); ordenaDataInicio(&v[a],n-a); } � Q3) int maiorMatricula(Pedido * lst) { int max1, max2; if (lst==NULL) return -1; max1 = lst->matricula; max2 = maiorMatricula (lst->prox); if (max1 > max2) return max1; else return max2; }
Compartilhar