ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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 ?
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.