#include <conio>
#include <stdlib>
#include <windows>
#include <string>
#include <ctype>
#include <iomanip>
#include <math>
char strg[64], operat[9][8], last[16], snum[32], dbtt[16], conv[32], atdb[9][16],
attt[9][16], notnum[16]={'+', '-', 'x', '(', static_cast
bhg[4]={' ', static_cast
std::string erro[2]={"syntax", "math"}, total(1,64), dd,
all[20]={" 0 ", " . ", "DEL", "ANS", " = ",
" 1 ", " 2 ", " 3 ", " - ", " + ",
" 4 ", " 5 ", " 6 ", bhg, " x ",
" 7 ", " 8 ", " 9 ", " ( ", " ) "} ;
int nloc, bkrg=0, tkrg=0, c=0, size, cint=0, cflt=0, Int, b,
x[9]={2, 8, 14, 20, 26}, y[9]={18, 14, 10, 6}, countBracket=0;
long double num[9][32], cnum[9][32], ans, flt, temp;
bool err_occ=false, dot=false, db=false, tt=false, op=false, sl=false,
sign=false, enter=false;
void looks()//make for the interface
{
//start make the box for input and output
gotoxy(2, 1);
cprintf("%c", 218);
gotoxy(28, 1);
cprintf("%c", 191);
gotoxy(2, 4);
cprintf("%c", 192);
gotoxy(28, 4);
cprintf("%c", 217);
gotoxy(2, 2);
cprintf("%c", 179);
gotoxy(28, 2);
cprintf("%c", 179);
gotoxy(2, 3);
cprintf("%c", 179);
gotoxy(28, 3);
cprintf("%c", 179);
for(int pot=3; pot<28; pot++)
{
gotoxy(pot, 1);
cprintf("%c", 196);
gotoxy(pot, 4);
cprintf("%c", 196);
}
for(int b=0, f=0; b<4; b++)
for(int d=0; d<5; d++, f++)
{
gotoxy(x[d], y[b]);
cprintf("%s", all[f]);
}
gotoxy(c+3, 2);
};
void error(int eat)//when the are error on input, this function is to show the warning
{
_setcursortype(0);
for(int z=0; z<6; z++)
{
gotoxy(3,2);
if(z%2==0)
textbackground(RED);
else
textbackground(BLACK);
cprintf("%s ERROR", erro[eat]);
Sleep(200);
}
gotoxy(3, 2);
cprintf(" ");
_setcursortype(1);
gotoxy(3, 2);
cout<
}
void clear()//function to clear the input character when the backspace button pressed
{
total=strg;
size=strlen(strg);
for(int clr=0; clr
{
operat[0][clr]=NULL;
atdb[0][clr]=NULL; //atdb[clr]=NULL;
attt[0][clr]=NULL; strg[clr]=NULL;
}
dot=false;
gotoxy(3, 2);
cputs(" ");
gotoxy(3, 2);
c=0; bkrg=0; tkrg=0;
};
void tekan()//function to make the number being entered is pressed
{
_setcursortype(0);
for(int b=0, f=0; b<4; b++)
for(int d=0; d<5; d++, f++)
{
dd=all[f];
if(dd[1]==a)
{
textbackground(CYAN);
gotoxy(x[d], y[b]);
cprintf("%s", all[f]);
Sleep(100);
textbackground(BLACK);
gotoxy(x[d], y[b]);
cprintf("%s", all[f]);
gotoxy(c+3, 2);
_setcursortype(2);
}
}
};
long double start_operation(int xat)
{
if(xat<=bkrg)
{
int at=0, check, tts=0, dbs=0;
for(int z=0, d=0; b
{
conv[z]=strg[b];
ch=conv[z];
if(isdigit(ch)||ch=='.')
{
snum[d]=ch;
d++;
}
else if(ch=='x'||ch==static_cast
{
if(isdigit(conv[z-1])==false)
{ //avoid the double operator being entered
if((operat[xat][at-1]=='-'||ch=='+')&&(operat[xat][at-1]=='+'||ch=='-'))
{
tts--;
at--;
operat[xat][at]=NULL;
attt[xat][tts]=NULL;
ch='+';
}
//avoid the double operator being entered
else if((operat[xat][at-1]=='+'||ch=='+')&&(operat[xat][at-1]=='-'||ch=='-'))
{
tts--;
at--;
operat[xat][at]=NULL;
attt[xat][tts]=NULL;
ch='-';
}
//check for negative sign
else if((operat[xat][at-1]=='x'||operat[xat][at-1]==static_cast
&&(ch=='-'||ch=='+'))
{
at--;
sign=true;
if(ch=='-')
sl=true;
}
else if(ch=='(')//look for opening bracket and solve the equation in the bracket
{
b++;
long double temp_no = start_operation(++xat);
num[--xat][at] = temp_no;
}
}
if(ch=='x'||ch==static_cast
{ //to set the digit character into one number
atdb[xat][dbs]=ch;
operat[xat][at]=ch;
num[xat][at]=_atold(snum);
dbs++;
}
if(ch=='(')//look for opening bracket and solve the equation in the bracket
{
b++;
long double temp_no = start_operation(++xat);
num[--xat][at] = temp_no;
}
if(ch=='(' && isdigit(operat[xat][at-1]))//check opening bracket the operator before { //it to set the digit character into one number
atdb[xat][dbs]=ch;
operat[xat][at]=ch;
num[xat][at]=_atold(snum);
dbs++;
}
//check for plus and minus operator and no negative sign
//to set the digit character into one number
else if((ch=='+'||ch=='-')&&sign==false)
{
attt[xat][tts]=ch;
operat[xat][at]=ch;
if(isdigit(conv[z-1]))
num[xat][at]=_atold(snum);
tts++;
}
if(sl==true)
num[xat][at]=0-num[xat][at];
sign=false;
for(int clr=0; clr<=d; clr++)
snum[clr]=NULL;
d=0;
at++;
}//end of non-digit
else if (ch=')') break;
}//end of for loop
num[xat][at]=_atold(snum);
for(int nloc=0;nloc
{
for(int nlo=0;nlo
dbtt[nloc]= atdb[xat][nlo];
for(int nlo=0;nlo
dbtt[nloc]= attt[xat][nlo];
}
//start make the calculation
for(nloc=0;nloc
{
for(check=0;check
{
if(dbtt[nloc]==operat[xat][check])
{
switch(dbtt[nloc])
{
case 'x':
case '(':
num[xat][check]*=num[xat][check+1];
break;
case static_cast
if(num[xat][check+1]==0)
{ error(1); }
else
num[xat][check]/=num[xat][check+1];
break;
case '-':
num[xat][check]-=num[xat][check+1];
break;
case '+':
num[xat][check]+=num[xat][check+1];
break;
}
break;
}
}//end of check loop
for(int set=check; set
{
num[xat][set+1]=num[xat][set+2];
operat[xat][set]=operat[xat][set+1];
}
}//end of nloc loop
}
b++;
return num[xat][0];
}
main()
{
system("mode con cols=29 lines=20");//set the consol size
SetConsoleTitle( "DOS Calculator" );
looks();
//start taking input and set the operator and operand
for(;;)
{
a=static_cast
if(enter==true)
clear();
_setcursortype(2);
if(isdigit(a)&&c<25) //condition if the character is digit and the total
{ //character below 25
tekan();
cout<
strg[c]=a;
c++;
op=false;
db=false;
enter=false;
}
//detect the character current='.' and character before current=')'
//and the total character below 25
else if(((a=='.'&& dot==false)&&strg[c-1]!=')')&&c<25)
{
tekan();
cout<
strg[c]=a;
c++;
dot=true;
enter=false;
}
//to check for the closing bracket where exist only when the
//opening bracket exist
else if(a==')'&& tkrg
{
tekan();
cout<
strg[c]=a;
c++;
tkrg++;
}
//to check for the operator '+' and '-' and opening bracket
else if((a=='+'|| a=='-'||a=='(')&&c<25)
{
tekan();
cout<
strg[c]=a;
c++;
if(a=='(')
bkrg++;
dot=false;
op=true;
enter=false;
}
//to check for operator '*' and '-'
//and the character before the current either
//not operator or closing bracket
else if(((a=='*'||a=='/')&&(op==false||strg[c-1]==')'))&&(c!=0&&c<25))
{
if(a=='*')a='x';
if(a=='/')a=static_cast
tekan();
cout<
strg[c]=a;
c++;
db=true;
dot=false;
op=true;
enter=false;
}
//detect the backspace button being pressed
else if((a=='\b'&&c!=0)&&enter==false)
{
a='E';
tekan();
c--;
putch('\b');
putch(NULL);
gotoxy(c+3, 2);
}
//condition when the enter or equal button being pressed
else if(a=='\r'||a=='=')
{
a='=';
b=0;
strg[c]='(';
tekan();
_setcursortype(0);
if(enter==false)
{
for(int z=0; z<5; z++)
if(strg[c-1]==notnum[z])
{
error(0); c--;
err_occ=true;
goto out;
}
if(bkrg!=tkrg)
{
error(0); c--;
err_occ=true;
goto out;
}
ans=start_operation(0);
for(; Int>0 ; Int/=10)
cint++;
gotoxy(3, 3);
//set the output into the standard output
cout<
op=false;
db=false;
enter=true;
sl=false;
}
out:
gotoxy(c+3, 2);
}
else if(a==27)
{
clrscr();
_setcursortype(0);
gotoxy(1, 5);
cout<<"This calculator made by"<
cout<
Sleep(5000);
exit(0);
}
if(c==25)
{
c--;
cout<<"\b";
_setcursortype(1);
}
}
}


No comments:
Post a Comment