Looking for critique. Teaching myself C.
https://gitlab.com/jmgibson1981/mycprogress
Thus far I've only made a very basic calculator. It works. I'm sure it won't be my last. I'm hoping it's ok to ask for constructive criticism. Thank you for your help and interest. |
That link leads to an endless spinner.
Ed |
It would be good to handle unrecognized operators with some sort of error indication.
You don't seem to be returning a value from math_line or main? I guess the compiler should be warning you about that. Quote:
|
The direct link works. Otherwise, there is some problem with SeaMonkey and GitLab.
jmgibson1981 - Compile the program with -Wall. Gcc will complain about the lack of return values from functions. Ed |
avoid code duplication, so
Code:
switch (b) { multiplication is ok, just the shell itself interprets * before passing to your app. As you wrote you can avoid it using '*' Check division by zero. |
Use real, meaningful variable names.
Do not combine multiple lines of code in one physical line. |
"The very first computer program that I ever wrote was eight lines long, took me six months to write, and had a bug in it." :D
#define PREACHER_MODE When you are writing any "algorithm," whether it be large or small, you should also be thinking about test cases. You should be thinking, simultaneously, about cases which demonstrate correct behavior, and(!) about cases which represent errors. "A calculator" should respond "intelligently" to any(!) string of characters that is given to it. It should never "crash." No matter what. In many languages – notably, Perl – packages are accompanied by sometimes-many-dozens of "automatic tests" which are applied before the package will consent to be installed. These tests help to verify that the package will, in fact, perform as intended on your computer. The formulation of these tests is somewhat of an art, but I have found that it is a very important discipline to develop. These disciplines will help you to say with confidence, not only that "your algorithm works," but that "it still works." (I promise you that you will be surprised when an "unrelated" change suddenly causes a test to fail. But, you will be very glad that the computer just caught it.) #undef PREACHER_MODE Welcome aboard. ("Welcome to my nightmare ...") Never be too hard on yourself. It can be a crazy way to make a living. :) |
Ok I'm still not sure about those test cases but I did make suggested changes. I also added the ability for it to detect remainders on a division solution. changed up the commenting a bit as well.
As far as the returns. I have been compiling with gcc and haven't seen any errors however i did add some to a few places. Trial and error a bit but it seems to be working just fine now. I await further guidance is you have any. https://gitlab.com/jmgibson1981/mycprogress |
Quote:
Code:
cc -Wall -Wextra lq-calc.c -o lq-calc Code:
lq-calc.c:73:13: warning: 'solution' may be used uninitialized in this function [-Wmaybe-uninitialized] |
Was able to remove the math_ret and ret
If I comment out solution int then it fails to compile saying that it isn't aren't initialized. Code:
$ gcc -Wall -O1 -o calc.e calc.c As it sits now. https://gitlab.com/jmgibson1981/mycprogress |
this is much better, although other operations (illegal chars) are still not checked.
For example what will print: calc.e 1 q 5 ? also the main function should return 0 only if everything was ok, otherwise something else (1). |
Quote:
Quote:
|
Very good. Did those. Got an empty response on
Code:
gcc -Wall -o calc calc.c |
Reading this thread makes me very glad I never pursued software. Hardware was bad enough.
|
Quote:
|
All times are GMT -5. The time now is 07:02 PM. |