Changes in tests/sum.cfa [ef346f7c:292642a]
- File:
-
- 1 edited
-
tests/sum.cfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/sum.cfa
ref346f7c r292642a 11 11 // Created On : Wed May 27 17:56:53 2015 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Sun Dec 23 23:00:38 201814 // Update Count : 28713 // Last Modified On : Sun May 19 11:21:02 2019 14 // Update Count : 330 15 15 // 16 16 17 17 #include <fstream.hfa> 18 18 #include <stdlib.hfa> 19 20 void ?{}( int & c, zero_t ) { c = 0; } // not in prelude21 19 22 20 trait sumable( otype T ) { … … 31 29 T sum( size_t size, T a[] ) { 32 30 T total = 0; // initialize by 0 constructor 33 for ( size_t i = 0; i < size; i += 1)31 for ( i; size ) 34 32 total += a[i]; // select appropriate + 35 33 return total; 36 34 } // sum 37 35 38 // Not in prelude.39 unsigned char ?+?( unsigned char t1, unsigned char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion40 unsigned char ?+=?( unsigned char & t1, unsigned char t2 ) { t1 = t1 + t2; return t1; }41 unsigned char ++?( unsigned char & t ) { t += 1; return t; }42 unsigned char ?++( unsigned char & t ) { unsigned char temp = t; t += 1; return temp; }43 44 // Not in prelude.45 void ?{}( unsigned char & c, zero_t ) { c = 0; }46 void ?{}( float & f, zero_t ) { f = 0.0; }47 void ?{}( double & d, zero_t ) { d = 0.0; }48 49 36 int main( void ) { 50 37 const int low = 5, High = 15, size = High - low; 51 38 52 unsigned char s = 0, a[size], v = (char)low;53 for ( int i = 0; i < size; i += 1, v += 1 ) {39 signed char s = 0, a[size], v = (char)low; 40 for ( int i = 0; i < size; i += 1, v += 1hh ) { 54 41 s += v; 55 42 a[i] = v; 56 43 } // for 57 44 sout | "sum from" | low | "to" | High | "is" 58 | sum( size, (unsigned char *)a ) | ", check" | (int)s; 45 | sum( size, (signed char *)a ) | ", check" | (signed char)s; 46 47 unsigned char s = 0, a[size], v = low; 48 for ( int i = 0; i < size; i += 1, v += 1hhu ) { 49 s += (unsigned char)v; 50 a[i] = (unsigned char)v; 51 } // for 52 sout | "sum from" | low | "to" | High | "is" 53 | sum( size, (unsigned char *)a ) | ", check" | (unsigned char)s; 54 55 short int s = 0, a[size], v = low; 56 for ( int i = 0; i < size; i += 1, v += 1h ) { 57 s += (short int)v; 58 a[i] = (short int)v; 59 } // for 60 sout | "sum from" | low | "to" | High | "is" 61 | sum( size, (short int *)a ) | ", check" | (short int)s; 59 62 60 63 int s = 0, a[size], v = low;
Note:
See TracChangeset
for help on using the changeset viewer.