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);
}