Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_BITS 32
- #define MAX_STR_SIZE 2048
- struct storage {
- char key[MAX_BITS + 1];
- char* value;
- storage & operator =(const storage &rhs) {
- strcpy(key, rhs.key);
- value = rhs.value;
- return *this;
- }
- };
- int HexToInt(char hex) //translate int to char
- {
- if (hex >= 'a' && hex <= 'f') {
- return hex - 'a' + 10;
- }
- else {
- return hex - '0';
- }
- }
- char IntToHex(int i) //translate char to int
- {
- if (i >= 10 && i <= 15) {
- return 'a' + i - 10;
- }
- else {
- return '0' + i;
- }
- }
- void RadixSort(storage *SortedStorage, int size)
- {
- storage *Temp = (storage *)malloc(size * sizeof(storage));
- for (int iter = 31; iter >= 0; iter--) {
- //CountSort(SortedStorage, size, i);
- int count[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
- //storage Temp[size];
- for (int i = 0; i < size; i++) { //calculate number of hexes in count
- count[HexToInt(SortedStorage[i].key[iter])]++;
- }
- for (int i = 0; i < 15; i++) { //sum of elements in count
- count[i + 1] += count[i];
- }
- for (int i = size - 1; i >= 0; i--) { //sorting into another array
- Temp[count[HexToInt(SortedStorage[i].key[iter])] - 1] = SortedStorage[i];
- count[HexToInt(SortedStorage[i].key[iter])]--;
- }
- memcpy(SortedStorage, Temp, (size) * sizeof(storage));
- }
- free(Temp);
- }
- int main()
- {
- storage *NewStorage;
- const int init_cap = 2;
- int max_size = init_cap; //memory cap
- NewStorage = (storage*)malloc(sizeof(storage) * init_cap);
- int fill = 0; //checker for amount of memory left
- char tmp = 0;
- int i = 0;
- do { //input
- i = 0;
- tmp = getchar();
- if (tmp == EOF || tmp == '\n') {
- break;
- }
- while (tmp != '\t') {
- NewStorage[fill].key[i] = tmp;
- i++;
- tmp = getchar();
- }
- NewStorage[fill].key[32] = '\0';
- i = 0;
- char buf[MAX_STR_SIZE + 1];
- scanf("%[^\n]s", buf);
- int new_str_size = strlen(buf) + 1;
- char * new_str = (char*)malloc(sizeof(char) * new_str_size);
- if (new_str == NULL) {
- printf("Ne ok\n");
- }
- new_str = strcpy(new_str, buf);
- (NewStorage + fill)->value = new_str;
- tmp = getchar();
- fill++;
- if (fill == max_size) {
- max_size *= 2;
- storage *Temp = (storage *)realloc(NewStorage, max_size * sizeof(storage));
- if (Temp == NULL)
- puts("NO");
- NewStorage = 0;
- NewStorage = Temp;
- }
- } while (tmp != EOF); /*(NewStorage + fill - 1)->key[0] != EOF && (NewStorage + fill)->value[i] != EOF*/
- RadixSort(NewStorage, fill);
- for (int i = 0; i < fill; i++) { //output
- for (int j = 0; j < 32; j++) {
- putchar((NewStorage + i)->key[j]);
- }
- putchar('\t');
- printf("%s", (NewStorage + i)->value);
- free((NewStorage + i)->value);
- putchar('\n');
- }
- free(NewStorage);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement