Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

zadanie Callcenter (kolejka w C)

Ostatnio zmodyfikowano 2020-03-31 22:23
Autor Wiadomość
Ukasz11233
Temat założony przez niniejszego użytkownika
zadanie Callcenter (kolejka w C)
» 2020-03-31 18:23:26
  Cześć, mam problem z programem który ma symulować kolejkę połączeń w callcenter. Mianowicie, chodzi o wywołania pętli w funkcji main, która nie wykonuje się tyle razy ile powinna (mniej razy). Np gdy powinna wykonuje się 2 razy, wykonuje się raz, a gdy powinna 6 razy to wykonuje się tylko 4 razy.  Przykładowe dane wejściowe powinny wyglądać następująco:

2   // liczba kolejnych wierszy

a 24 111222333  // tutaj: a-informacja czy a(add) do kolejki lub r(remove); 24- dl. polaczenia; 11...33 - numer tel.

r 28

Czy mógłby ktoś mi wytłumaczyć dlaczego program tak się zachowuje?

C/C++
#include <stdio.h>
#include <stdlib.h>
#define N 100

struct Operation {
    char typ;
    char * number;
    int secs;
};

struct Queue {
    int rear, front, size;
    struct Operation arr[ N ];
};

struct Queue * creat_queue() {
    struct Queue * queue =( struct Queue * ) malloc( sizeof( struct Queue ) );
    queue->rear = N - 1;
    queue->front = queue->size = 0;
    return queue;
};

void add_to_queue( struct Queue * queue, struct Operation item ) {
    queue->rear =( queue->rear + 1 ) % N;
    queue->arr[ queue->rear ] = item;
    queue->size = queue->size + 1;
}

struct Operation remove_from_queue( struct Queue * queue ) {
    struct Operation item = queue->arr[ queue->front ];
    queue->front =( queue->front + 1 ) % N;
    queue->size = queue->size - 1;
    return item;
}

int isEmpty( struct Queue * queue ) {
    return( queue->size == 0 );
}

void output( struct Queue * queue ) {
    struct Operation operation;
    while( !isEmpty( queue ) ) {
        operation = remove_from_queue( queue );
        printf( "%s %d\n", operation.number, operation.secs );
    }
}

void object( struct Queue * queue ) {
    struct Operation operation;
    scanf( "%c %d %s", & operation.typ, & operation.secs, operation.number );
    if( operation.typ == 'a' ) {
        add_to_queue( queue, operation );
    }
    else if( operation.typ == 'r' ) {
        struct Operation tmp;
        tmp = remove_from_queue( queue );
        tmp.secs = operation.secs - tmp.secs;
        add_to_queue( queue, tmp );
    }
   
}

int main()
{
    int k;
    scanf( "%d", & k );
    struct Queue * queue = creat_queue();
    while( k-- ) {
        object( queue );
    }
    output( queue );
    return 0;
}
[ \c pp ]
P-176581
pekfos
» 2020-03-31 22:23:50
Wczytujesz numer telefonu pod przypadkowy adres w pamięci. operation.number musi wskazywać na poprawną pamięć zanim wczytasz tam coś przez scanf().
P-176584
« 1 »
  Strona 1 z 1