Advertisement
argentinapb

Untitled

Jun 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct Data {
  5. int dia;
  6. int mes;
  7. int ano;
  8. };
  9. typedef struct Data Data;
  10.  
  11. struct Endereco {
  12. int numero;
  13. char rua[256];
  14. char cidade[256];
  15. char estado[256];
  16. };
  17. typedef struct Endereco Endereco;
  18.  
  19. struct Cliente {
  20. char cpf[256];
  21. char nome[256];
  22. char telefone[256];
  23. Endereco endereco;
  24. Data nascimento;
  25. struct Cliente* ant;
  26. struct Cliente* prox;
  27. };
  28. typedef struct Cliente Cliente;
  29.  
  30. struct Produto {
  31. char codigo[256];
  32. char descricao[256];
  33. int estoque;
  34. double preco_unitario;
  35. struct Produto* ant;
  36. struct Produto* prox;
  37. };
  38. typedef struct Produto Produto;
  39.  
  40. struct Venda {
  41. Cliente cliente;
  42. Produto produto;
  43. int qtde;
  44. struct Venda* ant;
  45. struct Venda* prox;
  46. };
  47. typedef struct Venda Venda;
  48.  
  49. Cliente* inicioCliente = NULL;
  50. Produto* inicioProduto = NULL;
  51. Venda* inicioVenda = NULL;
  52.  
  53. void pushCliente(Cliente* novoCliente){
  54. if(inicioCliente == NULL) {
  55. inicioCliente = (Cliente*) malloc(sizeof(Cliente));
  56. inicioCliente = novoCliente;
  57. return;
  58. }
  59. Cliente* iterator = inicioCliente;
  60. while(iterator->prox != NULL){
  61. iterator = iterator->prox;
  62. }
  63. iterator->prox = novoCliente;
  64. novoCliente->ant = iterator;
  65. return;
  66. }
  67.  
  68. void apagarCliente(){
  69. printf("digite o cpf: ");
  70. char cpf[256];
  71. int x = 0;
  72. //digitar nome do cliente
  73. fgets(cpf, 256, stdin);
  74. Cliente* iterator = inicioCliente;
  75. Venda *procurar = inicioVenda;
  76. while(procurar != NULL){
  77. if(strcmp(cpf, procurar->cliente.cpf)== 0){
  78. x = 1;
  79. }
  80. procurar = procurar->prox;
  81. }
  82. if(x == 0){
  83. while(iterator != NULL){
  84. if(strcmp(cpf, iterator->cpf) == 0){
  85. if(iterator->ant == NULL && iterator->prox == NULL) {
  86. free(iterator);
  87. inicioCliente = NULL;
  88. } else if(iterator->ant == NULL) {
  89. inicioCliente = iterator->prox;
  90. iterator->prox->ant = NULL;
  91. free(iterator);
  92. } else if(iterator->prox == NULL) {
  93. iterator->ant->prox = NULL;
  94. free(iterator);
  95. } else {
  96. iterator->ant->prox = iterator->prox;
  97. iterator->prox->ant = iterator->ant;
  98. free(iterator);
  99. }
  100. }
  101. iterator = iterator->prox;
  102. }
  103. }
  104. }
  105. void editarCliente(){
  106. char username[256];
  107. //digitar nome do cliente
  108. fgets(username, 256, stdin);
  109. Cliente* iterator = inicioCliente;
  110. while(iterator != NULL){
  111. if(strcmp(username, iterator->nome) == 0){
  112. fgets(iterator->nome, 256, stdin);
  113. fgets(iterator->cpf, 256, stdin);
  114. fgets(iterator->telefone, 256, stdin);
  115. fgets(iterator->endereco.rua, 256, stdin);
  116. scanf("%d", &iterator->endereco.numero);
  117. getchar();
  118. fgets(iterator->endereco.cidade, 256, stdin);
  119. fgets(iterator->endereco.estado, 256, stdin);
  120. scanf("%d/%d/%d", &iterator->nascimento.dia, &iterator->nascimento.mes, &iterator->nascimento.ano);
  121. getchar();
  122. }
  123. iterator = iterator->prox;
  124. }
  125. }
  126.  
  127. void mostrarClientes(){
  128. Cliente* iterator = inicioCliente;
  129. while(iterator != NULL){
  130. printf("---------------------------\n");
  131. printf("Nome: %s", iterator->nome);
  132. printf("CPF: %s", iterator->cpf);
  133. printf("Telefone: %s", iterator->telefone);
  134. printf("Rua: %sNúmero %d\n%s%s", iterator->endereco.rua, iterator->endereco.numero, iterator->endereco.cidade, iterator->endereco.estado);
  135. printf("Nascimento: %d/%d/%d\n", iterator->nascimento.dia, iterator->nascimento.mes, iterator->nascimento.ano);
  136. iterator = iterator->prox;
  137. }
  138. }
  139.  
  140. void pushProduto(Produto* novoProduto){
  141. if(inicioProduto == NULL) {
  142. inicioProduto = (Produto*) malloc(sizeof(Produto));
  143. inicioProduto = novoProduto;
  144. return;
  145. }
  146. Produto* iterator = inicioProduto;
  147. while(iterator->prox != NULL){
  148. iterator = iterator->prox;
  149. }
  150. iterator->prox = novoProduto;
  151. novoProduto->ant = iterator;
  152. return;
  153. }
  154.  
  155. void apagarProduto(){
  156. printf("digite o codigo: ");
  157. char codigoProduto[256];
  158. int x = 0;
  159. //digitar nome do cliente
  160. fgets(codigoProduto, 256, stdin);
  161. Produto* iterator = inicioProduto;
  162. Venda *procurar = inicioVenda;
  163. while(procurar !=NULL){
  164. if(strcmp(codigoProduto, procurar->produto.codigo)== 0){
  165. x = 1;
  166. }
  167. procurar = procurar->prox;
  168. }
  169. if(x == 0){
  170. while(iterator != NULL){
  171. if(strcmp(codigoProduto, iterator->codigo) == 0){
  172. if(iterator->ant == NULL && iterator->prox == NULL) {
  173. free(iterator);
  174. inicioProduto = NULL;
  175. } else if(iterator->ant == NULL) {
  176. inicioProduto = iterator->prox;
  177. iterator->prox->ant = NULL;
  178. free(iterator);
  179. } else if(iterator->prox == NULL) {
  180. iterator->ant->prox = NULL;
  181. free(iterator);
  182. } else {
  183. iterator->ant->prox = iterator->prox;
  184. iterator->prox->ant = iterator->ant;
  185. free(iterator);
  186. }
  187. }
  188. iterator = iterator->prox;
  189. }
  190. }
  191. }
  192. void editarProduto(){
  193. char codigoProduto[256];
  194. fgets(codigoProduto, 256, stdin);
  195. Produto* iterator = inicioProduto;
  196. while(iterator != NULL){
  197. if(strcmp(codigoProduto, iterator->codigo) == 0){
  198. fgets(iterator->codigo, 256, stdin);
  199. fgets(iterator->descricao, 256, stdin);
  200. scanf("%lf", &iterator->preco_unitario);
  201. scanf("%d", &iterator->estoque);
  202. getchar();
  203. }
  204. iterator = iterator->prox;
  205. }
  206. }
  207.  
  208. void mostrarProdutos(){
  209. Produto* iterator = inicioProduto;
  210. while(iterator != NULL){
  211. printf("---------------------------\n");
  212. printf("Codigo: %s", iterator->codigo);
  213. printf("Descricao: %s", iterator->descricao);
  214. printf("Preco unitario: %.2lf\n", iterator->preco_unitario);
  215. printf("Quantidade em estoque: %d\n", iterator->estoque);
  216. iterator = iterator->prox;
  217. }
  218. }
  219. void pushVenda(Venda* novoVenda){
  220. if(inicioVenda == NULL) {
  221. inicioVenda = (Venda*) malloc(sizeof(Venda));
  222. inicioVenda = novoVenda;
  223. return;
  224. }
  225. Venda* iterator = inicioVenda;
  226. while(iterator->prox != NULL){
  227. iterator = iterator->prox;
  228. }
  229. iterator->prox = novoVenda;
  230. novoVenda->ant = iterator;
  231. return;
  232. }
  233. int procura(Cliente *novoCliente, Produto *novoProduto){
  234. int x = 0;
  235. char codigo[256],cpf[256];
  236. printf("digite o cpf: ");
  237. fgets(cpf, 256, stdin);
  238. printf("digite o codigo: ");
  239. fgets(codigo, 256, stdin);
  240. Cliente *aux = inicioCliente;
  241. Produto *aux2 = inicioProduto;
  242. while(aux != NULL){
  243. if(strcmp(cpf , aux->cpf)== 0)
  244. x++;
  245. aux = aux->prox;
  246. }
  247. while(aux2 !=NULL){
  248. if(strcmp(codigo ,aux2->codigo) == 0){
  249. x++;
  250. }
  251. }
  252. return x;
  253.  
  254. }
  255. void apagarVenda(){
  256. char codigoVenda[256];
  257. //digitar nome do cliente
  258. fgets(codigoVenda, 256, stdin);
  259. Venda* iterator = inicioVenda;
  260. while(iterator != NULL){
  261. if(strcmp(codigoVenda, iterator->produto.codigo) == 0){
  262. if(iterator->ant == NULL && iterator->prox == NULL) {
  263. free(iterator);
  264. inicioVenda = NULL;
  265. } else if(iterator->ant == NULL) {
  266. inicioVenda = iterator->prox;
  267. iterator->prox->ant = NULL;
  268. free(iterator);
  269. } else if(iterator->prox == NULL) {
  270. iterator->ant->prox = NULL;
  271. free(iterator);
  272. } else {
  273. iterator->ant->prox = iterator->prox;
  274. iterator->prox->ant = iterator->ant;
  275. free(iterator);
  276. }
  277. }
  278. iterator = iterator->prox;
  279. }
  280. }
  281. void editarVenda(){
  282. char codigoProduto[256];
  283. fgets(codigoProduto, 256, stdin);
  284. Venda* iterator = inicioVenda;
  285. while(iterator != NULL){
  286. if(strcmp(codigoProduto, iterator->produto.codigo) == 0){
  287. fgets(iterator->produto.codigo, 256, stdin);
  288. fgets(iterator->cliente.cpf, 256, stdin);
  289. scanf("%d", &iterator->qtde);
  290. getchar();
  291. }
  292. iterator = iterator->prox;
  293. }
  294. }
  295. void mostrarVendas(){
  296. Venda* iterator = inicioVenda;
  297. while(iterator != NULL){
  298. printf("---------------------------\n");
  299. printf("Quantidade: %d", iterator->qtde);
  300. printf("Cpf do cliente: %s", iterator->cliente.cpf);
  301. printf("Codigo: %s\n", iterator->produto.codigo);
  302. iterator = iterator->prox;
  303. }
  304. }
  305.  
  306. int main(){
  307. int opc;
  308. do {
  309. printf("1.Adicionar Cliente\n2.Mostrar Cliente\n3.Apagar Cliente\n4.Editar Cliente\n5.Adicionar Produto\n6.Mostrar Produto\n7.Apagar Produto\n8.Editar Produto\n9.Adicionar Venda\n10.Mostrar Venda\n11.Apagar Venda\n12.Editar Venda\n0.Sair\n ");
  310. scanf("%d", &opc);
  311. getchar();
  312. switch(opc){
  313. //insere
  314. case 1: {
  315. Cliente* novoCliente = (Cliente*) malloc(sizeof(Cliente));
  316. novoCliente->prox = novoCliente->ant = NULL;
  317. printf("Digite o nome: ");
  318. fgets(novoCliente->nome, 256, stdin);
  319. printf("digite o cpf: ");
  320. fgets(novoCliente->cpf, 256, stdin);
  321. printf("Digite o telefone: ");
  322. fgets(novoCliente->telefone, 256, stdin);
  323. printf("Digite a rua: ");
  324. fgets(novoCliente->endereco.rua, 256, stdin);
  325. printf("Digite o numero: ");
  326. scanf("%d", &novoCliente->endereco.numero);
  327. getchar();
  328. printf("Digite a cidade: ");
  329. fgets(novoCliente->endereco.cidade, 256, stdin);
  330. printf("Digite o estado: ");
  331. fgets(novoCliente->endereco.estado, 256, stdin);
  332. printf("Digite a data de aniversario dd/mm/aaaa: ");
  333. scanf("%d/%d/%d", &novoCliente->nascimento.dia, &novoCliente->nascimento.mes, &novoCliente->nascimento.ano);
  334. getchar();
  335. pushCliente(novoCliente);
  336. break;
  337. //mostra
  338. } case 2: {
  339. mostrarClientes();
  340. break;
  341. //apaga
  342. } case 3: {
  343. apagarCliente();
  344. //edita
  345. } case 4: {
  346. editarCliente();
  347. } case 5: {
  348. Produto* novoProduto = (Produto*) malloc(sizeof(Produto));
  349. novoProduto->prox = novoProduto->ant = NULL;
  350. printf("Digite o codigo: ");
  351. fgets(novoProduto->codigo, 256, stdin);
  352. printf("Digite a descricao: ");
  353. fgets(novoProduto->descricao, 256, stdin);
  354. printf("Digite o preco unitario: ");
  355. scanf("%lf", &novoProduto->preco_unitario);
  356. printf("Digite a quantidade de estoque: ");
  357. scanf("%d", &novoProduto->estoque);
  358. getchar();
  359. pushProduto(novoProduto);
  360. break;
  361. //mostra
  362. } case 6: {
  363. mostrarProdutos();
  364. break;
  365. //apaga
  366. } case 7: {
  367. apagarProduto();
  368. break;
  369. //edita
  370. } case 8: {
  371. editarProduto();
  372. break;
  373. } case 9: {
  374. int t;
  375. t = procura(inicioCliente, inicioProduto);
  376. if(t == 2){
  377. Venda* novoVenda = (Venda*) malloc(sizeof(Venda));
  378. novoVenda->prox = novoVenda->ant = NULL;
  379. printf("Digite o codigo: ");
  380. fgets(novoVenda->produto.codigo,256 ,stdin);
  381. printf("Digite a quantidade: ");
  382. scanf("%d", &novoVenda->qtde);
  383. printf("Digite o CPF: ");
  384. fgets(novoVenda->cliente.cpf, 256, stdin);
  385.  
  386. getchar();
  387. pushVenda(novoVenda);
  388. }
  389. else
  390. printf("codigo ou cpf invalido\n");
  391. break;
  392. } case 10: {
  393. mostrarVendas();
  394. break;
  395. } case 11: {
  396. apagarVenda();
  397. break;
  398.  
  399. } case 12: {
  400. editarVenda();
  401. break;
  402. }
  403.  
  404. }
  405. } while(opc != 0);
  406.  
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement