Solved

Operations on Stack values

Posted on 2009-07-06
2
211 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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
PDF library for Delphi 2 135
Best book to learn C++ 4 87
Where are the c++ header files to use in Eclipse? 4 50
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

749 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