C++ Bc. 18 cpp: Porovnání verzí
Bez shrnutí editace |
Bez shrnutí editace |
||
Řádek 1: | Řádek 1: | ||
#include <iostream> | |||
#include <sstream> | |||
std::istream& komentar(std::istream& inp); | |||
int main() | |||
{ | |||
using namespace std; | |||
istringstream | |||
data("/* vstup obsahuje souradnice x a y */ " | |||
"/* x */ 123.54 /* y */ 345.53 " | |||
" 738.34 542.43 433.22 543.98 " | |||
" 832.64 343.25 534.17 541.23 " | |||
" 892,83 336.34 /* konec dat */ "); | |||
double x, y; | |||
double sx = 0, sy = 0; | |||
int pocet = 0; | |||
// funkce komentar je definovana jako manipulator bez parametru | |||
while (data >> komentar >> x >> komentar >> y) | |||
{ | |||
cout << x << " " << y << endl; | |||
sx += x; | |||
sy += y; | |||
pocet++; | |||
} | |||
if (pocet) | |||
cout << "\nteziste: " << sx/pocet << " " << sy/pocet << endl; | |||
else | |||
cout << "na vstupu nejsou zadne souradnice\n"; | |||
} | |||
std::istream& komentar(std::istream& inp) | |||
{ | |||
char z; | |||
while (inp >> z) | |||
{ | |||
// komentar zacina dvoji znaku /* | |||
if (z != '/' || inp.peek() != '*') | |||
{ | |||
inp.putback(z); | |||
return inp; | |||
} | |||
inp.get(); // nactu znak '*' | |||
// komentar konci dvojici znaku */ | |||
while (inp >> z) | |||
if (z == '*' && inp.peek() == '/') | |||
{ | |||
inp.get(); // nactu znak '/' | |||
break; // hledam dalsi komentar | |||
} | |||
} | |||
return inp; | |||
} | |||
123.54 345.53 | |||
738.34 542.43 | |||
433.22 543.98 | |||
832.64 343.25 | |||
534.17 541.23 | |||
teziste: 532.382 463.284 | |||
[ [[C plus plus Bc. 18 | Zpět]] ] | [ [[C plus plus Bc. 18 | Zpět]] ] |
Verze z 12. 3. 2006, 16:02
#include <iostream> #include <sstream> std::istream& komentar(std::istream& inp); int main() { using namespace std; istringstream data("/* vstup obsahuje souradnice x a y */ " "/* x */ 123.54 /* y */ 345.53 " " 738.34 542.43 433.22 543.98 " " 832.64 343.25 534.17 541.23 " " 892,83 336.34 /* konec dat */ "); double x, y; double sx = 0, sy = 0; int pocet = 0; // funkce komentar je definovana jako manipulator bez parametru while (data >> komentar >> x >> komentar >> y) { cout << x << " " << y << endl; sx += x; sy += y; pocet++; } if (pocet) cout << "\nteziste: " << sx/pocet << " " << sy/pocet << endl; else cout << "na vstupu nejsou zadne souradnice\n"; } std::istream& komentar(std::istream& inp) { char z; while (inp >> z) { // komentar zacina dvoji znaku /* if (z != '/' || inp.peek() != '*') { inp.putback(z); return inp; } inp.get(); // nactu znak '*' // komentar konci dvojici znaku */ while (inp >> z) if (z == '*' && inp.peek() == '/') { inp.get(); // nactu znak '/' break; // hledam dalsi komentar } } return inp; }
123.54 345.53 738.34 542.43 433.22 543.98 832.64 343.25 534.17 541.23 teziste: 532.382 463.284
[ Zpět ]