C++ Bc. 18 cpp
#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;
}
[ Zpět ]