| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // io.c --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Peter A. Buhr
|
|---|
| 10 | // Created On : Wed Mar 2 16:56:02 2016
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Wed Aug 23 21:27:50 2017
|
|---|
| 13 | // Update Count : 79
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <fstream>
|
|---|
| 17 |
|
|---|
| 18 | int main() {
|
|---|
| 19 | char c; // basic types
|
|---|
| 20 | short int si;
|
|---|
| 21 | unsigned short int usi;
|
|---|
| 22 | int i;
|
|---|
| 23 | unsigned int ui;
|
|---|
| 24 | long int li;
|
|---|
| 25 | unsigned long int uli;
|
|---|
| 26 | long long int lli;
|
|---|
| 27 | unsigned long long int ulli;
|
|---|
| 28 | float f;
|
|---|
| 29 | double d;
|
|---|
| 30 | long double ld;
|
|---|
| 31 | float _Complex fc;
|
|---|
| 32 | double _Complex dc;
|
|---|
| 33 | long double _Complex ldc;
|
|---|
| 34 | enum { size = 10 };
|
|---|
| 35 | char s1[size], s2[size];
|
|---|
| 36 |
|
|---|
| 37 | int x = 3, y = 5, z = 7;
|
|---|
| 38 | sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
|
|---|
| 39 | sout | 1 | 2 | 3 | endl;
|
|---|
| 40 | sout | '1' | '2' | '3' | endl;
|
|---|
| 41 | sout | 1 | "" | 2 | "" | 3 | endl;
|
|---|
| 42 | sout | endl;
|
|---|
| 43 |
|
|---|
| 44 | sout | "opening delimiters" | endl;
|
|---|
| 45 | sout
|
|---|
| 46 | | "x (" | 1
|
|---|
| 47 | | "x [" | 2
|
|---|
| 48 | | "x {" | 3
|
|---|
| 49 | | "x =" | 4
|
|---|
| 50 | | "x $" | 5
|
|---|
| 51 | | "x £" | 6
|
|---|
| 52 | | "x ¥" | 7
|
|---|
| 53 | | "x ¡" | 8
|
|---|
| 54 | | "x ¿" | 9
|
|---|
| 55 | | "x «" | 10
|
|---|
| 56 | | endl | endl;
|
|---|
| 57 |
|
|---|
| 58 | sout | "closing delimiters" | endl;
|
|---|
| 59 | sout
|
|---|
| 60 | | 1 | ", x"
|
|---|
| 61 | | 2 | ". x"
|
|---|
| 62 | | 3 | "; x"
|
|---|
| 63 | | 4 | "! x"
|
|---|
| 64 | | 5 | "? x"
|
|---|
| 65 | | 6 | "% x"
|
|---|
| 66 | | 7 | "¢ x"
|
|---|
| 67 | | 8 | "» x"
|
|---|
| 68 | | 9 | ") x"
|
|---|
| 69 | | 10 | "] x"
|
|---|
| 70 | | 11 | "} x"
|
|---|
| 71 | | endl | endl;
|
|---|
| 72 |
|
|---|
| 73 | sout | "opening/closing delimiters" | endl;
|
|---|
| 74 | sout
|
|---|
| 75 | | "x`" | 1 | "`x'" | 2
|
|---|
| 76 | | "'x\"" | 3 | "\"x:" | 4
|
|---|
| 77 | | ":x " | 5 | " x\t" | 6
|
|---|
| 78 | | "\tx\f" | 7 | "\fx\v" | 8
|
|---|
| 79 | | "\vx\n" | 9 | "\nx\r" | 10
|
|---|
| 80 | | "\rx"
|
|---|
| 81 | | endl | endl;
|
|---|
| 82 |
|
|---|
| 83 | sout | "override opening/closing delimiters" | endl;
|
|---|
| 84 | sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4 | endl;
|
|---|
| 85 | sout | endl;
|
|---|
| 86 |
|
|---|
| 87 | ifstream in; // create / open file
|
|---|
| 88 | open( &in, "io.data", "r" );
|
|---|
| 89 |
|
|---|
| 90 | sout | "input bacis types" | endl;
|
|---|
| 91 | &in | c // character
|
|---|
| 92 | | si | usi | i | ui | li | uli | lli | ulli // integral
|
|---|
| 93 | | f | d | ld // floating point
|
|---|
| 94 | | fc | dc | ldc // floating-point complex
|
|---|
| 95 | | cstr( s1 ) | cstr( s2, size ); // C string, length unchecked and checked
|
|---|
| 96 | sout | endl;
|
|---|
| 97 |
|
|---|
| 98 | sout | "output basic types" | endl;
|
|---|
| 99 | sout | c | ' ' | endl // character
|
|---|
| 100 | | si | usi | i | ui | li | uli | lli | ulli | endl // integral
|
|---|
| 101 | | f | d | ld | endl // floating point
|
|---|
| 102 | | fc | dc | ldc | endl; // complex
|
|---|
| 103 | sout | endl;
|
|---|
| 104 |
|
|---|
| 105 | sout | "tuples" | endl;
|
|---|
| 106 | [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
|
|---|
| 107 | sout | t1 | t2 | endl; // print tuple
|
|---|
| 108 | sout | endl;
|
|---|
| 109 |
|
|---|
| 110 | sout | "toggle separator" | endl;
|
|---|
| 111 | sout | f | "" | d | "" | ld | endl // floating point without separator
|
|---|
| 112 | | sepDisable | fc | dc | ldc | endl // complex without separator
|
|---|
| 113 | | fc | sepOn | dc | ldc | endl // local separator add
|
|---|
| 114 | | sepEnable | fc | dc | ldc | endl // complex with separator
|
|---|
| 115 | | fc | sepOff | dc | ldc | endl // local separator removal
|
|---|
| 116 | | s1 | sepOff | s2 | endl // local separator removal
|
|---|
| 117 | | s1 | "" | s2 | endl; // local separator removal
|
|---|
| 118 | sout | endl;
|
|---|
| 119 |
|
|---|
| 120 | sout | "change separator" | endl;
|
|---|
| 121 | sout | "from \"" | sep | "\"";
|
|---|
| 122 | sepSet( sout, ", $" ); // change separator, maximum of 15 characters
|
|---|
| 123 | sout | " to \"" | sep | "\"" | endl;
|
|---|
| 124 | sout | f | d | ld | endl
|
|---|
| 125 | | fc | dc | ldc | endl
|
|---|
| 126 | | s1 | s2 | endl
|
|---|
| 127 | | t1 | t2 | endl; // print tuple
|
|---|
| 128 | sout | endl;
|
|---|
| 129 | sout | "from \"" | sep | "\" ";
|
|---|
| 130 | sepSet( sout, " " ); // restore separator
|
|---|
| 131 | sout | "to \"" | sep | "\"" | endl;
|
|---|
| 132 | sout | f | d | ld | endl
|
|---|
| 133 | | fc | dc | ldc | endl
|
|---|
| 134 | | s1 | s2 | endl
|
|---|
| 135 | | t1 | t2 | endl; // print tuple
|
|---|
| 136 | sout | endl;
|
|---|
| 137 |
|
|---|
| 138 | sout | "check sepOn/sepOff" | endl;
|
|---|
| 139 | sout | sepOn | 1 | 2 | 3 | sepOn | endl; // no separator at start/end of line
|
|---|
| 140 | sout | 1 | sepOff | 2 | 3 | endl; // locally turn off implicit separator
|
|---|
| 141 | sout | sepOn | sepOn | 1 | 2 | 3 | sepOn | sepOff | sepOn | '\n'; // no separator at start/end of line
|
|---|
| 142 | sout | 1 | 2 | 3 | "\n\n" | sepOn; // no separator at start of next line
|
|---|
| 143 | sout | 1 | 2 | 3 | endl;
|
|---|
| 144 | sout | endl;
|
|---|
| 145 |
|
|---|
| 146 | sout | "check enable/disable" | endl;
|
|---|
| 147 | sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separation
|
|---|
| 148 | sout | 1 | sepOn | 2 | 3 | endl; // locally turn on implicit separator
|
|---|
| 149 | sout | sepEnable | 1 | 2 | 3 | endl | sepDisable; // globally turn on/off implicit separation
|
|---|
| 150 | sout | 1 | 2 | 3 | endl | sepEnable; // globally turn on implicit separation
|
|---|
| 151 | sout | 1 | 2 | 3 | sepOn | sepDisable | endl; // ignore seperate at end of line
|
|---|
| 152 | sout | 1 | 2 | 3 | sepOn | sepEnable | endl; // separator at end of line
|
|---|
| 153 | sout | 1 | 2 | 3 | endl;
|
|---|
| 154 | sout | endl;
|
|---|
| 155 |
|
|---|
| 156 | // sout | fmt( d, "%8.3f" ) || endl;
|
|---|
| 157 | // sout | endl;
|
|---|
| 158 |
|
|---|
| 159 | sepSetTuple( sout, " " ); // set tuple separator from ", " to " "
|
|---|
| 160 | sout | t1 | t2 | " \"" | sep | "\"" | endl;
|
|---|
| 161 | sepSetTuple( sout, ", " ); // reset tuple separator to ", "
|
|---|
| 162 | sout | t1 | t2 | " \"" | sep | "\"" | endl;
|
|---|
| 163 | sout | t1 | t2 | endl; // print tuple
|
|---|
| 164 | sout | endl;
|
|---|
| 165 |
|
|---|
| 166 | [int, int, const char *, double] t3 = { 3, 4, "a", 7.2 };
|
|---|
| 167 | sout | [ 3, 4, "a", 7.2 ] | endl;
|
|---|
| 168 | sout | t3 | endl;
|
|---|
| 169 | sepSetTuple( sout, " " );
|
|---|
| 170 | sout | t3 | endl;
|
|---|
| 171 | sout | sepOn | t3 | sepDisable | t3 | sepEnable | t3 | endl;
|
|---|
| 172 | sepSet( sout, "^" );
|
|---|
| 173 | sepSetTuple( sout, "-" );
|
|---|
| 174 | sout | t3 | 3 | 4 | t3 | endl;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | // Local Variables: //
|
|---|
| 178 | // tab-width: 4 //
|
|---|
| 179 | // compile-command: "cfa io.c" //
|
|---|
| 180 | // End: //
|
|---|