Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/examples/sum.c

    rbd85400 r784deab  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  5 16:47:44 2016
    13 // Update Count     : 139
     12// Last Modified On : Mon Jan  4 07:40:24 2016
     13// Update Count     : 126
    1414//
    1515
    16 #include <fstream>
     16#include "fstream.h"
    1717
    1818context sumable( type T ) {
     
    2222        T ++?( T * );
    2323        T ?++( T * );
    24 }; // sumable
     24};
    2525
    2626forall( type T | sumable( T ) )
     
    3030                total += a[i];                                                                  // select +
    3131        return total;
    32 } // sum
     32}
    3333
    3434// 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 recursion
    37 // char ++?( char *op ) { *op += 1; return *op; }
    38 // char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
     35const char 0;
     36char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion
     37char ++?( char *op ) { *op += 1; return *op; }
     38char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
    3939
    4040int main( void ) {
    4141        const int low = 5, High = 15, size = High - low;
     42
    4243        ofstream *sout = ofstream_stdout();
    43 #if 0
    4444
    4545        char s = 0, a[size];
     
    6161                 | sum( size, (int *)a ) | ", check " | (int)s | endl;
    6262
     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
    6372        float s = 0.0, a[size];
    6473        float v = low / 10.0;
     
    6978        sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "
    7079                 | sum( size, (float *)a ) | ", check " | (float)s | endl;
    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
     80}
    9181
    9282// Local Variables: //
    9383// tab-width: 4 //
    94 // compile-command: "cfa sum.c" //
     84// compile-command: "cfa sum.c fstream.o iostream.o iterator.o" //
    9585// End: //
Note: See TracChangeset for help on using the changeset viewer.