Sunteți pe pagina 1din 6

Structuri de date - Culegere de probleme n C++ 1gVV

8. FiLHUH

Problema 8.1 bV bLV @MM LV VV gV @V VLM
LM MMMM MMM gV WV @ V. iMMVV gVMM bV W M M WV
LMMW . 1VMM VVLM @MMM M b MM gV WV VbV
11111.. 1MbV M VVLM V a programului se face cu comanda

C:\>Prob81 FISIER.TST


Rezolvare:


#include<stdio.h>
#include<stdlib.h>
#include<CONIO.H>
void main(int argc,char *paramstr[]){
FILE*pf;
int c;
clrscr();
if(argc==2){
if(!(pf=fopen(paramstr[1],"r")))
{printf("\nNu se poate deschide fisierul!");
exit(1);}
puts("\n");
}
else {printf("\nNr necorespunzator de argumente in linia de comanda!");
exit(1);}
while((c=getc(pf))!=EOF) putc(c,stdout);
fclose(pf);
}








Structuri de date - Culegere de probleme n C++ 1gVV

Problema 8.2 S bV bLV @MM LV LWL g WVLWL
LM MMM MMM gV. 1MM L MMMVV gVMM WM M WV
LMMW g M MML V WV @ MMV`
- LWL LM MMM gVMM b@VLL g MM gV VM
V MM 1WWVMq WL gV bLMMb [MM @MV
vizualizat cu comanda DIR );
- WVLWL MM gV L@ g bVV MM WV gV
normal

Rezolvare:

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<io.h>// pt prototipul functiei unlink
#include<string.h>
#include<process.H>
void main(int argc,char *paramstr[]){
clrscr();
unsigned char opt;
int c;
FILE *fp,*fp1;
if(argc==2){
if(!(fp=fopen(paramstr[1],"r"))){
printf("\nNu se poate deschide fisierul!");
exit(1);}
puts("\n");
if(!(fp1=fopen("TEMP.TXT","w")))
{printf("\nNu se poate deschide fisierul!");
exit(1);}
}
else{
printf("nr necorespunzator de parametri in linia de comanda!");
exit(1);}

printf("\nCriptare/Decriptare (c/d) ?");
fflush(stdin);
scanf("%d",&opt);
while((c=getc(fp))!=EOF){
if(c>125)putc((char(c-125)),fp1);
Structuri de date - Culegere de probleme n C++ 1gVV

if(c<125)putc((char(c+125)),fp1);
}
printf("\ns-a criptat/decriptat");

fclose(fp);
fclose(fp1);
unlink(paramstr[1]);
rename("TEMP.TXT",paramstr[1]);
if(opt==c)
_dos_setfileattr(paramstr[1],FA_HIDDEN);
else _dos_setfileattr(paramstr[1],_A_HIDDEN );
getch();
}


Problema 8.3 S bV bLV MM @M LV WVVMM VLVM WV
@ V LLVV 111 WM-MM gVq MMMVV gVMM
citindu-se din linia de comanda.


Rezolvare:

`L N 1\1\>1 \ >111L111 LL L1L LL 11_ >11N . 11@\111111 LL L1L11 L>1L
111 1\111.
- >L L11Lg1L L1L1L1 L1 L1L1L1 L11 11g1L111 >_LL111L1 _1 >L ]11@L
1 >11g1111 11g1L11111
- >L NL1111L LL LL>1 L1L1L1 L71>1 LL] 1 >11N .

LL 11 L71>1 LL>1 L1L1L1 >L _11L 1 >11N
111 111Z1L 111 111 LL _11 11 L1

LL L71>1 >L 11L1L1L11LZ 111 111 LL >_ 11

- cnd s- ]11> 1 >11g11 LL 11g1L1 >L \1L\1LZ >11N 1 111L 1L LL
111 111 LL _11 11 11LL 111 L1L1L1





Structuri de date - Culegere de probleme n C++ 1gVV

Rezolvare:

#include <stdio.h>
#include<string.h>
#include<conio.h>
#include <alloc.h>//malloc
#include<stdlib.h>//malloc

typedef struct tnod{
unsigned char info;
int nr;
struct tnod*paep;
}TNOD;
typedef enum{false,true} boolean;
boolean vb;
int contor1=0;
int contor2=0;
TNOD * vs;
FILE * fp;
unsigned char c;

TNOD * inserare( TNOD* ps, unsigned char cc){
TNOD * p;
p=(TNOD*)malloc(sizeof(TNOD));
p->info=cc;
p->nr=1;
p->paep=ps;
return(p);
}
boolean verificare(TNOD *ps, unsigned char cc){
if(ps==NULL)return false;
else{
while((ps->info!=cc)&&(ps->paep!=0))ps=ps->paep;
if(ps->info==cc){
(ps->nr)++;
return true;
}
else return false;
}
}
Structuri de date - Culegere de probleme n C++ 1gVV

void scriere(TNOD* ps){
if(!ps)
printf("\nStiva vida");
else while(ps){
switch(ps->info){
case 13:printf("\n CR %d",ps->nr);
case 10:printf("\n LF %d",ps->nr);
default:printf("\n%c %d",ps->info,ps->nr);
}
ps=ps->paep;
}
}
void numarare(TNOD * ps){
while(ps){
contor1+=ps->nr;
contor2++;
ps=ps->paep;
}
}
TNOD * ordonare(TNOD *ps){
unsigned char vb;
TNOD *p1,*p2,*temp;
vb=0;
p1=ps;
while(p1){
p2=p1->paep;
while(p2!=0){
if(p1->nr<p2->nr){
temp->info=p1->info;
temp->nr=p1->nr;
p1->info=p2->info;
p1->nr=p2->nr;
p2->info=temp->info;
p2->nr=temp->nr;
}
p2=p2->paep;
}
if(!vb){
ps=p1;
vb=1;
Structuri de date - Culegere de probleme n C++ 1gVV

}
p1=p1->paep;
}
return ps;
}

void main(int argc,char *paramstr[]){
clrscr();
FILE*fp;
int c;
vs=0;
if(argc==2){
if((fp=fopen(paramstr[1],"r"))==0)
{printf("\nNu se poate deschide fisierul!");
exit(1);}
puts("\n");
}
else {printf("\nNr necorespunzator de argumente in linia de comanda!");
exit(1);}
while((c=getc(fp))!=EOF){
vb=verificare(vs,c);
if(!vb) vs=inserare(vs,c);
}
fclose(fp);
scriere(vs);
vs=ordonare(vs);
printf("\nStiva ordonata!");
scriere(vs);
numarare(vs);
printf("\nNr de caractere din fisier: %d",contor1);
printf("\nNr de caractere distincte: %d",contor2);
}

S-ar putea să vă placă și