Advertisement
catalyn

afisare nr prime din lista

May 7th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. ifstream f("date.in");
  9. ofstream g("date.out");
  10.  
  11. struct nod
  12. {
  13. int info;
  14. nod *adr;
  15. };
  16.  
  17. nod *prim;
  18.  
  19. int nrprim(int x)
  20. {
  21. int i,ok=1;
  22. for (i=2;i<=sqrt(x);i++)
  23. if (x%i==0)
  24. ok=0;
  25.  
  26. if (ok==1)
  27. return 1;// e prim
  28. return 0;// nu e prim
  29. }
  30.  
  31. void creare()
  32. {
  33. int n,i;
  34. nod *p,*nou;
  35. while (f>>n)
  36. if (prim==NULL)
  37. {
  38. prim=new nod;
  39. prim->info=n;
  40. p=prim;
  41. }
  42. else
  43. {
  44. nou=new nod;
  45. nou->info=n;
  46. p->adr=nou;
  47. p=nou;
  48.  
  49.  
  50. }
  51. p->adr=NULL;
  52. }
  53.  
  54.  
  55. void afis()
  56. {
  57. nod *p=prim;
  58. while (p)
  59. {
  60. if((nrprim(p->info))==1)
  61. {g<<p->info<<" ";
  62. p=p->adr;}
  63. }
  64. }
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70. creare();
  71. afis();
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement