LeperMessiah
Hectopat
- Katılım
- 2 Aralık 2019
- Mesajlar
- 61
Merhabalar. Elimde şöyle korkunç bir soru var. Benim limitlerimi aştığı için bir de foruma yazayım dedim.
Soru metni:
"Normally, arithmetic calculation by hand is done as follows:
( 3 + 4 ) * 5 – 6.
Here, the operator is placed between two operands. Let's call this type of parsing as handparsing.
One weakness of this method is that parentheses are required.
Otherwise, we may have unwanted results as given below:
(3 + 4) × 5 – 6 = 29.
3 + 4 × 5 – 6 = 17.
3 + 4 × (5 – 6) = –1.
(3 + 4) × (5 – 6) = –7.
Alternatively, we can place the operands first, followed by the operator:
(3 + 4) × 5 – 6.
3 4 + 5 × 6 –
Parsing reads left-to-right and performs any operation on the last two operands:
3 4 + 5 × 6 –
7 5 × 6 –
35 6 –
29.
Let's call this type of parsing as computerparsing.
Benefits of computerparsing:
There is no ambiguity and we do not need andy parenthesis.
Conversion from handparsing to computerparsing can be processed easily using stacks and queues.
Write a complete C program to.
Convert a handparsing to the computerparsing arithmetic calculation and,
Evaluate the computerparsing expression.
In the output, give the push and enqueue operation events of your stack and queue as given in the example inputs and outputs.
You can directly parse the input sequence from left to right.
You can assume that there are five arithmetic operators:
^ (Exponent), (highest precedence, precedence Value = 3)
* (Multiplication), (has the same precedence level as division, precedence Value = 2)
/ (Division), (has the same precedence level as multiplication, precedence Value = 2)
+ (Addition), (has the same precedence level as subtraction, precedence Value = 1)
- (Subtraction) (has the same precedence level as addition, precedence Value = 1)
You can also assume that entered numbers are integers.
There is always One white-space character between operands, operators and parenthesis.
Handparsing to computerparsing: Steps.
Parse the input handparsing expression:
İf you find an operand, add it to the queue.
İf you find an operator, pop off the stack S1 all operators (not parents) that have equal or higher precedence and add them to the queue.
Then push the operator found in the input expression onto the stack s1.
İf you find a left parenthesis, push it onto the stack S1.
İf you find a right parenthesis, pop operators from the stack S1 and add them to the queue until you find the matching left parenthesis, which you can ignore.
When you reach the end of the input expression, copy all remaining operators in the stack to the queue.
Return the queue.
Steps for evaluation of expression in computerparsing.
Create the New stack S2 (it has the size of queue).
Iterate over the elements (operators and operands) in the queue and do following for each element.
If the current element is an operand (i. E. A number), push it onto the stack S2.
If the current element is an operator (i. E. ^, *, /, +, -), Pop the related operands for the operator the stack s2. apply the operator onto the popped operands and push the result back to the stack S2.
If you come to the end of the queue (i. E. İterating all the elements in the queue), the number stored in the stack will be the result of the expression".
Ekte işleyişle alakalı bir örnek mevcut.
Soru metni:
"Normally, arithmetic calculation by hand is done as follows:
( 3 + 4 ) * 5 – 6.
Here, the operator is placed between two operands. Let's call this type of parsing as handparsing.
One weakness of this method is that parentheses are required.
Otherwise, we may have unwanted results as given below:
(3 + 4) × 5 – 6 = 29.
3 + 4 × 5 – 6 = 17.
3 + 4 × (5 – 6) = –1.
(3 + 4) × (5 – 6) = –7.
Alternatively, we can place the operands first, followed by the operator:
(3 + 4) × 5 – 6.
3 4 + 5 × 6 –
Parsing reads left-to-right and performs any operation on the last two operands:
3 4 + 5 × 6 –
7 5 × 6 –
35 6 –
29.
Let's call this type of parsing as computerparsing.
Benefits of computerparsing:
There is no ambiguity and we do not need andy parenthesis.
Conversion from handparsing to computerparsing can be processed easily using stacks and queues.
Write a complete C program to.
Convert a handparsing to the computerparsing arithmetic calculation and,
Evaluate the computerparsing expression.
In the output, give the push and enqueue operation events of your stack and queue as given in the example inputs and outputs.
You can directly parse the input sequence from left to right.
You can assume that there are five arithmetic operators:
^ (Exponent), (highest precedence, precedence Value = 3)
* (Multiplication), (has the same precedence level as division, precedence Value = 2)
/ (Division), (has the same precedence level as multiplication, precedence Value = 2)
+ (Addition), (has the same precedence level as subtraction, precedence Value = 1)
- (Subtraction) (has the same precedence level as addition, precedence Value = 1)
You can also assume that entered numbers are integers.
There is always One white-space character between operands, operators and parenthesis.
Handparsing to computerparsing: Steps.
Parse the input handparsing expression:
İf you find an operand, add it to the queue.
İf you find an operator, pop off the stack S1 all operators (not parents) that have equal or higher precedence and add them to the queue.
Then push the operator found in the input expression onto the stack s1.
İf you find a left parenthesis, push it onto the stack S1.
İf you find a right parenthesis, pop operators from the stack S1 and add them to the queue until you find the matching left parenthesis, which you can ignore.
When you reach the end of the input expression, copy all remaining operators in the stack to the queue.
Return the queue.
Steps for evaluation of expression in computerparsing.
Create the New stack S2 (it has the size of queue).
Iterate over the elements (operators and operands) in the queue and do following for each element.
If the current element is an operand (i. E. A number), push it onto the stack S2.
If the current element is an operator (i. E. ^, *, /, +, -), Pop the related operands for the operator the stack s2. apply the operator onto the popped operands and push the result back to the stack S2.
If you come to the end of the queue (i. E. İterating all the elements in the queue), the number stored in the stack will be the result of the expression".
Ekte işleyişle alakalı bir örnek mevcut.
Son düzenleyen: Moderatör: