K
K
khipster2016-04-03 12:53:29
PHP
khipster, 2016-04-03 12:53:29

From the point of view of the OOP paradigm, what objects should be distinguished in the calculator program?

The most common calculator: plus, minus, multiplication.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Muhammad, 2016-04-03
@muhammad_97

One class: Calculator.

D
Denis Zagaevsky, 2016-04-03
@zagayevskiy

I would suggest the following:
* Lexical analyzer - reads the text, on request returns the next token (number, operation, bracket, etc.), knows where it is now relative to the text;
* Parser - takes a lexeme from a lexical, stores the state relative to the expression (not the text), builds a certain representation (for example, policy );
* Perhaps, actually, this is a representation, if standard containers are not enough;
* Interpreter - calculates the final value (the result of the expression) from the view;
* Well, perhaps, the notorious calculator, so that all this can be beautifully combined and the calculation looked like this: input a string, get the result at the output.
PS This is all you need if you are not going to use just eval, but want to actually write it yourself.
PPS read the rest of the answerers, and to clarify - what I described is just a "spherical calculator in a vacuum" that does not depend on the UI, does not depend on anything, in fact.

A
Alexander Litvinenko, 2016-04-03
@edli007

This question is more philosophical and depends on the vision of the structure of your program.
You can make, for example, only the MVC framework, where there will be two Ms,
- Where the first one will validate the data
- and the second one will perform basic arithmetic calculations.
C will implement the general application logic and V is clear.
Total 4 classes.
And you can, for example, wrap each input number in a class, and cram various methods and properties there, where there can be anything, from the history of changes to the number of digits of the number.
And at the same time, these classes can contain other classes, for example, the history of changes can be a separate class.
Add abstract classes here too.
Based on all this, it turns out that classes can occur on an infinitely small amount of logic, and classes responsible for "large structures" depend on the vision of the application structure by the programmer himself.

S
Shirshov Alexander, 2016-04-03
@Keanor

The main class is one: calculator. Further depends on the degree of sophistication of the author. From the less sophisticated: add, sub, div, mul methods to the most sophisticated, the Operation, Number, Operand classes with various properties like value, priority, number systems ...

X
xmoonlight, 2016-04-04
@xmoonlight

In short: the only class is the calculator class.
If you create a Competent Calculator using OOP, then:
1. You need to be able to store all previous operations and states of variables
2. You need to be able to move to any place in previous calculations and make edits in the form of a version (a new "branch" from the place of editing in the history of calculations)
3 You need to use support for entering direct logic algebra expressions and evaluating expressions using Polish reverse notation.
For example: 1+3*(4-cos(0))
4. You need to be able to work with finite and infinite sets with a given accuracy.
5. You need to be able to describe in history the entire order of internal transformations and calculations at the request of the user.
6. It is necessary to create a system for working with the transformation and simplification of expressions.
7. Be able to work with real numbers
, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question