Calculation

I am a beginner and I need help writing a calculation program.
The program should be able to handle at least 8 operators, square, sin, cos, tan, powers, +, -, *, /.
The program should be able to handle all these things in one expression.

For example: v for square, s for sin, c for cos, t for tan and ^ for power. v(s3.4/5^8). T
he program needs to understand parenthesis. And square, sin, cos and tan should be used as a "prefix" (executed on the following operator).
And when '=' is put in the result is written out, the program resets and restarts.

And if you could put in some comentaries in the code, so I know waht you've dont. It would be very helpful for the future.

Please help me!!!

Thank you
Last edited on
Unfortunately, no one here is going to do your homework for you.

I don't know why CS 101/102 professors continue to give this as homework. It is not a particularly simple thing to do.

You might want to google around "recursive descent".

Good luck.
It is NOT simple, at all.

For a while, I've tried to do this, and have never succeded...

If you get a clue, contact me. I'd love to know.

PS: See the book listed in my Bio, it has an exercise after chapter 12 describing how to do this in detail.
hello i'm mo3taz from egypt,
i,ve solved this problem but just for the operators +,-;
example input: 1+2+3-2+3-4+5.
out put: the result
don't forget the fullstop at the end
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
39
40
41
42
43
44
45
46
47
48
49
50
51
# include <iostream.h>
int main()
{
int sum = 0; 
int x , y;                                     // two successive numbers in the input between them
char test = 'r';                           // an operator
char op;                                    // stands for an operator
while (test == 'c' || test == 'r') // tests for continue(c) or restart (r)
{
 if ( test == 'r')
 {
  sum = 0;
  cout << "___________________________________________________________________";
  cout << "\nplease enter some successive numbers between each two an operator\n";
  cout << "the equation encludes just '+' or '-' ( example:1+2-3+4.) \n";
 }
 if (test == 'c')
  {
  cout << " please enter another sequence to add to the old sequence\n";
  cout << "the old value was : " << sum << endl;
  }
 cin >> x;
 cin >> op;
 cin >> y;
 if (op == '+')
  sum = (sum + x + y);
 else if ( op == '-')
  sum = (sum + x - y);

 while (op != '.')

  {
   cin >> op;
   if (op == '.') break ;  // notice that the operator may be '.'
   cin >> y ;              // after entering the while loop
   if (op == '+')
   sum = (sum + y);
   else if (op == '-')
   sum = (sum - y);

  }

  cout <<"the result of this sequenc is: \n:" << sum << endl;
  cout << "please enter c to continue calculating to the old result\n";
  cout << "please enter r to restart and calculate a new value\n";
  cout << " please enter e to exit \n ";
  cin>> test;
  if (test == 'e')
  break;
  }
}

if there is any problem with this tell me , and i'll try to insert other operators
my e_mail is : [email protected]
Last edited on
There is a way to solve this problem with a recurive descent:

I have *fully* copied this code from Arnold Willemers page (http://www.willemer.de/informatik/cpp/rekursion.htm)
But you can also find it in one of his books.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
enum tToken 
{
    PLUS, MINUS, MUL, DIV, LPAR, RPAR, NUMBER, END, ERROR
};

// Globale Variablen:
tToken aktToken;        // das zuletzt erkannte Token
double TokenZahlenWert; // der Wert bei Zahlenkonstanten

char *srcPos;           // Programm-Position

tToken sucheToken()
// Sucht ab der aktuellen Stringposition das nächste Token.
// Hier werden auch die Zahlenkonstanten bestimmt und in
// der globalen Variablen TokenZahlenWert abgelegt.
{
    aktToken = ERROR;
    if (*srcPos==0)
    {
        aktToken = END;
    } 
    else 
    {
        switch (*srcPos)
        {
            case '(': aktToken=LPAR;  break;
            case ')': aktToken=RPAR;  break;
            case '*': aktToken=MUL;   break;
            case '/': aktToken=DIV;   break;
            case '+': aktToken=PLUS;  break;
            case '-': aktToken=MINUS; break;
        }
        if (*srcPos>='0' && *srcPos<'9') 
        {
            aktToken=NUMBER;
            TokenZahlenWert = 0.0;
        }
        while (*srcPos>='0' && *srcPos<'9') 
        {
            TokenZahlenWert *= 10;
            TokenZahlenWert += *srcPos-'0';
            srcPos++;
        }
        if (aktToken != NUMBER)
        {
            srcPos++;
        }
    }
    return aktToken;
}

tToken Error(char *s)
// Meldung ausgeben und Fehler-Token zurückliefern
{
    cerr << s << endl;
    return ERROR;
}

double PlusMinus(); // Prototyp

double Klammern()
// wertet Klammern, Zahlen und Vorzeichen aus
// ruft Klammern() und PlusMinus() dadurch rekursiv auf!
{
    double Wert;
    switch(aktToken)
    {
        case NUMBER:
            sucheToken();
            return TokenZahlenWert;
        case MINUS:
            sucheToken();
            return -Klammern();
        case LPAR:
            sucheToken();
            Wert = PlusMinus();
            if (aktToken != RPAR)
            {
               return Error(") expected");
            {
            sucheToken();
            return Wert;
        case END:
            return 1;
    }
    return Error("primary expected");
}

double MulDiv()
// wertet Multiplikation und Division aus
// ruft Klammern() auf, dadurch rekursiv!
{
    double Wert;
    // rufe erst die Operation mit der nächsthöheren
    // Priorität auf
    Wert = Klammern();
    while (aktToken==MUL || aktToken==DIV)
    {
        if (aktToken==MUL)
        {
            sucheToken();
            Wert *= Klammern();
        }
        else if (aktToken==DIV)
        {
          sucheToken();
          Wert /= Klammern();
        }
    }
    return Wert;
}

double PlusMinus()
// wertet Summe und Differenz aus
// ruft MulDiv() auf, dadurch rekursiv
{
    double Wert;
    // rufe erst die Operation mit der nächsthöheren
    // Priorität auf
    Wert = MulDiv();
    while (aktToken==PLUS || aktToken==MINUS)
    {
        if (aktToken==PLUS)
        {
            sucheToken();
            Wert += MulDiv();
        }
        else if (aktToken==MINUS)
        {
            sucheToken();
            Wert -= MulDiv();
        }
    }
    return Wert;
}

double Auswertung(char *s)
{
    srcPos = s;
    sucheToken();      // bestimme das erste Token vorweg
    return PlusMinus();
}

int main(int argc, char* argv[])
{
    double Wert = Auswertung(argv[1]);
    cout << Wert << endl;
    return 0;
}


its in german, sorry for that :)

It loops trough all token and looks what it is and then calls the needed function.



Hope it helps

wooden nose
Last edited on
Thanks for your help, you've been vary helpful.
Topic archived. No new replies allowed.