Solved

Operations on Stack values

Posted on 2009-07-06
2
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 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