Liste

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
BunnyFeet
Junior
Junior
Posts: 2
Joined: 03 Jun 2014, 23:17
Judet: Bucureşti

Liste

Post by BunnyFeet » 03 Jun 2014, 23:20

Intr-o lista liniara simplu inlantuita , adaugati dupa fiecare numar perfect media aritmetica a vecinilor.
Am tot incercat sa o rezolv si ma blochez , atunci cand trebuie sa fac media aritmetica a vecinilor.
Daca puteti sa ma ajutati cu aceasta problema v-as ramane recunoscator.

Code: Select all

#include <iostream>
#include <fstream>

using namespace std;
struct nod{ int info;
            nod *urm;};
nod *prim, *ultim;
void adauga(nod *&prim, nod *&ultim, int x)
{
    if(prim==0)
    {
        prim= new nod;
        prim->info=x;
        prim->urm=NULL;
        ultim=prim;
    }
    else
    {
        nod *p;
        p=new nod;
        p->info=x;
        p->urm=NULL;
        ultim->urm=p;
        ultim=p;
    }
}
int perfect ( int n)
{
    int i,s=0;
    for(i=1;i<=n/2;i++)
        if(n%i==0)
        s=s+i;
    if(s==n)
        return 1;
    else
        return 0;
}
void listare (nod *prim)
{
    nod *p;
    p=prim;
    while(p)
    {
        cout<<p->info<<" ";
        p=p->urm;
    }
}
void inserare_dupa(nod *&q, int x)
{
    nod *p;
    p=new nod;
    p->info=x;
    p->urm=q->urm;
    q->urm=p;
    q=p;
}
void vecini (nod *prim)
{
    nod *p;
    p=prim;
    while(p)
    {
        if(perfect(p->urm->info)==1)
            inserare_dupa(p,p->info+p->urm->urm->info); // nu inteleg de ce nu merge , cand pun conditia astfel .
            else
        p=p->urm;
    }
}

int main()
{
    fstream f("date.in",ios::in);
    int nr;
    while(f>>nr)
        adauga(prim,ultim,nr);
    listare(prim);
    cout<<endl;
    vecini(prim);
    listare(prim);
    return 0;
}



User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: Liste

Post by Marius Bancila » 04 Jun 2014, 00:24

Ce nu merge mai exact? Ai facut debug? Te-ai asigurat ca p->urm->urm exista?
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

BunnyFeet
Junior
Junior
Posts: 2
Joined: 03 Jun 2014, 23:17
Judet: Bucureşti

Re: Liste

Post by BunnyFeet » 04 Jun 2014, 19:47

nu reusesc sa gasesc modalitatea prin care sa inserez media aritmetica dupa fiecare numar perfect.

Post Reply