LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2010, 02:36 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
string expresion C++


Hello,i have the following code:
Code:
string s = "1234";
stack<int> st;
for(unsigned int i = 0; i < s.size(); i++)
{
   st.push(atoi(&s[i]));
}
I want to push in to a stack every character in s as an INT,but my code how its writen pushes 1234 then 234 then 34 then 4...how can i modify it to push 1 than 2,3,4 ....Thanx in advance.
 
Old 10-14-2010, 02:46 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
Hello,i have the following code:
Code:
string s = "1234";
stack<int> st;
for(unsigned int i = 0; i < s.size(); i++)
{
   st.push(atoi(&s[i]));
}
I want to push in to a stack every character in s as an INT,but my code how its writen pushes 1234 then 234 then 34 then 4...how can i modify it to push 1 than 2,3,4 ....Thanx in advance.
Your code does the right thing - 'atoi' expects a null-terminated string and your code works the way it works exactly for this reason.

You need a temporary null-terminated string holding just one ASCII digit to achieve the code beahvior you want.
 
Old 10-14-2010, 03:35 PM   #3
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
ok,i made the correction,my next question is: if i have string s = "123 324 546" i want to push iinto the stack 123 then 324 then 546 and not 1 2 3 3 2 4 5 4 6 ? How can it be done ?
 
Old 10-14-2010, 03:53 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Use stringstreams.

Something smells like homework here. Tell us what the real problem is.
 
Old 10-14-2010, 04:08 PM   #5
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
Is not homework,just doing exercises from The C++ Programming Language Special,still don`t know why it matters what it is....i need to calculate a string expresion like:

Code:
string s = "122+21-45-11*2*4";
I used Reverse Polish notation but my eproblem only works with numbers from 0-9
 
Old 10-14-2010, 08:27 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
Is not homework,just doing exercises from The C++ Programming Language Special,still don`t know why it matters what it is....i need to calculate a string expresion like:

Code:
string s = "122+21-45-11*2*4";
I used Reverse Polish notation but my eproblem only works with numbers from 0-9
Without seeing the code it's impossible to say what's wrong.
 
Old 10-14-2010, 11:21 PM   #7
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
Your problem is to evaluate an arbituary simple mathematic expression, right?

Start your value stack with 0 as the first item, because a empty expression evaluates to 0, if you agree.

Start your operator stack with '+' as the operator, because an expression like "100" evaluates to 100, which is to be added to the 0 in the value stack.

Scan through your expression, decide what to do when which operator is encountered.

Do everything with pencil and paper first, you will find you way through.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 08:55 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
math expresion evaluator Starch Linux - Newbie 1 05-05-2005 08:10 PM
Expresion Solver needed kamransoomro84 Programming 3 08-01-2004 11:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration