Você está na página 1de 7

// trabAlg.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include <tchar.h> #include <stdio.

h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> #define #define #define #define #define AP 1 RP 0 ARQ "test.txt" ARQ2 "test.txt" TM 1000000

struct reg { // char id[2]; char mat[13]; // Matricula //char nome[40]; // nome do aluno //char disc[6]; // nome da disciplina //char nota[3]; // nota na disciplina //char faltas[2]; // numero de faltas //char ap[3]; // Situaao 1 Aprovado, 0 Reprovado } cad; int qtr = TM; char *grr[12]; int *grri1, *grri2; FILE *fp; int menu(void); // mosta menu na tela void add(void); // adiciona registro void apagar(void); // apaga registro void pesq(void); // pesquisa registro void parq_aleatorio(void); // prenche arquivo com valores aleatorios void ord_arq(void); // ordena aquivo int show(void); // armazena matricula a partir do arquivo em um vetor void showm(void); // ordena vetor /* retorna ponteiro do arquivo para a matricula do registro numreg */ char *pg_mat(int numreg); // abre arq no modo modo, retorna 0 sucesso, -1 erro int abrir(char *arq, char modo[5]); // ordena o arquivo fp usando quicksort com limite esquerdo eq e direito dr void qs_arq(FILE *fp, int eq, int dr); // inverte as posioes dos registros i e j no arquivo fp void troca(FILE *fp, long i, long j);

// ordena a string str usando quicksort com limite esquerdo eq e direito dr void qs_str(char str[][12], int eq, int dr); // ordena o vetor de inteiros mat usando quicksort com limite esquerdo eq e dire ito dr void qs(int *mat, int eq, int dr); // ordena o vetor de inteiros mat de tamanho qt usando shell void shell(int *mat, int qt); /* pesquisa binaria no arquivo fp e qtreg registros, retorna inteiro da posicao do registro que contem a string str */ int pesqbin(FILE *fp, char *str, int qtreg); /* retorna os segundos entre as chamadas da funcao. op 0 inicia contagem e retor na 1, op 1 retorna quantidade de seg apos a ultima chamada com parametro 1, op != {0,1} retorna -1 (erro) */ double cont_seg(int op); int main() { char escolha; int tmatriz = qtr; // long t, u; for (; ;) { escolha = menu(); switch (escolha) { case '1': add(); break; case '2': apagar(); break; case '3': pesq(); break; case '4': tmatriz = show(); break; case '5': cont_seg(0); parq_aleatorio(); printf("O processo demorou %f segundos\n", cont_seg(1)); break; case '6': ord_arq(); break; case '7': showm(); break; case '0': exit(0); } } return 0; } int menu() {

char s[2]; do { printf("1 - Inserir\n"); printf("2 - Excluir\n"); printf("3 - Consultar\n"); printf("4 - Colocar matriculas em um vetor\n"); printf("5 - Preencher com valores aleatorios\n"); printf("6 - Ordenar arquivo criado com a opcao 5\n"); printf("7 - ordenar vetor\n"); printf("0 - Sair\n"); printf("Escolha: "); gets(s); printf("\n"); } while (!strchr("12345670", *s)); return tolower(*s); } void add() { if (abrir(ARQ, "a+")) return; /* printf("Digite as informacoes do aluno:\n"); printf("Matricula:"); gets(cad.mat); printf("Nome: "); gets(cad.nome); printf("Disciplina: "); gets(cad.disc); printf("Nota: "); gets(cad.nota); printf("Faltas: "); gets(cad.faltas); printf("Aprovado (0-nao 1-sim): "); gets(cad.ap); fwrite(&cad, sizeof(struct reg), 1, stdout); if (fwrite(&cad, sizeof(struct reg), 1, fp) != 1) printf("Erro ao escrever no arquivo. Tente novamente!"); qtr++;*/ fclose(fp); } void apagar() { } void parq_aleatorio(void) { register int i, j; char k; if (abrir(ARQ2, "w")) return; printf("Digite a quantidade de registros para gerar:\n"); scanf("%d", &qtr); for (j = 0; j < qtr; j++) { // gera campos aleatorios strcpy(cad.mat, "GRR2011"); //strcpy(cad.nome, "Aluno"); //strcpy(cad.disc, "TI"); // cad.id[0]=j%10+'0'; for (i = 7; i < 11; i++) cad.mat[i] = rand() % 10 + '0'; /*for (i = 5; i < 39; i++)

{ while (!isalnum(k = rand() % 127)); cad.nome[i] = k; } for (i = 2; i < 5; i++) cad.disc[i] = rand() % 10 + '0'; for (i = 0; i < 2; i++) cad.nota[i] = rand() % 10 + '0'; cad.faltas[0] = rand() % 10 + '0'; if (atoi(cad.nota) >= 70) cad.ap[0] = AP + '0'; else cad.ap[0] = RP + '0'; // cad.ap[2]='\n';*/ fwrite(&cad, sizeof(struct reg), 1, fp); // escreve no arquivo } fclose(fp); } int abrir(char *arq, char modo[5]) { if ((fp = fopen(arq, modo)) == NULL) { // se erro retorna -1 printf("O arquivo %s nao pode ser aberto", arq); return -1; } return 0; } void ord_arq() { if (abrir(ARQ2, "r+")) return; qs_arq(fp, 0, qtr - 1); fclose(fp); } void qs_arq(FILE *fp, int eq, int dr) { int i = eq, j = dr; char s[12]; strcpy(s, pg_mat((i + j) / 2)); do { while (strcmp(pg_mat(i), s) < 0 && i < dr) i++; while (strcmp(pg_mat(j), s) > 0 && j > eq) j--; if (i <= j) { troca(fp, (long)i, (long)j); i++, j--; } } while (i <= j); if (eq < j) qs_arq(fp, eq, j); if (i < dr) qs_arq(fp, i, dr); } char *pg_mat(int numreg) { fseek(fp, numreg*sizeof(cad), SEEK_SET); fread(&cad, sizeof(cad), 1, fp); return cad.mat; }

void troca(FILE *fp, long i, long j) { char a[sizeof(cad)], b[sizeof(cad)]; fseek(fp, sizeof(cad)*i, fread(a, sizeof(cad), 1, fseek(fp, sizeof(cad)*j, fread(b, sizeof(cad), 1, fseek(fp, fwrite(a, fseek(fp, fwrite(b, } void pesq() { // int i; char s[12]; struct reg *p; p = &cad; printf("Digite a Matricula:\n"); gets(s); if (abrir(ARQ2, "r")) return; fseek(fp, (pesqbin(fp, s, qtr))*sizeof(cad), SEEK_SET); fread(p, sizeof(cad), 1, fp); fwrite(p, sizeof(cad), 1, stdout); // printf("\n%s-%2s\n", cad.mat, p->nota); } int pesqbin(FILE *fp, char *str, int qtreg) { register int ini = 0, meio, fim = qtreg - 1; while (ini <= fim) { meio = (ini + fim) / 2; if (strcmp(pg_mat(meio), str) > 0) fim = meio - 1; else if (strcmp(pg_mat(meio), str) < 0) ini = meio + 1; else return meio; } return -1; } int show() { int i = 0; char s[12]; if ((grri1 = (int *)malloc(qtr * (sizeof(int)))) == NULL || (grri2 = (int *) malloc(qtr * (sizeof(int)))) == NULL) { printf("Memoria insuficiente"); return 0; } if (abrir(ARQ2, "r")) return 0; while (!fseek(fp, i*sizeof(cad), SEEK_SET) && fread(&cad, sizeof(cad), 1, fp )) { // printf("%s\n", cad.mat); // strcpy(grr[i], cad.mat); // strcpy(s, grr[i]); SEEK_SET); fp); SEEK_SET); fp);

sizeof(cad)*j, SEEK_SET); sizeof(cad), 1, fp); sizeof(cad)*i, SEEK_SET); sizeof(cad), 1, fp);

strcpy(s, cad.mat); s[0] = s[1] = s[2] = '0'; grri1[i] = grri2[i] = atoi(s); i++; } return i; } void showm() { register int i; FILE *fp2; /* cont_seg(0); qs_str(grr, 0, qtr - 1); printf("qs_str demorou %f segundos\n", cont_seg(1)); */ cont_seg(0); qs(grri1, 0, qtr - 1); printf("QuickSort demorou %f segundos\n", cont_seg(1)); cont_seg(0); //shell(grri2, qtr); printf("Shell demorou %f segundos\n", cont_seg(1)); if ((fp2 = fopen("matord.txt", "w+")) == NULL) printf("O arquivo nao pode ser aberto"); fprintf(fp2, "matriz ordenada:\n%13s%9s\n", "qs", "shell"); for (i = 0; i < qtr; i++) fprintf(fp2, "%9d\t %9d\t%3s\n", grri1[i], grri2[i], grri1[i] == grri2[i] ? "sim" : "nao"); // fprintf(fp2, "%d\n", grri1[i]); fclose(fp2); } void qs_str(char str[][12], int eq, int dr) { register int i, j; char *x, temp[12]; i = eq, j = dr; x = str[(i + j) / 2]; do { while (strcmp(str[i], x) < 0 && i < dr) i++; while (strcmp(str[j], x) > 0 && j > eq) j--; if (i <= j) { strcpy(temp, str[i]); strcpy(str[i], str[j]); strcpy(str[j], temp); i++, j--; } } while (i <= j); if (eq < j) qs_str(str, eq, j); if (i < dr) qs_str(str, i, dr); // printf("QS String:\n"); // printf("Comparacoes: %d\tTrocas: %d\n", c, t); } void qs(int *mat, int eq, int dr) {

register int i = eq, j = dr; int x, temp; x = mat[(i + j) / 2]; do { while (mat[i] < x && i < dr) i++; while (mat[j] > x && j > eq) j--; if (i <= j) { temp = mat[i]; mat[i] = mat[j]; mat[j] = temp; i++, j--; } } while (i <= j); if (eq < j) qs(mat, eq, j); if (i < dr) qs(mat, i, dr); } void shell(int *mat, int qt) { register int i, j, step, k; int tmp, vstep[7] = {21, 13, 9, 5, 3, 2, 1}; for (k = 0; k < 7; k++) { step = vstep[k]; for (i = step; i < qt; i++) { tmp = mat[i]; for (j = i - step; tmp < mat[j] && j >= 0; j = j - step) mat[j + step] = mat[j]; mat[j + step] = tmp; } } } double cont_seg(int op) { static clock_t start, end; if (op == 0) { start = clock(); return 1; } else if (op == 1) { end = clock(); return ((double)(end - start)) / CLOCKS_PER_SEC; } else return 0; }

Você também pode gostar