Advertisement
kraxor

7. kotprog

Nov 23rd, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define INPUT_FILE      "be.txt"
  6. #define OUTPUT_FILE     "ki.txt"
  7. #define MAX_WORD_SIZE   200
  8.  
  9. int read_error() {
  10.     fprintf(stderr, "Read error!\n");
  11.     return -2;
  12. }
  13.  
  14. char *encode(const char *str, const int *key, int cols);
  15. char *decode(const char *str, const int *key, int cols);
  16.  
  17. int main() {
  18.     FILE *in = fopen(INPUT_FILE, "r");
  19.     if (!in) {
  20.         fprintf(stderr, "Can't read \"%s\"!\n", INPUT_FILE);
  21.         return -1;
  22.     }
  23.  
  24.     int func, cols;
  25.     if (fscanf(in, "%d %d", &func, &cols) != 2) return read_error();
  26.  
  27.     int key[10];
  28.     size_t i;
  29.     for (i = 0; i < cols; i++) {
  30.         if (fscanf(in, "%d", &key[i]) != 1) return read_error();
  31.     }
  32.  
  33.     char *str = (char *) malloc(sizeof(char) * MAX_WORD_SIZE + 1);
  34.     if (fscanf(in, "%s", str) != 1) return read_error();
  35.  
  36.     if (func == 1) {
  37.         str = encode(str, key, cols);
  38.     } else {
  39.         str = decode(str, key, cols);
  40.     }
  41.  
  42.     FILE *out = fopen(OUTPUT_FILE, "w");
  43.     if (!out) {
  44.         fprintf(stderr, "Can't write \"%s\"!\n", OUTPUT_FILE);
  45.         return -3;
  46.     }
  47.  
  48.     fprintf(out, "%s\n", str);
  49.     fclose(out);
  50. }
  51.  
  52. char *encode(const char *str, const int *key, int cols) {
  53.     int rows = strlen(str) / cols;
  54.     char **table = (char **) malloc(sizeof(char *) * rows);
  55.  
  56.     size_t i, j;
  57.     for (i = 0; i < rows; i++) {
  58.         table[i] = (char *) malloc(sizeof(char) * cols);
  59.         for (j = 0; j < cols; j++) {
  60.             // printf("%c ", str[i * cols + j]);
  61.             table[i][j] = str[i * cols + j];
  62.         }
  63.         // printf("\n");
  64.     }
  65.  
  66.     char *coded = (char *) malloc(sizeof(char) * strlen(str) + 1);
  67.     for (i = 0; i < cols; i++) {
  68.         // printf("Key: %d\n", key[i]);
  69.         for (j = 0; j < rows; j++) {
  70.             // printf("%d %d %d %c\n", i, j, i * rows + j, table[j][key[i] - 1]);
  71.             coded[i * rows + j] = table[j][key[i] - 1];
  72.         }
  73.     }
  74.     coded[strlen(str)] = '\0';
  75.     //printf("coded: '%s'\n", coded);
  76.     return coded;
  77. }
  78.  
  79. char *decode(const char *str, const int *key, int cols) {
  80.     int rows = strlen(str) / cols;
  81.     char **table = (char **) malloc(sizeof(char *) * cols);
  82.  
  83.     size_t i, j;
  84.     for (i = 0; i < cols; i++) {
  85.         table[key[i]] = (char *) malloc(sizeof(char) * rows + 1);
  86.         for (j = 0; j < rows; j++) {
  87.             // printf("%d %d %d\n", i, j, key[i]);
  88.             table[key[i]][j] = str[i * rows + j];
  89.             // printf("%c ", table[key[i]][j]);
  90.         }
  91.         // printf("\n");
  92.     }
  93.  
  94.     char *decoded = (char *) malloc(sizeof(char) * strlen(str) + 1);
  95.     for (j = 0; j < rows; j++) {
  96.         for (i = 1; i <= cols; i++) {
  97.             decoded[j * cols + i - 1] = table[i][j];
  98.         }
  99.     }
  100.     decoded[strlen(str)] = '\0';
  101.    
  102.     return decoded;
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement