?
Solved

add and substract

Posted on 2003-02-25
3
Medium Priority
?
257 Views
Last Modified: 2010-04-01
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
Comment
Question by:learncpp
[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
3 Comments
 
LVL 8

Accepted Solution

by:
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 40

Expert Comment

by:Kyle Abrahams
ID: 8022721
*cough* HOMEWORK *cough*
0
 

Author Comment

by:learncpp
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

Independent Software Vendors: 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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