Category : C Source Code
Archive   : CXLCALC.ZIP
Filename : NEWCALC.C

 
Output of file : NEWCALC.C contained in archive : CXLCALC.ZIP
/* This program is a programmer's calculator designed to assist programmers
** with byte operations. It generates the answer in the four bases.
**
** Language : Microsoft C version 5.10
** Programmer : Terence A.Thomas
** Version : 2.50
** Date : July 13, 1989
** Libraries : CXL Entended Functions for C
** Usage : Newcalc.(exe)
**
*/

#include /* standard header file for CXL prototypes */
#include /* header file with scancode values */

#define NUM_OPS 10 /* number of selectable operators */
#define NUM_OCT 8 /* number of BASE * numbers */

/* define the help function indices */

#define OVERVIEW 1 /* overview of the calculator */
#define CAL_MODE 2 /* describes the different mode */
#define DECIMAL 3 /* help for decimal mode */
#define HEXADEC 4 /* help for hex mode */
#define OCTAL 5 /* help for the octal mode */
#define BINARY 6 /* help for the binary mode */
#define OPERAND 7 /* help for the operand field */
#define FUNCTION 8 /* help for the functions */
#define ANSWER 9 /* help for the answer window */
#define QUIT 10 /* help for quitting */
#define EDIT 11 /* help on how to edit */
#define INV_OCT 12 /* help on invalid octal values */
#define INV_BIN 13 /* help on invalid binary values */
#define OPERATR 14 /* help on operators */

/* Define the major prototypes to be used in the program */

void date_time(void); /* date and time function */
void add_shadow(void); /* adds shadow to a given window */
void mouse_init(void); /* initializes the mouse */
void easy_quit(void); /* allows exit from any place */
void decimal(void); /* decimal mode */
void hexadec(void); /* hexadecimal mode */
void octal(void); /* octal mode */
void binary(void); /* binary mode */
void operators(void); /* displays logical operators */
int legal_ops(char *op); /* checks for legal operators */
int check_oct(char *oct); /* checks for legal octal numbers */
int check_bin(char *bin); /* checks for legal binary numbers */
void display_ops(void); /* display the legal operators */

int *scrn; /* screen buffer location */
long op1,op2; /* global operand values */
char value1[26],value2[26]; /* global value input strings */
char func[2]; /* global calculator function string */

static WINDOW wn[2]; /* declare two windows */

char *legal_operators[] = {"+","-","*","/","~","&","|","^","<",">",NULL};

main()
{
videoinit(); /* init the video system to current system */
kbclear(); /* clear the CXL keyboard buffer */
clearkeys(); /* and the DOS keyboard buffer */

mouse_init(); /* init the mouse system if there is one */
numon(); /* turn on the NUMLOCK key by default */

if ((scrn = ssave()) == NULL)
{
printf("\aThere is an error in allocating memory for this program\n");
exit(1);
}

clrscrn(); /* clear the screen */
hidecur(); /* hide the cursor */
fill_(0,0,24,79,176,LCYAN|_RED); /* fill the screen */

prints(24,24,LGREEN|_RED,"[ALT-X] To Exit [F5] For Help");

setonkey(0x2d00,easy_quit,0); /* set ALT-X to the exit routine */
/* set F5 to context sensitive help */
whelpdef("CALC.HLP",_F5,WHITE|_LGREY,BLUE|_LGREY,YELLOW|_LGREY,YELLOW|_BLUE,add_shadow);

setkbloop(date_time); /* while we don't do a thing */
present(); /* present opening screen */

op1 = op2 = 0L;
value1[0] = value2[0] = func[0] = NULL; /* init values */
calc_menu(); /* main menu for program */
normal_exit();
}

/* This function terminates the program normally */

normal_exit()
{
srestore(scrn); /* restore the screen to original state */

if ((machid()) != IBMPCAT) /* test for an IBM AT class machine */
numoff(); /* if not an AT, turn NUMLOCK off */

showcur(); /* enable cursor */
gotoxy_(24,5);
exit(0);
}

/* This function adds a shadow to the active window */

void add_shadow(void)
{
wshadow(LGREY|_BLACK);
}

/* This function displays the date and time and numlock status */

void date_time(void)
{
prints(0,0,WHITE|_RED,sysdate(0));
prints(0,72,WHITE|_RED,systime(1));
if (kbstat() & NUMLOCK)
prints(24,72,YELLOW|_RED,"NUM LOCK");
else
fill_(24,72,24,79,176,LCYAN|_RED);
prints(0,31,LCYAN|_RED,"Programmers' Calc");
}

/* This function initiates the mouse system and support if present */

void mouse_init(void)
{
if (msinit()) /* check to see if we have a mouse on board */
{
mssupport(MS_FULL); /* offer full support */
msspeed(50,100); /* set the mouse's speed */
msgotoxy(12,49); /* position mouse cursor */
}
}

/* This function presents the opening screen (sound and stuff !!!) */

present()
{
int snd = 3000;

while (snd > 2000)
{
sound_(snd,4);
snd-=90;
}
wn[0] = wopen(8,20,15,60,3,BLUE|_LGREY,LBLUE|_LGREY);
add_shadow();

wcenters(1,RED|_LGREY,"Programmers' Calc Ver. 2.5");
wcenters(2,RED|_LGREY,"Utilizing the CXL Functions");
wcenters(3,RED|_LGREY,"Written by Terence 'House' Thomas");
wcenters(4,RED|_LGREY|BLINK,"Press A Key!");

whelpcat(OVERVIEW); /* set help to the overview */
waitkeyt(182); /* wait five seconds for key to be hit */
wclose(); /* and then close the window */
}

/* This function allows the user to easily quit the program */

void easy_quit(void)
{
int ch;

whelpush(); /* save current help category */
whelpcat(QUIT); /* and set help to information on 'QUIT' */

if (_mouse)
mshidecur(); /* hide the mouse cursor */

wn[4] = wopen(9,26,13,55,2,YELLOW|_LGREY,WHITE|_LGREY);
add_shadow();
wputs("\n\a Quit Calc! Are you sure? \033A\166Y\b");
clearkeys(); /* clear keyboard */
showcur();

ch = wgetchf("YyNn",'Y');
if (toupper(ch) == 'Y')
{
wclose();
normal_exit();
}
wclose();
hidecur(); /* hide the display cursor */
whelpop(); /* pop the current help category off of the stack */

if (_mouse)
msshowcur(); /* redisplay the cursor again */
}

/* This is the menu function for the program */

calc_menu()
{
while (_winfo.errno != W_ESCPRESS) /* test for ESC pressed */
{
wmenubeg(5,10,8,43,2,BROWN|_LGREY,MAGENTA|_LGREY,add_shadow);
wmenuitem(0,1," Mode ",'M',1,M_HASPD,NULL,0,CAL_MODE);
wmenuitxt(1,2,BLACK|_LGREY,"Select a calculation mode");

wmenubeg(7,11,12,30,3,BLUE|_CYAN,BLUE|_CYAN,NULL);
wmenuitem(0,1," Decimal F1 ",'D',2,M_CLOSE,decimal,0x3b00,DECIMAL);
wmenuitem(1,1," Hexadecimal F2 ",'H',3,M_CLOSE,hexadec,0x3c00,HEXADEC);
wmenuitem(2,1," Octal F3 ",'O',4,M_CLOSE,octal,0x3d00,OCTAL);
wmenuitem(3,1," Binary F4 ",'B',5,M_CLOSE,binary,0x3e00,BINARY);
wmenuend(2,M_PD|M_SAVE,0,1,YELLOW|_CYAN,MAGENTA|_CYAN,BLACK|_CYAN,WHITE|_MAGENTA);

wmenuitem(0,9," Quit ",'Q',6,M_CLALL,NULL,0,QUIT);
wmenuitxt(1,2,BLACK|_LGREY,"Exits the calculator ");
wmenuiba(easy_quit,NULL);

wmenuend(1,M_HORZ|M_SAVE,0,0,LBLUE|_LGREY,MAGENTA|_LGREY,BLACK|_LGREY,YELLOW|_MAGENTA);
wmenuget();
}
}

/* This function implements the decimal mode */

void decimal(void)
{
if (_mouse)
mshidecur();

wn[0] = wopen(8,20,15,45,1,CYAN|_BLUE,LGREY|_BLUE);
add_shadow();
wtitle(" Decimal Mode ",TLEFT,CYAN|_BLUE);

/* Data input setup */
sprintf(value1,"%ld",op1);
sprintf(value2,"%ld",op2); /* convert to decimal */

winpbeg(WHITE|_BLUE,WHITE);
wprints(1,1,YELLOW|_BLUE,"Operand:");
winpdef(1,9,value1,"9999999999",'9',1,NULL,OPERAND); /* get value1 */
wprints(2,1,YELLOW|_BLUE,"Operand:");
winpdef(2,9,value2,"9999999999",'9',1,NULL,OPERAND); /* get value2 */
wprints(4,1,GREEN|_BLUE,"Function:");
winpdef(4,10,func,"*",0,2,legal_ops,FUNCTION); /* get function */
winpfba(display_ops,NULL);

if (winpread() != W_ESCPRESS)
{
op1 = atol(value1);
op2 = atol(value2); /* convert string to values */
give_answer(); /* and display the answer */
}

wclose();
if (_mouse)
msshowcur();
hidecur();
}

/* This function implements the hexadecimal mode */

void hexadec(void)
{
if (_mouse)
mshidecur();

wn[0] = wopen(10,20,17,45,1,BLUE|_CYAN,WHITE|_BROWN);
add_shadow();
wtitle(" Hexadecimal Mode ",TLEFT,BLUE|_CYAN);

/* Data input setup */
sprintf(value1,"%lx",op1);
sprintf(value2,"%lx",op2);

winpbeg(CYAN|_BROWN,MAGENTA|_LGREY);
wprints(1,1,YELLOW|_BROWN,"Operand:");
winpdef(1,9,value1,"HHHHHHHHHH",'9',1,NULL,OPERAND); /* get value1; 'H' = hex only */
wprints(2,1,YELLOW|_BROWN,"Operand:");
winpdef(2,9,value2,"HHHHHHHHHH",'9',1,NULL,OPERAND); /* get value2 */
wprints(4,1,GREEN|_BROWN,"Function:");
winpdef(4,10,func,"*",0,2,legal_ops,FUNCTION);
winpfba(display_ops,NULL);

if (winpread() != W_ESCPRESS)
{
sscanf(value1,"%lx",&op1);
sscanf(value2,"%lx",&op2); /* convert to hex */
give_answer(); /* and give the answer */
}

wclose();
if (_mouse)
msshowcur();
hidecur();
}

/* This function implements the octal mode */

void octal(void)
{
if (_mouse)
mshidecur();

wn[0] = wopen(12,20,19,45,1,MAGENTA|_CYAN,WHITE|_MAGENTA);
add_shadow();
wtitle(" Octal Mode ",TLEFT,WHITE|_CYAN);

/* Data input setup */
sprintf(value1,"%lo",op1);
sprintf(value2,"%lo",op2); /* convert to octal value */

winpbeg(BLACK|_MAGENTA,YELLOW|_BLUE);
wprints(1,1,LGREY|_MAGENTA,"Operand:");
winpdef(1,9,value1,"9999999999",'9',1,check_oct,OPERAND);
wprints(2,1,LGREY|_MAGENTA,"Operand:");
winpdef(2,9,value2,"9999999999",'9',1,check_oct,OPERAND);
wprints(4,1,YELLOW|_MAGENTA,"Function:");
winpdef(4,10,func,"*",0,2,legal_ops,FUNCTION);
winpfba(display_ops,NULL);

if (winpread() != W_ESCPRESS)
{
sscanf(value1,"%lo",&op1);
sscanf(value2,"%lo",&op2); /* convert to octal */
give_answer();
}

wclose();
if (_mouse)
msshowcur();
hidecur();
}

/* This function implements the octal mode.
** NOTE: Microsoft C doesn't inclusively support
** conversion from any base to binary by using
** sprintf or sscanf. The Microsoft function
** "ltoa" is the only macro available to convert
** standard bases to irregular bases (binary,trinary) */

void binary(void)
{
long tmp1,tmp2,sum;
int i,j;

tmp1 = tmp2 = 0L; /* init temporary values */

if (_mouse)
mshidecur();

wn[0] = wopen(10,28,17,53,1,BLACK|_CYAN,BLACK|_LGREY);
add_shadow();
wtitle(" Binary Mode ",TLEFT,RED|_CYAN);

/* Data input setup */
ltoa(op1,value1,2);
ltoa(op2,value2,2); /* convert to binary value */

winpbeg(WHITE|_LGREY,MAGENTA);
wprints(1,1,RED|_LGREY,"Operand:");
winpdef(1,9,value1,"9999999999",'9',1,check_bin,OPERAND);
wprints(2,1,RED|_LGREY,"Operand:");
winpdef(2,9,value2,"9999999999",'9',1,check_bin,OPERAND);
wprints(4,1,LBLUE|_LGREY,"Function:");
winpdef(4,10,func,"*",0,2,legal_ops,FUNCTION);
winpfba(display_ops,NULL);

if (winpread() != W_ESCPRESS)
{
for (i=strlen(value1); i>0; i--) /* start at end of string */
{
if (value1[i] == '1') /* if bit position is on */
{
sum = 1L;
if (i < strlen(value1)-1)
{
for(j=1; j < (strlen(value1)-i); j++)
sum*=2;
}
tmp1+=sum; /* and assign value */
}
}

for (i=strlen(value2); i>0; i--) /* start at end of string */
{
if (value2[i] == '1') /* if bit position is on */
{
sum = 1L;
if (i < strlen(value2)-1)
{
for(j=1; j < (strlen(value2)-i); j++)
sum*=2;
}
tmp2+=sum; /* and assign value */
}
}
op1 = tmp1;
op2 = tmp2;
give_answer();
}
wclose();
if (_mouse)
msshowcur();
hidecur();
}

give_answer()
{
long result = 0L;
int _do = 1;
char buffer[31];

buffer[0] = NULL;

whelpush(); /* save current help */
whelpcat(ANSWER); /* set category to answer */

switch(func[0])
{
case '+': result = op1+op2;
break;
case '-': result = op1-op2;
break;
case '*': result = op1*op2;
break;
case '/': result = op1/op2;
break;
case '~': result = ~(op2);
break;
case '&': result = op1&op2;
break;
case '|': result = op1|op2;
break;
case '^': result = op1^op2;
break;
case '>': result = op1 >> op2;
break;
case '<': result = op1 << op2;
break;
default: beep(); /* Sound bell */
_do = 0; /* do not give an answer */
break;
}

if (_do)
{
ltoa(result,buffer,2); /* convert to binary value */
wn[0] = wopen(4,30,14,77,3,BLUE|_RED,YELLOW|_LGREY);
add_shadow();
wtitle(" Answer Window ",TLEFT,BLACK|_RED);

wgotoxy(2,1);
wprintf(" Decimal Value:%10ld\n",result);
wgotoxy(3,1);
wprintf("Hexadecimal Value:%10lx\n",result);
wgotoxy(4,1);
wprintf(" Octal Value:%10lo\n",result);
wgotoxy(5,1);
wprintf(" Binary Value:%10s\n",buffer); /* print converted result */

if (result >= 0 && result <=256)
{
wgotoxy(6,1);
wprintf(" ASCII Value:%10c",result);
}
wcenters(8,BLINK|LGREEN|_LGREY,"Press Any Key");
waitkeyt(360);
wclose();
}
else
wperror("Sorry, but invalid function chosen");

whelpop(); /* restore current help */
}

/* This function checks for legal operators */
int legal_ops(op)
char *op;
{
int c;

whelpush(); /* save current help */
whelpcat(FUNCTION); /* set to 'FUNCTION' help screen */

for (c=0; c if ((strcmpi(op,legal_operators[c])) == 0)
{
break;
whelpop(); /* restore current help */
return(0);
}
if (c == 10)
{
wperror("Invalid operator, try again");
whelpop(); /* restore current help */
return(1);
}
}

/* This function checks for legal octal values */

int check_oct(oct)
char *oct;
{
int len;

whelpush();
whelpcat(INV_OCT);

for (len=0; len {
if (oct[len] > '7')
{
wperror("Number not an octal number");
whelpop();
return(1);
}
}
whelpop();
return(0);
}

/* This function checks for legal binary characters */

int check_bin(bin)
char *bin;
{
int len;

whelpush();
whelpcat(INV_BIN);

for (len=0; len {
if (bin[len] > '1')
{
wperror("Number not a binary number");
whelpop();
return(1);
}
}
whelpop();
return(0);
}

/* This function displays the legal operators' window */

void display_ops(void)
{
whelpush();
whelpcat(OPERATR);

wn[1] = wopen(2,32,9,75,1,LMAGENTA|_RED,LMAGENTA|_RED);
add_shadow();
wtitle(" Logical Operators ",TCENTER,LGREY|_RED);
wputs(" + Add - Subtract * Multiply\n");
wputs(" / Divide ~ 1's Compl. & Bit AND\n");
wputs(" | Log. OR ^ Exclusive OR < Bit LEFT\n");
wputs(" > Bit RIGHT");
wcenters(5,CYAN|_RED,"Use These Operators");
waitkey();
whelpop();
wclose();
}


  3 Responses to “Category : C Source Code
Archive   : CXLCALC.ZIP
Filename : NEWCALC.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/