Advertisement
argentinapb

Untitled

Jun 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 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. char username[256];
  70. //digitar nome do cliente
  71. fgets(username, 256, stdin);
  72. Cliente* iterator = inicioCliente;
  73. while(iterator != NULL){
  74. if(strcmp(username, iterator->nome) == 0){
  75. if(iterator->ant == NULL && iterator->prox == NULL) {
  76. free(iterator);
  77. inicioCliente = NULL;
  78. } else if(iterator->ant == NULL) {
  79. inicioCliente = iterator->prox;
  80. iterator->prox->ant = NULL;
  81. free(iterator);
  82. } else if(iterator->prox == NULL) {
  83. iterator->ant->prox = NULL;
  84. free(iterator);
  85. } else {
  86. iterator->ant->prox = iterator->prox;
  87. iterator->prox->ant = iterator->ant;
  88. free(iterator);
  89. }
  90. }
  91. iterator = iterator->prox;
  92. }
  93. }
  94.  
  95. void editarCliente(){
  96. char username[256];
  97. //digitar nome do cliente
  98. fgets(username, 256, stdin);
  99. Cliente* iterator = inicioCliente;
  100. while(iterator != NULL){
  101. if(strcmp(username, iterator->nome) == 0){
  102. fgets(iterator->nome, 256, stdin);
  103. fgets(iterator->cpf, 256, stdin);
  104. fgets(iterator->telefone, 256, stdin);
  105. fgets(iterator->endereco.rua, 256, stdin);
  106. scanf("%d", &iterator->endereco.numero);
  107. getchar();
  108. fgets(iterator->endereco.cidade, 256, stdin);
  109. fgets(iterator->endereco.estado, 256, stdin);
  110. scanf("%d/%d/%d", &iterator->nascimento.dia, &iterator->nascimento.mes, &iterator->nascimento.ano);
  111. getchar();
  112. }
  113. iterator = iterator->prox;
  114. }
  115. }
  116.  
  117. void mostrarClientes(){
  118. Cliente* iterator = inicioCliente;
  119. while(iterator != NULL){
  120. printf("---------------------------\n");
  121. printf("Nome: %s", iterator->nome);
  122. printf("CPF: %s", iterator->cpf);
  123. printf("Telefone: %s", iterator->telefone);
  124. printf("Rua: %sNúmero %d\n%s%s", iterator->endereco.rua, iterator->endereco.numero, iterator->endereco.cidade, iterator->endereco.estado);
  125. printf("Nascimento: %d/%d/%d\n", iterator->nascimento.dia, iterator->nascimento.mes, iterator->nascimento.ano);
  126. iterator = iterator->prox;
  127. }
  128. }
  129.  
  130. void pushProduto(Produto* novoProduto){
  131. if(inicioProduto == NULL) {
  132. inicioProduto = (Produto*) malloc(sizeof(Produto));
  133. inicioProduto = novoProduto;
  134. return;
  135. }
  136. Produto* iterator = inicioProduto;
  137. while(iterator->prox != NULL){
  138. iterator = iterator->prox;
  139. }
  140. iterator->prox = novoProduto;
  141. novoProduto->ant = iterator;
  142. return;
  143. }
  144.  
  145. void apagarProduto(){
  146. char codigoProduto[256];
  147. //digitar nome do cliente
  148. fgets(codigoProduto, 256, stdin);
  149. Produto* iterator = inicioProduto;
  150. while(iterator != NULL){
  151. if(strcmp(codigoProduto, iterator->codigo) == 0){
  152. if(iterator->ant == NULL && iterator->prox == NULL) {
  153. free(iterator);
  154. inicioProduto = NULL;
  155. } else if(iterator->ant == NULL) {
  156. inicioProduto = iterator->prox;
  157. iterator->prox->ant = NULL;
  158. free(iterator);
  159. } else if(iterator->prox == NULL) {
  160. iterator->ant->prox = NULL;
  161. free(iterator);
  162. } else {
  163. iterator->ant->prox = iterator->prox;
  164. iterator->prox->ant = iterator->ant;
  165. free(iterator);
  166. }
  167. }
  168. iterator = iterator->prox;
  169. }
  170. }
  171.  
  172. void editarProduto(){
  173. char codigoProduto[256];
  174. fgets(codigoProduto, 256, stdin);
  175. Produto* iterator = inicioProduto;
  176. while(iterator != NULL){
  177. if(strcmp(codigoProduto, iterator->codigo) == 0){
  178. fgets(iterator->codigo, 256, stdin);
  179. fgets(iterator->descricao, 256, stdin);
  180. scanf("%lf", &iterator->preco_unitario);
  181. scanf("%d", &iterator->estoque);
  182. getchar();
  183. }
  184. iterator = iterator->prox;
  185. }
  186. }
  187.  
  188. void mostrarProdutos(){
  189. Produto* iterator = inicioProduto;
  190. while(iterator != NULL){
  191. printf("---------------------------\n");
  192. printf("Codigo: %s", iterator->codigo);
  193. printf("Descricao: %s", iterator->descricao);
  194. printf("Preco unitario: %.2lf\n", iterator->preco_unitario);
  195. printf("Quantidade em estoque: %d\n", iterator->estoque);
  196. iterator = iterator->prox;
  197. }
  198. }
  199. void pushVenda(Venda* novoVenda){
  200. if(inicioVenda == NULL) {
  201. inicioVenda = (Venda*) malloc(sizeof(Venda));
  202. inicioVenda = novoVenda;
  203. return;
  204. }
  205. Venda* iterator = inicioVenda;
  206. while(iterator->prox != NULL){
  207. iterator = iterator->prox;
  208. }
  209. iterator->prox = novoVenda;
  210. novoVenda->ant = iterator;
  211. return;
  212. }
  213. int procura(Cliente *novoCliente, Produto *novoProduto){
  214. int cpf, x = 0;
  215. char codigo[100];
  216. printf("digite o cpf: ");
  217. scanf("%d", &cpf);
  218. printf("digite o codigo: ");
  219. fgets(codigo, 100, stdin);
  220. Cliente *aux = inicioCliente;
  221. Produto *aux2 = inicioProduto;
  222. while(aux != NULL){
  223. if(cpf == aux->cpf)
  224. x++;
  225. aux = aux->prox;
  226. }
  227. while(aux2 !=NULL){
  228. if(strcmp(codigo ,aux2->codigo) == 0){
  229. x++;
  230. }
  231. }
  232. return x;
  233.  
  234. }
  235. void mostrarProdutos(){
  236. Venda* iterator = inicioVenda;
  237. while(iterator != NULL){
  238. printf("---------------------------\n");
  239. printf("Codigo: %s", iterator->codigo);
  240. printf("Descricao: %s", iterator->descricao);
  241. printf("Preco unitario: %.2lf\n", iterator->preco_unitario);
  242. printf("Quantidade em estoque: %d\n", iterator->estoque);
  243. iterator = iterator->prox;
  244. }
  245. }
  246. int main(){
  247. int opc;
  248. do {
  249. scanf("%d", &opc);
  250. getchar();
  251. switch(opc){
  252. //insere
  253. case 1: {
  254. Cliente* novoCliente = (Cliente*) malloc(sizeof(Cliente));
  255. novoCliente->prox = novoCliente->ant = NULL;
  256. fgets(novoCliente->nome, 256, stdin);
  257. fgets(novoCliente->cpf, 256, stdin);
  258. fgets(novoCliente->telefone, 256, stdin);
  259. fgets(novoCliente->endereco.rua, 256, stdin);
  260. scanf("%d", &novoCliente->endereco.numero);
  261. getchar();
  262. fgets(novoCliente->endereco.cidade, 256, stdin);
  263. fgets(novoCliente->endereco.estado, 256, stdin);
  264. scanf("%d/%d/%d", &novoCliente->nascimento.dia, &novoCliente->nascimento.mes, &novoCliente->nascimento.ano);
  265. getchar();
  266. pushCliente(novoCliente);
  267. break;
  268. //mostra
  269. } case 2: {
  270. mostrarClientes();
  271. break;
  272. //apaga
  273. } case 3: {
  274. apagarCliente();
  275. //edita
  276. } case 4: {
  277. editarCliente();
  278. } case 5: {
  279. Produto* novoProduto = (Produto*) malloc(sizeof(Produto));
  280. novoProduto->prox = novoProduto->ant = NULL;
  281. fgets(novoProduto->codigo, 256, stdin);
  282. fgets(novoProduto->descricao, 256, stdin);
  283. scanf("%lf", &novoProduto->preco_unitario);
  284. scanf("%d", &novoProduto->estoque);
  285. getchar();
  286. pushProduto(novoProduto);
  287. break;
  288. //mostra
  289. } case 6: {
  290. mostrarProdutos();
  291. break;
  292. //apaga
  293. } case 7: {
  294. apagarProduto();
  295. break;
  296. //edita
  297. } case 8: {
  298. editarProduto();
  299. break;
  300. } case 9: {
  301. int t;
  302. t = procura(inicioCliente, inicioProduto);
  303. if(t == 2){
  304. Venda* novoVenda = (Venda*) malloc(sizeof(Venda));
  305. novoVenda->prox = novoVenda->ant = NULL;
  306. scanf("%d", &novoVenda->qtde);
  307. scanf("%d", &novoVenda->Cliente.cpf);
  308. getchar();
  309. pushVenda(novoVenda);
  310. }
  311. break;
  312. } case 10:{
  313. mostrarVendas();
  314. break;
  315.  
  316. }
  317.  
  318. }
  319. } while(opc != 0);
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement