Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #pragma pack(4)
- #define MAX_IME 12
- #define MAX_PREZIME 12
- #define MAX_JMBG 14
- void ispisNajstariju(char*);
- typedef struct { //adrese za pristup u pack(4)
- char ime[MAX_IME]; // 2 -> 13
- char prezime[MAX_PREZIME]; // 14 -> 25
- char pol; // 26 -> 27
- short danR, mesecR, godinaR; // 28 -> 29, 30 -> 31, 32 -> 33
- char JMBG[MAX_JMBG]; // 34 -> 47
- } Osoba;
- int main(){
- short n;
- int i;
- do{
- printf("Unesite broj osoba koji zelite da unesete: ");
- scanf("%hu", &n);
- }while(n <= 0);
- char* buffer = (char *)malloc(sizeof(short) + n * sizeof(Osoba));
- *buffer = n;
- for(i = sizeof(short); i < sizeof(short) + n * sizeof(Osoba); i += sizeof(Osoba)){
- char tmp[14];
- short danTmp;
- printf("Unos imena %ld osobe: ", (i - sizeof(short)) / sizeof(Osoba));
- scanf("%s", tmp);
- strncpy(&buffer[i], tmp, MAX_IME);
- printf("Unos prezimena %ld osobe: ", (i - sizeof(short))/sizeof(Osoba));
- scanf("%s", tmp);
- strncpy(&buffer[i + MAX_IME], tmp, MAX_PREZIME);
- char tmpPol = 'M';
- do{
- getchar(); //resava bug iz c jezika gde se uzima \n sa bafera iz prethodnog unosa.
- printf("Unesite pol (M/Z): ");
- tmpPol = toupper(getchar());
- }while(tmpPol != 'M' && tmpPol != 'Z');
- buffer[i + MAX_IME + MAX_PREZIME] = toupper(tmp[0]);
- printf("Uneti dan rodjenja: ");
- scanf("%hi", &danTmp);
- buffer[i + MAX_IME + MAX_PREZIME + 2*sizeof(char)] = danTmp;
- printf("Uneti mesec rodjenja: ");
- scanf("%hi", &danTmp);
- buffer[i + MAX_IME + MAX_PREZIME + 2*sizeof(char) + sizeof(short)] = danTmp;
- printf("Uneti godinu rodjenja: ");
- scanf("%hi", &danTmp);
- buffer[i + MAX_IME + MAX_PREZIME + 2*sizeof(char) + 2*sizeof(short)] = danTmp;
- printf("Godina: %hi\n", (buffer[i + MAX_IME + MAX_PREZIME + 2*sizeof(char) + 2*sizeof(short)]));
- printf("Uneti JMBG osobe: ");
- scanf("%s", tmp);
- strncpy(&buffer[i + MAX_IME + MAX_PREZIME + 2*sizeof(char) + 3*sizeof(short)], tmp, MAX_JMBG);
- }
- ispisNajstariju(buffer);
- free(buffer);
- return EXIT_SUCCESS;
- }
- void ispisNajstariju(char *buffer){
- int i = sizeof(short),
- n = *buffer;
- printf("\n------ Najstarija osoba -------");
- printf("\nIme najstarije osobe: %s", &buffer[i]);
- i += MAX_IME;
- printf("\nPrezime najstarije osobe: %s", &buffer[i]);
- i += MAX_PREZIME;
- printf("\nPol osobe je: %c", (char)(buffer[i]));
- i += 2 * sizeof(char); // +2 jer je pack(4), inace bi bilo +1
- printf("\nDatum rodjenja je %hi/", (short)(buffer[i]));
- i += sizeof(short);
- printf("%hi/", (short)(buffer[i]));
- i += sizeof(short);
- printf("%hi", (short)(buffer[i]));
- i += sizeof(short);
- printf("\nJMBG osobe je: %s", &buffer[i]);
- printf("\n--------------------------------\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement