![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
off-topic slightly...what about forgetting the string thing.
depending on your I/O scheme it may differ in emplementation but... what if you enter the number didgit by digit first into a lifo container like a stack, then insert digit by digit into a fifo container like a queue, then pop() each and add the result cumulatively as each pops by mutiplying it by pow(10, i) where i is a loop index starting at zero? example: 123 is your num fifo pops like 1, 2, 3 lifo pops like 3, 2, 1 1*10^0 = 1; 3*10^0 = 3; add them you get 4. 2*10^1 = 20; 2*10^1 = 20; add them you get 40. 3*10^2 = 300; 1*10^2 = 100; add them you get 400. you know the end value of i by the size() of the container. add all three you get 444 which = 123 + 321. or if this sounds like gibberish then fuck me, nevermind. :p
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
ignoring crap like I/O (this prog only accepts 3-digit numbers) here's the basic idea:
#include <iostream>
#include <stack>
#include <queue>
#include <cmath> //for pow()
using namespace std;
int main()
{
//declare data structures
stack<float> lifo;
queue<float> fifo;
//value to be printed at end
int final = 0;
//populate the containers...
lifo.push(7);
lifo.push(6);
lifo.push(8);
fifo.push(7);
fifo.push(6);
fifo.push(8);
for(float i = 0; i < 3; i++)//run three times
{
//run algorithm
final += ( lifo.top() * pow(10, i) ) + (fifo.front() * pow(10, i) );
//remove those values for next iteration
lifo.pop();
fifo.pop();
}
//show result
cout<<final<<endl;
//pause for output
system("PAUSE");
return 0;
}
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#13 | |
|
Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0
![]() |
Going back to C is always an option :-)
Quote:
__________________
Don't comment bad code - rewrite it. - The Elements of Programming Style (Kernighan & Plaugher) |
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In C, sprintf is a very good option. However, this is the C++ forum. C++ has its own mechanisms available that do these things. An OP is not generally looking to change languages as a solution, but is attempting to learn the language associated with the forum in which the question is posted.
It's also somewhat confounding to me that some respondents are still recommending 'atoi', which is a baddie, and recommending that the OP process each character independently. That's what the intended tools are for, and they cover the job more thoroughly than your average coder will be able to do. Simply consider a number represented in scientific notation to get an idea of what I mean. Compare the code in posts #3 and #8 to that in #12, bearing in mind that the code in #8 is doing it three times, not just once, as an example of handling input in three different forms. If I were the OP, I'd have grabbed the solution long ago, run with it, and be done with it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
I've wrapped DaWei's and Soulstorm's recommendation of using stringstreams into two (what I think are) useful functions, and a sample of their use in main().
cpp Syntax (Toggle Plain Text)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#16 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
You can also reverse a positive number without any string conversions, like so:
int reverse_decimal(int number)
{
int rebmun = 0;
while (number > 0)
{
rebmun *= 10;
rebmun += (number % 10);
number /= 10;
}
return rebmun;
}(Note that if you give it 100 it will give you 1, indistinguishable from 001 - if you give it 1, it'll give you 1 back. So reversing twice doesn't give you back the original number. If you change the 10's to 2's it'll reverse it in binary.) Last edited by Klipt; Dec 10th, 2006 at 3:59 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 5:45 PM |
| Convert a string to unicode string | sma_soft | Delphi | 3 | Nov 16th, 2005 5:06 AM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 12:32 PM |
| function | solomon_13000 | Java | 6 | Apr 2nd, 2005 11:42 PM |