algebra calculator

Sep 28, 2009 at 12:01am
i dont know how to do this but, i need some thing (calculator) where the user types in the whole problem in cluding operators and the program calculates the thing and the problem can be any amount long... is this possible?
help me out if it is please.
Sep 28, 2009 at 4:24am
Can you give more details?
Sep 28, 2009 at 5:39am
Why don't you write a program which calls a function that converts your infix expression to postfix notation and then pass this postfix version of your expression to a function which evaluates it and gives result. You can check any good Data Structure book for both these tasks. Both these functions will you use stack, so check the stack chapter in the book, if infix-postfix type separate chapter is not included in the book.
Your expression will be input using a string, for a beginner its a tough task but you will learn a lot of things while reading all this.
Sep 28, 2009 at 8:05pm
where would i learn how to do this...
and more details please
and what book are you talking about...
i am really confused
Last edited on Sep 28, 2009 at 8:25pm
Sep 28, 2009 at 9:22pm
You need to write a simple parser along with the grammar that will interpret the user's input. I had to write something very similar. (a calculator) I recommend Stroustrup's Principles of programming. It is a great text book and that particular problem is covered in detail. I warn you though, writing a parser/grammar/tokenizer is not very simple. If you have never seen this concept it will take you a while to swallow it.
Sep 29, 2009 at 10:26pm
It works but y is it so slow???


code:


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
#include <std_lib_facilities.h>
int main()
{
    cout << "Enter the expression we can handle + ,-,* and /";
    double lval = 0;
    double rval;
    char op;
    double res;
    cin >> lval;
    if (!cin) error ("no first operand");




    while (cin >> op) {
          cin >> rval;
      if (!cin) error ("no second operand");    
      switch (op) {


    case '+':
    lval += rval;
    break;
    
    case '-':
    lval -= rval;
    break;
    
    case '*':
    lval *= rval;
    break;
    
    case '/':
    lval /= rval;
    break;
    
default:
   cout << "Result:" << lval << '\n';
   keep_window_open();
   return 0;
                    }
           }
error ("Bad Expression");
}
Topic archived. No new replies allowed.