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>
- #include <stdbool.h>
- #include <math.h>
- #include "ispit.h"
- #pragma pack(4)
- typedef bool polozeno(char);
- char *createBuffer(int, int);
- bool validateHex(char *);
- int convertHexToBin(char *);
- int prebroj(char *, polozeno *);
- bool isPassed(char);
- int main()
- {
- int n,
- m;
- char *buffer;
- printf("Unesite broj studenata: ");
- scanf("%d", &n);
- printf("Unesite broj predmeta studenata: ");
- scanf("%d", &m);
- buffer = createBuffer(n, m);
- printf("\nUkupno polozenih ispita: %d.", prebroj(buffer, isPassed));
- free(buffer);
- return EXIT_SUCCESS;
- }
- char *createBuffer(int n, int m)
- {
- char *buffer = NULL;
- if (n > 0 && m > 0)
- {
- int i,
- j;
- char hexNum[33];
- buffer = (char *)malloc(2 * sizeof(int) + n * m * sizeof(ispit));
- *buffer = n;
- *(buffer + sizeof(int)) = m;
- for (i = 0; i < n; i++)
- {
- printf("Unos podataka o ispitima za %d. studenta: \n", i + 1);
- for (j = 0; j < m; j++)
- {
- fflush(stdin);
- int skip = 2 * sizeof(int) + i * m * sizeof(ispit) + j * sizeof(ispit);
- printf("Unos dana ispita broj %d: ", j + 1);
- scanf("%hd", (short *)&buffer[skip]);
- printf("Unos meseca ispita broj %d: ", j + 1);
- scanf("%hd", (short *)&buffer[skip + sizeof(short)]);
- printf("Unos godine ispita broj %d: ", j + 1);
- scanf("%hd", (short *)&buffer[skip + 2 * sizeof(short)]);
- printf("Unos ocene ispita broj %d: ", j + 1);
- fflush(stdin);
- scanf("%i", buffer + skip + 3 * sizeof(short));
- do
- {
- fflush(stdin);
- printf("Unos sifre ispitnog roka za ispit broj %d u HEX formatu: 0x", j + 1);
- scanf("%s", hexNum);
- } while (!validateHex(hexNum));
- *((int *)&buffer[skip + 3 * sizeof(short) + 2 * sizeof(char)]) = convertHexToBin(hexNum);
- printf("Unos imena profesora za ispit broj %d: ", j + 1);
- scanf("%s", buffer + skip + 3 * sizeof(short) + 2 * sizeof(char) + sizeof(int));
- }
- }
- return buffer;
- }
- }
- bool validateHex(char *hex)
- {
- int i;
- for (i = 0; i < strlen(hex); i++)
- {
- hex[i] = toupper(hex[i]);
- if ((hex[i] < '0' || hex[i] > '9') && (hex[i] < 'A' || hex[i] > 'F'))
- {
- return false;
- }
- }
- return true;
- }
- int convertHexToBin(char *hex)
- {
- int ret = 0,
- i;
- float powIndex = 0;
- for (i = strlen(hex) - 1; i >= 0; i--, powIndex++)
- {
- int tmp;
- if (hex[i] > '0' && hex[i] < '9')
- {
- tmp = hex[i] - '0';
- }
- else
- {
- switch (hex[i])
- {
- case 'A': // znamo da ce sva slova biti velika jer smo ih izmenili u validate funkciji
- tmp = 10;
- break;
- case 'B':
- tmp = 11;
- break;
- case 'C':
- tmp = 12;
- break;
- case 'D':
- tmp = 13;
- break;
- case 'E':
- tmp = 14;
- break;
- case 'F':
- tmp = 15;
- break;
- }
- }
- ret += tmp * pow(16, powIndex);
- }
- return ret;
- }
- int prebroj(char *buffer, polozeno *function)
- {
- int n = *buffer,
- m = *(buffer + sizeof(int)),
- counter = 0,
- i,
- j;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++)
- {
- int skip = 2 * sizeof(int) + i * m * sizeof(ispit) + j * sizeof(ispit);
- if (function(*((char *)&buffer[skip + 3 * sizeof(short)])))
- {
- counter++;
- }
- }
- }
- return counter;
- }
- bool isPassed(char ocena)
- {
- return ocena > 6 ? true : false;
- }
Advertisement
Add Comment
Please, Sign In to add comment