Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: improve my code

  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    improve my code

    Code:
    #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);
          }
       }
    }
    Last edited by oracleguy; 08-14-2009 at 08:54 PM. Reason: please use code tags

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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.
    OracleGuy

  • #3
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    OG, his title says it all... Improve his code! Geez. He made it VERY clear

    -Shane

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by TheShaner View Post
    OG, his title says it all... Improve his code! Geez. He made it VERY clear

    -Shane
    Lol, fair point, and at least it wasn't like "help" or "code".
    OracleGuy

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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...

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    You should probably read the guide to smart questions before posting again, since this kind of thread is mentioned specifically several times in there.

  • Users who have thanked ghell for this post:

    zero++ (10-18-2009)

  • #8
    New Coder
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    heuu what is this thing supposed to do, all i see is alot of random variables and operations.

  • #9
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile Sorry...

    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.

  • #10
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    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.
    Call me Brick, if you like!

  • Users who have thanked BrickInTheWall for this post:

    zero++ (10-18-2009)

  • #11
    Banned
    Join Date
    Jun 2007
    Location
    Web Designer
    Posts
    321
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You should put few meaningfull comments so that others can understand what the code is trying to do.

  • Users who have thanked it career for this post:

    zero++ (10-18-2009)

  • #12
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile thanks

    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

  • #13
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    About the comment and variable... I will try to not to do that again later. thankz


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •