Advertisement
kdelekta

Untitled

Apr 5th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct callcenter{
  6.     char *nr;
  7.     int time;
  8. }callcenter;
  9.  
  10. typedef struct queue{
  11.     int first, last, size;
  12.     callcenter *arr;
  13. }queue;
  14.  
  15. queue *create_queue(int size) {
  16.     queue *q = malloc(sizeof(queue));
  17.     q -> first = 0;
  18.     q -> last = 0;
  19.     q -> size = size;
  20.     q -> arr = malloc(size * sizeof(callcenter));
  21.     return q;
  22. }
  23.  
  24. void del_queue(queue *q) {
  25.     free(q -> arr);
  26.     free(q);
  27. }
  28.  
  29. callcenter get(queue *q) {
  30.     if(q -> first + 1 < q -> size){
  31.         q -> first++;
  32.         return q -> arr[q -> first];
  33.     }else {
  34.         q -> first = 0;
  35.         return q -> arr[q -> first];
  36.     }
  37. }
  38.  
  39. void put(char* nr, int time, queue *q) {
  40.     if(q -> last + 1 < q -> size) {
  41.         q -> last++;
  42.         q -> arr[q -> last].nr = nr;
  43.         q -> arr[q -> last].time = time;
  44.     } else {
  45.         q -> last = 0;
  46.         q -> arr[q -> last].nr = nr;
  47.         q -> arr[q -> last].time = time;
  48.     }
  49. }
  50.  
  51. bool empty(queue *q){
  52.     return q -> first == q -> last;
  53. }
  54.  
  55. int main() {
  56.     int n;
  57.     scanf("%d", &n);
  58.     queue *que = create_queue(n);
  59.     queue *res = create_queue(n);
  60.     for(int i = 0; i < n; i++) {
  61.         char type;
  62.         int time;
  63.         scanf(" %c %d", &type, &time);
  64.         printf("char: %c time: %d", type, time);
  65.         if(type == 'a'){
  66.             char nr[16];
  67.             scanf(" %s", &nr);
  68.             printf(" nr : %s", nr);
  69.             put(nr, time, que);
  70.         }
  71.         else if(type == 'r'){
  72.             callcenter mem = get(que);
  73.             int t = time - mem.time;
  74.             put(mem.nr, t, res);
  75.         }
  76.     }
  77.     while(!empty(res)){
  78.         callcenter ans = get(res);
  79.         printf("%d %s \n", ans.time, ans.nr);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement