// ------------------------------------------------------------ // w0.h base, and text output functions All C++ // ------------------------------------------------------------ /* NB This is w.h with the definitions of ftab and roll removed: the completion of these functions are exercises. Base section has definitions we always use Simplify by not using a text win class Keep all code generic Combine declarations and definitions in one file: hence ifndef-define-endif wraps this whole file. */ #ifndef w_h #define w_h // ------------------------------------------------------------------ // top-level functions #ifndef fptype_1 #define fptype_1 typedef double (*fptype1)( double); #endif #ifndef fptype_2 #define fptype_2 typedef double (*fptype2)( double,double); #endif #ifndef fptype_3 #define fptype_3 typedef double (*fptype3)( double,double,double); #endif // ----------------------------------------------------------------- // the fun function classes #ifndef fun_1 #define fun_1 class fun { public: virtual double f(double) = 0; }; #endif #ifndef fun_2 #define fun_2 class fun2 { public: virtual double f(double, double) = 0; }; #endif #ifndef fun_3 #define fun_3 class fun3 { public: virtual double f(double, double, double) = 0; }; #endif // --------------------------------------------------------------------- // our usual selection of libraries #include // Stream library #include // Console i/o library #include // for gets #include // for atoi #include // for format strings #include // string library #include // Math library // some global constants (defined later) const extern double Pi; const extern double Piby2; const extern double Piby4; const extern double Piby180; const extern double Exp1; // some global functions int QUIT; // 1 or 0 void wait(); // wait for key press 'q' -> quit = 1 int roll(int i, int n, int di = 1); // di cyclic perms of 1..n starting at i double fid(double x); // functional identity double sqr(double x); // ------------------------------------------------------ // io functions void nl(int dn = 1); // new line(s) void p(char *s); // print s to current position void pl(char *s); // print s with new line void p(double x); // print x to current position void pl(double x); // print x with new line void p(char *s,double x); // print s x void pl(char*s,double x); // print s x void banner(char *s); // print banner void pf(char *fmt, ...); // p formatted data void pfl(char *fmt, ...); // p formatted data with nl void r(char *s); // read string s, and nl void r(double &x); // read string s, x = atof(s) void r(char *s, double &x); void ftab(fptype1 f, char *fs, // tabulate x f(x) double xa, double xb, int nx); void ftab(fun *f, char *fs, // tabulate x f(x) double xa, double xb, int nx); void precision(int n); // sets cout precision // ------------------------------------------------------------ // w.cpp definitions (implementations) // ------------------------------------------------------------ const double Pi = 3.1415926535897932; const double Piby2 = 1.5707963267948966; const double Piby4 = 0.7853981633974483; const double Piby180 = 1.7453292519943296e-2; const double Exp1 = 2.7182818284590452; char *MARGSTR = " "; char *TABSTR = "%s(%10.6f) = %12.6f"; // ---------------------------------------------------------- void wait() { char c; c = getc(stdin); if (c == 'q') QUIT = 1; else QUIT = 0; } double fid(double x) { return x; } double sqr(double x) { return x*x; } // ----------------------------------------------------------- void nl(int dn) { for (int i = 0; i < dn; i++) cout << "\n"; cout << MARGSTR; cout.flush(); } void p(char *s) { cout << s;} void pl(char *s) { p(s); nl(); } void p(double x) { cout << x; } void pl(double x) { p(x); nl(); } void p(char *s,double x) { p(s); p(x); } void pl(char*s,double x) { p(s); pl(x); } void banner(char *s) { int len = strlen(s)+2,i; for (i = 0; i < len; i++) p("-"); nl(); p(" "); pl(s); for (i = 0; i < len; i++) p("-"); nl(); } void setmarg(char *m) { strcpy(MARGSTR,m); } void pf(char *fmt, ...) // p for formatted data { char s[80]; va_list argptr; va_start(argptr, fmt); vsprintf(s,fmt, argptr); va_end(argptr); p(s); } void pfl(char *fmt, ...) // .. and nl { char s[80]; va_list argptr; va_start(argptr, fmt); vsprintf(s,fmt, argptr); va_end(argptr); p(s); nl();} void r(char* s) // read a string into s { gets(s); } void r(double &x) { char s[80]; gets(s); x = atof(s); } void r(char *s, double &x) { p(s); r(x); nl(); } void precision(int n = 7) { cout.precision(n); } // call iostream fun // ----------------------------------------------------------- #endif