AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Prime number checking from file

Go down

Prime number checking from file

Post by Lucaci Andrei on Thu Jun 14, 2012 5:27 am

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* main.cpp
* main.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: Jun 14, 2012
* Author: sin
*/

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

vector<int> read(vector<int> list, string filename){
ifstream in (filename.c_str(), ios::in);
string line, to_;
int a;
stringstream to_int;
while(getline(in, line)){
istringstream token(line);
getline(token, to_, ',');
istringstream int_id(to_);
int_id>>a;
list.push_back(a);
}
return (list);
}

bool isprime(int s){
for (int i=2;i<s/2;i++)
if (s%i==0) return (false);
return (true);
}

int main(){
vector<int> list;
string filename;
cout<<"Filename: ";
cin>>filename;
list=read(list, filename);
cout<<"The prime numbers: ";
for (int i=0;i<(int)list.size();i++){
if (list[i]==1 or list[i]==2) cout<<list[i]<<" ";
if (isprime(list[i])){
cout<<list[i]<<" ";
}
}
return (0);
}

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum