Advertisement
BORUTO-121

morze_coder_decoder.c

Sep 2nd, 2022 (edited)
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include "slova.h"
  4.  
  5. char decode(char *slovo,int vel){
  6.     char slova[26][5]={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
  7.     char brojevi[10][6]={nula,jedan,dva,tri,cetiri,pet,sest,sedam,osam,devet};
  8.     if(vel<5){
  9.         int i;
  10.         for(i=0;i<26 && strcmp(slova[i],slovo)!=0;i++);
  11.         return 'A'+i;
  12.     }
  13.     int i;
  14.     for(i=0;i<10 && strcmp(brojevi[i],slovo)!=0;i++);
  15.     return '0'+i;
  16. }
  17.  
  18. void dekodiraj(){
  19.     FILE* pok1=fopen("morze.txt","r");
  20.     FILE* upisi=fopen("decode.txt","w");
  21.     //FILE* pok2=fopen("morze.txt","r");
  22.     if(pok1==NULL){
  23.         printf("Datoteka %s ne postoji","morze.txt");
  24.  
  25.     }
  26.     else{
  27.     int c;
  28.    
  29.     while((c=fgetc(pok1))!=EOF){
  30.         int razmaci=1;
  31.         for(;c==' ' && c!=EOF;c=fgetc(pok1),razmaci++);
  32.         if(razmaci>1)fputc(' ',upisi);
  33.         int brojac=0;
  34.         char slovo[6]="";
  35.         for(;c!=' ' && c!=EOF;slovo[brojac]=(char)c,brojac++,c=fgetc(pok1));
  36.         fputc(decode(slovo,brojac),upisi);
  37.     }
  38.       fclose(pok1);
  39.       fclose(upisi);
  40.     }
  41. }
  42.  
  43. int is_letter(char c){
  44.   return (c>='A' && c<='Z') || (c>='a' && c<='z');
  45. }
  46. int is_number(char c){
  47.   return (c>='0' && c<='9');
  48. }
  49. char to_upper(char c){
  50.   if(c>='A' && c<='Z') return c;
  51.   return c+'A'-'a';
  52. }
  53.  
  54. void zakodiraj(){
  55.   char slova[26][5]={A,B,C,D,E,F,G,H,I,J,K,L,
  56.   M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
  57.   char brojevi[10][6]={nula,jedan,dva,tri,cetiri,pet,sest,sedam,osam,devet};
  58.   FILE* pok1=fopen("tekst.txt","r");
  59.   FILE* upisi=fopen("morze.txt","w");
  60.   int c;
  61.   while((c=fgetc(pok1))!=EOF){
  62.     if(is_letter(c))
  63.       fputs(slova[(int)(to_upper(c)-'A')],upisi);
  64.     else if(is_number(c))
  65.       fputs(brojevi[(int)(c-'0')],upisi);
  66.    
  67.     fputc(' ',upisi);
  68.   }
  69.   fclose(pok1);
  70.   fclose(upisi);
  71. }
  72. int main(){
  73.   zakodiraj();
  74.   dekodiraj();
  75.   return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement