Changes in tests/sum.cfa [292642a:ef346f7c]
- File:
-
- 1 edited
-
tests/sum.cfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/sum.cfa
r292642a ref346f7c 11 11 // Created On : Wed May 27 17:56:53 2015 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Sun May 19 11:21:02 201914 // Update Count : 33013 // Last Modified On : Sun Dec 23 23:00:38 2018 14 // Update Count : 287 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 prelude 19 21 20 22 trait sumable( otype T ) { … … 29 31 T sum( size_t size, T a[] ) { 30 32 T total = 0; // initialize by 0 constructor 31 for ( i; size)33 for ( size_t i = 0; i < size; i += 1 ) 32 34 total += a[i]; // select appropriate + 33 35 return total; 34 36 } // sum 35 37 38 // Not in prelude. 39 unsigned char ?+?( unsigned char t1, unsigned char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion 40 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 36 49 int main( void ) { 37 50 const int low = 5, High = 15, size = High - low; 38 51 39 signed char s = 0, a[size], v = (char)low;40 for ( int i = 0; i < size; i += 1, v += 1 hh) {52 unsigned char s = 0, a[size], v = (char)low; 53 for ( int i = 0; i < size; i += 1, v += 1 ) { 41 54 s += v; 42 55 a[i] = v; 43 56 } // for 44 57 sout | "sum from" | low | "to" | High | "is" 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; 58 | sum( size, (unsigned char *)a ) | ", check" | (int)s; 62 59 63 60 int s = 0, a[size], v = low;
Note:
See TracChangeset
for help on using the changeset viewer.