Program: losujący liczby do pliku liczby.txt, a następnie wyszukujący te których suma cyfr wynosi 20. Liczby te zostają zapisane w osobnym pliku liczbySuma20.txt.
Wykorzystane dane wejściowe:
611 98 671 680 697 42 446 945 68 273 967 638 622 901 622 105 157 577 121 624 228 994
610 687 408 130 819 976 452 941 66 104 935 994 781 411 499 596…
…209 314 235 824 357 970 932 85 199 530 402 914 300 506 419 443 397 528 32 38
67 135 495 388 948 830 132 948 823 147 315 285 508 311 32 986 231 356
Dane wyjściowe:
596
Kompilator: Dev C++
Kod programu:
//Zapisanie liczb z sumą cyfr 20
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
using namespace std;
int wybor(char tab[]);
int main(int argc, char *argv[])
{
srand(time(NULL));
fstream obiekt1;
fstream obiekt2;
obiekt1.open("liczby.txt",ios::out);
obiekt2.open("liczbySuma20.txt",ios::out);
int i;
int los;
char cTab[100];
int iSuma;
string sTEXT;
int a,b,c;
string t1,t2,t3;
for (i=0;i<99;i++)
cTab[i] = 0;
if (obiekt1.is_open()){
for (i=0;i<100;i++){
los = rand()%(999+1);
obiekt1 << los <<"\t";
}
obiekt1.close();
obiekt1.open("liczby.txt",ios::in);
while (!obiekt1.eof()) {
obiekt1.getline(cTab,255,0x09);
if (cTab[0] != NULL){
sTEXT = cTab;
t1 = cTab[0];
t2 = cTab[1];
t3 = cTab[2];
a = atoi(t1.c_str());
b = atoi(t2.c_str());
c = atoi(t3.c_str());
iSuma = a+b+c;
if (iSuma ==20)
obiekt2 << sTEXT << "\t";
wybor(cTab);}}}
else
cout<<"Nie uzyskano dostepu do pliku!\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
int wybor(char tab[]){
string tekst = tab;
string a1,a2,a3;
a1 = tab[0];
a2 = tab[1];
a3 = tab[2];
int a,b,c;
a = atoi(a1.c_str());
b = atoi(a2.c_str());
c = atoi(a3.c_str());
int suma = a+b+c;
return suma;
}