Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream.h>
- #include <stdlib.h>
- #include <fstream.h>
- #include <malloc.h>
- using namespace std;
- fstream plik;
- struct ts_lista{
- int key;
- struct ts_lista *next;
- }ts_lista;
- struct ts_lista *head=NULL, *x=NULL, *oldhead=NULL, *temp = NULL;
- void zapisz(struct ts_lista *head)
- {
- if(head)
- {
- plik << head->key << endl;
- zapisz(head -> next);
- }
- }
- struct ts_lista *dodaj(struct ts_lista *head, int liczba)
- {
- struct ts_lista *elem = NULL;
- while(head -> next != NULL)head = head -> next;
- elem = (struct ts_lista *)malloc(sizeof(struct ts_lista));
- elem -> key = liczba;
- elem -> next = NULL;
- head -> next = elem;
- head = elem;
- return head;
- }
- int czypierwsza(struct ts_lista *head, int liczba)
- {
- struct ts_lista *x;
- x = head;
- while (x != NULL)
- {
- if ((liczba % x -> key)==0) return 0;
- x = x -> next;
- }
- return 1;
- }
- main()
- {
- struct ts_lista *x, *oldhead, *temp;
- int n;
- int a = 1;
- int dzielnik;
- plik.open( "SE.txt", std::ios::out );
- //Nie sprawdzam czy plik istnieje czy nie, gdyz kazde uruchomienie tego programu i tak nadpisuje dane, wiec jesli plik nie istnieje, zostanie utworzony.
- cout << "Podaj dlugosc przedzialu : ";
- cin >> n;
- head = (struct ts_lista *)malloc(sizeof(struct ts_lista));
- head -> key = 2;
- head -> next = NULL;
- oldhead = head;
- for (int i = 3; i != n; i++)
- {
- if (czypierwsza(head, i)) dodaj(head, i);
- }
- head = oldhead;
- zapisz(head);
- plik.close();
- cout << endl;
- cout << "Pomyslnie wygenerowano liczby pierwsze z zakresu " << "< 2 ; " << n << " >" << endl;
- cout << "Liczby znajduja sie w pliku SE.txt znajdujacym sie w folderze z tym programem." << endl;
- cout << "Wcisnij dowolny klawisz by zakonczyc." << endl;
- fflush(stdin);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement