gutility.cpp
/**************************************************************************/
/* Author....... Todd A. Miller */
/* Dialect...... Turbo C++ 3.0 */
/* Description.. This file contains useful graphics utilities. */
/**************************************************************************/
#ifndef GR_LIB
#define GR_LIB
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <graphics.h>
#define AUTHOR "Programmer Name"
#define COURSE "Course Num"
#define PI M_PI
#define DELAY_TIME 500
#define OFFSET 1
void opengraph(int=BLACK, int=0);
void print_header(int, int=YELLOW);
void print_header(char*, int=YELLOW);
int run_again(int=LIGHTCYAN);
void pause(int=LIGHTCYAN);
char* integer(int);
char* real(double, int, int);
char* get_static_string();
double normalize(double, int&);
void readln(FILE*, char*);
void readln(FILE*);
int feoln(FILE*);
double log(int, double);
int ipow(int, int);
int ilog(int, int);
#define ROUND(x) (((x)>0) ? long((x)+.5) : long((x)-.5))
#define SQR(x) ((x)*(x))
#define MIN(x,y) (((x)<(y))?(x):(y))
#define MAX(x,y) (((x)>(y))?(x):(y))
void opengraph(int background_color, int prn_scrn)
{
int driver, mode, error;
driver = DETECT;
initgraph(&driver, &mode, "c:\\tc\\BGI");
error = graphresult();
if (error != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(error));
exit(error);
}
if (driver == VGA)
setgraphmode(VGAMED);
setbkcolor(background_color);
if (prn_scrn)
{
setpalette(BLACK, 63);
setpalette(WHITE, 0);
}
}
void print_header(int n, int color)
{
char title[80] = "Program #";
int old_color;
old_color = getcolor();
setcolor(color);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
strcat(title, integer(n));
outtextxy(getmaxx()/2, 1*(textheight(title)+OFFSET), title);
outtextxy(getmaxx()/2, 2*(textheight(title)+OFFSET), COURSE);
outtextxy(getmaxx()/2, 3*(textheight(title)+OFFSET), "by");
outtextxy(getmaxx()/2, 4*(textheight(title)+OFFSET), AUTHOR);
setcolor(old_color);
}
void print_header(char* title, int color)
{
int old_color;
old_color = getcolor();
setcolor(color);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(getmaxx()/2, 1*(textheight(title)+OFFSET), title);
outtextxy(getmaxx()/2, 2*(textheight(title)+OFFSET), COURSE);
outtextxy(getmaxx()/2, 3*(textheight(title)+OFFSET), "by");
outtextxy(getmaxx()/2, 4*(textheight(title)+OFFSET), AUTHOR);
setcolor(old_color);
}
int run_again(int color)
{
char prompt[80] = "Would you like to run again (Y/N)? ";
char reply[2] = "";
int old_color;
old_color = getcolor();
setcolor(color);
settextjustify(LEFT_TEXT, BOTTOM_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(OFFSET, getmaxy()-OFFSET, prompt);
do
{
fflush(stdin);
reply[0] = getch();
reply[0] = toupper(reply[0]);
}
while ((reply[0] != 'Y') && (reply[0] != 'N'));
outtextxy(textwidth(prompt)+OFFSET, getmaxy()-OFFSET, reply);
delay(DELAY_TIME);
setfillstyle(EMPTY_FILL, 0);
strcat(prompt, "Y");
bar(0, getmaxy()-textheight(prompt)-OFFSET, textwidth(prompt)+OFFSET, getmaxy());
setcolor(old_color);
return(reply[0] == 'Y');
}
void pause(int color)
{
char* prompt = "Press any key to continue...";
int old_color = getcolor();
fflush(stdin);
setcolor(color);
settextjustify(LEFT_TEXT, BOTTOM_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(OFFSET, getmaxy()-OFFSET, prompt);
getch();
bar(0, getmaxy()-textheight(prompt)-OFFSET, textwidth(prompt)+OFFSET, getmaxy());
setcolor(old_color);
}
char* integer(int n)
{
char* s = get_static_string();
int tens;
int i = 0;
if (n < 0)
s[i++] = '-';
n = abs(n);
tens = ipow(10, ilog(10, n));
for (; tens>0; ++i)
{
s[i] = n / tens + '0';
n %= tens;
tens /= 10;
}
s[i] = NULL;
return(s);
}
char* real(double x, int len, int dec=-1)
{
char* s = get_static_string();
if (dec >= 0)
{
int int_len, n;
double frac_value;
int tens;
int sign = 0;
if (x < 0)
{
x = -x;
++sign;
}
n = x;
frac_value = x - n;
int_len = ilog(10, n) + 1;
if (len-dec-1-sign < int_len)
len = int_len + dec + 1 + sign;
tens = ipow(10, int_len-1);
for (int j=0; j<len-dec-1-int_len-sign; ++j)
s[j] = ' ';
if (sign)
s[j++] = '-';
for (int i=j; i<len-dec-1; ++i)
{
s[i] = n / tens + '0';
n %= tens;
tens /= 10;
}
s[i++] = '.';
for (j=0; j<dec; ++j)
{
s[i+j] = int(frac_value * 10) + '0';
frac_value *= 10;
frac_value -= int(frac_value);
}
s[i+j] = NULL;
}
else
{
int exp = 0;
int sign = 0;
x = normalize(x, exp);
if (x < 0)
{
x = -x;
++sign;
s[0] = '-';
}
if (len-sign < 7)
len = 7 + sign;
s[sign] = int(x) + '0';
x -= int(x);
s[sign+1] = '.';
for (int i=0; i<len-6; ++i)
{
x *= 10;
s[sign+2+i] = int(x) + '0';
x -= int(x);
}
s[sign+2+i++] = 'E';
if (exp < 0)
s[sign+2+i++] = '-';
else
s[sign+2+i++] = '+';
s[sign+2+i++] = exp / 10 % 10 + '0';
s[sign+2+i++] = exp % 10 + '0';
s[sign+2+i] = NULL;
}
return(s);
}
char* get_static_string()
{
static char s[256];
return(s);
}
double normalize(double x, int &n) // Used within the real function.
{
if (abs(x) >= 10)
return(normalize(x/10, ++n));
else if (x == 0)
return(0);
else if (abs(x) < 1)
return(normalize(x*10, --n));
else
return(x);
}
int feoln(FILE *f)
{
unsigned char c;
fscanf(f, "%c", &c);
if (feof(f) || (c == '\n') || (c == '\r'))
return(1);
ungetc(c, f);
return(0);
}
void readln(FILE* f, char *s)
{
char ch;
int i = 0;
if (!feof(f) )
ch = fgetc(f);
while (!feof(f) && (ch != '\n') && (ch != '\r'))
{
s[i++] = ch;
ch = fgetc(f);
}
s[i] = NULL;
}
void readln(FILE* f)
{
char ch;
if (!feof(f))
ch = fgetc(f);
while (!feof(f) && (ch != '\n') && (ch != '\r'))
ch = fgetc(f);
}
double log(int b, double x)
{
if (x > 0)
return(log(x) / log(b));
else if (x < 0)
return(-(log(-x) / log(b)));
else
return(0);
}
int ipow(int x, int n) { return((n>0) ? (x*ipow(x,n-1)) : (1)); }
int ilog(int b, int n) { return((n>=b) ? (1+ilog(b,n/b)) : (0)); }
#undef AUTHOR
#undef COURSE
#undef HEADER_COLOR
#undef MESSAGE_COLOR
#undef OFFSET
#endif GR_LIB