Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Advance calculator (http://www.go4expert.com/forums/advance-calculator-t16456/)

2007160935 6Mar2009 11:28

Advance calculator
anyone can show me a codes for calculator which determines a space, a letters and symbols

for example:
Enter a equations:
1 + 3 = 3
1 + b = invalid character
1 + * = invalid character

xpi0t0s 6Mar2009 19:28

Re: Advance calculator
No but I'll help you figure it out. What have you got so far and where are you stuck?

2007160935 8Mar2009 09:25

Re: Advance calculator
what I mean is, if anyone tell me how's that? or show me the codes for better improvements

Check this out!
I just want to improve this codes :shy:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int  left, right;  // Operands
    char oper;          // Operator
    int  result;        // Resulting value
    while (cin >> left >> oper >> right) {
        switch (oper) {
            case '+': result = left + right;
            case '-': result = left - right;
            case '*': result = left * right;
            case '/': result = left / right;
            default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
        cout << result << endl << endl;

    return 0;

2007160935 8Mar2009 09:26

Re: Advance calculator
1 + 3 = 4, suck i didnt notice what im typing xD:

shabbir 8Mar2009 10:38

Re: Advance calculator
There is nothing much but if you want to add some complex Bodmas calculation you would need to do a lot.

xpi0t0s 8Mar2009 16:55

Re: Advance calculator
The code has very limited scope so unless you want to extend it to a full floating point expression parser (which is quite a complex beast) there's not a lot to say. You asked for comments, but leading to what? Do you just want to improve it within the limited scope of taking two integers and an operator? If so then the only thing you can really add is more operators. If you want to extend it to a full expression evaluator including order of precedence rules (e.g. 2+3*5=17, not 25) then this is typically done either using a pair of stacks, one for operators and one for numbers/variables, or by converting the expression to reverse polish notation (e.g. 2 3 5 * + gives the result 17). An RPN expression evaluator is a lot simpler to do and could be a good first step.

All times are GMT +5.5. The time now is 09:40.