Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:57:25+00:00 2026-06-17T14:57:25+00:00

I am stuck in resolving a PHP script. I want to calculate a result

  • 0

I am stuck in resolving a PHP script.

I want to calculate a result from a set of instructions. Instructions comprise of a keyword and a number that are separated by a space per line. Instructions are loaded from file and results are output to the screen. Any number of Instructions can be specified. Instructions are operators (add, divide, subtract, multiply). The instructions will ignore mathematical precedence. The last instruction should be “apply” and a number (e.g., “apply 3”). The calculator is then initialised with that number and the previous instructions are applied to that number.

[Input]
add 2
multiply 3
apply 3
[Output]
15

this is what i have tried but i cant get the logic to complete the methods

class Calculator {
    public $result = 0;
    public $queue = Array();
    public parseString($text) {
       // parse input string
       $cmds = explode(" ", $text);
       foreach($cmds as $cmd) {
          $cmd = trim($cmd);
          if(!$cmd) continue; // blank or space, ignoring
          $this->queue[] = $cmd;
       }

       // lets process commands
       $command = false;
       foreach($this->queue as $index => $cmd) { 
           if(is_number($cmd)) {
               // if it's number fire previous command if exists
               if(!$command || !method_exists($this, $command)) {
                   throw new Exception("Unknown command $command");
               }
               $this->$command($index, $cmd);
           }else{
               $command = $cmd;
           }
       }
    }
    public function apply($index, $number) {
       // manipulate $result, $queue, $number
    }
    public function add($index, $number) {
       // manipulate $result, $queue, $number
    }
    public function substract($index, $number) {
       // manipulate $result, $queue, $number
    }
}

$calculator = new Calculator();
$calculator->parseString('...');

how can i call or switch the add,divide,multiply,substract and how to distinguish and trigger apply word

any kind of help will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:57:27+00:00Added an answer on June 17, 2026 at 2:57 pm

    You should process the apply first and then cut it out of your queue array. Before you start looping through with the commands, simply test for the apply command and run it first. This simplifies the whole process.

    After many minutes of experimentation and chatting, has been resolved.

    <?php
    error_reporting(E_ALL);
    class Calculator {
    
    public $result = 0;
    public $queue = array();
    
    public function parseString($text) {
    
        // parse input string
        $split = explode(" ", $text); //This gets your input into new lines
        for ($i = 0; $i < count($split); $i += 2) $pairs[] = array($split[$i], $split[$i+1]);
    
        foreach ($pairs as $bits) {
            if ($bits[0] == "apply") {
                $this->apply($bits[1]); //Set result equal to apply.
                $this->queue[] = "output";
            } else {
                $this->queue[] = $bits; //Set the queue item as an array of (command, value).
            }
        }
        //var_dump($this->queue);
        //die;
        // lets process commands
        foreach ($this->queue as $index => $cmd) {
            if ($cmd == "output") {
                echo "Answer: " .$this->result;
                return;
            } else {
                switch($cmd[0]) {
                    case "add":
                        $this->add($cmd[1]);
                        break;
                    case "subtract":
                        $this->subtract($cmd[1]);
                        break;
                    case "multiply":
                        $this->multiply($cmd[1]);
                        break;
                    case "divide":
                        $this->divide($cmd[1]);
                        break;
                    default:
                        echo "Unknown command!";
                        break;
                }
            }
        }
    }
    
    public function apply($number) {
        // manipulate $result, $queue, $number
        $this->result = $number;
    
    }
    
    public function add($number) {
        // manipulate $result, $queue, $number
        $this->result += $number;
    
    }
    
    public function subtract($number) {
        // manipulate $result, $queue, $number
        $this->result -= $number;
    
    }
    
    public function multiply($number) {
        // manipulate $result, $queue, $number
        $this->result *= $number;
    }
    
    public function divide($number) {
        // manipulate $result, $queue, $number
        $this->result /= $number;
    }
    
    } 
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck here and i spend last 2 days resolving this issue but
I've got stuck on resolving why does keyboard in my app appears on screen
I'm stuck on the question for Exercise 17.1.2 from HTDP. This is what I've
I'm stuck on the question for Exercise 17.1.2 from HTDP. This is what I've
I'm currently using php to populate a form with selections from a database. The
I am writing a PHP/MySQL application that maintains a masterlist of user preferences and
After reading tons of documentation and questions from here I'm still stuck with the
In the process of learning to use Mongo and PHP, and got stuck with
Stuck on KVCs in Obj-C again. I am wanting to use KVC to find
Been stuck on this for ages. Currently learning Django and done some bits on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.