[SOLVED] Tried to solve a problem. Probably a better way. Asking for tips to simplify.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Tried to solve a problem. Probably a better way. Asking for tips to simplify.
My first attempt at creating something. I have no doubt there is a better way to do this. be gentle please. this is to verify that floats or integers are the only acceptable input for my calculator i've been working on.
Code:
int valid_numbers(char numtest[24])
{
// declare and set variables
int total_chars = strlen(numtest);
int total_char_test;
int difftest;
for (int i = 0; i < total_chars; i++)
{
if (isdigit(numtest[i]))
{
// if number is digit then increment test variable
total_char_test++;
}
}
// set variable difference total & test
difftest = total_chars - total_char_test;
if (difftest == 1 && strchr(numtest, '.') != NULL)
{
return 0;
}
if (difftest > 1)
{
return 1;
}
return 0;
}
*edit* found it didn't work.updated code above to working config.
Last edited by jmgibson1981; 01-21-2023 at 10:19 PM.
You didn't initialize total_char_test to 0, it might work by accident right now, but depending on some unrelated things might fail to work later. I think the compiler should be warning you about this if you set the right options.
For readability, I would suggest naming that variable total_digits, or something like that instead. Also, the 24 in char numtest[24] is not doing anything so I suggest to remove it (maybe you want to add a test against total_chars that it's 24 or less?)
Do you consider "123." or ".123" as valid integers or floats? (That's not rhetorical, any choice you make here is fine, as long as you've thought about it)
you can try to count non-digit chars, and stop (return 1) if more than one found or is not dot. That would be probably a bit better.
You can also check the minus sign (if you wish). And probably you want to handle longer numbers too. And check the end-of-string.
Otherwise it looks ok.
regarding validity, a "regular" decimal number must not start with 0 (zero) if it is >1 and must not end with 0 if there is a decimal point somewhere. But these are not that important, you can simply cut them (leading and trailing zeros) if you wish.
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,056
Rep:
Quote:
Originally Posted by pan64
regarding validity, a "regular" decimal number must not start with 0 (zero) if it is >1 and must not end with 0 if there is a decimal point somewhere. But these are not that important, you can simply cut them (leading and trailing zeros) if you wish.
?
100 and 100.0 both valid decimal numbers both end in 0 or am I missing somthing?
You could approach it with ever increasing checks.
Plus or minus should only be the first character.
There only should ever be one decimal point.
The rest can be digits, but there are some rules to consider which you can decide as to what is allowable.
Actually, if you are seriously interested in knowing more about "how cheap calculators actually do it," you may wish to someday explore the concept of a FSM = Finite-State Machine.
"Finite-State Machines" are actually a very-important but maybe counter-intuitive class of algorithms which are actually very relevant to problems of this sort. In summary, the "machine" processes its input "one input at a time," while maintaining an internal "state" to dictate exactly how it should process "any possible input" at "this particular time (in this particular 'state' ...)
It looks like they are using a finite state machine to handle operator precedence. It's certainly possible to do this when there are only two* levels of precedence (plus/minus vs multiply/divide), but in my opinion it's not a good approach because it leads to an overly complex set of states, and you should not follow this example.
* or any finite number of levels n if you're willing to handle 2ⁿ states, but you can't handle arbitrarily nested parentheses with a finite state machine.
?
100 and 100.0 both valid decimal numbers both end in 0 or am I missing somthing?
Yes, 001000.34 is not really acceptable, 213.343000 again. That's what I wanted to explain. Next time I will try to explain better, that was not that perfect.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.