...

View Full Version : improve my code



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);
}
}
}

oracleguy
08-14-2009, 07:57 PM
Thats it? No explanation? What are you looking for, a critique on your style, what?

Your use a global variables is very bad, you shouldn't be doing that. Fix that first and repost your code.

You have no documentation on your code, adding some would be a good idea.

TheShaner
08-14-2009, 09:53 PM
OG, his title says it all... Improve his code! Geez. He made it VERY clear :p

-Shane

oracleguy
08-14-2009, 10:34 PM
OG, his title says it all... Improve his code! Geez. He made it VERY clear :p

-Shane

Lol, fair point, and at least it wasn't like "help" or "code". :)

Spookster
08-15-2009, 12:07 AM
And it's okay to name variables using a meaningful name. Avoid abbreviating unless it is very obvious what the abbreviation is. Google C/C++ coding standards. There is a fairly standard guide out there you can use. It will improve readability of your code and in many cases will help to root out potential problems in your code as well.

zero++
09-03-2009, 12:15 AM
sorry for all that. I'm still new with c++ and in forum, so there so many i didn't know about. I got many problem when coding this program and I gave up on doing this with my little knowledge. so, what I want are overall improvement as you can do from the aspects of processes and output. hope this helped...

ghell
09-08-2009, 05:27 PM
You should probably read the guide to smart questions (http://www.catb.org/~esr/faqs/smart-questions.html) before posting again, since this kind of thread is mentioned specifically several times in there.

Blind99
09-19-2009, 02:41 AM
heuu what is this thing supposed to do, all i see is alot of random variables and operations.

zero++
10-18-2009, 08:06 AM
first of all i want to apologize because I'm rarely posting. It because I,m quiet busy all my day.(sorry, bad in English)

anywhere, this is code is to make a calculator.

BrickInTheWall
10-18-2009, 01:23 PM
Oh god, what a mess. Work on giving approprate variable names first. Global variables are also never a good solution. I'm working on a calculator right now and have no global variables what so ever. Also if you're building a calculator to parse input strings, I suggest looking at either the Shunting Yard-Algorithm, top-down analysis, which is similar to recursive descent parsers.

it career
10-18-2009, 01:30 PM
You should put few meaningfull comments so that others can understand what the code is trying to do.

zero++
10-18-2009, 02:02 PM
yea, i like to know about that. But you will need to teach me more about that because i don't think that i know about that and my knowledge on c++ still basic

zero++
10-18-2009, 02:07 PM
About the comment and variable... I will try to not to do that again later. thankz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum