?
Solved

Operations on Stack values

Posted on 2009-07-06
2
Medium Priority
?
215 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

801 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