Advertisement
FoguinhoS

Untitled

Nov 18th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <string.h>
  5. #include <conio.h>
  6. #include <iostream>
  7. #include <string.h>
  8. #define MAX 60
  9.  
  10. using namespace std;
  11.  
  12. struct PADRAO
  13. {
  14. int codigo;
  15. char nomeLivro[MAX];
  16. char nomeAutor[MAX];
  17. char genero[MAX];
  18. int ano;
  19. float preco;
  20.  
  21. PADRAO(): codigo(0),nomeLivro(),nomeAutor(),genero(),ano(0),preco(0)
  22. {
  23. }
  24.  
  25. };
  26.  
  27.  
  28.  
  29. void Draw(int slash,const char * options[],HANDLE hConsole,char arrows[],int OPTIONS);
  30. int Move(int * slash, HANDLE hConsole, char arrows[],int OPTIONS);
  31. void menu1(PADRAO livro[], int * QTDLIVROS );
  32. void menu2(PADRAO livro[]);
  33. void menu3(PADRAO livro[]);
  34. void menu4(PADRAO livro[],HANDLE hConsole,int * QTDLIVROS);
  35. int setInfo();
  36. int cstring_cmp(const void *a, const void *b) ;
  37.  
  38.  
  39. int cstring_cmp(const void *a, const void *b)
  40. {
  41. const char **ia = (const char **)a;
  42. const char **ib = (const char **)b;
  43. return strcmp(*ia, *ib);
  44. }
  45.  
  46.  
  47. float GravarFloat(int maximo)
  48. {
  49. fflush(stdin); //Limpa o buffer para evitar problemas.
  50. char numero_s[maximo];
  51. fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
  52. float numero_f = atof(numero_s); //Converte string em Float para retornar no final.
  53. return numero_f;
  54. }
  55.  
  56. //Usado para gravar numeros Inteiros.
  57.  
  58. int GravarInteiro(int maximo)
  59. {
  60. fflush(stdin); //Limpa o buffer para evitar problemas.
  61. char numero_s[maximo];
  62. fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
  63. int numero_i = atoi(numero_s); //Converte string em Int para retornar no final.
  64. return numero_i;
  65. }
  66.  
  67. //Usado para gravar chars
  68.  
  69. char * GravarChar(int maximo, char numero_s[])
  70. {
  71.  
  72. fflush(stdin); //Limpa o buffer para evitar problemas.
  73. fgets(numero_s,maximo,stdin); //Metodo similar a Scanf ou Cin porem mais seguro, Limita??o de caracteres.
  74. for(int i = 0; i < maximo; i++)
  75. {
  76. if(numero_s[i] == '\n')
  77. {
  78. numero_s[i] = '\0';
  79. }
  80. }
  81.  
  82. }
  83.  
  84.  
  85. // Fun??o usada para desenhar o menu e as setas
  86.  
  87. void Draw(int slash,const char * options[],HANDLE hConsole,char arrows[],int OPTIONS)
  88. {
  89.  
  90. system("cls");
  91. for(int i = 0; i < OPTIONS;i++)
  92. {
  93. if(slash == i)
  94. {
  95. SetConsoleTextAttribute(hConsole, 12); // Muda a cor para vermelho
  96. }
  97. else
  98. {
  99. SetConsoleTextAttribute(hConsole, 15); // Muda a cor para branco
  100. }
  101.  
  102. printf(" %c %s\n",arrows[i],options[i]);
  103. }
  104.  
  105. SetConsoleTextAttribute(hConsole, 15);
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. // Fun??o usada para mover o menu com as setas
  113.  
  114. int Move(int * slash, HANDLE hConsole, char arrows[],int OPTIONS)
  115. {
  116.  
  117. char key = getch();
  118. //printf("%d",key); Usado para testar o "id" de cada tecla
  119. //system("pause");
  120.  
  121.  
  122. if(key == 80) // Down Key
  123. {
  124. if(*slash == OPTIONS-1)
  125. {
  126. arrows[*slash] = ' ';
  127. *slash = 0;
  128. arrows[*slash] = (char)26;
  129.  
  130. }
  131. else
  132. {
  133. arrows[*slash] = ' ';
  134. *slash += 1;
  135. arrows[*slash] = (char)26;
  136. }
  137. }
  138. if(key == 72) // Up key
  139. {
  140. if(*slash == 0)
  141. {
  142. arrows[*slash] = ' ';
  143. *slash = OPTIONS-1;
  144. arrows[*slash] = (char)26;
  145.  
  146. }
  147. else
  148. {
  149. arrows[*slash] = ' ';
  150. *slash -= 1;
  151. arrows[*slash] = (char)26;
  152. }
  153. }
  154. if(key == 13)
  155. {
  156. if(*slash >= 0 && *slash < OPTIONS)
  157. {
  158. return *slash;
  159. }
  160.  
  161. }
  162. return (-1);
  163. }
  164.  
  165.  
  166. void menu1(PADRAO livro[],int * QTDLIVROS)
  167. {
  168. system("cls");
  169. int quant = *QTDLIVROS;
  170. printf("Codigo: %d\n",quant);
  171. printf("Titulo: ");
  172. GravarChar(MAX,livro[quant].nomeLivro);
  173. printf("Autor: ");
  174. GravarChar(MAX,livro[quant].nomeAutor);
  175. printf("Genero: ");
  176. GravarChar(MAX,livro[quant].genero);
  177. printf("Ano de Lancamento: ");
  178. livro[quant].ano = GravarInteiro(5);
  179. printf("Preco de Custo: ");
  180. livro[quant].preco = GravarFloat(5);
  181. (*QTDLIVROS)+= 1;
  182. }
  183. void menu2(PADRAO livro[])
  184. {
  185. }
  186. void menu3(PADRAO livro[])
  187. {
  188. }
  189. void menu4(PADRAO livro[],HANDLE hConsole,int * QTDLIVROS)
  190. {
  191. int voltar = 0;
  192. int key = 0;
  193. while(voltar == 0)
  194. {
  195. key = 0;
  196. char arrows[7] ={char(26),' ',' ',' ',' ',' '};
  197. int slash = 0;
  198. const char * options[6]=
  199. {
  200. "1 - Buscar por genero e ano",
  201. "2 - Buscar por preco",
  202. "3 - Numero de livros por genero",
  203. "4 - Livros em ordem alfabetica por nome autor",
  204. "5 - Livros em ordem alfabetica por nome do livro",
  205. "6 - Voltar"
  206. };
  207. int myresult = -1;
  208. while(myresult == (-1))
  209. {
  210. Draw(slash,options,hConsole,arrows,6);
  211. myresult = Move(&slash,hConsole,arrows,6);
  212. }
  213. system("cls");
  214. switch(myresult+1)
  215. {
  216. case 1:
  217.  
  218. printf("Digite o Genero desejado: ");
  219. char cGenero[MAX];
  220. GravarChar(MAX,cGenero);
  221. printf("\nDigite o ano minimo: ");
  222. int iAnoMin;
  223. iAnoMin = GravarInteiro(5);
  224. printf("\nDigite o ano maximo: ");
  225. int iAnoMax;
  226. iAnoMax = GravarInteiro(5);
  227. for(int i = 0; i < *QTDLIVROS; i++)
  228. {
  229.  
  230. if((strcmpi(livro[i].genero,cGenero)==0) && (livro[i].ano >= iAnoMin) && (livro[i].ano <= iAnoMax))
  231. {
  232. printf("\n%s - %s (%d)",livro[i].nomeLivro,livro[i].nomeAutor,livro[i].ano);
  233. }
  234. }
  235. printf("\n Aperte Enter para continuar");
  236. key =0;
  237. while(key != 13)
  238. {
  239. fflush(stdin);
  240. key = getch();
  241. }
  242. break;
  243. case 2:
  244. printf("\nDigite o preco minimo: ");
  245. int iPrecoMin;
  246. iPrecoMin = GravarInteiro(5);
  247. printf("\nDigite o preco maximo: ");
  248. int iPrecoMax;
  249. iPrecoMax = GravarInteiro(5);
  250. for(int i = 0; i < *QTDLIVROS; i++)
  251. {
  252.  
  253. if((livro[i].preco >= iPrecoMin) && (livro[i].preco <= iPrecoMax))
  254. {
  255. printf("\n%s - %s (%d) R$: %.2f",livro[i].nomeLivro,livro[i].nomeAutor,livro[i].ano,livro[i].preco);
  256. }
  257. }
  258. printf("\n Aperte Enter para continuar");
  259. key =0;
  260. while(key != 13)
  261. {
  262. fflush(stdin);
  263. key = getch();
  264. }
  265. break;
  266. case 3:
  267. {
  268. int iGeneros[MAX];
  269. char cGeneros[MAX][MAX];
  270. int iGenerosCadastrados = 0;
  271.  
  272. for(int i = 0; i < *QTDLIVROS; i++)
  273. {
  274. for(int j = 0; j < iGenerosCadastrados+1; j++)
  275. {
  276.  
  277. if(j == iGenerosCadastrados)
  278. {
  279. strcpy(cGeneros[iGenerosCadastrados],livro[i].genero);
  280. iGeneros[iGenerosCadastrados] = 1;
  281. iGenerosCadastrados++;
  282. break;
  283. }
  284.  
  285. if(!( j == (iGenerosCadastrados+1)))
  286. {
  287. if(strcmpi(cGeneros[j],livro[i].genero)==0)
  288. {
  289. iGeneros[j] +=1;
  290. break;
  291. }
  292. }
  293.  
  294.  
  295. }
  296. }
  297.  
  298. for( int i = 0; i < iGenerosCadastrados; i++)
  299. {
  300. printf("%s ( %d )\n",cGeneros[i],iGeneros[i]);
  301. }
  302. printf("\n Aperte Enter para continuar");
  303. key =0;
  304. while(key != 13)
  305. {
  306. fflush(stdin);
  307. key = getch();
  308. }
  309. }
  310. break;
  311. case 4:
  312. {
  313.  
  314. char arrows[7] ={char(26),' '};
  315. int slash = 0;
  316. const char * options[6]=
  317. {
  318. "Quick Sort",
  319. "Bubble Sort",
  320. };
  321.  
  322. int myresult = -1;
  323. while(myresult == (-1))
  324. {
  325. Draw(slash,options,hConsole,arrows,2);
  326. myresult = Move(&slash,hConsole,arrows,2);
  327. }
  328. system("cls");
  329. if(myresult == 0)
  330. {
  331. printf("Q U I C K S O R T\n\n");
  332.  
  333. char * cAutores[*QTDLIVROS];
  334.  
  335. for(int i = 0; i < *QTDLIVROS; i++)
  336. {
  337.  
  338. //cAutores[i] = livro[i].nomeAutor;
  339. memcpy (cAutores[i],livro[i].nomeAutor, strlen(livro[i].nomeAutor)+1 );
  340. char str[100];
  341.  
  342. itoa(i, str, 10);
  343. char str2[101] = "^";
  344. strcat(str2,str);
  345. strcat(cAutores[i],str2);
  346. }
  347.  
  348. size_t strings_len = sizeof(cAutores) / sizeof(char *);
  349. qsort(cAutores, strings_len, sizeof(char *), cstring_cmp); // QUICK SORT > BUBBLE SORT
  350.  
  351. char cVetorSequencia[*QTDLIVROS][MAX];
  352. char cAutoresNovo[*QTDLIVROS][MAX];
  353. int iVetorSequencia[*QTDLIVROS];
  354.  
  355. for(int i = 0; i < *QTDLIVROS; i++)
  356. {
  357. printf("1");
  358. char TEMPSTRING[MAX];
  359. strcpy(TEMPSTRING,cAutores[i]);
  360.  
  361. for( int j = 0;j < MAX; j++)
  362. {
  363. printf("2");
  364. if(TEMPSTRING[j] == '\0')
  365. {
  366. break;
  367. }
  368. if(TEMPSTRING[j] == '^')
  369. {
  370. strcpy(cAutoresNovo[i],"");
  371. printf("%s",cAutoresNovo[i]);
  372. printf("3");
  373. for( int l = 0; l < j; l++)
  374. {
  375. char aux = TEMPSTRING[l];
  376. cAutoresNovo[i][l] = aux;
  377. printf("[%d] %c - ",j,TEMPSTRING[l]);
  378.  
  379. }
  380.  
  381. strcpy(cVetorSequencia[i],"");
  382. for(int k = i+1;k < MAX ;k++)
  383. {
  384.  
  385. int atual = 0;
  386. if(TEMPSTRING[k] == '\0')
  387. {
  388. break;
  389. }
  390. else
  391. {
  392. char aux = TEMPSTRING[k];
  393. cVetorSequencia[i][atual] = aux;
  394. atual ++;
  395.  
  396. }
  397. }
  398. iVetorSequencia[i] = atoi(cVetorSequencia[i]);
  399. break;
  400. }
  401.  
  402. }
  403.  
  404. }
  405. system("cls");
  406. for(int i = 0; i < *QTDLIVROS; i++)
  407. {
  408.  
  409. printf("\n%s - %s (%d) R$: %.2f",livro[iVetorSequencia[i]].nomeLivro,livro[iVetorSequencia[i]].nomeAutor,livro[iVetorSequencia[i]].ano,livro[iVetorSequencia[i]].preco);
  410. }
  411. printf("\n Aperte Enter para continuar");
  412. key =0;
  413. while(key != 13)
  414. {
  415. fflush(stdin);
  416. key = getch();
  417. }
  418. }
  419. else
  420. {
  421. printf("B U B B L E S O R T\n\n");
  422.  
  423. }
  424. }
  425. break;
  426. case 5:
  427. //menu4(livro, hConsole,QTDLIVROS);
  428. break;
  429. case 6:
  430.  
  431. voltar = 1;
  432. break;
  433. }
  434. }
  435. }
  436.  
  437.  
  438.  
  439.  
  440.  
  441. main()
  442. {
  443. int QTDLIVROS = 80;
  444. PADRAO livro[QTDLIVROS];
  445. QTDLIVROS = 0;
  446.  
  447. HANDLE hConsole;
  448. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  449.  
  450.  
  451.  
  452.  
  453. int sair = 0;
  454. while(sair == 0)
  455. {
  456. char arrows[7] ={char(26),' ',' ',' '};
  457. int slash = 0;
  458. system("title Pousada");
  459. int values[] = { 40, 10, 100, 90, 20, 25 };
  460. const char * options[5]=
  461. {
  462. "1 - Adicionar um novo livro no acervo",
  463. "2 - Editar ou Excluir informacoes",
  464. "3 - Buscar um determinado livro",
  465. "4 - Imprimir relatorios",
  466. "5 - Sair"
  467. };
  468.  
  469.  
  470. /*const char * objetos[8]
  471. {
  472. "zata","carro","casa","abelha","moto","barro","minhoca","Ajar"
  473. };
  474.  
  475. size_t strings_len = sizeof(objetos) / sizeof(char *);
  476. qsort(objetos, strings_len, sizeof(char *), cstring_cmp);
  477. */
  478.  
  479. int myresult = -1;
  480. while(myresult == (-1))
  481. {
  482. Draw(slash,options,hConsole,arrows,5);
  483. myresult = Move(&slash,hConsole,arrows,5);
  484. }
  485. switch(myresult+1)
  486. {
  487. case 1:
  488. menu1(livro,&QTDLIVROS);
  489. break;
  490. case 2:
  491. menu2(livro);
  492. break;
  493. case 3:
  494.  
  495. menu3(livro);
  496. break;
  497. case 4:
  498.  
  499. menu4(livro,hConsole,&QTDLIVROS);
  500. break;
  501. case 5:
  502. sair = 1;
  503. break;
  504. }
  505.  
  506. }
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement