0



I have been assigned with a coding homework in CPP where I'm supposed to create a Matrix calculator. This would be an ok-ish task considering all I need to do is matrix calculations, use polymorphism, consider memory consumption, and some more criteria. What I'm struggling with is that is should be a console like environment.

Meaning start the app and I'm in a prompt where I type commands like:

scan x[3][3] \n // this creates new matrix labeled 'x' and waits for 9 ints ( longs possibly ) to by typed.

z = add x y \n // or
z = x + y \n

I am familiar with automata theory ( to some degree ) and making it this simple shouldn't be a problem. ( that practically has nothing to do with automata ) Simple meaning one command per line - because that's what I'm doing now. I have some parser class that breaks down the command, and than I do the necessary changes. Its more of a if-else tree going from first word to the last. If I encounter unrecognized word - Grammar/Syntax error.

What I'm asking is some tips on how to make it more .. bash like, for instance.
Since the app is run in bash..
FIRST Q: how do i achieve a history of typed commands ? rn when i push arrowup i get those ^[[A.
SECOND Q: Some hints how start parsing some more complicated commands like: a = b = c * ( d + q ) ( implying that 'c' can multiply "(d + q)" and 'd' is addable to 'q' etc.. ) bcs that cant be done with the static way my parser works rn.

Thank you all.

Proximity
  • 3
  • 1
  • It is rather broad for a question, too for me to try to answer. But as soon as I read *parser*, I always wonder whether lex-yacc (or flex-bison) would be worth it... – Serge Ballesta Jun 08 '20 at 11:36
  • @SergeBallesta going through books you mentioned .. i think what i wished to do, is far beyond what is expected of me. But thank you anyways. Very interesting topic. – Proximity Jun 08 '20 at 11:53
  • Input in C++ is line-buffered, which means you can only receive input after the user pressed [enter]. To have a "live" interaction you need to look into libraries ike ncurses. Making a list of recent commands should be pretty easy. Reacting to up/down key and updating the display might be the hard part. – Lukas-T Jun 08 '20 at 12:49

1 Answers1

0

For your first question, look at libreadline or libeditline. If you do not want to lift a finger, run your program under the rlwrap wrapper.

For your second question, look at Simple library or implementation for a mathematical expression evaluator . Look for one that allows you to define and use variables.

Botje
  • 26,269
  • 3
  • 31
  • 41