Simple calculator -- code review

This is a discussion on Simple calculator -- code review within the C++ Programming forums, part of the General Programming Boards category; I just put together this simple integer calculator and I'm hoping I can get a few honest opinions of it ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    725

    Simple calculator -- code review

    I just put together this simple integer calculator and I'm hoping I can get a few honest opinions of it (if reviews are favorable I might continue developing it ) from the adoring public. Feel free to critique the code flow, user interface etc.

    Features:
    - Expression-style input (parentheses and precedence)
    - Bignum support

    Design and implementation notes:
    - I would have posted the code inline, but it's >500 lines long and vB might not like it.
    - I used gotos for the error handling mechanism, not exceptions. However the code should (hopefully) be as easy to follow.
    - You need GMP to build. Tested on GMP 4.1.2.
    - The code isn't very structured and a largely disproportionate amount is lumped in main().
    - It will only handle integers and [+-*/%()] operations, but I've tried to make it versatile and capable of ignoring small errors if they do not affect the calculations.
    - If you really want to know, it converts all expressions to postfix using the shunting yard algorithm and uses RPN for evaluation.
    - Hope a kind soul will check if this builds on non-Windows platforms.
    - I used C <stdio.h> because MinGW seems to have tendencies to bloat executables using C++ streams by a substantial amount.

    Thanks in advance.
    Attached Files Attached Files
    Last edited by jafet; 09-21-2006 at 03:55 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  2. #2
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Why didnt you use cout ?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Could you post an exe please?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    l2u: MinGW <*streams> code is really bloated -- it adds some 200+K to even Hello World programs. Gawd knows why. <stdio.h> makes for smaller I/O code -- about 5 ~ 10K even for large programs.

    blumfluff: I originally thought of that, but I'm not sure if the board mods would allow that. Some forums don't. And I only have a Windows version.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It just that i would like to try it but I would need to add some of those header files.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>MinGW <*streams> code is really bloated -- it adds some 200+K to even Hello World programs. Gawd knows why.<<

    The stl is statically linked so the bloat is only really for the initial use. If you want to reduce the bloat and, incidentally, have access to a wide character implementation of the stl, stlport is a good alternative - provided you dynamically link with it, of course (but then you'd have to distribute the stlport dll for those without stlport...).

    >>I used C <stdio.h> because MinGW seems to have tendencies to bloat executables using C++ streams by a substantial amount.<<

    Why include stdio.h instead of cstdio?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > blumfluff: I originally thought of that, but I'm not sure if the board mods would allow that.
    Probably not.
    Besides, you asked for a code review - can't review the code from looking at the exe.

    My first comment is that main() is WAY too long. Use some more functions to split the work up a bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Thanks for the constructive criticism so far

    In the process of splitting main(), I had to remove the goto mechanism and replace it with try-catch handlers. So much for that... the new code is here. I also added in an exponentiation operator, just for fun (you can't reach very big numbers with multiplication very quickly) (try 2^(2^(2^(2^(2^2 ) It also uses iostreams now.

    I also have a version which uses a home-brewed bignum library; however the library itself is some 43 KB large and is (of course) asymptotically slower than GMP. If there are enough of you who want to check out the compiled app but don't want to bother installing GMP I might post it on my own site.
    Attached Files Attached Files
    Last edited by jafet; 09-23-2006 at 12:59 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21