/* * streams.cpp * * Created on: 03.10.2012 * Author: trifon */ #include #include #include #include using namespace std; void read_write_experiments() { int a[10] = { 65, 66, 67, 68, 4, 5, 6, 7, 8, 9 }; char s[5] = "Ivan"; char s2[6] = "Peter"; // cout.write((char*)a, 12); // cout.put('A').put('B').put('C'); // cout << "ABC"; /* s[10] = 'X'; cout << s << endl; cout << a << endl; cout.write(s,400); */ char c = 'A'; int x = 20; double y = 3.5; bool b = true; // cout << c << ' ' << x << ' ' << y << ' ' << b // << ' ' << s << ' ' << a << ' ' << cout; // cin >> s; //cout << s2 << endl; int a2[10]; cin.read((char*)a2, sizeof(int)); cout << a2[0]; } struct Point { double x,y; }; // операция за вход istream& operator>>(istream& is, Point& p) { is >> p.x >> p.y; return is; } // операция за изход ostream& operator<<(ostream& os, Point const& p) { os << "(" << p.x << "," << p.y << ")"; return os; } class Point3D : public Point { private: double z; // вход friend istream& operator>>(istream&, Point3D&); // изход friend ostream& operator<<(ostream&, Point3D const&); public: operator double() { return sqrt(x*x + y*y + z*z); } Point3D(double _x = 0) : z(_x) { x = y = 0; } }; istream& operator>>(istream& is, Point3D& p) { operator>>(is,(Point&)p) >> p.z; return is; } ostream& operator<<(ostream& os, Point3D const& p) { os << "[" << (Point&)p << "," << p.z << "]\n"; return os; } void redefine_operators() { cout << "Въведете точка: "; Point3D p; cin >> p; p.x *= 2; p.y = 0; cout << "Променената точка: " << p << endl; } void f(double x) { cout << "Числото е: " << x << endl; } void g(Point3D p) { cout << "Точката е " << p; } void stream_state() { Point3D p; // cin >> p; // f(p); // g(1.5); cin >> p; if (!cin) { cin.clear(); char tmp[100]; cin.getline(tmp, 100); cout << "Изчистих буфера: " << tmp << endl; cin >> p; } cout << "Good: " << cin.good() << endl; cout << "EOF: " << cin.eof() << endl; cout << "Fail: " << cin.fail() << endl; cout << "Bad: " << cin.bad() << endl; cout << "state: " << cin.rdstate() << endl; cout << "Точката е: " << p; } void formatting() { for(int i = 1; i < 10; i++) { // установява режим с извеждане на десетична точка // cout.setf(ios::fixed); // установява научен режим (мантиса и експонента) // cout.setf(ios::scientific); // установява точност (брой цифри, евентуално след десетичната точка) cout.precision(i); cout << "Precision = " << i << ": " << 10.27 << endl; } for(int i = 1; i < 10; i++) { // установява широчина на полето cout.width(i); // установява запълване на полето cout.fill('.'); // установява подравняване в полето cout.setf(ios::internal | ios::showbase); cout.setf(ios::left); cout.setf(ios::right); cout << hex << 12312 << endl;// << 123412 << endl; } } ostream& mymanipulator(ostream& os) { os.width(8); os.fill('?'); return os; } void manipulators() { cout << setw(5) << hex << setfill('?') << setiosflags(ios::internal | ios::showbase) << 123 << endl; cout << "Тест на mymanipulator" << endl; cout << 123 << endl << mymanipulator << 123 << endl; // cout << binary(123); <-- по-сложно! } void files() { ifstream fi("test.txt"); // ifstream fi2; // fi2.open("test.txt"); char line[100]; fi.get(line, 100); cout << line << endl; char c; fi.get(c); cout << (int)c << endl; } int main() { // read_write_experiments(); // redefine_operators(); // stream_state(); // formatting(); // manipulators(); files(); return 0; }