Solved

Posted on 2003-02-25
Medium Priority
265 Views
Follwing program converts string of int. to integers and adds them.  Can you add code to this program to substracts integers as well, if there is any nagetive number.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s, container;
int pos, x, x2, result = 0, nextpos, init = 0;

cout<<"Enter expression to evaluate: ";
cin >> s;
pos = s.find('+',init);

while(1)
{
container = s.substr(init, pos);
x = atoi( container.c_str() );
result += x;

nextpos = s.find('+',pos+1);
if( nextpos == -1 )
{
result += atoi( s.substr( pos+1, s.length()-1 ).c_str() );
break;
}
init = pos+1;
pos = nextpos;
}

cout<<"\nResult:"<<result<<endl;

return 0;

}
0
Question by:learncpp

LVL 8

Accepted Solution

Exceter earned 40 total points
ID: 8018534
>> Can you add code to this program to substracts integers as well, if there is any nagetive number.

No, but you can. Try it and come back when you run into trouble.

As I said in your last question in regard to this, this is a terribly complex way of evauating expressions. I suggest that you read up on Polish Notation.

To make this do subtraction you have to write code that will recognize which operator is at a given position and perform the appropriate action.

Exceter
0

LVL 41

Expert Comment

ID: 8022721
*cough* HOMEWORK *cough*
0

Author Comment

ID: 8176302
int subs(string s, int initM){

int result2 = 0;
int  pos_minus, x2, count;
initM = 0;
pos_minus = s.find('-',initM);

if (pos_minus >= 0)                        //enter the loop if - exits

{

string container2;
int pos_minus, next_minus,next_sign,pos_end;
int length;                                     //first_minus;

count = 0;

pos_minus = s.find('-',initM);

while (count<s.length())
{

pos_minus = s.find('-',initM);
next_minus = s.find('-',pos_minus+1);
next_sign = s.find('+',pos_minus+1);

if( (next_minus == -1) || (next_sign == -1))
{
result2 += atoi( s.substr( pos_minus+1, s.length()-1 ).c_str() );
break;
}

if (next_minus < next_sign)
pos_end = next_minus;
else
pos_end = next_sign;

length = pos_end - pos_minus+1;

container2 = s.substr(pos_minus + 1,length);
x2 = atoi(container2.c_str());
result2 +=x2;

initM = pos_end + 1;
count++;
}

}

return result2;

}
//I got it.  Thanks
0

## Featured Post

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month12 days, 20 hours left to enroll