👤

Se consideră o expresie aritmetică fără paranteze, în care operanzii sunt cifre, iar operatorii sunt + sau −. Să se evalueze expresia dată.

Răspuns :

#include <fstream>
using namespace std;
int main()
{
    ifstream f("calcul3.in");
    ofstream g("calcul3.out");
    int e,x;
    char c;
    f>>e;
    g<<e;
    while(f>>c)
    {
        if(c!='=')
        {
            f>>x;
            g<<c<<x;
            if(c=='+')e=e+x;
            else e=e-x;
        }
    }
    g<<"="<<e;
    return 0;
}