operators and operands

Mar 30, 2010 at 4:21pm
Hi please i need help with this home work it's due tomorrow. Any will be highly appreciated.



I am supposed to write a C++ program that reads in operands and operators and then separates them in the output. For example if i read in 1+3*4/5 the out put should be 1 3 4 5
+ * /
the operators need to be separated from the operands.
Thank you
Mar 30, 2010 at 4:35pm
Do you handle paranthesis? Error handling? Integers or floating point values? I suppose next step will be to implement a calculator using a stack? How much do you know? What are you allowed to use? vectors, arrays or similar?

still many open question, please provide us with more

Maikel
Last edited on Mar 30, 2010 at 4:38pm
Mar 30, 2010 at 4:46pm
I am still sort of new to C++ programming. Not vectors because we kind of skipped that. Also not stacks because we haven't reached that stage. Arrays,functions will be good if possible. The variables should be of double so they can accommodate both integers and real numbers.
Mar 30, 2010 at 4:58pm
Then lets think about an algorithm.

First the variables we need: Since we use arrays, there should be an upper bound of numbers we can read (and one of operators also..). I tend to store those bounds in a const var.
Then we need an array of numbers and one of ops. Since there is (in the easy way) always one more num as op, it would be like this:

1
2
3
    const int MaxOps = 40;
    double num[MaxOps + 1];
    char op[MaxOps];


After this we should ask for an easy math expression with +-/*. The standard input is line buffered, so we know that the use inputs a line and lets say, if we read a newline char '\n' the expression ends. What we now need is a for loop that reads up to MaxOps numbers and ops from user input. Like this

1
2
3
    for (i = 0; i < MaxOps+1; i++) {
        // lets do some crazy input here
    }


how does it work? we assume that the user do always something like this:

(number) (op) (number) (op) (number)


That makes it easy to read from input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        // READ A NUMBER WITH CIN
        if (!(cin >> num[i])) { // like this its more easy, but less powerfull
            // do some Error Handling
            cerr << "Error occured! - Exiting\n";
            return 1;
        }

        // READ A CHAR (here operator) if NEWLINE QUIT!
        char c = cin.get();
        if (c == '\n') { // read new line so you
            break;       // reached end of expression
        }
        op[i] = c;

       // remember: (num) (op) (num) (op) (num) (newline) 



At last stage we need to print them out. Its up to you, to put everything together.

If you still need help, you're welcome.

Maikel
Last edited on Mar 30, 2010 at 5:05pm
Mar 30, 2010 at 5:03pm
Thank you maikel
Mar 30, 2010 at 6:54pm
you could also just do two sweeps over the input. The first loop you print out all the numbers and the second loop you print out all operators. Voila: separation.

No need for storing anything there. ;-)

something along these lines.
1
2
3
4
5
6
7
8
for (int i = 0; i < size_of_input; ++i)
  if ( is_a_number( input[i] ) )
    cout << input[i] << " ";
cout << endl;
for (int i = 0; i < size_of_input; ++i)
  if ( is_an_operator( input[i] ) )
    cout << input[i] << " ";
cout << endl;


Ciao, Imi.
Mar 30, 2010 at 7:32pm
Thanks IMI but how will the compiler realize that 'is_an_operator' is an operator because your cin should be like 1/2+3*4.
Mar 30, 2010 at 9:14pm
Maikel there is an error on the program
it says here illegal break
Mar 30, 2010 at 9:15pm
okay i figured that out
Mar 31, 2010 at 8:03am
Thanks IMI but how will the compiler realize that 'is_an_operator' is an operator because your cin should be like 1/2+3*4.


I thought about something like:
1
2
3
bool is_an_operator(char c) {
  return c == '*' || c == '+' || c == '-' || c == ... // and so on..
}


Ciao, Imi.
Topic archived. No new replies allowed.