Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Leandro Soares
- School: INETE
- Date: 19-03-2012
- Time: 20:19 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <ctype.h>
- #include <string.h>
- #define maxRuns 13
- #define ENC "Encode"
- #define DEC "Decode"
- char Menu_Geral(void);
- void Ask_Fname(char *sname);
- char Encode_Letter(char letter, char type[]);
- void code_file(char *fname, char type[]);
- void Add_ext(char *sname);
- int main(void) {
- char option_MG, option, sname[20];
- char test;
- sname[0] = '\0';
- option_MG = option = '\0';
- while( option_MG != 'e' ) {
- option_MG = Menu_Geral();
- if( option_MG == 'q' ) {
- Ask_Fname(sname);
- code_file(sname, ENC);
- }
- else if( option_MG == 'w' ) {
- Ask_Fname(sname);
- code_file(sname, DEC);
- }
- }
- return 0;
- }
- char Menu_Geral(void) {
- char option = '\0';
- system("cls");
- printf("\t\tMenu Geral\n\n");
- printf("\tq - Encode\n");
- printf("\tw - Decode\n");
- printf("\te - Sair\n\n");
- while( option != 'q' && option != 'w' && option != 'e' ) {
- fflush(stdin);
- option = getch();
- }
- fflush(stdin);
- return option;
- }
- void Ask_Fname(char *sname) {
- system("cls");
- printf("Nome do ficheiro: ");
- gets(sname);
- }
- char Encode_Letter(char letter, char type[]) {
- int idxa, idxb;
- char min, max;
- if( ( letter >= 'a' && letter <= 'z' ) || ( letter >= 'A' && letter <= 'Z' ) ) {
- if( islower(letter) ) {
- min = 'a';
- max = 'z';
- }
- else if ( isupper(letter) ){
- min = 'A';
- max = 'Z';
- }
- for(idxb = maxRuns, idxa = letter; idxb > 0 ; idxb--, idxa++) {
- if( idxa == max )
- idxa = min-1;
- }
- letter = idxa;
- }
- else
- letter = '\0';
- return letter;
- }
- void code_file(char *fname, char type[]) {
- FILE * pFileE, *pFileOutput;
- char strbuffer[5000], strbufferOUT[5000];
- int idx = 0;
- strbufferOUT[0] = strbuffer[0] = '\0';
- Add_ext(fname);
- pFileE = fopen(fname,"r");
- if( !strcmp(type, DEC) )
- pFileOutput = fopen("decoded.txt", "w");
- if( !strcmp(type, ENC) )
- pFileOutput = fopen("encoded.txt", "w");
- while( fgets(strbuffer,4997,pFileE) != NULL ){
- while( strbuffer[idx] != '\0' ) {
- if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
- strbufferOUT[idx] = Encode_Letter(strbuffer[idx], type);
- else
- strbufferOUT[idx] = strbuffer[idx];
- idx++;
- }
- strbufferOUT[idx] = '\0';
- fputs(strbufferOUT, pFileOutput);
- idx = 0;
- }
- system("cls");
- if( !strcmp(type, DEC) )
- printf("Descodificacao completa!!\n\n");
- if( !strcmp(type, ENC) )
- printf("Codificacao completa!!\n\n");
- system("pause");
- fclose(pFileOutput);
- fclose(pFileE);
- }
- void Add_ext(char *sname) {
- while( *sname != '\0' ) sname++;
- *(sname) = '.';
- *(sname+1) = 't';
- *(sname+2) = 'x';
- *(sname+3) = 't';
- *(sname+4) = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement