zero++
08-14-2009, 07:46 PM
#include <iostream>
#include <conio>
#include <stdlib>
#include <windows>
#include <string>
#include <ctype>
#include <iomanip>
#include <math>
char strg[64], operat[8], last[16], snum[32], dbtt[16], conv[32], atdb[16],
attt[16], notnum[16]={'+', '-', 'x', '(', static_cast<char>(246)},
bhg[4]={' ', static_cast<char>(246), ' ', '\0'}, a, ch;
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,tts=0, dbs=0, size, cint=0, cflt=0, Int,
x[9]={2, 8, 14, 20, 26}, y[9]={18, 14, 10, 6};
long double num[32], cnum[32], ans, flt, temp;
bool err_occ=false, dot=false, db=false, tt=false, op=false, sl=false,
sign=false, enter=false;
void looks()
{
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)
{
_setcursortype(0);
for(int z=0; z<6; z++)
{
gotoxy(3,2);
if(z%2==0)
textbackground(RED);
else
textbackground(65);
cprintf("%s ERROR", erro[eat]);
Sleep(200);
}
gotoxy(3, 2);
cprintf(" ");
_setcursortype(1);
gotoxy(3, 2);
cout<<strg<<"\b";
}
void clear()
{
total=strg;
size=strlen(strg);
for(int clr=0; clr<size; clr++)
{
operat[clr]=NULL;
dbtt[clr]=NULL; //atdb[clr]=NULL;
attt[clr]=NULL; strg[clr]=NULL;
}
dot=false;
gotoxy(3, 2);
cputs(" ");
gotoxy(3, 2);
c=0; tts=0; dbs=0; bkrg=0; tkrg=0;
}
int tekan()
{
_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(200);
textbackground(65);
gotoxy(x[d], y[b]);
cprintf("%s", all[f]);
gotoxy(c+3, 2);
_setcursortype(2);
return 0;
}
}
}
void main()
{
system("mode con cols=29 lines=20");
looks();
for(;;)
{
a=static_cast<char>(getch());
if(enter==true)
clear();
_setcursortype(2);
if(isdigit(a)&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
op=false;
db=false;
enter=false;
}
else if(((a=='.'&& dot==false)&&strg[c-1]!=')')&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
dot=true;
enter=false;
}
else if(a==')'&& tkrg<bkrg)
{
tekan();
cout<<a;
strg[c]=a;
c++;
tkrg++;
}
else if((a=='+'|| a=='-'||a=='(')&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
if(a=='(')
bkrg++;
dot=false;
op=true;
enter=false;
}
else if(((a=='*'||a=='/')&&(op==false||strg[c-1]==')'))&&(c!=0&&c<25))
{
if(a=='*')a='x';
if(a=='/')a=static_cast<char>(246);
tekan();
cout<<a;
strg[c]=a;
c++;
db=true;
dot=false;
op=true;
enter=false;
}
else if((a=='\b'&&c!=0)&&enter==false)
{
a='E';
tekan();
c--;
putch('\b');
putch(NULL);
gotoxy(c+3, 2);
}
else if(a=='\r'||a=='=')
{
a='=';
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;
}
int at=0, check;
for(int z=0, b=0, d=0; z<c;z++, b++)
{
conv[z]=strg[b];
ch=conv[z];
if(isdigit(ch)||ch=='.')
{
snum[d]=ch;
d++;
}
else if(ch=='x'||ch==static_cast<char>(246)||ch=='('||ch=='-'||ch=='+')
{
if(isdigit(conv[z-1])==false)
{
if((operat[at-1]=='-'||ch=='+')&&(operat[at-1]=='+'||ch=='-'))
{
tts--;
at--;
operat[at]=NULL;
attt[tts]=NULL;
ch='+';
}
else if((operat[at-1]=='+'||ch=='+')&&(operat[at-1]=='-'||ch=='-'))
{
tts--;
at--;
operat[at]=NULL;
attt[tts]=NULL;
ch='-';
}
else if((operat[at-1]=='x'||operat[at-1]==static_cast<char>(246))&&(ch=='-'||ch=='+'))
{
at--;
sign=true;
if(ch=='-')
sl=true;
}
}
if(ch=='x'||ch==static_cast<char>(246)||ch=='(')
{
atdb[dbs]=ch;
operat[at]=ch;
num[at]=_atold(snum);
dbs++;
}
else if((ch=='+'||ch=='-')&&sign==false)
{
attt[tts]=ch;
operat[at]=ch;
if(isdigit(conv[z-1]))
num[at]=_atold(snum);
tts++;
}
if(sl==true)
num[at]=0-num[at];
sign=false;
for(int clr=0; clr<=d; clr++)
snum[clr]=NULL;
d=0;
at++;
}//end of non-digit
}//end of for loop
num[at]=_atold(snum);
for(int nloc=0;nloc<at;)
{
for(int nlo=0;nlo<dbs;nlo++, nloc++)
dbtt[nloc]= atdb[nlo];
for(int nlo=0;nlo<tts;nlo++, nloc++)
dbtt[nloc]= attt[nlo];
}
for(nloc=0;nloc<at;nloc++)
{
for(check=0;check<at;check++)
{
if(dbtt[nloc]==operat[check])
{
switch(dbtt[nloc])
{
case 'x':
case '(':
num[check]*=num[check+1];
break;
case static_cast<char>(246):
if(num[check+1]==0)
{ error(1); goto out; }
else
num[check]/=num[check+1];
break;
case '-':
num[check]-=num[check+1];
break;
case '+':
num[check]+=num[check+1];
break;
}
break;
}
}//end of check loop
for(int set=check; set<at; set++)
{
num[set+1]=num[set+2];
operat[set]=operat[set+1];
}
}//end of nloc loop
ans=num[0];
Int=ans;
for(; Int>0 ; Int/=10)
cint++;
gotoxy(3, 3); //fixed
cout<<setw(25)<<setiosflags(ios::fixed)<<ans;
op=false;
db=false;
enter=true;
sl=false;
}
out:
gotoxy(c+3, 2);
}
else if(a==27)
exit(0);
if(c==25)
{
c--;
cout<<"\b";
_setcursortype(1);
}
}
}
#include <conio>
#include <stdlib>
#include <windows>
#include <string>
#include <ctype>
#include <iomanip>
#include <math>
char strg[64], operat[8], last[16], snum[32], dbtt[16], conv[32], atdb[16],
attt[16], notnum[16]={'+', '-', 'x', '(', static_cast<char>(246)},
bhg[4]={' ', static_cast<char>(246), ' ', '\0'}, a, ch;
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,tts=0, dbs=0, size, cint=0, cflt=0, Int,
x[9]={2, 8, 14, 20, 26}, y[9]={18, 14, 10, 6};
long double num[32], cnum[32], ans, flt, temp;
bool err_occ=false, dot=false, db=false, tt=false, op=false, sl=false,
sign=false, enter=false;
void looks()
{
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)
{
_setcursortype(0);
for(int z=0; z<6; z++)
{
gotoxy(3,2);
if(z%2==0)
textbackground(RED);
else
textbackground(65);
cprintf("%s ERROR", erro[eat]);
Sleep(200);
}
gotoxy(3, 2);
cprintf(" ");
_setcursortype(1);
gotoxy(3, 2);
cout<<strg<<"\b";
}
void clear()
{
total=strg;
size=strlen(strg);
for(int clr=0; clr<size; clr++)
{
operat[clr]=NULL;
dbtt[clr]=NULL; //atdb[clr]=NULL;
attt[clr]=NULL; strg[clr]=NULL;
}
dot=false;
gotoxy(3, 2);
cputs(" ");
gotoxy(3, 2);
c=0; tts=0; dbs=0; bkrg=0; tkrg=0;
}
int tekan()
{
_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(200);
textbackground(65);
gotoxy(x[d], y[b]);
cprintf("%s", all[f]);
gotoxy(c+3, 2);
_setcursortype(2);
return 0;
}
}
}
void main()
{
system("mode con cols=29 lines=20");
looks();
for(;;)
{
a=static_cast<char>(getch());
if(enter==true)
clear();
_setcursortype(2);
if(isdigit(a)&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
op=false;
db=false;
enter=false;
}
else if(((a=='.'&& dot==false)&&strg[c-1]!=')')&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
dot=true;
enter=false;
}
else if(a==')'&& tkrg<bkrg)
{
tekan();
cout<<a;
strg[c]=a;
c++;
tkrg++;
}
else if((a=='+'|| a=='-'||a=='(')&&c<25)
{
tekan();
cout<<a;
strg[c]=a;
c++;
if(a=='(')
bkrg++;
dot=false;
op=true;
enter=false;
}
else if(((a=='*'||a=='/')&&(op==false||strg[c-1]==')'))&&(c!=0&&c<25))
{
if(a=='*')a='x';
if(a=='/')a=static_cast<char>(246);
tekan();
cout<<a;
strg[c]=a;
c++;
db=true;
dot=false;
op=true;
enter=false;
}
else if((a=='\b'&&c!=0)&&enter==false)
{
a='E';
tekan();
c--;
putch('\b');
putch(NULL);
gotoxy(c+3, 2);
}
else if(a=='\r'||a=='=')
{
a='=';
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;
}
int at=0, check;
for(int z=0, b=0, d=0; z<c;z++, b++)
{
conv[z]=strg[b];
ch=conv[z];
if(isdigit(ch)||ch=='.')
{
snum[d]=ch;
d++;
}
else if(ch=='x'||ch==static_cast<char>(246)||ch=='('||ch=='-'||ch=='+')
{
if(isdigit(conv[z-1])==false)
{
if((operat[at-1]=='-'||ch=='+')&&(operat[at-1]=='+'||ch=='-'))
{
tts--;
at--;
operat[at]=NULL;
attt[tts]=NULL;
ch='+';
}
else if((operat[at-1]=='+'||ch=='+')&&(operat[at-1]=='-'||ch=='-'))
{
tts--;
at--;
operat[at]=NULL;
attt[tts]=NULL;
ch='-';
}
else if((operat[at-1]=='x'||operat[at-1]==static_cast<char>(246))&&(ch=='-'||ch=='+'))
{
at--;
sign=true;
if(ch=='-')
sl=true;
}
}
if(ch=='x'||ch==static_cast<char>(246)||ch=='(')
{
atdb[dbs]=ch;
operat[at]=ch;
num[at]=_atold(snum);
dbs++;
}
else if((ch=='+'||ch=='-')&&sign==false)
{
attt[tts]=ch;
operat[at]=ch;
if(isdigit(conv[z-1]))
num[at]=_atold(snum);
tts++;
}
if(sl==true)
num[at]=0-num[at];
sign=false;
for(int clr=0; clr<=d; clr++)
snum[clr]=NULL;
d=0;
at++;
}//end of non-digit
}//end of for loop
num[at]=_atold(snum);
for(int nloc=0;nloc<at;)
{
for(int nlo=0;nlo<dbs;nlo++, nloc++)
dbtt[nloc]= atdb[nlo];
for(int nlo=0;nlo<tts;nlo++, nloc++)
dbtt[nloc]= attt[nlo];
}
for(nloc=0;nloc<at;nloc++)
{
for(check=0;check<at;check++)
{
if(dbtt[nloc]==operat[check])
{
switch(dbtt[nloc])
{
case 'x':
case '(':
num[check]*=num[check+1];
break;
case static_cast<char>(246):
if(num[check+1]==0)
{ error(1); goto out; }
else
num[check]/=num[check+1];
break;
case '-':
num[check]-=num[check+1];
break;
case '+':
num[check]+=num[check+1];
break;
}
break;
}
}//end of check loop
for(int set=check; set<at; set++)
{
num[set+1]=num[set+2];
operat[set]=operat[set+1];
}
}//end of nloc loop
ans=num[0];
Int=ans;
for(; Int>0 ; Int/=10)
cint++;
gotoxy(3, 3); //fixed
cout<<setw(25)<<setiosflags(ios::fixed)<<ans;
op=false;
db=false;
enter=true;
sl=false;
}
out:
gotoxy(c+3, 2);
}
else if(a==27)
exit(0);
if(c==25)
{
c--;
cout<<"\b";
_setcursortype(1);
}
}
}