// ------------------------------------------------------------ // ww.cpp base, and text output functions All C++ // ------------------------------------------------------------ /* 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. */ #include "ww.h" // ------------------------------------------------------------ // ww.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"; int QUIT = 0; // ---------------------------------------------------------- void wait() { char c; c = getc(stdin); if (c == 'q') QUIT = 1; else QUIT = 0; } // di cyclic perms of 1..n starting at i int roll(int i, int n, int di) { int r = (i+di)%n; if (r == 0) return n; return r; } #ifndef gcd_f #define gcd_f int gcd(int n,int m) // Euclid { int r = n%m; if (r == 0) return m; else return gcd(m,r); } #endif 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 ftab(fptype1 f, char *fs, double xa, double xb, int nx) { double x = 0,y = 0,dx = (xb-xa)/nx; nl(2); for (int i = 0; i <= nx; i++) { x = xa + i*dx; y = f(x); pfl(TABSTR,fs,x,y); } } void ftab(fun *f, char *fs, double xa, double xb, int nx) { double x = 0,y = 0,dx = (xb-xa)/nx; nl(2); for (int i = 0; i <= nx; i++) { x = xa + i*dx; y = f->f(x); pfl(TABSTR,fs,x,y); } } void precision(int n = 7) { cout.precision(n); } // call iostream fun // -----------------------------------------------------------