Computer Science Canada strreal emulation |
Author: | chrispminis [ Tue Mar 07, 2006 11:06 pm ] | ||
Post subject: | strreal emulation | ||
We all know Turing comes with a few helpful conversion commands. strint intstr strreal etc. But for one of my assignments, these are forbidden. Now one of the sub assignments for this main one is to emulate the strreal function. I have already succeeded in creating a multidigit integer converter but im stumped on decimal numbers. Here's what I have so far:
You could either improve on this code, or write your own. Im pretty tired right now, and I'm not thinking right, and I've deviated from that code many times, sometimes getting closer but never quite there. If someone could please help. And BTW anyone at my school who stumbles across this, feel free to use it. |
Author: | Delos [ Wed Mar 08, 2006 8:49 am ] | ||||
Post subject: | |||||
Quite the problem you have here, eh? A couple of pointers:
Could be replaced by:
Simple, eh? As for the rest of your code - a big problem arises in that you're assuming the string inputted will always be of a certain length or greater. If it isn't, well - you get some errors occuring in your exponents. Let's try a different approach. Again, accept a string input of your real number. You can do a proof of it to make sure it is a valid number. Next, identify the position of the decimal (using index()), and use some string manipulation to excise it. (i.e., "5.403" would become "5403"). Convert this number to a real number, but using your integer algorithm. Finally, based upon the initial position of the decimal, divide your number by the requisite expontent of 10. Yatta! |
Author: | Andy [ Wed Mar 08, 2006 11:05 am ] | ||
Post subject: | |||
you're trying too hard...
EDIT: now its a bit longer, but has error checking. if the thing is not strreal ok, it'll output 31212012, (my pathetic attempt at writeing error in leet) |
Author: | Tony [ Wed Mar 08, 2006 11:26 am ] |
Post subject: | |
now for bonus points, write myStrrealok() ![]() |
Author: | jamonathin [ Wed Mar 08, 2006 12:54 pm ] | ||
Post subject: | |||
![]() |
Author: | Andy [ Wed Mar 08, 2006 1:08 pm ] | ||
Post subject: | |||
err wtf.. you're using strintok.. the whole point was to not use any of those functions...
|
Author: | jamonathin [ Wed Mar 08, 2006 7:27 pm ] | ||
Post subject: | |||
Wellz then. here's what I would do instead.
However comparing string values is much easier and more efficient. |
Author: | Cervantes [ Wed Mar 08, 2006 8:07 pm ] | ||
Post subject: | |||
You guys are missing one very important feature:
Should be true, but it outputs false. ![]() |
Author: | Andy [ Wed Mar 08, 2006 9:17 pm ] |
Post subject: | |
blah blah cervantes, shush you. jamonathin, you're trying too hard.. why use an array when you can simply use ascii values? |
Author: | jamonathin [ Wed Mar 08, 2006 11:17 pm ] |
Post subject: | |
true true, i forgot about those. I am stumped tho on how to do what cervantes is talkin about; ("2e3"). How can you change it to int, or at least run a for loop with that 3 as a max. ![]() |
Author: | Andy [ Wed Mar 08, 2006 11:57 pm ] |
Post subject: | |
i update my code tomorrow at work.. |