Here is complete source. Fully functional until you enter a value I mentioned above.
Code:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
struct Tlistitem {
int i;
unsigned short index;
Tlistitem *next;
};
//-----
void prime_number_recursion(int& number, Tlistitem* p, int& count, char mod) {
bool prime_number = true;
if (('1' == mod) || ('3' == mod)) for (int i = 2; ('3' == mod) ? i <= (int)sqrt(number) : i < number; i++)
if ((double)number / i == double(number / i)) {prime_number = false; break;}
if (prime_number) if (cislo > p->i) {
p->next = new Tlistitem;
p->next->index = p->index + 1;
p = p->next;
p->i = number;
p->next = NULL;
}
if (number < count) prime_number_recursion(++number, p, count, mod);
}
void view_contents(Tlistitem* p, long elapsed_time) {
if (NULL != p) do {
p = p->next;
} while (NULL != p->next);
cout << "\nCELKOVY PROCESOROVY CAS: "<< elapsed_time << "\nPOCET NALEZENYCH PRVOCISEL: " << p->index + 1 << endl;
}
Tlistitem* initialization() {
Tlistitem *ptr = new Tlistitem;
ptr->i = 0;
ptr->index = 0;
ptr->next = NULL;
return ptr;
}
//------
int main() {
string command;
while (1) {
cout << "how many numbers to search for prime-numbers?:\n> ";
cin >> command;
if ((0 != atoi(command.c_str())) && (65535 >= atoi(command.c_str()))) break;
else cout << "input a integer number from <1, 65536)" << endl;
}
int count = atoi(command.c_str());
while (1) {
cout << "mode of searching:\n1 - no optimalization\n2 - prime-numbers optimalization\n3 - square-root optimalization\n4 - both optimalizations\n> ";
cin >> command;
if ((0 < atoi(command.c_str())) && (4 >= atoi(command.c_str()))) break;
else cout << "invalid input" << endl;
}
Tlistitem *beginning = initialization();
Tlistitem *p = beginning;
int number = 0;
cout << "working.......";
long start = clock();
prime_number_recursion(number, p, count, command[0]);
long elapsed_time = clock() - start;
vypsatobsah(beginning, elapsed_time);
return 0;
}
There is no code for freeing allocated memory yet, I know it. And only modes 1 and 3 work. It doesn't matter, important is just the memory question.