summation of an arithmatic equation

my problem is ,,,user will input something like 1+2-3+4-5,,,,, and user will not tell the nth term,,,but i have to calculate the result.how can i calculate this without knowing nth term,,or first term etc. like this problem i have to print (-1) for this series ,and input will be just one line.
Last edited on
Is that a series at all? If it was, the maybe-implied infinite series certainly doesn't converge.
On the other hand, 1 + 2 - 3 + 4 - 5 = -1; this appears to be the the solution.

1
2
3
4
int main() {
    using it = std::istream_iterator<int>;
    std::cout << std::accumulate(it{std::cin}, it{}, 0) << '\n';
}

Live demo:
http://coliru.stacked-crooked.com/a/22c9fb4f36faa8c4
suppose it is not a series,,then how can i calculate this?? can u give me the whole code??
Last edited on
Last edited on
it's not working,,,any more idea??
the code expects numbers as input and it will output the sum of thos numbers.
the reading will stop when it fails (EOF or a non-numeric input). that part would be equivalent to
1
2
3
4
int n;
while( std::cin>>n ){
   //...
}


perhaps the issue is in your input (for example, you put spaces behind the signs)
OK, CL final starts in less than 15 mins (forza Juve!), so my answer is a little bit incomplete - that and this is perhaps

your homework so you need to complete a couple of things yourself, these are mentioned below:
- read the input as a std::string and then 'parse' the string searching for the + or - characters. I've also used a struct

to store the results, so the oustanding tasks for you are:
(a) what if the first char in the input string is not + or - but a number outright - program needs to handle it and
(b) overloading the operator + for struct SignedNum (hint: used std::stringstream)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# include <iostream>
# include <string>
# include <vector>

struct SignedNum
{
    char m_sign;
    std::string m_number;

    SignedNum(const char sign, const std::string& number)
    : m_sign(sign), m_number(number){}
};

std::ostream& operator << (std::ostream& os, const SignedNum& s)
{
    os << s.m_sign << s.m_number;
    return os;
}
int main()
{
    std::cout << "enter the signed numbers: \n";
    std::string numbers{};
    getline(std::cin, numbers);

    auto found = numbers.find_first_of("+-");
    std::vector<SignedNum> signed_numbers{};

    while (found != std::string::npos)
    {
        auto found_next = numbers.find_first_of("+-", found + 1);
        {
            signed_numbers.emplace_back(SignedNum(numbers[found],
                        std::string{numbers.substr(found+1, (found_next - 1) - found)}));
        }
        found = found_next;
    }
    for (const auto& elem : signed_numbers)std::cout << elem ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>

int main()
{
    std::cout << "on a single line enter expression consisting of non-negative integers\n"
                 "separatated the binary + or - operators\n"
                 "for example: 23 + 52 - 31 + 1 +2+3-4+5\n? " ;

    std::string str ;
    std::getline( std::cin, str ) ; // read in a complete line

    // place the string with white spaces removed into sanitised_str
    std::string sanitised_str ;
    for( char c : str ) if( !std::isspace(c) ) sanitised_str += c ;

    // create an input string stream to read from sanitised_str
    std::istringstream stm(sanitised_str) ;

    // compute the result and print it out.
    int result = 0 ; // initialise to zero

    int number ;
    while( stm >> number ) result += number ; // note: result + -5 == result - 5

    std::cout << "result: " << result << '\n' ;
}
Last edited on
Topic archived. No new replies allowed.