#include<fstream>
#include<iostream>
using namespace std;
struct nod{
char culoare;
nod* urm;};
void adaugare_coada(nod* &prim,nod* &ultim,char x)
{nod* p=new nod;
p->culoare=x;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void adaugare_stiva(nod* &vf, int x)
{nod* p;
p=new nod;
p->culoare=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva/Coada goala";
else
while(vf!=NULL)
{cout<<vf->culoare<<" | ";
vf=vf->urm;}
cout<<endl;
}
int main()
{nod *c1=NULL,*c2=NULL,*c=NULL,*uc1=NULL,*uc2=NULL;
ifstream f("date");
char x;
int n;
f>>n;
int i=0;
while(i<n/2)
{f>>x;
i++;
adaugare_coada(c1,uc1,x);
}
while(f>>x)
adaugare_coada(c2,uc2,x);
afisare(c1);
afisare(c2);
while(c1 && c2)
{adaugare_stiva(c,c1->culoare);
c1=c1->urm;
if(c->culoare=='R')
while(c)
{adaugare_coada(c2,uc2,c->culoare);
c=c->urm;}
if(c1 && c2)
{adaugare_stiva(c,c2->culoare);
c2=c2->urm;
if(c->culoare=='R')
while(c)
{adaugare_coada(c1,uc1,c->culoare);
c=c->urm;}
}
}
}
Orar semigroup #2
Archive for mai 2012
5/30 - Joc carti
marți, 29 mai 2012
by DlMuresan
Categories:
carti,
coada,
joc,
stiva
|
Leave a comment
5/24
Se citesc nr dintr-un fisier. Sa se calculeze media aritmetica. Sa se insereze dupa fiecare nr, nr sau de cifre.
#include<iostream>
#include<fstream>
using namespace std;
struct nod{int info;
nod* urm;};
void adaugare(nod* &vf, double x)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Lista goala";
else
while(vf!=NULL)
{cout<<vf->info<<" | ";
vf=vf->urm;}
cout<<endl;
}
int nrcif(int x)
{int i=0;
while(x)
{i++;x=x/10;}
return i;}
void inserare(nod* vf,int x)
{nod* p=new nod;
p->info=x;
p->urm=vf->urm;
vf->urm=p;}
void stergere(nod* &vf)
{nod* p,*q=vf;
int k=0;
while(q->urm && k<10)
{
if(q->urm->info>9 && q->urm->info<100)
{
//cout<<q->urm->info;
p=q->urm;
q=q->urm->urm;
delete p;}
else q=q->urm;
k++;
}
}
int main()
{ifstream f("date.in");
nod* vf=NULL;
double i=0,s=0,x;
while(f>>x)
{i++;
adaugare(vf,x);
s=s+x;}
afisare(vf);
double m=s/i;
m=m*100;
m=(int)m;
m=m/100;
cout<<"Media: "<<m<<endl;
nod* p=vf;
while(vf!=NULL)
{inserare(vf,nrcif(vf->info));
vf=vf->urm->urm;}
afisare(p);
vf=p;
cout<<endl;
stergere(vf);
//afisare(vf);
}
joi, 24 mai 2012
by DlMuresan
Categories:
liste
|
Leave a comment
5/23
Se citesc nr intregi care se aseaza intr-o stiva. Sa se creeze o coada cu nr neprime din stiva. Fiecare nr va fi urmat de nr sau de divizori.
#include<iostream>
#include<fstream>
using namespace std;
struct stiva{int info;
stiva* urm;};
struct coada{int info,nrdiv;
coada* urm;};
void adaugare_coada(coada* &prim,coada* &ultim,int x,int y)
{coada* p=new coada;
p->info=x;
p->nrdiv=y;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void adaugare_stiva(stiva* &vf, int x)
{stiva* p;
p=new stiva;
p->info=x;
p->urm=vf;
vf=p;}
void afisare_coada(coada* vf)
{if(vf==NULL)
cout<<"Stiva/Coada goala";
else
while(vf!=NULL)
{cout<<vf->info<<" "<<vf->nrdiv<<" | ";
vf=vf->urm;}
cout<<endl;
}
int primm(int n)
{int d,i,ok=1;
if(n==1 || n==0)
return 0;
for(d=2;d<=n/2;d++)
if(n%d==0)
ok=0;
return ok;}
int nrdiv(int x)
{int i=0;
for(int d=1;d<=x;d++)
if(x%d==0)
i++;
return i;}
void afisare_stiva(stiva* vf)
{if(vf==NULL)
cout<<"Stiva/Coada goala";
else
while(vf!=NULL)
{cout<<vf->info<<" | ";
vf=vf->urm;}
cout<<endl;
}
int main()
{coada* prim=NULL;
stiva* vf=NULL;
coada* ultim=NULL;
int x,n,i=0;
ifstream f("date");
while(f>>x)
adaugare_stiva(vf,x);
afisare_stiva(vf);
while(vf)
{
if(nrdiv(vf->info)>2)
adaugare_coada(prim,ultim,vf->info,nrdiv(vf->info));
vf=vf->urm;
}
afisare_coada(prim);
}
marți, 22 mai 2012
by DlMuresan
Categories:
coada,
stiva
|
Leave a comment
5/18
Se citesc dintr-un fisier nr. Sa se creeze o coada cu nr prime si o stiva cu cele neprime. Sa se inlocuiasca fiecare nr din coada cu nr de cifre ale lui. Sa se stearga din stiva nr pana la intalnirea unui nr de 2 cifre. Cate nr s-au sters? Sa se concateneze elementele din coada la sfarsitul stivei. Sa ase afiseze noua stiva.
#include<iostream>
#include<fstream>
using namespace std;
struct nod{int info;
nod* urm;};
void adaugare_coada(nod* &prim,nod* &ultim,int x)
{nod* p=new nod;
p->info=x;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void stergere(nod* &prim)
{nod* p;
p=prim;
prim=prim->urm;
delete p;
}
void adaugare_stiva(nod* &vf, int x)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva/Coada goala";
else
while(vf!=NULL)
{cout<<vf->info<<" | ";
vf=vf->urm;}
cout<<endl;
}
int primm(int n)
{int d,i,ok=1;
if(n==1 || n==0)
return 0;
for(d=2;d<=n/2;d++)
if(n%d==0)
ok=0;
return ok;}
int nrcif(int x)
{int i=0;
while(x)
{i++;x=x/10;}
return i;}
int main()
{ifstream f("date");
nod* prim=NULL;
nod* ultim;
nod* vf=NULL;
int x;
while(f>>x)
if(primm(x))
adaugare_coada(prim,ultim,x);
else adaugare_stiva(vf,x);
cout<<"Coada"<<endl;
afisare(prim);
cout<<"Stiva"<<endl;
afisare(vf);
nod*p=prim;
while(p)
{p->info=nrcif(p->info);
p=p->urm;}
cout<<"Noua coada"<<endl;
afisare(prim);
int j=0;
while(vf)
{if(nrcif(vf->info)!=2)
{stergere(vf);j++;}
else break;
}
cout<<"Noua stiva"<<endl;
afisare(vf);
cout<<"S-au sters "<<j<<" elemente din stiva"<<endl;
cout<<"Stiva concatenata cu coad"<<endl;
while(prim)
{adaugare_stiva(vf,prim->info);
prim=prim->urm;}
afisare(vf);
}
joi, 17 mai 2012
by DlMuresan
Categories:
coada,
stiva
|
Leave a comment
5/17
Din fisierul text AVIOANE.TXT se citesc: n, codurile celor n avioane,
urmate de o insiruire de DA si NU (date de conditiile atmosferice).
Afisati:
a) coada initiala
b) ordinea in care decoleaza avioanele si numarul de incercari de decolare ale fiecarui avion
Obs: avioanele care decoleaza se sterg din lista.
Ex: daca AVIOANE.TXT: 3 111 222 333 DA NU NU NU DA DA DA DA
Avioanele decoleaza in ordinea: 111 o incercare(prima)
333 2 incercari
222 3 incercari
#include<iostream>Fisier
#include<fstream>
using namespace std;
struct nod{
int cod,nr;
nod* urm;};
void adaugare(nod* &prim, nod* &ultim, int cod, int nr)
{nod* p=new nod;
p->cod=cod;
p->nr=nr;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void stergere(nod* &prim)
{nod* p;
p=prim;
prim=prim->urm;
delete p;
}
void afisare(nod* prim)
{if(prim==NULL)
cout<<"Coada goala";
cout<<prim->cod<<" | din a "<<prim->nr<<"-a incercare"<<endl;
}
int main()
{int n,cod,nr,i;
ifstream f("date");
nod* prim=NULL;
nod* ultim=NULL;
f>>n;
while(n)
{f>>cod;
n--;
adaugare(prim,ultim,cod,0);
}
char a[3];
while(prim)
{f>>a;
if(strcmp(a,"DA")==0)
{prim->nr++;
cout<<"Decoleaza ";
afisare(prim);
stergere(prim);}
else {prim->nr++;
adaugare(prim,ultim,prim->cod,prim->nr);
stergere(prim);}
}
}
3 111 222 333 DA NU NU NU DA DA
by DlMuresan
Categories:
coada
|
Leave a comment
5/16
Se citesc notele unui elev intr-o coada. Afisati ce note nu a luat. Verificati daca a luat 10 dupa 4. Calculati media, stiind ca nota de la teza e pe ultima pozitie.
#include<iostream>Problema 9
#include<fstream>
using namespace std;
struct nod{int info;
nod* urm;};
void adaugare(nod* &prim, nod* &ultim, int x)
{nod* p=new nod;
p->info=x;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void afisare(nod* prim)
{if(prim==NULL)
cout<<"Coada goala";
else while(prim->urm)
{cout<<prim->info<<" | ";
prim=prim->urm;}
cout<<prim->info<<" (Teza)";
cout<<endl;
}
void zece(nod* prim)
{while(prim->urm!=NULL)
if(prim->info==4 && prim->urm->info==10)
{cout<<"A luat 10 dupa 4";
break;}
else {prim=prim->urm;}
cout<<"Nu a luat 10 dupa 4";
}
float medie(nod* prim)
{float m,s;
int i=0;
while(prim->urm)
{s=s+prim->info;
i++;
prim=prim->urm;}
s=s/i;
s=s*100;
s=(int)s;
s=(float)s/100;
s=s*3+prim->info;
s=s/4;
s=s*100;
s=(int)s;
s=(float)s/100;
return s;}
int note(int x, nod* prim)
{while(prim!=NULL)
{if(prim->info==x)
return 1;
prim=prim->urm;}
return 0;}
int main()
{ifstream f("date");
int x,k;
nod* prim=NULL;
nod* ultim=NULL;
while(f>>x)
adaugare(prim,ultim,x);
afisare(prim);
cout<<"Nu a luat notele: ";
for(int i=1;i<=10;i++)
if(note(i,prim)==0)
cout<<i<<" ";
cout<<endl;
zece(prim);
cout<<endl<<"Media: "<<medie(prim);
}
#include<iostream>Fişier
#include<fstream>
using namespace std;
struct nod{char cuv[10],v1[10],v2[10],v3[10];
int corect;
nod* urm;};
void adaugare(nod* &prim, nod* &ultim, char cuv[], char v1[], char v2[], char v3[], int corect)
{nod* p=new nod;
strcpy(p->cuv,cuv);
strcpy(p->v1,v1);
strcpy(p->v2,v2);
strcpy(p->v3,v3);
p->corect=corect;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void stergere(nod* &prim)
{nod* p;
p=prim;
prim=prim->urm;
delete p;
}
void afisare(nod* prim)
{if(prim==NULL)
cout<<"Coada goala";
cout<<endl<<prim->cuv<<endl;
cout<<"1. "<<prim->v1<<endl;
cout<<"2. "<<prim->v2<<endl;
cout<<"3. "<<prim->v3<<endl;
cout<<"Raspuns: ";
}
int main()
{ifstream f("date");
int x,k,punctaj;
nod* prim=NULL;
nod* ultim=NULL;
char cuv[10],v1[10],v2[10],v3[10];
int p=0,r,corect;
cout<<"Punctaj pe intrebare: ";
cin>>k;
while(f>>cuv>>v1>>v2>>v3>>corect)
adaugare(prim,ultim,cuv,v1,v2,v3,corect);
//afisare(prim);
while(prim)
{afisare(prim);
cin>>r;
if(r==prim->corect)
{p=p+k;
prim=prim->urm;}
else {p--;
adaugare(prim,ultim,prim->cuv,prim->v1,prim->v2,prim->v3,prim->corect);
stergere(prim);}
}
cout<<endl<<"Punctaj: "<<p;
}
gras
fat
fatigue
faringe
1
lingau
guilty
grasul
tudy
2
logan
logy
car
airplane
3
dolgopolov
tennis_player
tennis_master
ukraineak_blatist
2
marți, 15 mai 2012
by DlMuresan
Categories:
coada
|
Leave a comment
5/15 - Coada, Totti
Adaugare, Afisare, Stergere
#include<iostream>
#include<fstream>
using namespace std;
struct nod{int info;
nod* urm;};
void adaugare(nod* &prim, nod* &ultim, int x)
{nod* p=new nod;
p->info=x;
p->urm=NULL;
if(prim==NULL)
prim=p;
else ultim->urm=p;
ultim=p;}
void afisare(nod* prim)
{if(prim==NULL)
cout<<"Coada goala";
else while(prim)
{cout<<prim->info<<" | ";
prim=prim->urm;}
cout<<endl;
}
void stergere(nod* &prim, int k)
{nod* p;
while(k && prim)
{p=prim;
prim=prim->urm;
delete p;
k--;}
}
int main()
{ifstream f("date");
int x,k;
nod* prim=NULL;
nod* ultim=NULL;
cin>>k;
while(f>>x)
adaugare(prim,ultim,x);
afisare(prim);
adaugare(prim,ultim,2);
stergere(prim,k);
afisare(prim);
}
luni, 14 mai 2012
by DlMuresan
Categories:
coada
|
Leave a comment
5/11
Se citesc dintr-un fisier text nr care se pun intr-o stiva. Cate cifre sunt in toate nr din stiva? Sa se construiasca o stiva cu nr prime. Sa se stearga elementele din stiva pana la prima aparitie a maximului. Sa se inverseze stiva.
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
int info;
nod* urm;};
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva goala";
else
while(vf!=NULL)
{cout<<vf->info<<" ";
vf=vf->urm;}
}
void adaugare(nod* &vf, int x)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
int nrcif(int x)
{int i=0;
while(x)
{i++;x=x/10;}
return i;}
int prim(int n)
{int d,i,ok=1;
if(n==1 || n==0)
return 0;
for(d=2;d<=n/2;d++)
if(n%d==0)
ok=0;
return ok;}
int main()
{ifstream f("date");
int x,s=0,m=0,mm=0;
nod* vf=NULL,*vf1=NULL,*p;
while(f>>x)
adaugare(vf,x);
nod* pp=vf;
afisare(vf);
cout<<endl;
vf=pp;
while(vf!=NULL)
{s=s+nrcif(vf->info);
if(prim(vf->info))
adaugare(vf1,vf->info);
if(vf->info>m)
m=vf->info;
vf=vf->urm;}
cout<<s<<" cifre in stiva"<<endl;
afisare(vf1);
cout<<endl;
while(vf1!=NULL)
{ if(vf1->info>mm)
mm=vf1->info;
vf1=vf1->urm;}
int ok=1;
vf=pp;
while(vf!=NULL)
{if(vf->urm->info==m)
{ p=vf;
vf=vf->urm;
delete p;
ok=0;}
if(ok==0)
break;
vf=vf->urm;}
afisare(vf);
cout<<endl;
while(vf1!=NULL)
{if(vf1->info!=mm)
{ p=vf;
vf1=vf1->urm;
delete p;}
vf1=vf1->urm;}
afisare(vf1);
cout<<endl<<endl;
}
duminică, 13 mai 2012
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/10
Se citesc numere dintr-un fisier text. Scrieti o functie care verifica daca elementul din varful stivei mai apare in stiva. Afisati numarul de elemente pare din stiva. Stergeti elemente din stiva pana la intalnirea unui nr de 3 cifre (care nu se sterge).
#include<iostream>Sa se inverseze o stiva. // NON-FINISHATA
#include<fstream>
using namespace std;
struct nod{
int info;
nod* urm;};
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva goala";
else
while(vf!=NULL)
{cout<<vf->info<<" ";
vf=vf->urm;}
}
void adaugare(nod* &vf, int x)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void verif_vf(nod* vf)
{int x=vf->info,ok=0;
while(vf->urm!=NULL)
{if(vf->urm->info==x)
ok=1;
vf=vf->urm;}
if(ok==1)
cout<<x<<" (varful) mai apare in stiva";
else cout<<x<<" (varful) nu mai apare in stiva";
}
int nrpare(nod* vf)
{int i=0;
while(vf!=NULL)
{if(vf->info%2==0)
i++;
vf=vf->urm;
}
return i;
}
void stergere(nod* &vf)
{nod* p;
while(vf->info>999 || vf->info<100)
{p=vf;
vf=vf->urm;
delete p;}
}
int main()
{nod* vf=NULL;
ifstream f("date");
int x;
while(f>>x)
adaugare(vf,x);
afisare(vf);
cout<<endl;
verif_vf(vf);
cout<<endl;
cout<<nrpare(vf)<<" nr pare"<<endl;
stergere(vf);
afisare(vf);
}
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
int info;
nod* urm;};
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva goala";
else
while(vf!=NULL)
{cout<<vf->info<<" ";
vf=vf->urm;}
}
void adaugare(nod* &vf, int x)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void verif_vf(nod* vf)
{int x=vf->info,ok=0;
while(vf->urm!=NULL)
{if(vf->urm->info==x)
ok=1;
vf=vf->urm;}
if(ok==1)
cout<<x<<" (varful) mai apare in stiva";
else cout<<x<<" (varful) nu mai apare in stiva";
}
int nrpare(nod* vf)
{int i=0;
while(vf!=NULL)
{if(vf->info%2==0)
i++;
vf=vf->urm;
}
return i;
}
void STADT_C(nod* &vf)
{nod* p;
while(vf->info>999 || vf->info<100)
{p=vf;
vf=vf->urm;
delete p;}
}
int main()
{nod* vf=NULL;
ifstream f("date");
int x;
while(f>>x)
adaugare(vf,x);
afisare(vf);
cout<<endl;
int nr=0,i=1,j;
while(vf!=NULL)
{nr++;
vf=vf->urm;}
nod* p=vf;
while(i<nr/2 && vf!=NULL)
{j=1;
p=vf;
while(j<nr-i)
{p=p->urm;
j++;}
vf->urm=p->urm;
vf->info=p->info;
i++;
}
afisare(p);
}
joi, 10 mai 2012
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/9
Se citesc nr dintr-un fisier. Sa se creeze doua stive, una cu nr pare, alta cu nr impare. Sa se afiseze stivele. Sa se afiseze nr de elemente din fiecare stiva. Sa se adauge stiva elementelor pare la sfarsitul stivei celor impare.
#include<iostream>2 copii dau cu zarul. Aruncarile fiecaruia se memoreaza in cate o stiva. Fiecare da cu zarul pana arunca 6. Cel care are mai multe puncte castiga. Afisati:
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva goala";
while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
int nr_el(nod* vf)
{int i=0;
while(vf!=NULL)
{i++;
vf=vf->urm;}
return i;
}
void concat(nod* vf1, nod* vf2)
{nod*p=vf1;
while(p->urm != NULL)
p=p->urm;
p->urm=vf2;
}
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
int main()
{nod *par=NULL,*impar=NULL;
ifstream f("date");
int x;
while(f>>x)
if(x%2==0)
adaug(x,par);
else adaug(x,impar);
afisare(par);
cout<<endl<<nr_el(par)<<" elemente";
cout<<endl<<endl;
afisare(impar);
cout<<endl<<nr_el(impar)<<" elemente";
cout<<endl<<endl;
concat(impar,par);
afisare(impar);
}
a) stiva fiecaruia
b) numarul de aruncari ale fiecaruia
c) castigatorul si punctele lui
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva goala";
while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
int suma(nod* vf)
{int i=0;
while(vf!=NULL)
{i+=vf->info;
vf=vf->urm;}
return i;
}
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
int main()
{nod *vf1=NULL,*vf2=NULL;
ifstream f("date");
int x;
f>>x;
while(x!=6)
{ adaug(x,vf1);
f>>x;}
f>>x;
while(x!=6)
{adaug(x,vf2);
f>>x;}
afisare(vf1);
cout<<endl<<suma(vf1)<<" puncte";
cout<<endl<<endl;
afisare(vf2);
cout<<endl<<suma(vf2)<<" puncte";
cout<<endl<<endl;
if(suma(vf1)>suma(vf2))
cout<<"Copilul 1 cu "<<suma(vf1);
else if(suma(vf2)>suma(vf1))
cout<<"Copilul 2 cu "<<suma(vf2);
else cout<<"=";
}
marți, 8 mai 2012
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/7
Scrieti o functie care concateneaza doua stive in prima stiva. (adresele varfurilor vor fi param. in functie)
#include<iostream>Un fisier text contine mai multe linii de forma: POP x sau PUSH y, reprezentand operatii care trebuie executate asupra unei stive:
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void afisare(nod* vf)
{while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void creare(nod* &vf, char n[])
{ifstream f(n);
int x;
while(f>>x)
adaug(x,vf);
}
void concat(nod* vf1, nod* vf2)
{nod*p=vf1;
while(p->urm != NULL)
p=p->urm;
p->urm=vf2;
}
int main()
{nod *vf1=NULL,*vf2=NULL,*p;
creare (vf1,"date");
creare (vf2,"date2");
afisare(vf1);
cout<<endl;
afisare(vf2);
cout<<endl;
concat(vf1,vf2);
cout<<endl;
afisare(vf1);
}
POP x => se scot din stiva x numere
PUSH y => se pune in stiva numarul y.
Sa se creeze o stiva asupra careia sa se efectueze toate operatiile specificate.
Sa se verifice daca la sfarsit stiva ramane vida. Obs. Daca nu se poate efectua operatia POP, programul se termina si se afiseaza EROARE.
#include<iostream>Fisier
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva vida";
while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
void POP(int x, nod* &vf)
{int i=0;
nod*p;
while(i<x)
{p=vf;
vf=vf->urm;
i++;
delete p;}
}
int main()
{nod *vf=NULL,*p,*vf2=NULL;
int x,i=0,nr=0;
char s[100];
ifstream f("date");
while(f>>s>>x)
{if(strcmp(s,"POP")==0)
if(x>=nr)
{cout<<"EROARE";return 0;}
else POP(x,vf);
else
if(strcmp(s,"PUSH")==0)
adaug(x,vf);nr++;
}
afisare(vf);
}
PUSH 1Se citesc dintr-un fisier text linii de forma: elev clasa numar_absente
PUSH 1
PUSH 1
PUSH 1
PUSH 1
POP 1
PUSH 6
POP 3
PUSH 1
POP 3
a) Creati 4 stive cu elevii si absentele lor, pentru fiecare nivel. (9,10,11,12)
b) Afisati numarul de absente pe nivele
c) Afisati numarul de elevi de pe fiecare nivel
Obs: clasa poate fi: 9A,9B, …12D
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
char n[20],c[4];
int na;
nod *urm;};
void adaug(char n[], char c[], int na, nod* &vf)
{nod* p;
p=new nod;
strcpy(p->n,n);
strcpy(p->c,c);
p->na=na;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva vida";
while(vf!=NULL){
cout<<vf->n<<" "<<vf->c<<" "<<vf->na<<" ";
vf=vf->urm;
cout<<endl;}
}
int nr_abs(nod* vf)
{int i=0;
while(vf!=NULL)
{i+=vf->na;
vf=vf->urm;}
return i;
}
int nr_el(nod* vf)
{int i=0;
while(vf!=NULL)
{i++;
vf=vf->urm;}
return i;
}
int main()
{nod *vf=NULL,*p,*vf2=NULL;
int na;
char n[20],pr[20],c[4];
nod *vf9=NULL,*vf10=NULL,*vf11=NULL,*vf12=NULL;
ifstream f("date");
while(f>>n)
{f>>c>>na;
if(strncmp(c,"9",1)==0)
adaug(n,c,na,vf9);
else if(strncmp(c,"10",2)==0)
adaug(n,c,na,vf10);
else if(strncmp(c,"11",2)==0)
adaug(n,c,na,vf11);
else if(strncmp(c,"11",2)==0)
adaug(n,c,na,vf12);
}
cout<<"Clasa 9"<<endl;
afisare(vf9);
cout<<nr_abs(vf9)<<" absente"<<endl;
cout<<nr_el(vf9)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 10"<<endl;
afisare(vf10);
cout<<nr_abs(vf10)<<" absente"<<endl;
cout<<nr_el(vf10)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 11"<<endl;
afisare(vf11);
cout<<nr_abs(vf11)<<" absente"<<endl;
cout<<nr_el(vf11)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 12"<<endl;
afisare(vf12);
cout<<nr_abs(vf12)<<" absente"<<endl;
cout<<nr_el(vf12)<<" elevi"<<endl;
cout<<endl;
}
duminică, 6 mai 2012
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/4
Sa se verifice daca 2 stive sunt identice. Stivele se creeaza introducand numere pana la 0, in fiecare.
#include<iostream>
using namespace std;
struct nod{
int info;
nod *urm;};
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
int main()
{nod *vf1=NULL,*p,*vf2=NULL;
int x,s=0,i=0;
cin>>x;
while(x)
{adaug(x,vf1);
cin>>x;}
cin>>x;
while(x)
{adaug(x,vf2);
cin>>x;}
while(vf1!=NULL){
if(vf1->info != vf2->info)
{cout<<"NU"; return 0;}
else
vf1=vf1->urm;
vf2=vf2->urm;}
cout<<"DA";
}
joi, 3 mai 2012
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/3
Se citesc numere din fişier într-o stivă. Să se afişeze stiva. Să se împartă stiva în două stive cu nr. egal de elemente. Dacă e nr. impar, să se şteargă elementul din mijloc.
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
void separare(nod* vf, nod* &vf1)
{nod*p=vf;
int nr=0;
while(p)
{nr++;
p=p->urm;}
if(nr%2==0)
{p=vf;
for(int i=1;i<nr/2;i++)
p=p->urm;
vf1=p->urm;
p->urm=NULL;}
else {p=vf;
for(int i=1;i<nr/2;i++)
p=p->urm;
vf1=p->urm->urm;
nod*q=p->urm;
delete q;
p->urm=NULL;}
}
int main()
{nod *q,*vf=NULL,*p,*vf1;
int x,s=0,i=0;
ifstream f("date");
while(f>>x)
adaug(x,vf);
//afisare(vf);
separare(vf,vf1);
afisare(vf);
cout<<endl;
afisare(vf1);
}
by DlMuresan
Categories:
memorie dinamica
|
Leave a comment
5/2 - Problema alocarea dinamica a memoriei
Doua variabile dinamice memoreaza DOB a doua persoane. Sa se afiseze persoana mai tanara.
#include<iostream>Se citesc nr de la tastatura pana la 0. Sa se creeze o stiva cu nr. Sa se afiseze stiva. Sa se afiseze suma el. Sa se afisee nr de el ale stivei. Sa se afiseze primele doua el din stiva. Sa se stearga stiva.
using namespace std;
struct DOB{
int a,l,z;
};
int main()
{DOB *p,*q;
p=new DOB;
q=new DOB;
cin>>(*p).a>>p->l>>p->z;
cin>>q->a>>q->l>>q->z;
if(p->a>q->a)
cout<<"a doua persoana";
else if(p->a<q->a)
cout<<"a doua persoana";
else if(p->l>q->l)
cout<<"a doua persoana";
else if(p->l<q->l)
cout<<"a doua persoana";
else if(p->z>q->z)
cout<<"a doua persoana";
else if(p->z<q->z)
cout<<"a doua persoana";
else cout<<"=";
delete p,q;
}
#include<iostream>
using namespace std;
struct nod{
int info;
nod *urm;};
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
int main()
{nod *vf=NULL,*p;
int x,s=0,i=0;
cin>>x;
while(x)
{adaug(x,vf);
cin>>x;}
afisare(vf);
p=vf;
while(vf!=NULL){
s+=vf->info;
vf=vf->urm;}
cout<<endl<<"Suma "<<s<<endl;
vf=p;
while(vf!=NULL){
i++;
vf=vf->urm;}
cout<<i<<" elemente in stiva"<<endl;
vf=p;
cout<<"Primele doua elemente din stiva ";
while(vf!=NULL){
i--;
if(i==1 || i==0)
cout<<vf->info<<" ";
vf=vf->urm;}
vf=p;
while(vf!=NULL)
{p=vf;
delete p;
vf=vf->urm;}
}
marți, 1 mai 2012
by DlMuresan
Categories:
memorie dinamica,
pointeri,
Structuri
|
Leave a comment