/********************************************************************* For interactive terminal use. Produce a crude graph of the function fx over the prompted for interval x1, x2. Querey for another plot until user sginals satisfaction. C.A. Bertulani May/21/2000 *********************************************************************/ typedef double Number; #include #include #include #include int main(){ void screen(Number (*)(Number)); cout << "This is sin(x). Try something different.\n"; Number fx(Number ); screen(fx); return 0; } /********************************************************************* Here is the fnction you want to plot. C.A. Bertulani May/21/2000 *********************************************************************/ Number fx(Number x){ return sin(x); } /********************************************************************* For interactive terminal use. Produce a crude graph of the function fx over the prompted for interval x1, x2. Querey for another plot until user sginals satisfaction. C.A. Bertulani May/21/2000 *********************************************************************/ #define ISCR 60 /* Number of horizontal and vertical posstions in display */ #define JSCR 21 #define BLANK ' ' #define ZERO '-' #define YY 'l' #define XX '-' #define FF 'x' void screen(Number (*fx)(Number)) { int jz,j,i; Number ysml,ybig,x2,x1,x,dyj,dx,y[ISCR+1]; char scr[ISCR+1][JSCR+1]; for (;;) { cout << "Enter x1 x2 (x1=x2 to stop):\t"; /* Query for another plot, */ cin >> x1 >> x2; /* quit if x1 = x2 */ if (x1 == x2) break; for (j=1;j<=JSCR;j++) /* Fill vertical sides with character 'l' */ scr[1][j]=scr[ISCR][j]=YY; for (i=2;i<=(ISCR-1);i++) { scr[i][1]=scr[i][JSCR]=XX; /* Fill top, bottom with character '-' */ for (j=2;j<=(JSCR-1);j++) /* Fill interior with blanks. */ scr[i][j]=BLANK; } dx=(x2-x1)/(ISCR-1); x=x1; ysml=ybig=0.0; /* Limits will include 0. */ for (i=1;i<=ISCR;i++) { /* Evaluate the fuction at equal intervals. */ y[i]=(*fx)(x); /* Find the largest and the smallest values. */ if (y[i] < ysml) ysml=y[i]; if (y[i] > ybig) ybig=y[i]; x += dx; } if (ybig == ysml) ybig=ysml+1.0; /* Be sure to separate top and bottom. */ dyj=(JSCR-1)/(ybig-ysml); jz=1-(int) (ysml*dyj); /* Note which row corresponds to 0. */ for (i=1;i<=ISCR;i++) { /* Place an indicator at function height and 0. */ scr[i][jz]=ZERO; j=1+(int) ((y[i]-ysml)*dyj); scr[i][j]=FF; } cout << setiosflags(ios::fixed); cout << setw(11) << setprecision(3) << ybig << " "; for (i=1;i<=ISCR;i++) cout << scr[i][JSCR]; cout << "\n"; for (j=(JSCR-1);j>=2;j--) { /* Display. */ cout << setw(12) << " "; for (i=1;i<=ISCR;i++) cout << scr[i][j]; cout << "\n"; } cout << setiosflags(ios::fixed); cout << setw(11) << setprecision(3) << ysml << " "; for (i=1;i<=ISCR;i++) cout << scr[i][1]; cout <<"\n"; cout << setiosflags(ios::fixed); cout<