Munashedov

Untitled

Nov 22nd, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define LENGTH 50
  7.  
  8. void PrintArray(int *array, int length);
  9. void push(char x);
  10. int pop(void);
  11. bool search(int number);
  12. void PrintStack(void);
  13.  
  14. int stack[256];
  15. int count = 0;
  16.  
  17. typedef struct Point{
  18.     int x;
  19.     int y;
  20.     int z;
  21. } Point;
  22.  
  23. void PrintPoint(Point *point);
  24. void SetPoint(Point *point, int value);
  25. void BubbleSortChars(char *array, int length);
  26.  
  27.  
  28. int main(void){
  29.     srand(1);
  30.    
  31.    
  32.     Point point = { .x = 5, .y = 10 };
  33.     Point *pointer = &point;
  34.     printf("%lu\n", sizeof(point));
  35.     PrintPoint(&point);
  36.     SetPoint(&point, 0);
  37.     PrintPoint(&point);
  38.     char *s[5] = {"Hello", " ", "world", ".", "\n"};
  39.     for(int i = 0; i < 5; i++){
  40.         printf("%s", s[i]);
  41.     }
  42.     for(int i = 3; i >= 0; i--){
  43.         printf("%s", s[i]);
  44.     }
  45.     printf("\n");
  46.  
  47.     int number = rand();
  48.     char characters[LENGTH];
  49.     for(int i = 0; i <= LENGTH - 1; ++i){
  50.         number = rand() % ('z' - 'a');
  51.         number += 'a';
  52.         characters[i] = number;
  53.        
  54.     }
  55.     characters[LENGTH-1] = '\0';
  56.     printf("%s\n", characters);
  57.  
  58.     BubbleSortChars(characters, LENGTH);
  59.     printf("%s\n", characters);
  60.     char current;
  61.     printf("(");
  62.     for(int i = 0; i < LENGTH; ++i){
  63.         printf("%c", characters[i]);
  64.         current = characters[i];
  65.         if(current != characters[i+1] && i < LENGTH-2){
  66.             printf(")(");
  67.         }
  68.     }
  69.     printf(")");
  70.     printf("\n");
  71.  
  72.     char cur = 0;
  73.     int counts[LENGTH] = {0};
  74.     int slot = 0;
  75.     for(int i = 1; i < LENGTH; ++i){
  76.         cur = characters[i-1];
  77.         if(characters[i] == cur) {
  78.             counts[slot]++;
  79.         } else {
  80.             slot++;
  81.         }
  82.        
  83.     }
  84.     for(int i = 0; i < slot; ++i){
  85.         counts[i]++;
  86.         if(i < slot-1){
  87.             printf("%i,", counts[i]);
  88.         } else {
  89.             printf("%i", counts[i]);
  90.         }
  91.     }
  92. }
  93.  
  94. void BubbleSortChars(char *array, int length){
  95.     for(int i = 0; i < length-1; ++i){
  96.         for(int j = 0; j < length-2; ++j){
  97.             if(array[j] > array[j+1]) {
  98.                 char temp = array[j+1];
  99.                 array[j+1] = array[j];
  100.                 array[j] = temp;
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106. void PrintPoint(Point *point){
  107.     printf("%d, %d, %d\n", point->x, point->y, point->z);
  108. }
  109.  
  110. void SetPoint(Point *point, int value){
  111.     point->x = value;
  112.     point->y = value;
  113.     point->z = value;
  114. }
  115.  
  116. void PrintArray(int *array, int length){
  117.     for(int i = 0; i < length; i++){
  118.         printf("%i,", *(array+i));
  119.     }
  120.     printf("\n");
  121. }
  122.  
  123. void push(char x){
  124.     if(count == 256){
  125.         fprintf(stderr, "Stack is full.\n");
  126.     } else {
  127.         stack[count] = x;
  128.         count++;
  129.     }
  130. }
  131.  
  132. int pop(void){
  133.     if(count == 0){
  134.         fprintf(stderr, "Stack is empty.\n");
  135.         exit(1);
  136.     } else {
  137.         int res = stack[count-1];
  138.         count--;
  139.         return res;
  140.     }
  141. }
  142.  
  143. bool search(int number){
  144.     for(int i = count; i > 0; i--){
  145.         if(stack[i] == number){
  146.             return true;
  147.         }
  148.     }
  149.     return false;
  150. }
  151.  
  152. void PrintStack(void){
  153.     printf("[");
  154.     for(int i = 0; i < count; i++){
  155.         printf("%X", stack[i]);
  156.         if(i != count-1){
  157.             printf(",");
  158.         }
  159.     }
  160.     printf("]");
  161.     printf("\n");
  162. }
Add Comment
Please, Sign In to add comment