Advertisement
Guest User

Untitled

a guest
May 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <ctype.h>
  6. #include <conio.h>
  7.  
  8. #define MAX 100
  9.  
  10. struct produto
  11. {
  12. int idProduto, quantidade;
  13. char nome[30];
  14. double valor;
  15. };
  16. int WriteProduto(struct produto Produto)
  17. {
  18. int arquivosGravados;
  19. void IncrementaQuantidade();
  20.  
  21. FILE *file = fopen("produtos.txt", "ab");
  22.  
  23. if (file == NULL)
  24. {
  25. printf("Erro de abertura de arquivo.\n");
  26. return 0;
  27. }
  28.  
  29. arquivosGravados = fwrite(&Produto,sizeof(struct produto), 1, file);
  30.  
  31. if (arquivosGravados != 1)
  32. {
  33. printf("Erro de gravacao do produto.\n");
  34. return 0;
  35. }
  36.  
  37. fclose(file);
  38. IncrementaQuantidade();
  39. return 1;
  40. }
  41. struct produto ReadProduto(int idProduto)
  42. {
  43. struct produto Produto;
  44.  
  45. FILE *file = fopen("produtos.txt", "rb");
  46.  
  47. fseek(file, idProduto*sizeof(struct produto), SEEK_SET);
  48.  
  49. fread(&Produto, sizeof(struct produto), 1, file);
  50.  
  51. fclose(file);
  52.  
  53. return Produto;
  54. }
  55. int CarregarProdutos(struct produto Produto[], int tamanhoVetor)
  56. {
  57. int quantidade;
  58.  
  59. FILE *file = fopen("produtos.txt", "rb");
  60.  
  61. if (file == NULL)
  62. {
  63. printf("\nErro. Nenhum produto registrado.");
  64. return 0;
  65. }
  66.  
  67. quantidade = fread(Produto, sizeof(struct produto), tamanhoVetor, file);
  68.  
  69. if (quantidade != tamanhoVetor)
  70. {
  71. printf("\nErro de gravacao.");
  72. return 0;
  73. }
  74.  
  75. return 1;
  76. }
  77. int GetQuantidade()
  78. {
  79. int quantidade, zero = 0;
  80.  
  81. FILE *file = fopen("config.txt", "rb");
  82.  
  83. if (file == NULL)
  84. {
  85. fclose(file);
  86. file = fopen("config.txt", "wb");
  87. fwrite(&zero, sizeof(int), 1, file);
  88. fclose(file);
  89. file = fopen("config.txt", "rb");
  90. }
  91.  
  92. fread(&quantidade, sizeof(int), 1, file);
  93.  
  94. fclose(file);
  95.  
  96. return quantidade;
  97. }
  98. void IncrementaQuantidade()
  99. {
  100. int valor = GetQuantidade() + 1;
  101. FILE *file = fopen("config.txt", "wb");
  102. fwrite(&valor, sizeof(int), 1, file);
  103. fclose(file);
  104. }
  105. void desenharCabecalhoNota(void)
  106. {
  107. printf("\n =======================================================\n");
  108. printf("\t\t Pobr's Wallet - NOTA FISCAL");
  109. printf("\n =======================================================\n");
  110. }
  111. void desenharCabecalho(void)
  112. {
  113. printf("\n =======================================================\n");
  114. printf("\t\t Pobr's Wallet");
  115. printf("\n =======================================================\n");
  116. }
  117. void desenharCabecalhoEstoque(void)
  118. {
  119. printf("\n =======================================================\n");
  120. printf("\t\t Estoque - Pobr's Wallet");
  121. printf("\n =======================================================\n");
  122. }
  123. int main()
  124. {
  125. setlocale(LC_ALL, "Portuguese");
  126.  
  127. int opInicial, produtos, unidadesProduto[produtos], qtdItens, respostaCliente, i, O, qtdItensEstoque, idCompra;
  128. double cpfCliente, valorProduto[produtos], valorPagar = 0;
  129. char nomeProduto[produtos], nomeCliente[25], procuraProduto[50];
  130. struct produto Produto, Produtos[MAX];
  131.  
  132. //desenharCabecalho();
  133.  
  134. printf("\n Olá Gilberto, bem-vindo!");
  135. system("pause");
  136.  
  137. Sleep(1500);
  138. do
  139. {
  140. fflush(stdin);
  141. system("cls");
  142.  
  143. desenharCabecalho();
  144. do
  145. {
  146. printf("\n O que deseja fazer? \n\n\t [1] - ACESSAR ESTOQUE \n\t [2] - ABRIR ATENDIMENTO \n\t [3] - SAIR");
  147. printf("\n Resposta: "); scanf("%i", &opInicial);
  148. if (opInicial != 1 && opInicial != 2 && opInicial != 3) printf("\n Desculpe, mas não entendi.");
  149. }while(opInicial != 1 && opInicial != 2 && opInicial != 3);
  150.  
  151. switch(opInicial)
  152. {
  153.  
  154. case 1:
  155. printf("\n Acessando o estoque ...");
  156. Sleep(500);
  157. system("cls");
  158.  
  159. desenharCabecalho();
  160.  
  161. do
  162. {
  163. printf("\n O que deseja fazer? \n\n\t [1] - ADICIONAR NOVOS PRODUTOS \n\t [2] - DELETEAR PRODUTOS EXISTENTES \n\t [3] - VOLTAR");
  164. printf("\n Resposta: "); scanf("%d", &O);
  165. if (O < 1 && O > 3) printf ("\n Desculpe, mas não entendi.");
  166. }while (O < 1 || O > 3);
  167.  
  168. switch(O)
  169. {
  170. case 1:
  171.  
  172. do
  173. {
  174. printf("\n Quantos produtos deseja adicionar: "); scanf("%d", &produtos);
  175.  
  176. if(produtos < 0) printf(" ERRO. Reveja sua resposta.");
  177.  
  178. }while(produtos < 0);
  179.  
  180. int i;
  181.  
  182. for (i = 0; i < produtos; i++)
  183. {
  184. fflush(stdin);
  185. printf(" Nome do %iº produto: ", i+1); scanf("%s", &Produto.nome);
  186. printf(" Unidades do %iº produto: ", i+1); scanf("%i", &Produto.quantidade);
  187. printf(" Valor unitário: R$ "); scanf("%lf", &Produto.valor);
  188.  
  189. if (WriteProduto(Produto)) printf("\n Produto adcionado com sucesso.\n");
  190. else printf("\n Erro ao adcionar o arquivo.");
  191. }
  192. printf("\n Voltando para o menu inicial ...");
  193. Sleep(1500);
  194.  
  195. break;
  196. case 2:
  197. printf("\n Opção não configurada.");
  198. Sleep(1500);
  199. break;
  200. }
  201. break;
  202. case 2:
  203. printf("\n Abrindo atendimeno ...");
  204. system("cls");
  205.  
  206. desenharCabecalho();
  207.  
  208. printf(" Olá, como é seu nome? "); scanf("%s", &nomeCliente);
  209.  
  210. printf("\n Bem vindo %s ao Pobr's Market!", nomeCliente);
  211.  
  212. printf("\n Abrindo estoque ...");
  213.  
  214. Sleep(2000);
  215.  
  216. system("CLS");
  217.  
  218. do
  219. {
  220. desenharCabecalho();
  221.  
  222. if (CarregarProdutos(Produtos, GetQuantidade()))
  223. {
  224. int j;
  225.  
  226. for (j = 0; j < GetQuantidade(); j++)
  227. {
  228. printf("\n IdProduto = %2d\tNome = %30s\tQuantidade = %d\tValor = %3.2lf", Produtos[*Produtos+j].idProduto, Produtos[*Produtos+j].nome, Produtos[*Produtos+j].quantidade,Produtos[*Produtos+j].valor);
  229. }
  230.  
  231. printf("\n\n Que produto deseja comprar(id): "); scanf("%d", &idCompra);
  232.  
  233. printf(" Quantas unidades deseja: "); scanf("%i", &qtdItens);
  234.  
  235. valorPagar += Produtos[idCompra].valor*qtdItens;
  236. do
  237. {
  238. printf("\n\n [1] - Continuar compra;\n [2] - Encerrar compra!\n Resposta: "); scanf("%i", &respostaCliente);
  239. }while(respostaCliente != 1 && respostaCliente != 2);
  240.  
  241. printf("\n Total a pagar: %lf", valorPagar);
  242.  
  243. getch();
  244. }
  245. else printf("\n Erro ao carregar os produtos.");
  246.  
  247. Sleep(700);
  248.  
  249. }while(respostaCliente != 2 );
  250.  
  251.  
  252. break;
  253. case 3:
  254. printf("\nFechando o sistema ...");
  255. Sleep(700);
  256. exit(1);
  257. break;
  258. }
  259. }while (opInicial != 3);
  260. return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement