Solved

Operations on Stack values

Posted on 2009-07-06
2
207 Views
Last Modified: 2012-05-07
So I'm still new to c++ after doing java for a while and really am starting to miss a lot of the things I took for granted in Java haha.  Anyways, I'm working on a linked list implementation of a stack that gets input and does a certain function to that input.  Anything that isn't a certain word or an integer is to be ignored.  Integers are to be pushed onto the stack while a plus sign ("+") for instance sums the top two nodes of the stack and replaces the 2 values with that 1 value.  Or putting the word "Empty" in the input would empty out the stack.  There are other functions, but you get the idea.  

One of the problems I'm running into now are how to do math functions on the string input. Right now, I'm trying to convert the string to an integer and back to a string but I'm unsure of how to do the last part.  Another problem is how to ignore anything that I don't need, specifically to deal with identifying integers as a part of the string input.  Attached is a part of my code.  I think if I'm able to get one of the operations to work correctly,
int Stack::Plus(){ //Add the top 2 values in the stack, remove both of the values and insert their sum

	  StackItems first;  //StackItems is a struct in my header file with typedef string StackItemType item and StackItems *below in the function

	  StackItems second;

	  first.item = topDisplay(); topDisplay gets the top item

	  int topOne = ConversiontoInt(first.item);

	  pop(); //removes top item

	  second.item=topDisplay();

	  pop();

         int topTwo = ConversiontoInt(second.item);

	  int Sum = topOne+topTwo;

         //convert back to string here

	}

Open in new window

0
Comment
Question by:JamesL1518
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 24787710
In C++, you can leav3 most of the work to STL classes (http://www.sgi.com/tech/stl/), so in your case the conversion from int to string could be as simple as the attached code (also does a lot of the ignoring stuff)
#include <sstream>
 

string ConvertToString(const int n) {
 

  stringstream ss;
 

  ss << n;
 

  return ss.str();

}

Open in new window

0
 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24787749
Well for the conversion of string to integer and vice versa, you could use:

atoi() and itoa() which perform string to integer and integer to string conversions respectively. This could help you as far as performing math operations on your string input is concerned.

FYI,

int atoi(const char *str) is the definition of the atoi() function.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now