Advertisement
Guest User

c

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. // SistemaCadastro.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <locale.h>
  6. #include <stdio.h>
  7. #include <Windows.h>
  8. #include <stdlib.h>
  9. #include <io.h>
  10.  
  11. int MenuPrincipal()
  12. {
  13. int Option = 0;
  14. printf("\t SISTEMA DE CADASTRO DE VEICULOS \n\n-> Selecione uma opção:\n1 para incluir um novo veículo\n2 para alterar um veículo\n3 para excluir um veículo\n4 para listar todos os veículos\n99 para finalizar o sistema!\nOpção desejada: ");
  15. scanf("%d", &Option);
  16.  
  17. return Option;
  18. }
  19.  
  20. int IncluirVeiculo()
  21. {
  22. return 0;
  23. }
  24.  
  25. int AlterarVeiculo()
  26. {
  27. return 0;
  28. }
  29.  
  30. int ExcluirVeiculo()
  31. {
  32. return 0;
  33. }
  34.  
  35. int ListarVeiculos()
  36. {
  37. ListaDiretorio("C:\\Veiculos");
  38. return 0;
  39. }
  40.  
  41. Veiculos ReadVeiculo(char* path)
  42. {
  43. Veiculos m;
  44.  
  45. if (fileExists(path))
  46. {
  47. FILE* f = fopen(path, "r+");
  48. fread(&m, sizeof(Veiculos), 1, f);
  49. return m;
  50. }
  51. else
  52. {
  53. printf("\n\nVeiculo não cadastrado no banco de dados!");
  54. }
  55. }
  56.  
  57. int WriteVeiculo(int CodigoVeiculo, char* NomeVeiculo, char* Modelo, char* Marca, float ConsumoGas, float ConsumoEta)
  58. {
  59. Veiculos m;
  60.  
  61. char temp[128];
  62. sprintf(temp, "C:\\Veiculos\\%d.bin", CodigoVeiculo);
  63.  
  64. if (fileExists(temp) == false)
  65. {
  66. FILE* f = fopen(temp, "a+");
  67. fwrite(&m, sizeof(Veiculos), 1, f);
  68. printf("\n\nVeiculo cadastrado com sucesso!\n\n");
  69. }
  70. else
  71. {
  72. printf("\n\nVeiculo já está cadastrado em nosso banco de dados!\n\n");
  73. }
  74. }
  75.  
  76. bool fileExists(char *filename)
  77. {
  78. if (FILE *arq = fopen(filename, "r"))
  79. return true;
  80.  
  81. return false;
  82. }
  83.  
  84. bool ListaDiretorio(const char *sDir)
  85. {
  86. struct _finddata_t c_file;
  87. int hFind;
  88.  
  89. char sPath[2048];
  90.  
  91. //Specify a file mask. *.* = We want everything!
  92. sprintf(sPath, "%s\\*.*", sDir);
  93.  
  94. hFind = _findfirst(sPath, &c_file);
  95.  
  96. if (hFind == -1)
  97. {
  98. printf("Erro ao encontrar o diretorio: [%s]\n", sDir);
  99. return false;
  100. }
  101.  
  102. do
  103. {
  104. //Find first file will always return "."
  105. // and ".." as the first two directories.
  106. if (strcmp(c_file.name, ".") != 0
  107. && strcmp(c_file.name, "..") != 0)
  108. {
  109. //Build up our file path using the passed in
  110. // [sDir] and the file/foldername we just found:
  111. sprintf(sPath, "%s\\%s", sDir, c_file.name);
  112.  
  113. Veiculos m;
  114.  
  115. m = ReadVeiculo(sPath);
  116.  
  117. printf("Veiculo:\n\t %d \t %s \t %s \t %s \t %1.2f \t %1.2f\n", m.Codigo, m.Nome, m.Modelo, m.Marca, m.cGasolina, m.cEtanol);
  118. }
  119. } while (_findnext(hFind, &c_file)); //Find the next file.
  120.  
  121. if (hFind != -1)
  122. _findclose(hFind);
  123.  
  124. return true;
  125. }
  126.  
  127. int _tmain(int argc, _TCHAR* argv[])
  128. {
  129. setlocale(LC_ALL, "Portuguese");
  130. int Option = 0;
  131.  
  132. if (!fileExists("C:\\Veiculos\\check-in.txt"))
  133. {
  134. CreateDirectory((LPCWSTR)"C:\\Veiculos", NULL);
  135. FILE *fp = fopen("C:\\Veiculos\\check-in.txt", "rb");
  136. }
  137.  
  138. do
  139. {
  140. Option = MenuPrincipal();
  141.  
  142. switch (Option)
  143. {
  144. //Incluir
  145. case 1:
  146. {
  147. IncluirVeiculo();
  148. break;
  149. }
  150.  
  151. //Alterar
  152. case 2:
  153. {
  154. AlterarVeiculo();
  155. break;
  156. }
  157.  
  158. //Exluir
  159. case 3:
  160. {
  161. ExcluirVeiculo();
  162. break;
  163. }
  164.  
  165. //Listar
  166. case 4:
  167. {
  168. ListarVeiculos();
  169. break;
  170. }
  171.  
  172. case 99:
  173. {
  174. Option = 99;
  175. break;
  176. }
  177.  
  178. default:
  179. {
  180. printf(">>> SELECIONE OUTRA OPÇÃO!\n\n");
  181. Option = 0;
  182. break;
  183. }
  184. }
  185. } while (Option != 99);
  186.  
  187. printf("\nObrigado por usar nossos sistemas...\n");
  188. return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement