Advertisement
FoxTuGa

Encode_Decode [Complete]

Mar 20th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. /*  Author: Leandro Soares
  2.     School: INETE
  3.     Date:   19-03-2012
  4.     Time:   20:19   */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <ctype.h>
  10. #include <string.h>
  11.  
  12. #define maxRuns 13
  13. #define ENC "Encode"
  14. #define DEC "Decode"
  15.  
  16. char Menu_Geral(void);
  17. void Ask_Fname(char *sname);
  18. char Encode_Letter(char letter, char type[]);
  19. void code_file(char *fname, char type[]);
  20. void Add_ext(char *sname);
  21.  
  22. int main(void) {
  23.     char option_MG, option, sname[20];
  24.     char test;
  25.  
  26.     sname[0] = '\0';
  27.     option_MG = option = '\0';
  28.  
  29.     while( option_MG != 'e' ) {
  30.         option_MG = Menu_Geral();
  31.  
  32.         if( option_MG == 'q' ) {
  33.             Ask_Fname(sname);
  34.             code_file(sname, ENC);
  35.         }
  36.         else    if( option_MG == 'w' ) {
  37.             Ask_Fname(sname);
  38.             code_file(sname, DEC);
  39.         }
  40.     }
  41.  
  42.     return 0;
  43. }
  44.  
  45. char Menu_Geral(void) {
  46.     char option = '\0';
  47.  
  48.     system("cls");
  49.     printf("\t\tMenu Geral\n\n");
  50.     printf("\tq - Encode\n");
  51.     printf("\tw - Decode\n");
  52.     printf("\te - Sair\n\n");
  53.  
  54.     while( option != 'q' && option != 'w' && option != 'e' ) {
  55.         fflush(stdin);
  56.         option = getch();
  57.     }
  58.     fflush(stdin);
  59.  
  60.     return option;
  61. }
  62.  
  63. void Ask_Fname(char *sname) {
  64.     system("cls");
  65.     printf("Nome do ficheiro: ");
  66.     gets(sname);
  67. }
  68. char Encode_Letter(char letter, char type[]) {
  69.     int idxa, idxb;
  70.     char min, max;
  71.  
  72.     if( ( letter >= 'a' && letter <= 'z' ) || ( letter >= 'A' && letter <= 'Z' ) ) {
  73.         if( islower(letter) ) {
  74.             min = 'a';
  75.             max = 'z';
  76.         }
  77.         else    if ( isupper(letter) ){
  78.             min = 'A';
  79.             max = 'Z';
  80.         }
  81.  
  82.         for(idxb = maxRuns, idxa = letter; idxb > 0 ; idxb--, idxa++) {
  83.             if( idxa == max )
  84.                 idxa = min-1;
  85.         }
  86.  
  87.         letter = idxa;
  88.     }
  89.     else
  90.         letter = '\0';
  91.  
  92.     return letter;
  93. }
  94. void code_file(char *fname, char type[]) {
  95.     FILE * pFileE, *pFileOutput;
  96.     char strbuffer[5000], strbufferOUT[5000];
  97.     int idx = 0;
  98.  
  99.     strbufferOUT[0] = strbuffer[0] = '\0';
  100.  
  101.     Add_ext(fname);
  102.     pFileE = fopen(fname,"r");
  103.     if( !strcmp(type, DEC) )
  104.         pFileOutput = fopen("decoded.txt", "w");
  105.     if( !strcmp(type, ENC) )
  106.         pFileOutput = fopen("encoded.txt", "w");
  107.  
  108.     while( fgets(strbuffer,4997,pFileE) != NULL ){
  109.         while( strbuffer[idx] != '\0' ) {
  110.             if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
  111.                 strbufferOUT[idx] = Encode_Letter(strbuffer[idx], type);
  112.             else
  113.                 strbufferOUT[idx] = strbuffer[idx];
  114.             idx++;
  115.         }
  116.         strbufferOUT[idx] = '\0';
  117.         fputs(strbufferOUT, pFileOutput);
  118.         idx = 0;
  119.     }
  120.     system("cls");
  121.     if( !strcmp(type, DEC) )
  122.         printf("Descodificacao completa!!\n\n");
  123.     if( !strcmp(type, ENC) )
  124.         printf("Codificacao completa!!\n\n");
  125.     system("pause");
  126.  
  127.     fclose(pFileOutput);
  128.     fclose(pFileE);
  129. }
  130. void Add_ext(char *sname) {
  131.     while( *sname != '\0' ) sname++;
  132.     *(sname) = '.';
  133.     *(sname+1) = 't';
  134.     *(sname+2) = 'x';
  135.     *(sname+3) = 't';
  136.     *(sname+4) = '\0';
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement