moj kod:
Spoiler: ukázať
Kód: Vybrať všetko
.bh12 3 a 7 19 14j
1 2 ...3 4 5ac 6 7
2 4 6 8
3 5 7 11
2 7 9
9, 225, 25
Kód: Vybrať všetko
.bh12 3 a 7 19 14j
1 2 ...3 4 5ac 6 7
2 4 6 8
3 5 7 11
2 7 9
9, 225, 25
Kód: Vybrať všetko
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a, sucet = 0, riadok = 1;
ifstream ifs;
ifs.open ("file.txt", std::ifstream::in);
while( ifs.good() )
{
ifs >> a;
if( a % 2 == 0 )
sucet += a;
// akykolvek dalsi podmieneny sucet
if( ifs.peek() == '\n' || !ifs.good() )
{
cout << "Sucet na " << riadok << ". riadku je " << sucet << endl;
riadok ++;
sucet = 0;
}
}
cout << "\nDone\n";
return 0;
}
Kód: Vybrať všetko
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
bool is_digit( int n )
{
return (n >= '0') && (n <= '9');
}
// preskocim vsetky neciselne znaky
void skip_nodigits( ifstream & ifs )
{
while( ! is_digit(ifs.peek()) )
ifs.get();
}
// nacitam cislo
int read_int( ifstream & ifs )
{
if( !ifs.good() )
exit(0);
int num = 0;
while( ifs.good() && is_digit(ifs.peek()) )
{
char c = ifs.get();
num = (num*10) + (c - '0');
}
return num;
}
bool is_prime( int num )
{
if( num <= 2 )
return false;
int sq = sqrt( num );
for( int i = 2; i <= sq; i++ )
if( num % i == 0 )
return false;
return true;
}
int main()
{
ifstream ifs ("file.txt");
int sumEven = 0, sumOdd = 0, sumPrime = 0;
if( ifs.is_open() == false )
cout << "chyba\n";
else
{
int num;
while( ifs.good() )
{
skip_nodigits( ifs );
num = read_int( ifs );
if( num % 2 == 0 )
sumEven += num;
else
sumOdd += num;
if( is_prime(num) )
sumPrime += num;
if( ifs.peek() == '\n' || !ifs.good() ) // novy riadok, alebo koniec suboru
{
cout << "sumEven =\t" << sumEven << "\n";
cout << "sumOdd =\t" << sumOdd << "\n";
cout << "sumPrime =\t" << sumPrime << "\n\n";
sumEven = sumOdd = sumPrime = 0;
}
}
}
ifs.close();
return 0;
}