Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- #include <conio.h>
- #include <iostream>
- #include <string.h>
- #define MAX 60
- using namespace std;
- struct PADRAO
- {
- int codigo;
- char nomeLivro[MAX];
- char nomeAutor[MAX];
- char genero[MAX];
- int ano;
- float preco;
- PADRAO(): codigo(0),nomeLivro(),nomeAutor(),genero(),ano(0),preco(0)
- {
- }
- };
- void Draw(int slash,const char * options[],HANDLE hConsole,char arrows[],int OPTIONS);
- int Move(int * slash, HANDLE hConsole, char arrows[],int OPTIONS);
- void menu1(PADRAO livro[], int * QTDLIVROS );
- void menu2(PADRAO livro[]);
- void menu3(PADRAO livro[]);
- void menu4(PADRAO livro[],HANDLE hConsole,int * QTDLIVROS);
- int setInfo();
- int cstring_cmp(const void *a, const void *b) ;
- int cstring_cmp(const void *a, const void *b)
- {
- const char **ia = (const char **)a;
- const char **ib = (const char **)b;
- return strcmp(*ia, *ib);
- }
- float GravarFloat(int maximo)
- {
- fflush(stdin); //Limpa o buffer para evitar problemas.
- char numero_s[maximo];
- fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
- float numero_f = atof(numero_s); //Converte string em Float para retornar no final.
- return numero_f;
- }
- //Usado para gravar numeros Inteiros.
- int GravarInteiro(int maximo)
- {
- fflush(stdin); //Limpa o buffer para evitar problemas.
- char numero_s[maximo];
- fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
- int numero_i = atoi(numero_s); //Converte string em Int para retornar no final.
- return numero_i;
- }
- //Usado para gravar chars
- char * GravarChar(int maximo, char numero_s[])
- {
- fflush(stdin); //Limpa o buffer para evitar problemas.
- fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
- for(int i = 0; i < maximo; i++)
- {
- if(numero_s[i] == '\n')
- {
- numero_s[i] = '\0';
- }
- }
- }
- // Fun??o usada para desenhar o menu e as setas
- void Draw(int slash,const char * options[],HANDLE hConsole,char arrows[],int OPTIONS)
- {
- system("cls");
- for(int i = 0; i < OPTIONS;i++)
- {
- if(slash == i)
- {
- SetConsoleTextAttribute(hConsole, 12); // Muda a cor para vermelho
- }
- else
- {
- SetConsoleTextAttribute(hConsole, 15); // Muda a cor para branco
- }
- printf(" %c %s\n",arrows[i],options[i]);
- }
- SetConsoleTextAttribute(hConsole, 15);
- }
- // Fun??o usada para mover o menu com as setas
- int Move(int * slash, HANDLE hConsole, char arrows[],int OPTIONS)
- {
- char key = getch();
- //printf("%d",key); Usado para testar o "id" de cada tecla
- //system("pause");
- if(key == 80) // Down Key
- {
- if(*slash == OPTIONS-1)
- {
- arrows[*slash] = ' ';
- *slash = 0;
- arrows[*slash] = (char)26;
- }
- else
- {
- arrows[*slash] = ' ';
- *slash += 1;
- arrows[*slash] = (char)26;
- }
- }
- if(key == 72) // Up key
- {
- if(*slash == 0)
- {
- arrows[*slash] = ' ';
- *slash = OPTIONS-1;
- arrows[*slash] = (char)26;
- }
- else
- {
- arrows[*slash] = ' ';
- *slash -= 1;
- arrows[*slash] = (char)26;
- }
- }
- if(key == 13)
- {
- if(*slash >= 0 && *slash < OPTIONS)
- {
- return *slash;
- }
- }
- return (-1);
- }
- void menu1(PADRAO livro[],int * QTDLIVROS)
- {
- system("cls");
- int quant = *QTDLIVROS;
- printf("Codigo: %d\n",quant);
- printf("Titulo: ");
- GravarChar(MAX,livro[quant].nomeLivro);
- printf("Autor: ");
- GravarChar(MAX,livro[quant].nomeAutor);
- printf("Genero: ");
- GravarChar(MAX,livro[quant].genero);
- printf("Ano de Lancamento: ");
- livro[quant].ano = GravarInteiro(5);
- printf("Preco de Custo: ");
- livro[quant].preco = GravarFloat(5);
- (*QTDLIVROS)+= 1;
- }
- void menu2(PADRAO livro[])
- {
- }
- void menu3(PADRAO livro[])
- {
- }
- void menu4(PADRAO livro[],HANDLE hConsole,int * QTDLIVROS)
- {
- int voltar = 0;
- int key = 0;
- while(voltar == 0)
- {
- key = 0;
- char arrows[7] ={char(26),' ',' ',' ',' ',' '};
- int slash = 0;
- const char * options[6]=
- {
- "1 - Buscar por genero e ano",
- "2 - Buscar por preco",
- "3 - Numero de livros por genero",
- "4 - Livros em ordem alfabetica por nome autor",
- "5 - Livros em ordem alfabetica por nome do livro",
- "6 - Voltar"
- };
- int myresult = -1;
- while(myresult == (-1))
- {
- Draw(slash,options,hConsole,arrows,6);
- myresult = Move(&slash,hConsole,arrows,6);
- }
- system("cls");
- switch(myresult+1)
- {
- case 1:
- printf("Digite o Genero desejado: ");
- char cGenero[MAX];
- GravarChar(MAX,cGenero);
- printf("\nDigite o ano minimo: ");
- int iAnoMin;
- iAnoMin = GravarInteiro(5);
- printf("\nDigite o ano maximo: ");
- int iAnoMax;
- iAnoMax = GravarInteiro(5);
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- if((strcmpi(livro[i].genero,cGenero)==0) && (livro[i].ano >= iAnoMin) && (livro[i].ano <= iAnoMax))
- {
- printf("\n%s - %s (%d)",livro[i].nomeLivro,livro[i].nomeAutor,livro[i].ano);
- }
- }
- printf("\n Aperte Enter para continuar");
- key =0;
- while(key != 13)
- {
- fflush(stdin);
- key = getch();
- }
- break;
- case 2:
- printf("\nDigite o preco minimo: ");
- int iPrecoMin;
- iPrecoMin = GravarInteiro(5);
- printf("\nDigite o preco maximo: ");
- int iPrecoMax;
- iPrecoMax = GravarInteiro(5);
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- if((livro[i].preco >= iPrecoMin) && (livro[i].preco <= iPrecoMax))
- {
- printf("\n%s - %s (%d) R$: %.2f",livro[i].nomeLivro,livro[i].nomeAutor,livro[i].ano,livro[i].preco);
- }
- }
- printf("\n Aperte Enter para continuar");
- key =0;
- while(key != 13)
- {
- fflush(stdin);
- key = getch();
- }
- break;
- case 3:
- {
- int iGeneros[MAX];
- char cGeneros[MAX][MAX];
- int iGenerosCadastrados = 0;
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- for(int j = 0; j < iGenerosCadastrados+1; j++)
- {
- if(j == iGenerosCadastrados)
- {
- strcpy(cGeneros[iGenerosCadastrados],livro[i].genero);
- iGeneros[iGenerosCadastrados] = 1;
- iGenerosCadastrados++;
- break;
- }
- if(!( j == (iGenerosCadastrados+1)))
- {
- if(strcmpi(cGeneros[j],livro[i].genero)==0)
- {
- iGeneros[j] +=1;
- break;
- }
- }
- }
- }
- for( int i = 0; i < iGenerosCadastrados; i++)
- {
- printf("%s ( %d )\n",cGeneros[i],iGeneros[i]);
- }
- printf("\n Aperte Enter para continuar");
- key =0;
- while(key != 13)
- {
- fflush(stdin);
- key = getch();
- }
- }
- break;
- case 4:
- {
- char arrows[7] ={char(26),' '};
- int slash = 0;
- const char * options[6]=
- {
- "Quick Sort",
- "Bubble Sort",
- };
- int myresult = -1;
- while(myresult == (-1))
- {
- Draw(slash,options,hConsole,arrows,2);
- myresult = Move(&slash,hConsole,arrows,2);
- }
- system("cls");
- if(myresult == 0)
- {
- printf("Q U I C K S O R T\n\n");
- char * cAutores[*QTDLIVROS];
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- //cAutores[i] = livro[i].nomeAutor;
- memcpy (cAutores[i],livro[i].nomeAutor, strlen(livro[i].nomeAutor)+1 );
- char str[100];
- itoa(i, str, 10);
- char str2[101] = "^";
- strcat(str2,str);
- strcat(cAutores[i],str2);
- }
- size_t strings_len = sizeof(cAutores) / sizeof(char *);
- qsort(cAutores, strings_len, sizeof(char *), cstring_cmp); // QUICK SORT > BUBBLE SORT
- char cVetorSequencia[*QTDLIVROS][MAX];
- char cAutoresNovo[*QTDLIVROS][MAX];
- int iVetorSequencia[*QTDLIVROS];
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- printf("1");
- char TEMPSTRING[MAX];
- strcpy(TEMPSTRING,cAutores[i]);
- for( int j = 0;j < MAX; j++)
- {
- printf("2");
- if(TEMPSTRING[j] == '\0')
- {
- break;
- }
- if(TEMPSTRING[j] == '^')
- {
- strcpy(cAutoresNovo[i],"");
- printf("%s",cAutoresNovo[i]);
- printf("3");
- for( int l = 0; l < j; l++)
- {
- char aux = TEMPSTRING[l];
- cAutoresNovo[i][l] = aux;
- printf("[%d] %c - ",j,TEMPSTRING[l]);
- }
- strcpy(cVetorSequencia[i],"");
- for(int k = i+1;k < MAX ;k++)
- {
- int atual = 0;
- if(TEMPSTRING[k] == '\0')
- {
- break;
- }
- else
- {
- char aux = TEMPSTRING[k];
- cVetorSequencia[i][atual] = aux;
- atual ++;
- }
- }
- iVetorSequencia[i] = atoi(cVetorSequencia[i]);
- break;
- }
- }
- }
- system("cls");
- for(int i = 0; i < *QTDLIVROS; i++)
- {
- printf("\n%s - %s (%d) R$: %.2f",livro[iVetorSequencia[i]].nomeLivro,livro[iVetorSequencia[i]].nomeAutor,livro[iVetorSequencia[i]].ano,livro[iVetorSequencia[i]].preco);
- }
- printf("\n Aperte Enter para continuar");
- key =0;
- while(key != 13)
- {
- fflush(stdin);
- key = getch();
- }
- }
- else
- {
- printf("B U B B L E S O R T\n\n");
- }
- }
- break;
- case 5:
- //menu4(livro, hConsole,QTDLIVROS);
- break;
- case 6:
- voltar = 1;
- break;
- }
- }
- }
- main()
- {
- int QTDLIVROS = 80;
- PADRAO livro[QTDLIVROS];
- QTDLIVROS = 0;
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- int sair = 0;
- while(sair == 0)
- {
- char arrows[7] ={char(26),' ',' ',' '};
- int slash = 0;
- system("title Pousada");
- int values[] = { 40, 10, 100, 90, 20, 25 };
- const char * options[5]=
- {
- "1 - Adicionar um novo livro no acervo",
- "2 - Editar ou Excluir informacoes",
- "3 - Buscar um determinado livro",
- "4 - Imprimir relatorios",
- "5 - Sair"
- };
- /*const char * objetos[8]
- {
- "zata","carro","casa","abelha","moto","barro","minhoca","Ajar"
- };
- size_t strings_len = sizeof(objetos) / sizeof(char *);
- qsort(objetos, strings_len, sizeof(char *), cstring_cmp);
- */
- int myresult = -1;
- while(myresult == (-1))
- {
- Draw(slash,options,hConsole,arrows,5);
- myresult = Move(&slash,hConsole,arrows,5);
- }
- switch(myresult+1)
- {
- case 1:
- menu1(livro,&QTDLIVROS);
- break;
- case 2:
- menu2(livro);
- break;
- case 3:
- menu3(livro);
- break;
- case 4:
- menu4(livro,hConsole,&QTDLIVROS);
- break;
- case 5:
- sair = 1;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement