0% found this document useful (0 votes)
34 views4 pages

Subiect 2019

The document is a C program that provides various functionalities such as reading a text file, displaying the text, counting lines, encoding and decoding messages based on specific conventions. It allows users to interact with the program through a menu-driven interface, offering options to perform different operations on the text. The program also includes an author information section.

Uploaded by

Stanislav Gratii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Subiect 2019

The document is a C program that provides various functionalities such as reading a text file, displaying the text, counting lines, encoding and decoding messages based on specific conventions. It allows users to interact with the program through a menu-driven interface, offering options to perform different operations on the text. The program also includes an author information section.

Uploaded by

Stanislav Gratii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

unsigned **citire(unsigned *n)


{FILE *f=fopen("date.in","rt");

char **s=(char **)malloc(10*sizeof(char*)),c;

int i,j;
s[0]=(char *)malloc((100*10)*sizeof(char));
for(i=1;i<10;i++)
s[i]=s[0]+100*i;

i=-1;
do
{++i;
j=-1;
do{fscanf(f,"%c",&s[i][++j]);
}while(s[i][j]!='\n');
s[i][j]='\0';

}while(s[i][j-1]!='.');

*n=i+1;

fclose(f);
return s;
}

void afisare(char **s,unsigned n)


{for(int i=0;i<n;i++,printf("\n"))
for(int j=0;j<strlen(s[i]);j++)
printf("%c",s[i][j]);
printf("\n");
}

void afisareV(char **s,unsigned n)


{for(int i=0;i<n;i++,printf("\n"))
for(int j=0;j<strlen(s[i]);j++)
if(s[i][j] != ' ' && s[i][j]!='.' && (s[i][j+1]==' ' || s[i][j+1]=='\0') && s[i]
[j+2]!=s[n-1][strlen(s[i])-1]) printf("%c,",s[i][j]);
else
printf("%c",s[i][j]);
printf("\n");
}

unsigned *afisareN(char **s,unsigned n)


{unsigned c,*v=(unsigned *)malloc(n*sizeof(unsigned));
for(int i=0;i<n;i++)
{c=0;
for(int j=0;j<strlen(s[i]);j++)
if(s[i][j] != ' ' && s[i][j]!='.' && (s[i][j+1]==' ' || s[i][j+1]=='\0')) c++;
v[i]=c;
}
return v;
}

void decodificare(char **s,unsigned n)


{int i,j,help,k,progresie;
char *p;
s[n-1][strlen(s[n-1])-1]='\0'; //nu mai iau in considerare punctul citit din
fisier

for(i=0;i<n;i++,printf("\n"))
{p=strtok(s[i]," ");
progresie=0;

while(p)
{help=0;
k=-1;
++progresie; //caracterele DE PE FIECARE LINIE CITITA sunt codificata pe rand
astfel:primul caracter in baza 8,al doilea
//in baza 10,al treilea in baza 16,al patrulea in baza 8,al
cincilea in baza 10 si tot asa

if((progresie-1)%3==0) for(j=strlen(p)-1;j>=0;j--) //progresie apartine


multimii {1,4,7,10,...}
{++k;
help=help+(p[j]-'0')*pow(8,k);
}

if((progresie-2)%3==0) for(j=strlen(p)-1;j>=0;j--) //progresie apartine


multimii {2,5,8,11,...}
{++k;
help=help+(p[j]-'0')*pow(10,k);
}

if((progresie-3)%3==0) for(j=strlen(p)-1;j>=0;j--) //progresie apartine


multimii {3,6,9,12,...}
{++k;
if(p[j]>='a') help=help+(p[j]-'a'+10)*pow(16,k);
else help=help+(p[j]-'0')*pow(16,k);
}
printf("%c",(char *)help);
p=strtok(NULL," ");}
}

}
void encode()
{FILE *f=fopen("encode.in","rt");
char *s=(char *)malloc(100*sizeof(char));

int i=-1;
do{fscanf(f,"%c",&s[++i]);}while(s[i]!='\n');

s[i]='\0';

fclose(f);

printf("%s (codificat) --> ",s);

int k=0,j;

for(j=0;j<strlen(s);j++)
{++k;
if((k-1)%3==0) printf("%o ",s[j]);
if((k-2)%3==0) printf("%u ",s[j]);
if((k-3)%3==0) printf("%x ",s[j]);
}

printf("\n");
}

void autor ()
{printf("\nProgramul a fost realizat de Ion POPESCU,grupa 3111\n");
}
int main()
{char optiune,**s;
unsigned n,i,*v;

do
{printf("Alege o optiune dintre cele de mai jos !\n\n");
printf("C-Citeste un text de mai multe randuri,terminat cu caracterul '.',text ce
contine %cnumere%c \n"
" separate prin spatiu sau TAB,caracterul final '.' fiind eliminat\n"
"A-Afiseaza textul citit\n"
"L-Calculeaza si afiseaza numarul de randuri din text\n"
"V-Afiseaza sirul citit,plasand dupa sfarsitul fiecarul cuvant (numar)
cate o virgula\n"
"N-Afiseaza numarul cuvintelor (numerelor) de pe fiecare rand continut in
text\n"
"D-Decodificarea mesajului primit, respectand conventia definita la
inceput\n"
"E-(Encode) va citi un text si-l va codifica,generand o insiruire de
coduri,conform conventiei\n"
"I-Afiseaza informatii despre autorul programului\n"
"T-Terminare program.\n",'"','"');

printf("\nIntrodu optiunea aleasa : ");


fflush(stdin);
scanf("%c",&optiune);
printf("\n");

switch(optiune)
{case 'c':
case 'C':s=citire(&n);
break;

case 'a':
case 'A':afisare(s,n);
break;

case 'l':
case 'L':printf("\nNumar randuri = %u\n",n);
break;

case 'v':
case 'V':afisareV(s,n);
break;

case 'n':
case 'N':v=afisareN(s,n);
printf("\nNumar cuvinte: %u",v[0]); for(i=1;i<n;i++) printf(", %u",v[i]);
printf("\n");
break;
case 'd':
case 'D':printf("\nSirul decodificat:\n\n");
decodificare(s,n);
break;

case 'e':
case 'E':encode();
break;

case 'i':
case 'I':autor();
break;

case 't':
case 'T':printf("\nAi ales parasirea programului !\n");
break;

default: printf("\nIntrodu o optiune valida !\n");


}
printf("\nPress any key to continue!\n");
getch();
system("cls");
}while(optiune!='t' && optiune !='T');

return 0;
}

You might also like