Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <fstream.h>
  4. #include <malloc.h>
  5.  
  6.  
  7. using namespace std;
  8. fstream plik;
  9.  
  10.  
  11.  
  12. struct ts_lista{
  13. int key;
  14. struct ts_lista *next;
  15. }ts_lista;
  16.  
  17. struct ts_lista *head=NULL, *x=NULL, *oldhead=NULL, *temp = NULL;
  18.  
  19. void zapisz(struct ts_lista *head)
  20. {
  21. if(head)
  22. {
  23. plik << head->key << endl;
  24. zapisz(head -> next);
  25. }
  26. }
  27.  
  28. struct ts_lista *dodaj(struct ts_lista *head, int liczba)
  29. {
  30. struct ts_lista *elem = NULL;
  31.  
  32. while(head -> next != NULL)head = head -> next;
  33.  
  34. elem = (struct ts_lista *)malloc(sizeof(struct ts_lista));
  35. elem -> key = liczba;
  36. elem -> next = NULL;
  37. head -> next = elem;
  38. head = elem;
  39.  
  40. return head;
  41. }
  42.  
  43. int czypierwsza(struct ts_lista *head, int liczba)
  44. {
  45. struct ts_lista *x;
  46. x = head;
  47.  
  48. while (x != NULL)
  49. {
  50. if ((liczba % x -> key)==0) return 0;
  51. x = x -> next;
  52. }
  53.  
  54. return 1;
  55. }
  56.  
  57.  
  58. main()
  59. {
  60. struct ts_lista *x, *oldhead, *temp;
  61.  
  62. int n;
  63. int a = 1;
  64. int dzielnik;
  65.  
  66.  
  67. plik.open( "SE.txt", std::ios::out );
  68. //Nie sprawdzam czy plik istnieje czy nie, gdyz kazde uruchomienie tego programu i tak nadpisuje dane, wiec jesli plik nie istnieje, zostanie utworzony.
  69.  
  70. cout << "Podaj dlugosc przedzialu : ";
  71. cin >> n;
  72.  
  73. head = (struct ts_lista *)malloc(sizeof(struct ts_lista));
  74. head -> key = 2;
  75. head -> next = NULL;
  76.  
  77. oldhead = head;
  78.  
  79. for (int i = 3; i != n; i++)
  80. {
  81. if (czypierwsza(head, i)) dodaj(head, i);
  82. }
  83.  
  84. head = oldhead;
  85.  
  86. zapisz(head);
  87.  
  88. plik.close();
  89. cout << endl;
  90. cout << "Pomyslnie wygenerowano liczby pierwsze z zakresu " << "< 2 ; " << n << " >" << endl;
  91. cout << "Liczby znajduja sie w pliku SE.txt znajdujacym sie w folderze z tym programem." << endl;
  92. cout << "Wcisnij dowolny klawisz by zakonczyc." << endl;
  93.  
  94. fflush(stdin);
  95.  
  96. getchar();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement