Changeset d63eeb0 for src/examples/sum.c
- Timestamp:
- Feb 9, 2016, 3:25:05 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 7528ba1
- Parents:
- 771b3c3 (diff), bd85400 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/sum.c
r771b3c3 rd63eeb0 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 4 07:40:24 201613 // Update Count : 1 2612 // Last Modified On : Fri Feb 5 16:47:44 2016 13 // Update Count : 139 14 14 // 15 15 16 #include "fstream.h"16 #include <fstream> 17 17 18 18 context sumable( type T ) { … … 22 22 T ++?( T * ); 23 23 T ?++( T * ); 24 }; 24 }; // sumable 25 25 26 26 forall( type T | sumable( T ) ) … … 30 30 total += a[i]; // select + 31 31 return total; 32 } 32 } // sum 33 33 34 34 // Required to satisfy sumable as char does not have addition. 35 const char 0;36 char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion37 char ++?( char *op ) { *op += 1; return *op; }38 char ?++( char *op ) { char temp = *op; *op += 1; return temp; }35 // const char 0; 36 // char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion 37 // char ++?( char *op ) { *op += 1; return *op; } 38 // char ?++( char *op ) { char temp = *op; *op += 1; return temp; } 39 39 40 40 int main( void ) { 41 41 const int low = 5, High = 15, size = High - low; 42 43 42 ofstream *sout = ofstream_stdout(); 43 #if 0 44 44 45 45 char s = 0, a[size]; … … 61 61 | sum( size, (int *)a ) | ", check " | (int)s | endl; 62 62 63 double s = 0.0, a[size];64 double v = low / 10.0;65 for ( int i = 0; i < size; i += 1, v += 0.1 ) {66 s += (double)v;67 a[i] = (double)v;68 }69 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "70 | sum( size, (double *)a ) | ", check " | (double)s | endl;71 72 63 float s = 0.0, a[size]; 73 64 float v = low / 10.0; … … 78 69 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 79 70 | sum( size, (float *)a ) | ", check " | (float)s | endl; 80 } 71 #endif 72 double s = 0, a[size]; 73 double v = low / 10.0; 74 75 for ( int i = 0; i < size; i += 1, v += 0.1 ) { 76 s += (double)v; 77 a[i] = (double)v; 78 } 79 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 80 | sum( size, (double *)a ) | ", check " | (double)s | endl; 81 82 // struct S { int i, j; } sarr[size]; 83 // struct S 0 = { 0, 0 }; 84 // struct S 1 = { 1, 1 }; 85 // S ?+?( S t1, S t2 ) { S s = { t1.i + t1.j, t2.i + t2.j }; return s; } 86 // S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; } 87 // S ++?( S *t ) { *t += 1; return *t; } 88 // S ?++( S *t ) { S temp = *t; *t += 1; return temp; } 89 // sum( size, sarr ); 90 } // main 81 91 82 92 // Local Variables: // 83 93 // tab-width: 4 // 84 // compile-command: "cfa sum.c fstream.o iostream.o iterator.o" //94 // compile-command: "cfa sum.c" // 85 95 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.