Last active
August 29, 2015 14:11
-
-
Save ad-m/0c369ca32ab2bdc87a95 to your computer and use it in GitHub Desktop.
Zadanie programowanie - laboratorium 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zadanie 1 | |
Użytkownik wprowadza n liczb całkowitych a program zapisuje je w odwrotnej kolejności do pliku | |
jeżeli plik istnieje to dopisuje liczby na końcu. */ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
int odwroc(int liczba){ | |
int r=0; | |
while(liczba%10){ | |
r = r*10+liczba%10; | |
liczba = liczba/10; | |
} | |
return r; | |
} | |
int main(void){ | |
ofstream in("output.txt", ios::ate); | |
int n; | |
cout << "Podaj n:"; | |
cin >> n; | |
int liczba; | |
for(int i=0; i<n; i++){ | |
cout << "Podaj liczbe numer " << i <<":"; | |
cin >> liczba; | |
cout << "Zapisano: " << odwroc(liczba) << endl; | |
in << odwroc(liczba) << endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zadanie 2 | |
Program wczytujący liczby z pliku z zadania 1 */ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
int odwroc(int liczba){ | |
int r=0; | |
while(liczba%10){ | |
r = r*10+liczba%10; | |
liczba = liczba/10; | |
} | |
return r; | |
} | |
int main(void){ | |
fstream in("output.txt",ios::in | ios::out); | |
int liczba, i=0; | |
while(!in.eof()){ | |
cout << "Wczytano liczbe numer " << i <<":"; | |
in >> liczba; | |
cout << odwroc(liczba) << endl; | |
i++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zadanie 3 | |
Napisz program, który wczyta z klawiatury tekst i zapisze go do pliku, następnie sprawdzi bezpośrednio | |
operując na pliku czy wprowadzony tekst jest palindromem. Nie używamy tablic. */ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
/* | |
bool palindrom(string tekst){ | |
for(int i=0; i<tekst.length()/2; i++){ | |
if(tekst[i] != tekst[tekst.length()/2-i+1]){ | |
return false; | |
} | |
} | |
return true; | |
} | |
*/ | |
int filesize(char* filename){ | |
std::ifstream in(filename, ios::ate); | |
return (int)in.tellg(); | |
} | |
char daj_znak(fstream& plik, int poz){ | |
char r; | |
plik.seekg(poz); | |
plik.get(r); | |
return r; | |
} | |
int main(){ | |
string tekst; | |
cout << "Podaj tekst: "; | |
cin >> tekst; | |
ofstream out("output.txt"); | |
out << tekst; | |
out.close(); | |
fstream in("output.txt", ios::in); | |
char a; | |
char b; | |
int n = filesize("output.txt"); | |
for(int i=0; i<n/2; i++){ | |
cout << daj_znak(in, i) << ":" << daj_znak(in, n/2-i+1) << endl; | |
if(daj_znak(in, i) != daj_znak(in, tekst.length()/2-i+1)){ | |
cout << "Nie palindrom"; | |
return false; | |
} | |
} | |
cout << "Palindrom"; | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zadanie 4 | |
Napisz program, który wczyta do pliku n-liczb, następnie pobierze z pliku, co drugą liczbę i obliczy sumę | |
jej cyfr i wpisze tą sumę do drugiego pliku w postaci np. 147 => 12 (każdy wpis w nowej linii). Nie | |
używamy tablic. */ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
int suma_cyfr(int liczba){ | |
int r = 0; | |
do{ | |
r+=liczba%10; | |
liczba/=10; | |
}while(liczba>10); | |
return r; | |
} | |
int main(){ | |
int n; | |
/* Wczytanie do pliku START */ | |
cout << "Podaj n:"; | |
cin >> n; | |
ofstream out("output.txt"); | |
int tekst; | |
for(int i=0; i<n; i++){ | |
cout << "Podaj liczbe: "; | |
cin >> tekst; | |
out << tekst << endl; | |
} | |
out.close(); | |
/* Wczytanie do pliku START */ | |
/* Nowy plik START */ | |
// */ | |
int right, fake, i=0; | |
ifstream plik("output.txt"); | |
ofstream nowy("nowy.txt", ios::trunc); | |
while(!plik.eof()){ | |
if(i%2){ | |
plik >> right; | |
nowy << right << "=>" << suma_cyfr(right) << endl; | |
cout << "Wpisano liczbe " << right << " o sumie " << suma_cyfr(right) << endl; | |
}else{ | |
plik >> fake; | |
cout << "Pominieto liczbe: " << fake << endl; | |
} | |
i++; | |
} | |
/* Nowy plik KONIEC */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zadanie 5 | |
Napisz program, który zapisze do pliku tekst wpisany z klawiatury. Następnie bezpośrednio operując na | |
pliku policzy ile jest w tekście liczb 2, 3 i 4 cyfrowych i wypisze ich ilość na ekran. Nie używamy tablic. */ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
int suma_cyfr(int liczba){ | |
int r = 0; | |
do{ | |
r+=liczba%10; | |
liczba/=10; | |
}while(liczba>10); | |
return r; | |
} | |
int main(){ | |
int n; | |
/* Wczytanie do pliku START */ | |
cout << "Podaj n:"; | |
cin >> n; | |
ofstream out("output.txt"); | |
int tekst; | |
for(int i=0; i<n; i++){ | |
cout << "Podaj liczbe: "; | |
cin >> tekst; | |
out << tekst << endl; | |
} | |
out.close(); | |
/* Wczytanie do pliku START */ | |
/* Nowy plik START */ | |
// */ | |
int dane, i=0; | |
ifstream plik("output.txt"); | |
ofstream nowy("nowy.txt", ios::trunc); | |
int trzycyfrowe=0, dwucyfrowe=0, jednocyfrowe=0; | |
while(!plik.eof()){ | |
plik >> dane; | |
if(999 > dane && dane > 99){ | |
trzycyfrowe++; | |
}else if(99 > dane && dane > 9){ | |
dwucyfrowe++; | |
}else if(9 > dane){ | |
jednocyfrowe++; | |
} | |
i++; | |
} | |
cout << "Trzyfrowe: " << trzycyfrowe << endl; | |
cout << "Dwucyfrowe: " << dwucyfrowe << endl; | |
cout << "Jednocyfrowe: " << jednocyfrowe << endl; | |
/* Nowy plik KONIEC */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment