MartinSRB

[НРС] Вежбе 3 - Задатак 2

Mar 9th, 2023 (edited)
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. void copyLEToLE(int x, void* copy){
  6.     for(int i=0; i < sizeof(x); i++){
  7.         *((char*)copy + i) = *((char*)&x + i);
  8.     }
  9. }
  10.  
  11. void copyLEToBE(int x, void* copy){
  12.     for(int i = 0; i < sizeof(x); i++){
  13.         *((char*)copy + i) = *((char*)&x + sizeof(x)-1-i);
  14.     }
  15. }
  16.  
  17. int Count(char* buffer){
  18.     char            lb              = *buffer;
  19.     unsigned short  N1              = *(buffer + sizeof(unsigned short) - 1),
  20.                     N2              = *(buffer + 2*sizeof(unsigned short) - 1);
  21.     int             counter         = 0,
  22.                     correction_for  = (N1 > N2) ? (N1 - N2)*sizeof(double) : 0,
  23.                     i,
  24.                     j;
  25.     for(i = sizeof(char) + 2*sizeof(unsigned short),
  26.         j = sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double);
  27.         i < sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double) - correction_for;
  28.         i += sizeof(double), j += sizeof(double)){
  29.             //printf("%lf %lf", *((double*)(buffer + i)), *((double*)(buffer + j)));
  30.             if((*((double*)(buffer + i))) > (*((double*)(buffer + j)))){
  31.                 ++counter;
  32.             }
  33.     }
  34.     printf("%c", lb);
  35.     switch (lb)
  36.     {
  37.         case 'L':
  38.             copyLEToLE(counter, (double*)&counter);
  39.             break;
  40.         case 'B':
  41.             copyLEToBE(counter, (double*)&counter);
  42.             break;
  43.         default:
  44.             printf("Greska1.");
  45.             return -1;
  46.     }
  47.     return counter;
  48. }
  49.  
  50. int main(){
  51.     unsigned short N1, N2;
  52.     printf("Unos broja niza 1: ");
  53.     scanf("%hu", &N1);
  54.     printf("Unos broja niza 2: ");
  55.     scanf("%hu", &N2);
  56.     char tmp_lb;
  57.     int uslov = 1,
  58.         i     = 0;
  59.     do{
  60.         printf("Da li su brojevi L ili B format? (b/l): ");
  61.         scanf(" %c", &tmp_lb); //'space' u scanf je da se ne duplira ispis (nije mi pomogao fflush(stdin))
  62.         if(tmp_lb == 'l' || tmp_lb == 'L' || tmp_lb == 'b' || tmp_lb == 'B'){
  63.             uslov = 0;
  64.         }
  65.     }while(uslov != 0);
  66.     tmp_lb = toupper(tmp_lb);
  67.     char* buffer = (char*)malloc(sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double) + N2*sizeof(double));
  68.     *buffer = tmp_lb; //buffer[0]
  69.     *(buffer + sizeof(unsigned short) - 1) = N1; //buffer[1-2]
  70.     *(buffer + 2*sizeof(unsigned short) - 1) = N2; //buffer[3-4]
  71.     printf("Unos Niza 1: \n");
  72.     for(i = 2*sizeof(unsigned short) + sizeof(char); i < 2*sizeof(unsigned short) + sizeof(char) + N1*sizeof(double); i+=sizeof(double)){
  73.         double tmp;
  74.         printf("Element %ld >> ", (i-sizeof(char)-2*sizeof(unsigned short))/sizeof(double));
  75.         scanf("%lf", &tmp);
  76.         switch(*buffer){
  77.             case 'L':
  78.                 copyLEToLE((int)tmp, buffer + i);
  79.                 break;
  80.             case 'B':
  81.                 copyLEToBE((int)tmp, buffer + i);
  82.                 break;
  83.             default:
  84.                 printf("Greska.");
  85.                 return EXIT_FAILURE; //izadji iz celog programa
  86.         }
  87.     }
  88.     printf("Unos Niza 2: \n");
  89.     for(i = 2*sizeof(unsigned short) + sizeof(char) + N1*sizeof(double); i < 2*sizeof(unsigned short) + sizeof(char) + N1*sizeof(double) + N2*sizeof(double); i+=sizeof(double)){
  90.         double tmp;
  91.         printf("Element %ld >> ", (i-sizeof(char)-2*sizeof(unsigned short))/sizeof(double));
  92.         scanf("%lf", &tmp);
  93.         switch(*buffer){
  94.             case 'L':
  95.                 copyLEToLE((int)tmp, buffer + i);
  96.                 break;
  97.             case 'B':
  98.                 copyLEToBE((int)tmp, buffer + i);
  99.                 break;
  100.             default:
  101.                 printf("Greska.");
  102.                 return EXIT_FAILURE; //izadji iz celog programa
  103.         }
  104.     }
  105.     int counter = Count(buffer);
  106.     if(counter != -1){
  107.         printf("Broj iznosi: %d\n", counter);
  108.     }
  109.     free(buffer);
  110.     return EXIT_SUCCESS;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment