Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Data {
- int dia;
- int mes;
- int ano;
- };
- typedef struct Data Data;
- struct Endereco {
- int numero;
- char rua[256];
- char cidade[256];
- char estado[256];
- };
- typedef struct Endereco Endereco;
- struct Cliente {
- char cpf[256];
- char nome[256];
- char telefone[256];
- Endereco endereco;
- Data nascimento;
- struct Cliente* ant;
- struct Cliente* prox;
- };
- typedef struct Cliente Cliente;
- struct Produto {
- char codigo[256];
- char descricao[256];
- int estoque;
- double preco_unitario;
- struct Produto* ant;
- struct Produto* prox;
- };
- typedef struct Produto Produto;
- struct Venda {
- Cliente cliente;
- Produto produto;
- int qtde;
- struct Venda* ant;
- struct Venda* prox;
- };
- typedef struct Venda Venda;
- Cliente* inicioCliente = NULL;
- Produto* inicioProduto = NULL;
- Venda* inicioVenda = NULL;
- void pushCliente(Cliente* novoCliente){
- if(inicioCliente == NULL) {
- inicioCliente = (Cliente*) malloc(sizeof(Cliente));
- inicioCliente = novoCliente;
- return;
- }
- Cliente* iterator = inicioCliente;
- while(iterator->prox != NULL){
- iterator = iterator->prox;
- }
- iterator->prox = novoCliente;
- novoCliente->ant = iterator;
- return;
- }
- void apagarCliente(){
- char username[256];
- //digitar nome do cliente
- fgets(username, 256, stdin);
- Cliente* iterator = inicioCliente;
- while(iterator != NULL){
- if(strcmp(username, iterator->nome) == 0){
- if(iterator->ant == NULL && iterator->prox == NULL) {
- free(iterator);
- inicioCliente = NULL;
- } else if(iterator->ant == NULL) {
- inicioCliente = iterator->prox;
- iterator->prox->ant = NULL;
- free(iterator);
- } else if(iterator->prox == NULL) {
- iterator->ant->prox = NULL;
- free(iterator);
- } else {
- iterator->ant->prox = iterator->prox;
- iterator->prox->ant = iterator->ant;
- free(iterator);
- }
- }
- iterator = iterator->prox;
- }
- }
- void editarCliente(){
- char username[256];
- //digitar nome do cliente
- fgets(username, 256, stdin);
- Cliente* iterator = inicioCliente;
- while(iterator != NULL){
- if(strcmp(username, iterator->nome) == 0){
- fgets(iterator->nome, 256, stdin);
- fgets(iterator->cpf, 256, stdin);
- fgets(iterator->telefone, 256, stdin);
- fgets(iterator->endereco.rua, 256, stdin);
- scanf("%d", &iterator->endereco.numero);
- getchar();
- fgets(iterator->endereco.cidade, 256, stdin);
- fgets(iterator->endereco.estado, 256, stdin);
- scanf("%d/%d/%d", &iterator->nascimento.dia, &iterator->nascimento.mes, &iterator->nascimento.ano);
- getchar();
- }
- iterator = iterator->prox;
- }
- }
- void mostrarClientes(){
- Cliente* iterator = inicioCliente;
- while(iterator != NULL){
- printf("---------------------------\n");
- printf("Nome: %s", iterator->nome);
- printf("CPF: %s", iterator->cpf);
- printf("Telefone: %s", iterator->telefone);
- printf("Rua: %sNúmero %d\n%s%s", iterator->endereco.rua, iterator->endereco.numero, iterator->endereco.cidade, iterator->endereco.estado);
- printf("Nascimento: %d/%d/%d\n", iterator->nascimento.dia, iterator->nascimento.mes, iterator->nascimento.ano);
- iterator = iterator->prox;
- }
- }
- void pushProduto(Produto* novoProduto){
- if(inicioProduto == NULL) {
- inicioProduto = (Produto*) malloc(sizeof(Produto));
- inicioProduto = novoProduto;
- return;
- }
- Produto* iterator = inicioProduto;
- while(iterator->prox != NULL){
- iterator = iterator->prox;
- }
- iterator->prox = novoProduto;
- novoProduto->ant = iterator;
- return;
- }
- void apagarProduto(){
- char codigoProduto[256];
- //digitar nome do cliente
- fgets(codigoProduto, 256, stdin);
- Produto* iterator = inicioProduto;
- while(iterator != NULL){
- if(strcmp(codigoProduto, iterator->codigo) == 0){
- if(iterator->ant == NULL && iterator->prox == NULL) {
- free(iterator);
- inicioProduto = NULL;
- } else if(iterator->ant == NULL) {
- inicioProduto = iterator->prox;
- iterator->prox->ant = NULL;
- free(iterator);
- } else if(iterator->prox == NULL) {
- iterator->ant->prox = NULL;
- free(iterator);
- } else {
- iterator->ant->prox = iterator->prox;
- iterator->prox->ant = iterator->ant;
- free(iterator);
- }
- }
- iterator = iterator->prox;
- }
- }
- void editarProduto(){
- char codigoProduto[256];
- fgets(codigoProduto, 256, stdin);
- Produto* iterator = inicioProduto;
- while(iterator != NULL){
- if(strcmp(codigoProduto, iterator->codigo) == 0){
- fgets(iterator->codigo, 256, stdin);
- fgets(iterator->descricao, 256, stdin);
- scanf("%lf", &iterator->preco_unitario);
- scanf("%d", &iterator->estoque);
- getchar();
- }
- iterator = iterator->prox;
- }
- }
- void mostrarProdutos(){
- Produto* iterator = inicioProduto;
- while(iterator != NULL){
- printf("---------------------------\n");
- printf("Codigo: %s", iterator->codigo);
- printf("Descricao: %s", iterator->descricao);
- printf("Preco unitario: %.2lf\n", iterator->preco_unitario);
- printf("Quantidade em estoque: %d\n", iterator->estoque);
- iterator = iterator->prox;
- }
- }
- void pushVenda(Venda* novoVenda){
- if(inicioVenda == NULL) {
- inicioVenda = (Venda*) malloc(sizeof(Venda));
- inicioVenda = novoVenda;
- return;
- }
- Venda* iterator = inicioVenda;
- while(iterator->prox != NULL){
- iterator = iterator->prox;
- }
- iterator->prox = novoVenda;
- novoVenda->ant = iterator;
- return;
- }
- int procura(Cliente *novoCliente, Produto *novoProduto){
- int cpf, x = 0;
- char codigo[100];
- printf("digite o cpf: ");
- scanf("%d", &cpf);
- printf("digite o codigo: ");
- fgets(codigo, 100, stdin);
- Cliente *aux = inicioCliente;
- Produto *aux2 = inicioProduto;
- while(aux != NULL){
- if(cpf == aux->cpf)
- x++;
- aux = aux->prox;
- }
- while(aux2 !=NULL){
- if(strcmp(codigo ,aux2->codigo) == 0){
- x++;
- }
- }
- return x;
- }
- void mostrarProdutos(){
- Venda* iterator = inicioVenda;
- while(iterator != NULL){
- printf("---------------------------\n");
- printf("Codigo: %s", iterator->codigo);
- printf("Descricao: %s", iterator->descricao);
- printf("Preco unitario: %.2lf\n", iterator->preco_unitario);
- printf("Quantidade em estoque: %d\n", iterator->estoque);
- iterator = iterator->prox;
- }
- }
- int main(){
- int opc;
- do {
- scanf("%d", &opc);
- getchar();
- switch(opc){
- //insere
- case 1: {
- Cliente* novoCliente = (Cliente*) malloc(sizeof(Cliente));
- novoCliente->prox = novoCliente->ant = NULL;
- fgets(novoCliente->nome, 256, stdin);
- fgets(novoCliente->cpf, 256, stdin);
- fgets(novoCliente->telefone, 256, stdin);
- fgets(novoCliente->endereco.rua, 256, stdin);
- scanf("%d", &novoCliente->endereco.numero);
- getchar();
- fgets(novoCliente->endereco.cidade, 256, stdin);
- fgets(novoCliente->endereco.estado, 256, stdin);
- scanf("%d/%d/%d", &novoCliente->nascimento.dia, &novoCliente->nascimento.mes, &novoCliente->nascimento.ano);
- getchar();
- pushCliente(novoCliente);
- break;
- //mostra
- } case 2: {
- mostrarClientes();
- break;
- //apaga
- } case 3: {
- apagarCliente();
- //edita
- } case 4: {
- editarCliente();
- } case 5: {
- Produto* novoProduto = (Produto*) malloc(sizeof(Produto));
- novoProduto->prox = novoProduto->ant = NULL;
- fgets(novoProduto->codigo, 256, stdin);
- fgets(novoProduto->descricao, 256, stdin);
- scanf("%lf", &novoProduto->preco_unitario);
- scanf("%d", &novoProduto->estoque);
- getchar();
- pushProduto(novoProduto);
- break;
- //mostra
- } case 6: {
- mostrarProdutos();
- break;
- //apaga
- } case 7: {
- apagarProduto();
- break;
- //edita
- } case 8: {
- editarProduto();
- break;
- } case 9: {
- int t;
- t = procura(inicioCliente, inicioProduto);
- if(t == 2){
- Venda* novoVenda = (Venda*) malloc(sizeof(Venda));
- novoVenda->prox = novoVenda->ant = NULL;
- scanf("%d", &novoVenda->qtde);
- scanf("%d", &novoVenda->Cliente.cpf);
- getchar();
- pushVenda(novoVenda);
- }
- break;
- } case 10:{
- mostrarVendas();
- break;
- }
- }
- } while(opc != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement