Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2006, 2:13 AM   #11
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Dec 10th, 2006, 3:17 AM   #12
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Dec 10th, 2006, 3:59 AM   #13
magnus.therning
Programmer
 
magnus.therning's Avatar
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 88
Rep Power: 3 magnus.therning is on a distinguished road
Going back to C is always an option :-)

Quote:
Originally Posted by Cool1Net6 View Post
In a recent contest, the problem was to reverse the digits of a number and add that new number to the old number to come up with a newer number. I had the knowledge to reverse the digits of a number by converting the original number to a string and swapping the positions of each character, but to add this new number to the original number, I needed to convert the new number back to an int. I lost the competition because I lacked this knowledge.

How do you convert a String to an Int?

-Cool-
You can always use sprintf or one of its family members.
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote
Old Dec 10th, 2006, 6:05 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 10th, 2006, 11:12 AM   #15
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <stdexcept>
  5.  
  6. template <typename T>
  7. T read_string( const std::string &input ) throw ( std::invalid_argument ) {
  8. std::istringstream source( input );
  9.  
  10. T result;
  11.  
  12. if ( !(source >> result) )
  13. // If the operation fails,
  14. // one possible course of action
  15. // is to throw an exception.
  16. throw std::invalid_argument( "Could not parse string!" );
  17.  
  18. return result;
  19. }
  20.  
  21. template <typename T>
  22. std::string to_string( const T &input ) {
  23. std::ostringstream result;
  24.  
  25. result << input;
  26. return result.str();
  27. }
  28.  
  29. int main() {
  30. std::cout << read_string<int>( "12" ) << std::endl;
  31. std::cout << read_string<float>( "3.14159" ) << std::endl;
  32. std::cout << to_string( 12 ) << std::endl;
  33. std::cout << to_string( 3.14159 ) << std::endl;
  34. }
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Dec 10th, 2006, 4:48 PM   #16
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
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 4:59 PM.
Klipt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
Convert a string to unicode string sma_soft Delphi 3 Nov 16th, 2005 6:06 AM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM
function solomon_13000 Java 6 Apr 3rd, 2005 12:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC