[73abe95] | 1 | // |
---|
[658f6de0] | 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. |
---|
[73abe95] | 6 | // |
---|
[dc8511c] | 7 | // ato.cfa -- |
---|
[73abe95] | 8 | // |
---|
[658f6de0] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Feb 4 08:10:57 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[33807a1e] | 12 | // Last Modified On : Fri Mar 15 17:58:35 2024 |
---|
| 13 | // Update Count : 145 |
---|
[73abe95] | 14 | // |
---|
[658f6de0] | 15 | |
---|
[73abe95] | 16 | #include <fstream.hfa> |
---|
| 17 | #include <stdlib.hfa> // ato, strto |
---|
[658f6de0] | 18 | |
---|
| 19 | int main( void ) { |
---|
[33807a1e] | 20 | // ato |
---|
| 21 | |
---|
[e672372] | 22 | const char * sptr = "-123"; |
---|
| 23 | int i = ato( sptr ); |
---|
[200fcb3] | 24 | sout | i | sptr; |
---|
[e672372] | 25 | sptr = "123"; |
---|
| 26 | unsigned int ui = ato( sptr ); |
---|
[200fcb3] | 27 | sout | ui | sptr; |
---|
[e672372] | 28 | |
---|
| 29 | sptr = "-123"; |
---|
| 30 | long int li = ato( sptr ); |
---|
[200fcb3] | 31 | sout | li | sptr; |
---|
[e672372] | 32 | sptr = "123"; |
---|
| 33 | unsigned long int uli = ato( sptr ); |
---|
[200fcb3] | 34 | sout | uli | sptr; |
---|
[e672372] | 35 | |
---|
[33807a1e] | 36 | sptr = " -123 "; // spaces allowed |
---|
[e672372] | 37 | long long int lli = ato( sptr ); |
---|
[200fcb3] | 38 | sout | lli | sptr; |
---|
[33807a1e] | 39 | sptr = " 123 "; // spaces allowed |
---|
[e672372] | 40 | unsigned long long int ulli = ato( sptr ); |
---|
[200fcb3] | 41 | sout | ulli | sptr; |
---|
[e672372] | 42 | |
---|
| 43 | sptr = "-123.456"; |
---|
| 44 | float f = ato( sptr ); |
---|
[200fcb3] | 45 | sout | f | sptr; |
---|
[e672372] | 46 | sptr = "-123.4567890123456"; |
---|
| 47 | double d = ato( sptr ); |
---|
[200fcb3] | 48 | sout | d | sptr; |
---|
[33807a1e] | 49 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
[e672372] | 50 | long double ld = ato( sptr ); |
---|
[200fcb3] | 51 | sout | ld | sptr; |
---|
[e672372] | 52 | |
---|
| 53 | sptr = "-123.456-123.456i"; |
---|
| 54 | float _Complex fc = ato( sptr ); |
---|
[200fcb3] | 55 | sout | fc | sptr; |
---|
[e672372] | 56 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
| 57 | double _Complex dc = ato( sptr ); |
---|
[200fcb3] | 58 | sout | dc | sptr; |
---|
[e672372] | 59 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
| 60 | long double _Complex ldc = ato( sptr ); |
---|
[200fcb3] | 61 | sout | ldc | sptr; |
---|
[33807a1e] | 62 | sptr = " 123.45678901234 -123.4567890i "; // spaces allowed |
---|
[e672372] | 63 | long double _Complex ldc2 = ato( sptr ); |
---|
[200fcb3] | 64 | sout | ldc2 | sptr; |
---|
[e672372] | 65 | |
---|
[33807a1e] | 66 | // strto |
---|
[e672372] | 67 | |
---|
| 68 | sptr = "-123"; |
---|
[33807a1e] | 69 | i = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 70 | sout | i | sptr; |
---|
[e672372] | 71 | sptr = "123"; |
---|
[33807a1e] | 72 | ui = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 73 | sout | ui | sptr; |
---|
[e672372] | 74 | |
---|
| 75 | sptr = "-123"; |
---|
[33807a1e] | 76 | li = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 77 | sout | li | sptr; |
---|
[e672372] | 78 | sptr = "123"; |
---|
[33807a1e] | 79 | uli = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 80 | sout | uli | sptr; |
---|
[e672372] | 81 | |
---|
[33807a1e] | 82 | sptr = " -123 "; // spaces allowed |
---|
| 83 | lli = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 84 | sout | lli | sptr; |
---|
[33807a1e] | 85 | sptr = " 123 "; // spaces allowed |
---|
| 86 | ulli = strto( sptr, 0p, 10 ); |
---|
[200fcb3] | 87 | sout | ulli | sptr; |
---|
[e672372] | 88 | |
---|
| 89 | sptr = "-123.456"; |
---|
[33807a1e] | 90 | f = strto( sptr, 0p ); |
---|
[200fcb3] | 91 | sout | f | sptr; |
---|
[e672372] | 92 | sptr = "-123.4567890123456"; |
---|
[33807a1e] | 93 | d = strto( sptr, 0p ); |
---|
[200fcb3] | 94 | sout | d | sptr; |
---|
[33807a1e] | 95 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
| 96 | ld = strto( sptr, 0p ); |
---|
[200fcb3] | 97 | sout | ld | sptr; |
---|
[e672372] | 98 | |
---|
| 99 | sptr = "-123.456-123.456i"; |
---|
[33807a1e] | 100 | fc = strto( sptr, 0p ); |
---|
[200fcb3] | 101 | sout | fc | sptr; |
---|
[33807a1e] | 102 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
| 103 | dc = strto( sptr, 0p ); |
---|
| 104 | sout | dc | sptr; |
---|
| 105 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
| 106 | ldc = strto( sptr, 0p ); |
---|
| 107 | sout | ldc | sptr; |
---|
| 108 | sptr = " 123.45678901234 -123.4567890i "; // spaces allowed |
---|
| 109 | ldc2 = strto( sptr, 0p ); |
---|
| 110 | sout | ldc2 | sptr; |
---|
[e672372] | 111 | |
---|
[33807a1e] | 112 | sptr = "2.0fred"; |
---|
| 113 | char * eptr = 0p; |
---|
| 114 | errno = 0; // reset |
---|
| 115 | f = strto( sptr, &eptr ); |
---|
| 116 | if ( errno == ERANGE ) sout | "out of range"; |
---|
| 117 | if ( eptr == sptr || // conversion failed, no characters generated |
---|
| 118 | *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? |
---|
| 119 | else assert( false ); |
---|
| 120 | |
---|
| 121 | sptr = "2 3x"; |
---|
| 122 | eptr = 0p; |
---|
| 123 | errno = 0; // reset |
---|
[e672372] | 124 | fc = strto( sptr, &eptr ); |
---|
[33807a1e] | 125 | if ( errno == ERANGE ) sout | "out of range"; |
---|
| 126 | if ( eptr == sptr || // conversion failed, no characters generated |
---|
| 127 | *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? |
---|
| 128 | else assert( false ); |
---|
| 129 | |
---|
| 130 | // convert |
---|
| 131 | |
---|
| 132 | sptr = "-123"; |
---|
| 133 | i = convert( sptr ); |
---|
| 134 | sout | i | sptr; |
---|
| 135 | sptr = "123"; |
---|
| 136 | ui = convert( sptr ); |
---|
| 137 | sout | ui | sptr; |
---|
[e672372] | 138 | |
---|
[33807a1e] | 139 | sptr = "-123"; |
---|
| 140 | li = convert( sptr ); |
---|
| 141 | sout | li | sptr; |
---|
| 142 | sptr = "123"; |
---|
| 143 | uli = convert( sptr ); |
---|
| 144 | sout | uli | sptr; |
---|
| 145 | |
---|
| 146 | sptr = " -123 "; // spaces allowed |
---|
| 147 | lli = convert( sptr ); |
---|
| 148 | sout | lli | sptr; |
---|
| 149 | sptr = " 123 "; // spaces allowed |
---|
| 150 | ulli = convert( sptr ); |
---|
| 151 | sout | ulli | sptr; |
---|
| 152 | |
---|
| 153 | sptr = "-123.456"; |
---|
| 154 | f = convert( sptr ); |
---|
| 155 | sout | f | sptr; |
---|
| 156 | sptr = "-123.4567890123456"; |
---|
| 157 | d = convert( sptr ); |
---|
| 158 | sout | d | sptr; |
---|
| 159 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
| 160 | ld = convert( sptr ); |
---|
| 161 | sout | ld | sptr; |
---|
| 162 | |
---|
| 163 | sptr = "-123.456-123.456i"; |
---|
| 164 | fc = convert( sptr ); |
---|
| 165 | sout | fc | sptr; |
---|
[e672372] | 166 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
[33807a1e] | 167 | dc = convert( sptr ); |
---|
[200fcb3] | 168 | sout | dc | sptr; |
---|
[e672372] | 169 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
[33807a1e] | 170 | ldc = convert( sptr ); |
---|
[200fcb3] | 171 | sout | ldc | sptr; |
---|
[e672372] | 172 | sptr = "123.45678901234-123.4567890i"; |
---|
[33807a1e] | 173 | ldc2 = convert( sptr ); |
---|
[200fcb3] | 174 | sout | ldc2 | sptr; |
---|
[33807a1e] | 175 | |
---|
| 176 | sptr = "2.0fred"; |
---|
| 177 | try { |
---|
| 178 | f = convert( sptr ); |
---|
| 179 | assert( false ); |
---|
| 180 | } catch( invalid_argument * ) { |
---|
| 181 | sout | "invalid argument" | sptr; |
---|
| 182 | } // try |
---|
| 183 | |
---|
| 184 | sptr = "2 3x"; |
---|
| 185 | try { |
---|
| 186 | fc = convert( sptr ); |
---|
| 187 | assert( false ); |
---|
| 188 | } catch( invalid_argument * ) { |
---|
| 189 | sout | "invalid argument" | sptr; |
---|
| 190 | } // try |
---|
[658f6de0] | 191 | } // main |
---|
| 192 | |
---|
| 193 | // Local Variables: // |
---|
| 194 | // tab-width: 4 // |
---|
[dc8511c] | 195 | // compile-command: "cfa ato.cfa" // |
---|
[658f6de0] | 196 | // End: // |
---|