Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- void copyLEToLE(int x, void* copy){
- for(int i=0; i < sizeof(x); i++){
- *((char*)copy + i) = *((char*)&x + i);
- }
- }
- void copyLEToBE(int x, void* copy){
- for(int i = 0; i < sizeof(x); i++){
- *((char*)copy + i) = *((char*)&x + sizeof(x)-1-i);
- }
- }
- int Count(char* buffer){
- char lb = *buffer;
- unsigned short N1 = *(buffer + sizeof(unsigned short) - 1),
- N2 = *(buffer + 2*sizeof(unsigned short) - 1);
- int counter = 0,
- correction_for = (N1 > N2) ? (N1 - N2)*sizeof(double) : 0,
- i,
- j;
- for(i = sizeof(char) + 2*sizeof(unsigned short),
- j = sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double);
- i < sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double) - correction_for;
- i += sizeof(double), j += sizeof(double)){
- //printf("%lf %lf", *((double*)(buffer + i)), *((double*)(buffer + j)));
- if((*((double*)(buffer + i))) > (*((double*)(buffer + j)))){
- ++counter;
- }
- }
- printf("%c", lb);
- switch (lb)
- {
- case 'L':
- copyLEToLE(counter, (double*)&counter);
- break;
- case 'B':
- copyLEToBE(counter, (double*)&counter);
- break;
- default:
- printf("Greska1.");
- return -1;
- }
- return counter;
- }
- int main(){
- unsigned short N1, N2;
- printf("Unos broja niza 1: ");
- scanf("%hu", &N1);
- printf("Unos broja niza 2: ");
- scanf("%hu", &N2);
- char tmp_lb;
- int uslov = 1,
- i = 0;
- do{
- printf("Da li su brojevi L ili B format? (b/l): ");
- scanf(" %c", &tmp_lb); //'space' u scanf je da se ne duplira ispis (nije mi pomogao fflush(stdin))
- if(tmp_lb == 'l' || tmp_lb == 'L' || tmp_lb == 'b' || tmp_lb == 'B'){
- uslov = 0;
- }
- }while(uslov != 0);
- tmp_lb = toupper(tmp_lb);
- char* buffer = (char*)malloc(sizeof(char) + 2*sizeof(unsigned short) + N1*sizeof(double) + N2*sizeof(double));
- *buffer = tmp_lb; //buffer[0]
- *(buffer + sizeof(unsigned short) - 1) = N1; //buffer[1-2]
- *(buffer + 2*sizeof(unsigned short) - 1) = N2; //buffer[3-4]
- printf("Unos Niza 1: \n");
- for(i = 2*sizeof(unsigned short) + sizeof(char); i < 2*sizeof(unsigned short) + sizeof(char) + N1*sizeof(double); i+=sizeof(double)){
- double tmp;
- printf("Element %ld >> ", (i-sizeof(char)-2*sizeof(unsigned short))/sizeof(double));
- scanf("%lf", &tmp);
- switch(*buffer){
- case 'L':
- copyLEToLE((int)tmp, buffer + i);
- break;
- case 'B':
- copyLEToBE((int)tmp, buffer + i);
- break;
- default:
- printf("Greska.");
- return EXIT_FAILURE; //izadji iz celog programa
- }
- }
- printf("Unos Niza 2: \n");
- 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)){
- double tmp;
- printf("Element %ld >> ", (i-sizeof(char)-2*sizeof(unsigned short))/sizeof(double));
- scanf("%lf", &tmp);
- switch(*buffer){
- case 'L':
- copyLEToLE((int)tmp, buffer + i);
- break;
- case 'B':
- copyLEToBE((int)tmp, buffer + i);
- break;
- default:
- printf("Greska.");
- return EXIT_FAILURE; //izadji iz celog programa
- }
- }
- int counter = Count(buffer);
- if(counter != -1){
- printf("Broj iznosi: %d\n", counter);
- }
- free(buffer);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment