News for hackers. Stuff that matters.
Re:Show HN: Calc, a simple command-line calulator in C by userbinator on Tuesday June 09, 2015 @11:29AM (#9686487)
I assume you posted this looking for feedback, so here is my advices:

- A 16-line main.c with a single function that does almost nothing is annoying to say the least; this application is simple enough that I think it really shouldn't be more than a single file.

- You really don't need to use dynamic allocation for this. Try a simpler algorithm like recursive-descent/precedence climbing, and tokenise+evaluate as you go.

- isSymbol()/getSymbol() should be merged into one function - you are doing duplicate work here since once you know that it is one of those special identifiers, you should be able to return the value immediately instead of doing another set of comparisons.

This is an almost-complete C interpreter in roughly the same number of lines of code as your calculator, but its structure is actually much simpler. It's probably a bit far toward the side of terseness, but good to study in any case:

https://github.com/rswier/c4/blob/master/c4.c

For understanding precedence climbing/recursive descent, this is a good article:

https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

One thing I see with a lot of beginners is a tendency to overcomplicate solutions, writing far more code than necessary. It takes practice to avoid this, but giving them some extremely minimal implementations to study can help them to better understand the essence of the problem and its solution; to a beginner, it is often simpler than they seem to be at first glance.

There are no comments on this comment yet.