Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my .c example of a FIFO and my structure
What I want to do is a FIFO that manages my structure as a piece of information, for example. Picking up an order in it specifies the customer and the number of pizzas and once the order is lifted it is queued in a service queue

What I have tried:

I have my two codes and append my codes
//FIFO
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

struct Cola {
    int p, u;
    float a[MAX];
};

void iniciaCola(struct Cola *c);
int agrega(struct Cola *c, float dato);
int retira(struct Cola *c, float *info);
void imprime(struct Cola *c);

int menu();
void procesaOpcion(int opcion, struct Cola *cola);

int main() {
    struct Cola unaCola;
    int opcion,r;
    float dato;

    iniciaCola(&unaCola);
    do {
        opcion = menu();

        if (opcion == 1) {
            printf("Numero a agregar a la cola?");
            scanf("%f",&dato);
            fflush(stdin);
            r = agrega(&unaCola,dato);
            if (r==0)
                printf("no fue posible agregar el numero [%f]\n",dato);
            else
                printf(">>> Agregué: (%.2f)\n",dato);
        } else if (opcion == 2) {
            r = retira(&unaCola, &dato);
            if (r==0)
                printf("No se retiro ningun numero\n");
            else
                printf("<<<< Retiré: (%.2f)\n",dato);
        }
        imprime(&unaCola);
    } while (opcion != 3);

    return 0;
}

int menu() {
    int op,r;
    do {
        printf("\n\n-------- MENU ---------------\n");
        printf("[1] Agregar un numero\n");
        printf("[2] Retirar un numero\n");
        printf("[3] Terminar\n");
        printf("Opcion? ");
        r = scanf("%d",&op);
        fflush(stdin);
        if (r==0)
            printf("Error en el formato\n");
    } while(r==0 || op<1 || op>3);
    return op;
}

void iniciaCola(struct Cola *c) {
    c->p = -1;
    c->u = -1;
}

int agrega(struct Cola *c, float dato) {
    int k,j;
    if (c->p == -1) {
        c->p = c->u = 0;
        c->a[0] = dato;
        return 1;
    }
    if (c->u==MAX-1) {
        /***** se alcanzo el ultimo elemento del arreglo
         revisar si c->p!=0 entonces hay espacio*****/
         if (c->p!=0){
           /*** compacta el arreglo ***/
           j=0;
           for(k=c->p; k<=c->u; k++){
            c->a[j]=c->a[k];
            j=j+1;
           }
           c->p=0;
           c->u=j-1;
         } else
        return 0;
    }
    c->u = c->u + 1;
    c->a[c->u] = dato;
    return 1;
}

int retira(struct Cola *c, float *info) {
    if (c->p == -1)
        return 0;
    *info = c->a[c->p];
    c->p = c->p+1;
    if (c->p > c->u) {
        c->u = -1;
        c->p = -1;
    }
    return 1;
}

void imprime(struct Cola *c) {
    int k;
    printf("Contenido de la cola: ");
    if (c->p == -1) {
        printf("Cola vacia\n");
        return;
    }
    for (k=c->p; k<=c->u; k++)
        printf("%.2f ",c->a[k]);
    printf("\n");

}




//struct
#include<stdio.h>
#include<windows.h>

struct ordenPizza{
    char cliente[200];
    char nombre[200];
    int cantidad;
    int precioUnitario;
    int monto;
};

void leerOrdenPizza(struct ordenPizza *unaOrden);
void imprimiOrdenPizza(struct ordenPizza *unaOrden);
int monto(struct ordenPizza *unaOrden);

int main()
{
    struct ordenPizza *o;
    o=malloc(sizeof(struct ordenPizza));

    leerOrdenPizza(o);
    monto(o);
    imprimiOrdenPizza(o);

    return 0;
}

void leerOrdenPizza(struct ordenPizza *unaOrden)
{
        printf("Oferta el precio por pizza es de $199\n");
        printf("Dame tu nombre\n");
        scanf("%s",unaOrden->cliente);
        fflush(stdin);
        printf("Cuantas pizzas quieres?\n");
        scanf("%d",&(unaOrden->cantidad));
        fflush(stdin);
        printf("De que sera tu pizza?\n");
        scanf("%s",unaOrden->nombre);
        fflush(stdin);
        printf("Escribe el precio de tu pizza?\n");
        scanf("%d",&(unaOrden->precioUnitario));

        fflush(stdin);
        system("cls");
}

int monto(struct ordenPizza *unaOrden)
{
    unaOrden->monto=(unaOrden->cantidad)*(unaOrden->precioUnitario);
}

void imprimiOrdenPizza(struct ordenPizza *unaOrden)
{
    printf("Tu nombre es: %s \n ",unaOrden->cliente);
    printf("Tu pizza es de: %s \n",unaOrden->nombre);
    printf("El monto es: $%d \n",unaOrden->monto);
}
Posted
Updated 28-Mar-18 9:57am

1 solution

In a (very) straightforward implementation, you replace the array of float with an array of OrdenPizza. A show you a replacement for the agrega function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 10

struct OrdenPizza
{
    char cliente[200];
    char nombre[200];
    int cantidad;
    int precioUnitario;
    int monto;
};

struct Cola
{
  int p, u;
  struct OrdenPizza op[MAX];
};

void iniciaCola(struct Cola *c)
{   
    c->p = -1;
    c->u = -1;
}
int agrega(struct Cola *c, const struct OrdenPizza * pOP)
{
    int k,j;
    if (c->p == -1)
    {
        c->p = c->u = 0;
        c->op[0] = *pOP;
        return 1;
    }
    if (c->u==MAX-1)
    {
        /***** se alcanzo el ultimo elemento del arreglo
         revisar si c->p!=0 entonces hay espacio*****/
        if (c->p != 0)
        {
          /*** compacta el arreglo ***/
          j=0;
          for(k=c->p; k<=c->u; k++)
          {
            c->op[j] = c->op[k];
            j=j+1;
          }
          c->p=0;
          c->u=j-1;
        }
        else
          return 0;
    }
    c->u = c->u + 1;
    c->op[c->u] = *pOP;
    return 1;
}

int main()
{
  struct Cola cola;
  iniciaCola(&cola);

  struct OrdenPizza op;
  strcpy(op.cliente, "Zorro");
  strcpy(op.nombre, "Z27");
  op.cantidad = 2;
  op.precioUnitario = 4;
    // op.monto = ?? this looks a dependent value
  agrega( &cola, &op);

  struct OrdenPizza * p = &cola.op [cola.u];
  printf("%s %s %d %d %d\n", p->cliente, p->nombre, p->cantidad, p->precioUnitario, p->monto);


  return 0;
}


In a more efficient approach, your FIFO maintains an array of pointers to OrdenPizza struct instances.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900